tensorflow手写数字识别(有注释)

 1 import tensorflow as tf
 2 import numpy as np
 3 # const = tf.constant(2.0, name='const')
 4 # b = tf.placeholder(tf.float32, [None, 1], name='b')
 5 # # b = tf.Variable(2.0, dtype=tf.float32, name='b')
 6 # c = tf.Variable(1.0, dtype=tf.float32, name='c')
 7 #
 8 # d = tf.add(b, c, name='d')
 9 # e = tf.add(c, const, name='e')
10 # a = tf.multiply(d, e, name='a')
11 # init = tf.global_variables_initializer()
12 #
13 # print(a)
14 # with tf.Session() as sess:
15 #     sess.run(init)
16 #     ans = sess.run(a, feed_dict={b: np.arange(0, 10)[:, np.newaxis]})
17 # print(a)
18 # print(ans)
19 
20 from tensorflow.examples.tutorials.mnist import input_data
21 mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)  # 载入数据集
22 
23 learning_rate = 0.5  # 学习率
24 epochs = 10  # 训练10次所有的样本
25 batch_size = 100  # 每批训练的样本数
26 
27 x = tf.placeholder(tf.float32, [None, 784])  # 为训练集的特征提供占位符
28 y = tf.placeholder(tf.float32, [None, 10])  # 为训练集的标签提供占位符
29 
30 W1 = tf.Variable(tf.random_normal([784, 300], stddev=0.03), name='W1')  # 初始化隐藏层的W1参数
31 b1 = tf.Variable(tf.random_normal([300]), name='b1')  # 初始化隐藏层的b1参数
32 W2 = tf.Variable(tf.random_normal([300, 10], stddev=0.03), name='W2')  # 初始化全连接层的W1参数
33 b2 = tf.Variable(tf.random_normal([10]), name='b2')  # 初始化全连接层的b1参数
34 
35 hidden_out = tf.add(tf.matmul(x, W1), b1)  # 定义隐藏层的第一步运算
36 hidden_out = tf.nn.relu(hidden_out)  # 定义隐藏层经过激活函数后的运算
37 
38 y_ = tf.nn.softmax(tf.add(tf.matmul(hidden_out, W2), b2))  # 定义全连接层的输出运算
39 
40 y_clipped = tf.clip_by_value(y_, 1e-10, 0.9999999)
41 cross_entropy = -tf.reduce_mean(tf.reduce_sum(y * tf.log(y_clipped) + (1 - y) * tf.log(1 - y_clipped), axis=1))
42 # 交叉熵
43 
44 optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cross_entropy)
45 # 梯度下降优化器,传入的参数是交叉熵
46 
47 init = tf.global_variables_initializer()  # 所有参数初始化
48 
49 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))  # 返回true|false
50 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))  # 将true转化为1,false转化为0
51 
52 # 开始训练
53 with tf.Session() as sess:
54     sess.run(init)
55     total_batch = int(len(mnist.train.labels) / batch_size)  # 计算每个epoch要迭代几次
56     for epoch in range(epochs):
57         avg_cost = 0
58         for i in range(total_batch):
59             batch_x, batch_y = mnist.train.next_batch(batch_size=batch_size)
60             _, c = sess.run([optimizer, cross_entropy], feed_dict={x: batch_x, y: batch_y})
61             # 其实上面这一步只需要跑optimizer这个优化器就好了,因为交叉熵也会同时跑。
62             # 但是我们想要得到交叉熵的值来作为损失函数,所以还需要跑一个交叉熵。
63             avg_cost += c / total_batch
64         print("Epoch:", (epoch + 1), "cost = ", "{:.3f}".format(avg_cost))  # 这是每训练完所有样本得到的损失值
65     print(sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels}))
66     # 因为之前的计算已经把中间参数计算出来了,所以这里只用最后的计算测试集就行了

猜你喜欢

转载自www.cnblogs.com/yuanninesuns/p/10906784.html