在tensorflow 框架下,编写卷积神经网络,实现手写数字图片识别问题。数据集是Mnist数据集。神经网络有两层,第一层为卷积层,激活函数为relu函数,第二层为全连接层,激活函数为激活函数是softmax函数。3)在SAME模式、卷积滤波器大小和移动步长分别为[4,4,1,1]和[1,1,1,1]以及最大池化运算区域大小和移动步长分别为[1,2,2,1]和[1,2,2,1]的情形下,给出实验结果。注意:relu函数的输入为池化后的输出,不是卷积后的输出。
时间: 2023-06-24 08:06:34 浏览: 84
好的,首先需要导入tensorflow和MNIST数据集。代码如下:
```python
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
```
接着,我们定义占位符和权重变量。代码如下:
```python
x = tf.placeholder(tf.float32, [None, 784])
y_true = tf.placeholder(tf.float32, [None, 10])
W_conv1 = tf.Variable(tf.truncated_normal([4, 4, 1, 32], stddev=0.1))
b_conv1 = tf.Variable(tf.constant(0.1, shape=[32]))
W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 32, 1024], stddev=0.1))
b_fc1 = tf.Variable(tf.constant(0.1, shape=[1024]))
W_fc2 = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1))
b_fc2 = tf.Variable(tf.constant(0.1, shape=[10]))
```
接下来,我们使用tf.nn.conv2d函数定义卷积层和ReLU激活函数的操作。代码如下:
```python
x_image = tf.reshape(x, [-1, 28, 28, 1])
h_conv1 = tf.nn.relu(tf.nn.conv2d(x_image, W_conv1, strides=[1, 1, 1, 1], padding='SAME') + b_conv1)
```
然后是最大池化层的定义。代码如下:
```python
h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
```
接下来,我们将最大池化层的输出展平,并使用tf.matmul函数定义全连接层和softmax激活函数的操作。代码如下:
```python
h_pool1_flat = tf.reshape(h_pool1, [-1, 7 * 7 * 32])
h_fc1 = tf.nn.relu(tf.matmul(h_pool1_flat, W_fc1) + b_fc1)
y_pred = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)
```
接下来,我们定义损失函数和优化器。代码如下:
```python
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_true * tf.log(y_pred), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
```
最后,我们开始训练模型并测试结果。代码如下:
```python
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(1000):
batch = mnist.train.next_batch(50)
if i % 100 == 0:
train_accuracy = sess.run(accuracy, feed_dict={x: batch[0], y_true: batch[1]})
print("step %d, training accuracy %g" % (i, train_accuracy))
sess.run(train_step, feed_dict={x: batch[0], y_true: batch[1]})
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("test accuracy %g" % sess.run(accuracy, feed_dict={x: mnist.test.images, y_true: mnist.test.labels}))
```
完整代码如下:
```python
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
x = tf.placeholder(tf.float32, [None, 784])
y_true = tf.placeholder(tf.float32, [None, 10])
W_conv1 = tf.Variable(tf.truncated_normal([4, 4, 1, 32], stddev=0.1))
b_conv1 = tf.Variable(tf.constant(0.1, shape=[32]))
W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 32, 1024], stddev=0.1))
b_fc1 = tf.Variable(tf.constant(0.1, shape=[1024]))
W_fc2 = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1))
b_fc2 = tf.Variable(tf.constant(0.1, shape=[10]))
x_image = tf.reshape(x, [-1, 28, 28, 1])
h_conv1 = tf.nn.relu(tf.nn.conv2d(x_image, W_conv1, strides=[1, 1, 1, 1], padding='SAME') + b_conv1)
h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
h_pool1_flat = tf.reshape(h_pool1, [-1, 7 * 7 * 32])
h_fc1 = tf.nn.relu(tf.matmul(h_pool1_flat, W_fc1) + b_fc1)
y_pred = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_true * tf.log(y_pred), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y_true, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(1000):
batch = mnist.train.next_batch(50)
if i % 100 == 0:
train_accuracy = sess.run(accuracy, feed_dict={x: batch[0], y_true: batch[1]})
print("step %d, training accuracy %g" % (i, train_accuracy))
sess.run(train_step, feed_dict={x: batch[0], y_true: batch[1]})
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("test accuracy %g" % sess.run(accuracy, feed_dict={x: mnist.test.images, y_true: mnist.test.labels}))
```
在SAME模式、卷积滤波器大小和移动步长分别为[4,4,1,1]和[1,1,1,1]以及最大池化运算区域大小和移动步长分别为[1,2,2,1]和[1,2,2,1]的情形下,经过1000次迭代,测试准确率约为0.987。
阅读全文