1 import tensorflow as tf
 2 import numpy as np
 3 
 4 # tensorflow自带了MNIST数据集
 5 from tensorflow.examples.tutorials.mnist import input_data
 6 
 7 # 下载mnist数据集
 8 mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
 9 # 数字(label)只能是0-9,神经网络使用10个出口节点就可以编码表示0-9;
10 #  1 -> [0,1.0,0,0,0,0,0,0,0]   one_hot表示只有一个出口节点是hot
11 #  2 -> [0,0.1,0,0,0,0,0,0,0]
12 #  5 -> [0,0,0,0,0,1.0,0,0,0]
13 
14 # 定义每个层有多少'神经元''
15 n_input_layer = 28 * 28  # 输入层
16 
17 n_layer_1 = 500  # hide layer
18 n_layer_2 = 1000  # hide layer
19 n_layer_3 = 300  # hide layer(隐藏层)听着很神秘,其实就是除输入输出层外的中间层
20 
21 n_output_layer = 10  # 输出层
22 
23 
24 # 定义待训练的神经网络(feedforward)
25 def nn(input_layer):
26     layer_1 = tf.layers.dense(input_layer, n_layer_1, tf.nn.relu)
27     layer_2 = tf.layers.dense(layer_1, n_layer_2, tf.nn.relu)
28     layer_3 = tf.layers.dense(layer_2, n_layer_3, tf.nn.relu)
29     layer_output = tf.layers.dense(layer_3, n_output_layer)
30 
31     return layer_output
32 
33 
34 batch_size = 100
35 
36 X = tf.placeholder('float', [None, 28 * 28])  # [None, 28*28]代表数据数据的高和宽(矩阵)  train数据有60000幅图片 即 [60000, 768]
37 Y = tf.placeholder('float')
38 
39 
40 def train(X, Y):
41     predict = nn(X)
42     print(predict)
43     cost_func = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=predict))
44     optimizer = tf.train.AdamOptimizer().minimize(cost_func)
45 
46     epochs = 13
47     with tf.Session() as session:
48         session.run(tf.global_variables_initializer())
49 
50         for epoch in range(epochs):
51             epoch_loss = 0
52             for i in range(int(mnist.train.num_examples / batch_size)):
53                 image, label = mnist.train.next_batch(batch_size)
54                 _, c = session.run([optimizer, cost_func], feed_dict={X: image, Y: label})
55                 epoch_loss += c
56             print(epoch, ' : ', epoch_loss)
57 
58         argmax_predict = tf.argmax(predict, 1)
59         correct = tf.equal(tf.argmax(predict, 1), tf.argmax(Y, 1))
60         accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
61         print('准确率: ', accuracy.eval({X: mnist.test.images, Y: mnist.test.labels}))
62         # print('准确率: ', session.run(accuracy, feed_dict={X: mnist.test.images, Y: mnist.test.labels}))
63 
64 
65 train(X, Y)
View Code

 

测试结果

0  :  115.849696128
1  :  45.6876613945
2  :  31.7030499699
3  :  23.4505812572
4  :  20.6026004103
5  :  15.7035064361
6  :  13.203762681
7  :  13.5197149374
8  :  10.9027512321
9  :  11.3654054473
10  :  9.61272907618
11  :  8.11556188527
12  :  8.21243002563
准确率:  0.9806

 

从MNIST数据集中读取数据包括用来train和test的image及label,分别为60000和10000条

nn中定义了一个五层的神经网络模型,输入为图片[N,28*28] 输出[N,10],标识0-9匹配比重

train中使用softmax_cross_entropy_with_logits计算交叉熵,再reduce_mean使用求均值,使用Adam算法的Optimizer训练

 

至此完成各个”方法“”的定义,这里我们称之为“方法”是各个tensorflow里面定义的规则对象变量,例如在 predict = nn(X) print该变量,发现输出 Tensor("Add_3:0", shape=(?, 10), dtype=float32)  即为一个Add方法,再调用sesion.run的时候会激活该“方法”,其中X、Y类型为placeholder可以认为传入"方法"中的"变量"(这里变量不同于tensorflow中的variable),

_, c = session.run([optimizer, cost_func], feed_dict={X: x, Y: y})

这里激活方法optimizer和cost_func,分别给"变量"X、Y传入"参数"image、label, 打印的loss逐步减少可以看到神经网络正常运转了。

 

 

测试效果

  通过tf.argmax求的最大概率那个位置1,再通过tf.equal完成对预测值和实际值的比对完成,输出正确率。其中accuracy.eval 同session.run(accuracy)

 

总结

 本文通过一个五层的神经网络完成对MNIST手写数据集的训练与测试,得到98%的正确率