机器学习笔记 - tensorflow 2.8版本不同优化器对比
2023-09-14 09:01:36 时间
1、目标概述
这里使用tensorflow 2.8版本和LeNet-5模型在mnist手写数字数据集上进行各种优化器的效率比较。为了展示现代优化器(在 TensorFlow 和 Keras 中的优化器)对训练的影响,我们将创建几个 LeNet 实例,并使用不同的优化器训练每个实例。
2、导入包准备数据
import tensorflow as tf
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
num_classes = 10
img_rows, img_cols, img_ch = 28, 28, 1
input_shape = (img_rows, img_cols, img_ch)
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train = x_train.reshape(x_train.shape[0], *input_shape)
x_test = x_test.reshape(x_test.shape[0], *input_shape)
print('Training data: {}'.format(x_train.shape))
print('Testing data: {}'.format(x_test.shape))
3、创建LeNet-5模型
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
def lenet(name&