Tensorflow学习4: MNIST数据集手写数字识别

1 MNIST数据集手写数字识别(简单版)

1
2
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#载入数据集
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)

#每个批次大小
batch_size = 100
#计算有多少批次
batch_num = mnist.train.num_examples // batch_size
input_x = tf.placeholder(tf.float32, [None, 784])
input_y = tf.placeholder(tf.float32, [None, 10])

#创建神经网络模型
W1 = tf.Variable(tf.truncated_normal([784,128], 0.,0.5))
b1 = tf.Variable(tf.zeros([128]) + 0.1)
L1 = tf.nn.relu(tf.matmul(input_x, W1) + b1)

W2 = tf.Variable(tf.truncated_normal([128,10], 0.,0.5))
b2 = tf.Variable(tf.zeros([10]) + 0.1)
L2 = tf.matmul(L1, W2) + b2

#tf.nn.softmax_cross_entropy_with_logits
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=L2, labels=input_y))
#loss = tf.reduce_mean(tf.square(L2 - input_y))
train = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

#获取用于显示的精度——优化效果
correct_indices = tf.equal(tf.argmax(input_y, 1), tf.argmax(L2, 1))
accuracy = tf.reduce_mean(tf.cast(correct_indices, tf.float32))

init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in range(20):
for batch in range(batch_num):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
sess.run(train, feed_dict={input_x:batch_xs, input_y:batch_ys})
acc = sess.run(accuracy, feed_dict={input_x:mnist.test.images, input_y:mnist.test.labels})
print("epoch:"+ str(epoch) + ", accuracy:"+ str(acc))

Extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
epoch:0, accuracy:0.9072
epoch:1, accuracy:0.9248
epoch:2, accuracy:0.9319
epoch:3, accuracy:0.9375
epoch:4, accuracy:0.9432
epoch:5, accuracy:0.9469
epoch:6, accuracy:0.9502
epoch:7, accuracy:0.9534
epoch:8, accuracy:0.9533
epoch:9, accuracy:0.9574
epoch:10, accuracy:0.9557
epoch:11, accuracy:0.9577
epoch:12, accuracy:0.9558
epoch:13, accuracy:0.9588
epoch:14, accuracy:0.9593
epoch:15, accuracy:0.9595
epoch:16, accuracy:0.9604
epoch:17, accuracy:0.9613
epoch:18, accuracy:0.9608
epoch:19, accuracy:0.962


总结:
1 整除 //
2 最后一层不使用激活函数,可以使用softmax

2 MNIST数据集手写数字识别(优化版)

1
2
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#载入数据集
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)

#每个批次大小
batch_size = 100
#计算有多少批次
batch_num = mnist.train.num_examples // batch_size

keep_prob = tf.constant(1., tf.float32)
lr = tf.Variable(0.01, dtype=tf.float32)
input_x = tf.placeholder(tf.float32, [None, 784])
input_y = tf.placeholder(tf.float32, [None, 10])

#创建神经网络模型

W1 = tf.Variable(tf.truncated_normal([784,300], 0.,0.5))
b1 = tf.Variable(tf.zeros([300]) + 0.1)
L1 = tf.nn.relu(tf.matmul(input_x, W1) + b1)
L1_drop = tf.nn.dropout(L1, keep_prob=keep_prob)

W2 = tf.Variable(tf.truncated_normal([300,100], 0.,0.5))
b2 = tf.Variable(tf.zeros([100]) + 0.1)
L2 = tf.nn.relu(tf.matmul(L1_drop, W2) + b2)

W3 = tf.Variable(tf.truncated_normal([100,10], 0.,0.5))
b3 = tf.Variable(tf.zeros([10]) + 0.1)
L3 = tf.matmul(L2, W3) + b3


#tf.nn.softmax_cross_entropy_with_logits
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=L3, labels=input_y))
# train = tf.train.RMSPropOptimizer(lr).minimize(loss)
train = tf.train.RMSPropOptimizer(lr).minimize(loss)

#获取用于显示的精度——优化效果
correct_indices = tf.equal(tf.argmax(input_y, 1), tf.argmax(L3, 1))
accuracy = tf.reduce_mean(tf.cast(correct_indices, tf.float32))

init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in range(20):
#每次迭代更新学习率
sess.run(tf.assign(lr, 0.01*(0.9**epoch) ))
for batch in range(batch_num):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
sess.run(train, feed_dict={input_x:batch_xs, input_y:batch_ys})
_accuracy = sess.run(accuracy, feed_dict={input_x:mnist.test.images, input_y:mnist.test.labels})
print("epoch:"+ str(epoch) + ", accuracy:"+ str(_accuracy))

Extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
epoch:0, accuracy:0.9192
epoch:1, accuracy:0.9496
epoch:2, accuracy:0.9543
epoch:3, accuracy:0.951
epoch:4, accuracy:0.9553
epoch:5, accuracy:0.9631
epoch:6, accuracy:0.9695
epoch:7, accuracy:0.9679
epoch:8, accuracy:0.968
epoch:9, accuracy:0.9705
epoch:10, accuracy:0.9659
epoch:11, accuracy:0.9745
epoch:12, accuracy:0.9742
epoch:13, accuracy:0.9757
epoch:14, accuracy:0.9765
epoch:15, accuracy:0.9769
epoch:16, accuracy:0.9768
epoch:17, accuracy:0.9771
epoch:18, accuracy:0.977
epoch:19, accuracy:0.9771


总结:
1 动态学习率
2 dropout
3 加1个隐层
4 使用RMSPropOptimizer优化器

꧁༺The༒End༻꧂