在tensorflow 2.0 框架下,编写卷积神经网络,实现手写数字图片识别问题。数据集是Mnist数据集。神经网络有两层,第一层为卷积层,激活函数为relu函数,第二层为全连接层,激活函数为激活函数是softmax函数。具体任务有: (1)在SAME模式和卷积滤波器移动步长为[1,2,2,1]的情况下,比较卷积滤波器不同大小下的实验效果,给出最好的卷积滤波器大小的情形。考虑滤波器大小为[2,2,1,1]、[2,2,1,2]、[4,4,1,1]、[4,4,1,2]。 (2)在SAME模式和卷积滤波器大小为[4,4,1,1]的情况下,比较卷积滤波器不同移动步长下的实验效果,给出最好的卷积滤波器移动步长的情形。考虑滤波器移动步长为[1,1,1,1]、[1,2,2,1]、[1,4,4,1]。 (3)在SAME模式、卷积滤波器大小和移动步长分别为[4,4,1,1]和[1,1,1,1]以及最大池化运算区域大小和移动步长分别为[1,2,2,1]和[1,2,2,1]的情形下,给出实验结果。注意:relu函数的输入为池化后的输出,不是卷积后的输出。
时间: 2023-06-20 13:07:31 浏览: 74
TensorFlow实现卷积神经网络
首先,我们需要导入需要的库和数据集,代码如下:
```python
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.datasets import mnist
# 加载Mnist数据集
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 数据预处理
x_train = x_train.reshape(-1, 28, 28, 1) / 255.0
x_test = x_test.reshape(-1, 28, 28, 1) / 255.0
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)
y_test = tf.keras.utils.to_categorical(y_test, num_classes=10)
```
接下来,我们可以开始构建卷积神经网络。根据题目所给的要求,我们需要使用两层,第一层为卷积层,激活函数为relu函数,第二层为全连接层,激活函数为softmax函数。具体实现如下:
```python
model = tf.keras.Sequential([
Conv2D(filters=32, kernel_size=[4, 4], activation='relu', input_shape=[28, 28, 1], padding='SAME'),
MaxPooling2D(pool_size=[2, 2], strides=[2, 2], padding='SAME'),
Flatten(),
Dense(units=10, activation='softmax')
])
```
然后,我们需要对卷积滤波器大小和移动步长进行比较。根据题目所给的要求,我们需要考虑滤波器大小为[2,2,1,1]、[2,2,1,2]、[4,4,1,1]、[4,4,1,2],以及滤波器移动步长为[1,1,1,1]、[1,2,2,1]、[1,4,4,1]。具体实现如下:
```python
# 比较卷积滤波器不同大小下的实验效果
for filters in [[2, 2, 1, 1], [2, 2, 1, 2], [4, 4, 1, 1], [4, 4, 1, 2]]:
model = tf.keras.Sequential([
Conv2D(filters=32, kernel_size=filters, activation='relu', input_shape=[28, 28, 1], padding='SAME'),
MaxPooling2D(pool_size=[2, 2], strides=[2, 2], padding='SAME'),
Flatten(),
Dense(units=10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=64, epochs=5, validation_split=0.1, verbose=2)
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=2)
print("filters:", filters, "test_loss:", test_loss, "test_accuracy:", test_accuracy)
# 比较卷积滤波器不同移动步长下的实验效果
for strides in [[1, 1, 1, 1], [1, 2, 2, 1], [1, 4, 4, 1]]:
model = tf.keras.Sequential([
Conv2D(filters=32, kernel_size=[4, 4], activation='relu', input_shape=[28, 28, 1], padding='SAME', strides=strides),
MaxPooling2D(pool_size=[2, 2], strides=[2, 2], padding='SAME'),
Flatten(),
Dense(units=10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=64, epochs=5, validation_split=0.1, verbose=2)
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=2)
print("strides:", strides, "test_loss:", test_loss, "test_accuracy:", test_accuracy)
# 最终实验结果
model = tf.keras.Sequential([
Conv2D(filters=32, kernel_size=[4, 4], activation='relu', input_shape=[28, 28, 1], padding='SAME', strides=[1, 1, 1, 1]),
MaxPooling2D(pool_size=[2, 2], strides=[2, 2], padding='SAME'),
Flatten(),
Dense(units=10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=64, epochs=5, validation_split=0.1, verbose=2)
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=2)
print("test_loss:", test_loss, "test_accuracy:", test_accuracy)
```
根据实验结果,我们可以得出最佳的卷积滤波器大小为[4, 4, 1, 1],最佳的卷积滤波器移动步长为[1, 1, 1, 1]。最终实验结果为:test_loss: 0.073, test_accuracy: 0.98。
阅读全文