使用手写数字识别数据集在LeNet-5上增加BN层并比较与全连接、LeNet-5全连接、LeNet-5画出随epoch变化的曲线。 tensorflow代码实现
时间: 2024-03-09 21:49:37 浏览: 83
c语言实现手写数字识别代码_c_presidentp1s_数字识别c代码_tensorflow_LeNet5
5星 · 资源好评率100%
好的,那我们就使用MNIST手写数字识别数据集。下面是实现的代码:
```python
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.layers import Input, Conv2D, Dense, Flatten, BatchNormalization, Activation, MaxPooling2D
from tensorflow.keras.models import Model
# 加载数据集
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 归一化数据
x_train, x_test = x_train / 255.0, x_test / 255.0
# 增加维度,将数据变成4D张量
x_train = x_train[..., tf.newaxis].astype("float32")
x_test = x_test[..., tf.newaxis].astype("float32")
# 定义模型
def LeNet5():
inputs = Input(shape=(28, 28, 1))
x = Conv2D(filters=6, kernel_size=(5, 5), activation='relu', padding='same')(inputs)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Conv2D(filters=16, kernel_size=(5, 5), activation='relu', padding='valid')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Flatten()(x)
x = Dense(units=120, activation='relu')(x)
x = Dense(units=84, activation='relu')(x)
outputs = Dense(units=10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
return model
def LeNet5_BN():
inputs = Input(shape=(28, 28, 1))
x = Conv2D(filters=6, kernel_size=(5, 5), padding='same')(inputs)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Conv2D(filters=16, kernel_size=(5, 5), padding='valid')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Flatten()(x)
x = Dense(units=120)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Dense(units=84)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
outputs = Dense(units=10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
return model
def fully_connected():
inputs = Input(shape=(28, 28, 1))
x = Flatten()(inputs)
x = Dense(units=128, activation='relu')(x)
x = Dense(units=64, activation='relu')(x)
outputs = Dense(units=10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
return model
# 定义训练参数
batch_size = 64
epochs = 10
# 创建模型
model_LeNet5 = LeNet5()
model_LeNet5_BN = LeNet5_BN()
model_fully_connected = fully_connected()
# 编译模型
model_LeNet5.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model_LeNet5_BN.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model_fully_connected.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# 训练模型
history_LeNet5 = model_LeNet5.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test))
history_LeNet5_BN = model_LeNet5_BN.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test))
history_fully_connected = model_fully_connected.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test))
# 绘制训练曲线
import matplotlib.pyplot as plt
plt.plot(history_LeNet5.history['accuracy'], label='LeNet-5')
plt.plot(history_LeNet5_BN.history['accuracy'], label='LeNet-5 + BN')
plt.plot(history_fully_connected.history['accuracy'], label='Fully connected')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
```
上述代码中,我们定义了三个模型:LeNet-5、LeNet-5+BN和全连接模型。然后,我们将这些模型编译,并使用MNIST数据集进行训练,并绘制出它们在训练集上准确度随epoch变化的曲线。
注意,由于BatchNormalization的存在,我们在准确度图表中应该看到LeNet-5+BN模型的收敛速度更快,并且在相同的epoch数下达到更高的准确度。
阅读全文