学习了一下Tensorflow官方教程,以下是在MNIST数据集上做卷积神经网络。
Tensorflow版本:1.10
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(dtype=tf.float32, shape=[None, 28 * 28])
x_images = tf.reshape(x, [-1, 28, 28, 1])
y_true = tf.placeholder(dtype=tf.float32, shape=[None, 10])
# 创建权
def weight_variable(shape):
initial = tf.truncated_normal(shape=shape, stddev=0.1)
return tf.Variable(initial)
# 创建偏置
def bias_variable(shape):
# 由于使用ReLU神经元,因此用一个较小的正数来初始化
initial = tf.constant(0.1, dtype=tf.float32, shape=shape)
return tf.Variable(initial)
# 卷积
def conv2d(x, W):
return tf.nn.conv2d(input=x, filter=W, strides=[1, 1, 1, 1], padding='SAME')
# 池化
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
# 第一层卷积
# 5x5x1是核,宽5,高5,厚度(颜色通道)1。32是输出特征数,即核数
W_conv1 = weight_variable(shape=[5, 5, 1, 32])
b_conv1 = bias_variable(shape=[32])
hidden_conv1 = tf.nn.relu(conv2d(x_images, W_conv1) + b_conv1) # [28, 28, 32]
hidden_pool1 = max_pool_2x2(hidden_conv1) # [14, 14, 32]
# 第二层卷积
W_conv2 = weight_variable(shape=[5, 5, 32, 64])
b_conv2 = bias_variable(shape=[64])
hidden_conv2 = tf.nn.relu(conv2d(x=hidden_pool1, W=W_conv2) + b_conv2) # [14, 14, 64]
hidden_pool2 = max_pool_2x2(hidden_conv2) # [7, 7, 64]
# 密集连接层
# 打平,然后全连接到1024个神经元去,处理整张图片
h_pool2_flat = tf.reshape(hidden_pool2, [-1, 7 * 7 * 64])
W_fc1 = weight_variable(shape=[7 * 7 * 64, 1024])
b_fc1 = bias_variable(shape=[1024])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
# 减少过拟合,dropout操作,屏蔽一部分神经元的输出
# 在训练时dropout一部分连接;在观察准确率时使用全部连接,不dropout
keep_prob = tf.placeholder(dtype=tf.float32)
h_fc1_drop = tf.nn.dropout(x=h_fc1, keep_prob=keep_prob)
# 输出层,10个分类
W_fc2 = weight_variable(shape=[1024, 10])
b_fc2 = bias_variable(shape=[10])
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
# 损失函数,用交叉熵
cross_entropy = -tf.reduce_sum(y_true * tf.log(y_conv))
# 优化器
train_step = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cross_entropy)
# 准确率
correct_prediction = tf.equal(tf.argmax(y_true, axis=1), tf.argmax(y_conv, axis=1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# 运行
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(1001):
batch_xs, batch_ys = mnist.train.next_batch(50)
sess.run(train_step, feed_dict={x: batch_xs, y_true: batch_ys, keep_prob: 0.5})
# 看性能指标
if i % 10 == 0:
acc = sess.run(accuracy, feed_dict={x: batch_xs, y_true: batch_ys, keep_prob: 1.0})
print('step', i, 'acc:', acc)
print('计算测试集acc')
test_acc = sess.run(accuracy, feed_dict={x: mnist.test.images,
y_true: mnist.test.labels,
keep_prob: 1.0})
print('test acc:', test_acc)
输出:
step 0 acc: 0.26
step 10 acc: 0.76
step 20 acc: 0.84
step 30 acc: 0.84
step 40 acc: 0.94
step 50 acc: 0.94
step 60 acc: 0.94
step 70 acc: 0.88
step 80 acc: 0.94
step 90 acc: 0.94
step 100 acc: 0.94
step 110 acc: 0.98
step 120 acc: 0.96
...
step 950 acc: 0.98
step 960 acc: 1.0
step 970 acc: 1.0
step 980 acc: 0.98
step 990 acc: 1.0
step 1000 acc: 1.0
计算测试集acc
test acc 0.983
由于我用的CPU版Tensorflow,所以训练很慢,只训练了1000次,准确率98.3%。