直接上源码:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("../data/", one_hot = True) #读图片数据集
sess = tf.InteractiveSession() #创建session
#训练数据
xs = tf.placeholder("float", shape=[None,784])
#训练标签数据
ys = tf.placeholder("float", shape=[None, 10])
#把xs改成4维张量,第一维表示样本数量,第2维和第3维代表图像长宽, 第4维代表图像通道数, 1表示黑白
x_image = tf.reshape(xs, [-1, 28, 28, 1])
##第一层: 卷积层
#过滤器大小为5*5,当前深度为1,过滤器深度为32
conv1_weights = tf.get_variable("conv1_weights", [5,5,1,32], initializer=tf.truncated_normal_initializer(stddev=0.1))
conv1_bias = tf.get_variable("conv1_bias", [32], initializer=tf.constant_initializer(0.0))
#移动步长为1,使用全0填充
conv1 = tf.nn.conv2d(x_image, conv1_weights, strides=[1,1,1,1], padding='SAME')
#激活函数Relu
relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_bias))
#28*28*32
##第二层:最大池化层
pool1 = tf.nn.max_pool(relu1, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
#14*14*32
##第三层:卷积层
conv2_weights = tf.get_variable("conv2_weights", [5,5,32,64], initializer=tf.truncated_normal_initializer(stddev=0.1))
#过滤器大小为5*5, 当前层深度为32, 过滤器的深度为64
conv2_bias = tf.get_variable("conv2_bias", [64], initializer=tf.constant_initializer(0.0))
conv2 = tf.nn.conv2d(pool1, conv2_weights, strides=[1,1,1,1], padding='SAME')
relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_bias))
#14*14*64
##第四层:最大池化层
#池化层过滤器大小为2*2,,移动步长为2,使用全0填充
pool2 = tf.nn.max_pool(relu2, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
#7*7*64
#第5层,全连接层
fc1_weights = tf.get_variable("fc_weights", [7*7*64, 1024], initializer=tf.truncated_normal_initializer(stddev=0.1))
fc1_bias = tf.get_variable("fc_bias", [1024], initializer=tf.constant_initializer(0.1))
pool2_vector = tf.reshape(pool2, [-1, 7*7*64])
fc1 = tf.nn.relu(tf.matmul(pool2_vector, fc1_weights)+fc1_bias)
#为了减少过拟合,加入Dropout层
keep_prob = tf.placeholder(tf.float32)
fc1_dropout = tf.nn.dropout(fc1, keep_prob)
#第六层,全连接层
fc2_weights = tf.get_variable("fc2_weights", [1024, 10], initializer=tf.truncated_normal_initializer(stddev=0.1)) #神经元节点数1024,分类节点10
fc2_bias = tf.get_variable("fc2_bias", [10], initializer=tf.constant_initializer(0.1))
fc2 = tf.matmul(fc1_dropout, fc2_weights) + fc2_bias
#第七层,输出层,softmax
y_conv = tf.nn.softmax(fc2)
#定义交叉熵网络
cross_entropy = -tf.reduce_sum(ys*tf.log(y_conv)) #定义交叉熵为loss的函数
train_step = tf.train.GradientDescentOptimizer(0.0001).minimize(cross_entropy) #调用优化器来优化,实际上通过大量数据,争取cross_entropy最小化
# tf.argmax()返回的是某一维度上其数据最大所在的索引值,在这里即代表预测值和真实值
# 判断预测值y和真实值ys中最大数的索引是否一致,y的值为1-10概率
correct_prediction = tf.equal(tf.arg_max(y_conv,1), tf.arg_max(ys,1))
# 用平均值来统计测试准确率
accurary = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
tf.global_variables_initializer().run()
for i in range(20000):#迭代20000次
batch = mnist.train.next_batch(50)
if i%100 == 0:
train_accuracy = accurary.eval(feed_dict={xs:batch[0], ys:batch[1], keep_prob: 1.0})
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={xs:batch[0], ys:batch[1], keep_prob: 0.5})
#在测试数据上测试准确率
内存不够,只测了2000张
print("test accuracy %g"%accurary.eval(feed_dict={xs: mnist.test.images[0:2000], ys: mnist.test.labels[0:2000], keep_prob: 1.0}))
训练结果截图: