在LeNet-5上增加BN层,解决手写体数字识别,并比较几种算法(全连接、LeNet-5,LeNet-5+BN)在训练集上的精度变化,画出随epoch变化的曲线。tensorflow实现
时间: 2024-03-06 13:51:58 浏览: 91
好的,下面是实现步骤:
1. 数据准备:首先需要准备手写数字MNIST数据集,可以使用TensorFlow自带的数据集加载函数进行加载。
2. 搭建LeNet-5模型:可以使用TensorFlow中的layers模块搭建LeNet-5模型,代码如下:
```python
import tensorflow as tf
from tensorflow.keras import layers
model = tf.keras.Sequential([
layers.Conv2D(filters=6, kernel_size=(5, 5), activation='relu', input_shape=(28, 28, 1)),
layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
layers.Conv2D(filters=16, kernel_size=(5, 5), activation='relu'),
layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
layers.Flatten(),
layers.Dense(units=120, activation='relu'),
layers.Dense(units=84, activation='relu'),
layers.Dense(units=10, activation='softmax')
])
```
3. 在LeNet-5上增加BN层:在LeNet-5的每个卷积层和全连接层之后都可以增加Batch Normalization层,代码如下:
```python
model = tf.keras.Sequential([
layers.Conv2D(filters=6, kernel_size=(5, 5), activation='relu', input_shape=(28, 28, 1)),
layers.BatchNormalization(),
layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
layers.Conv2D(filters=16, kernel_size=(5, 5), activation='relu'),
layers.BatchNormalization(),
layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
layers.Flatten(),
layers.Dense(units=120, activation='relu'),
layers.BatchNormalization(),
layers.Dense(units=84, activation='relu'),
layers.BatchNormalization(),
layers.Dense(units=10, activation='softmax')
])
```
4. 比较几种算法在训练集上的精度变化:可以使用TensorFlow中的ModelCheckpoint回调函数保存每个epoch的模型权重,在训练过程中记录各个算法在训练集上的精度,并绘制出随epoch变化的曲线,代码如下:
```python
import tensorflow as tf
from tensorflow.keras import layers
# 加载MNIST数据集
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
# 对数据进行处理
x_train = x_train.reshape((-1, 28, 28, 1)).astype('float32') / 255.
x_test = x_test.reshape((-1, 28, 28, 1)).astype('float32') / 255.
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)
y_test = tf.keras.utils.to_categorical(y_test, num_classes=10)
# 搭建LeNet-5模型
model_lenet5 = tf.keras.Sequential([
layers.Conv2D(filters=6, kernel_size=(5, 5), activation='relu', input_shape=(28, 28, 1)),
layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
layers.Conv2D(filters=16, kernel_size=(5, 5), activation='relu'),
layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
layers.Flatten(),
layers.Dense(units=120, activation='relu'),
layers.Dense(units=84, activation='relu'),
layers.Dense(units=10, activation='softmax')
])
# 在LeNet-5上增加BN层
model_lenet5_bn = tf.keras.Sequential([
layers.Conv2D(filters=6, kernel_size=(5, 5), activation='relu', input_shape=(28, 28, 1)),
layers.BatchNormalization(),
layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
layers.Conv2D(filters=16, kernel_size=(5, 5), activation='relu'),
layers.BatchNormalization(),
layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
layers.Flatten(),
layers.Dense(units=120, activation='relu'),
layers.BatchNormalization(),
layers.Dense(units=84, activation='relu'),
layers.BatchNormalization(),
layers.Dense(units=10, activation='softmax')
])
# 定义全连接网络
model_fc = tf.keras.Sequential([
layers.Flatten(input_shape=(28, 28, 1)),
layers.Dense(units=256, activation='relu'),
layers.Dense(units=128, activation='relu'),
layers.Dense(units=64, activation='relu'),
layers.Dense(units=10, activation='softmax')
])
# 使用ModelCheckpoint回调函数保存每个epoch的模型权重
checkpoint_lenet5 = tf.keras.callbacks.ModelCheckpoint('lenet5.h5', save_best_only=True)
checkpoint_lenet5_bn = tf.keras.callbacks.ModelCheckpoint('lenet5_bn.h5', save_best_only=True)
checkpoint_fc = tf.keras.callbacks.ModelCheckpoint('fc.h5', save_best_only=True)
# 训练LeNet-5模型
model_lenet5.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history_lenet5 = model_lenet5.fit(x_train, y_train, batch_size=32, epochs=20, validation_data=(x_test, y_test), callbacks=[checkpoint_lenet5])
# 训练在LeNet-5上增加BN层的模型
model_lenet5_bn.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history_lenet5_bn = model_lenet5_bn.fit(x_train, y_train, batch_size=32, epochs=20, validation_data=(x_test, y_test), callbacks=[checkpoint_lenet5_bn])
# 训练全连接网络
model_fc.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history_fc = model_fc.fit(x_train, y_train, batch_size=32, epochs=20, validation_data=(x_test, y_test), callbacks=[checkpoint_fc])
# 绘制训练集上的精度变化曲线
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_fc.history['accuracy'], label='Fully Connected')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.show()
```
希望这些代码能对您有所帮助!
阅读全文