对我们之前构造的简单神经网络进行一些修改,就可以进行MINIST手写数字的分类了。接下来详细逐部分分析代码。
1. 引入MNIST数据(主函数)
程序运行后会自动下载,之后再次运行会直接使用已下载好的数据,但是也有存在网络不好下载不成功的情况,可以百度上搜到下载至本地,文件夹命名为MNIST_data。
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True) # get MINIST data
数据中包含55000张训练图片,每张图片的分辨率是28×28,所以我们的训练网络输入应该是28×28=784个像素数据。而每张图片的类别用向量表示,如: [0,0,0,1,0,0,0,0,0,0]表示3,[0,0,0,0,1,0,0,0,0,0]表示4
2. 构造神经层
这部分跟《Tensorboard进行可视化 2》文中的代码没有什么不同。
def add_layer(inputs, in_size, out_size, n_layer,activation_function=None):
layer_name = 'layer%s' % n_layer
with tf.name_scope(layer_name):
with tf.name_scope('weights'):
Weights = tf.Variable(tf.random_normal([in_size,out_size]), name='W') # Weight matrix
tf.summary.histogram(layer_name+'/weights',Weights)
with tf.name_scope('biases'):
biases = tf.Variable(tf.zeros([1, out_size])+0.1, name='b') #Biases is not suggested to be zero, so set +0.1 here
tf.summary.histogram(layer_name+'/biases',biases)
with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.add(tf.matmul(inputs,Weights),biases)
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b,)
tf.summary.histogram(layer_name+'/outputs',outputs)
return outputs
add_layer()函数做了4件事:
(1)给需要在Tensorboard中显示的变量的图层命名:使用with tf.name_scope()
(2)定义变量: Weight, biases, Wx_plus_b的值:使用tf.Variable() ,tf.add(),tf.matmul()
(3)定义神经层output为激活函数的值: 未指定激活函数时使用Wx_plus_b
(4)绘制Histogram图:tf.summary.histogram(,)
![](https://img.haomeiwen.com/i1780773/4ffdf3abad3697bd.png)
3. 计算预测准确率(这部分是在用test数据集测试时调用的)
compute_accuracy()函数做了3件事:
1、使用prediction神经层来预测测试集中的数字,预测结果(概率)存储到y_pre。
2、获取y_pre中概率最大值的下标向量,和v_ys最大值的下标向量,下标相等的就是预测正确的一组。是否预测正确以0,1数字存储在correct_prediction里面。tf.argmax(),tf.equal(), tf.cast()。
3、输出预测准确率:就是对correct_prediction取均值。
def compute_accuracy(v_xs, v_ys):
global prediction
y_pre = sess.run(prediction, feed_dict={xs:v_xs})
print(sess.run(tf.argmax(y_pre, 1)), sess.run(tf.argmax(v_ys, 1)))
correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
result = sess.run(accuracy, feed_dict={xs:v_xs, ys:v_ys})
return result
![](https://img.haomeiwen.com/i1780773/550cf329aa3f001c.png)
函数说明:
tf.argmax(input, 1) 按行(列=0)计算最大值
返回:Tensor 一般是行或列的最大值下标向量
tf.equal(A, B)是对比这两个矩阵或者向量的相等的元素,
如果是相等的那就返回True,不相等返回False,
返回的值的矩阵维度和A是一样的
tf.cast(, float32)把布尔型转化为浮点型
4、定义input数据的容器Placeholder(主函数)
# Store Observed Data with placeholder
with tf.name_scope('inputs'):
xs = tf.placeholder(tf.float32, [None,784],name='x_input') #28x28
ys = tf.placeholder(tf.float32, [None,10],name='y_input') # 0-9
5、增加神经层 (主函数)
# add output layer
prediction = add_layer(xs, 784, 10, n_layer=1, activation_function=tf.nn.softmax) # use tf.nn.softmax for classification
6、定义损失函数为交叉熵,使用GradientDescent最小化交叉熵(主函数)
# define loss function - cross entropy
with tf.name_scope('loss'):
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),
reduction_indices=[1]))
tf.summary.scalar('loss',cross_entropy)
# use Gradient Descent Optimizer to minimize loss
with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
7、创建Session,打包Summary, 记录logs,初始化变量(主函数)
sess = tf.Session()
merged = tf.summary.merge_all() # pack summary
writer = tf.summary.FileWriter('logs/',sess.graph)
# initiation
init = tf.global_variables_initializer()
sess.run(init)
8、训练数据,输出结果
(1)为避免训练太慢,每批取100张图片进行训练。
(2)每50次打印一次summary 并用测试集测试准确度。
for i in range(1000):
batch_xs,batch_ys = mnist.train.next_batch(100) # get 100 per batch, in case train is too slow
sess.run(train_step,feed_dict={xs:batch_xs,ys:batch_ys}) # learn 1000 times
if i%50==0:
print(compute_accuracy(mnist.test.images, mnist.test.labels))
result = sess.run(merged, feed_dict={xs:batch_xs,ys:batch_ys})
writer.add_summary(result,i)
以下是Tensorboard中的结果显示:
![](https://img.haomeiwen.com/i1780773/e9c30a427a8046f0.png)
![](https://img.haomeiwen.com/i1780773/de92a42456ce69cf.png)
![](https://img.haomeiwen.com/i1780773/d425240f81ba856e.png)
网友评论