手写数字识别思路
1.导入手写数字图片数据库
2.分析图片的特点, 定义变量
3.搭建模型
4.训练模型
5.测试模型
6.保存模型
7.读取、使用模型
准备(ssl防止证书过期报错)
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
1.导入手写数字图片数据库
from tensorflow.examples.tutorials.mnist import input_data
# 1.加载手写数字数据库
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
print("输入数据:\n", mnist.train.images)
print("输入数据的shape:", mnist.train.images.shape)
说明:
2.分析图片的特点, 定义变量
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
3.搭建模型
W = tf.Variable(tf.random_normal([784, 10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x, W) + b)
# 损失函数
cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(prediction), reduction_indices=1))
# 定义参数
learning_rate = 0.01
# 使用梯度下降优化器
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
4.训练模型
training_epoch = 25
batch_size = 100
display_step = 1
saver = tf.train.Saver()
model_path = "log/mnist_model.ckpt"
with tf.Session() as sess:
# 初始化所有变量
sess.run(tf.global_variables_initializer())
for epoch in range(training_epoch):
total_batch = int(mnist.train.num_examples/batch_size)
avg_cost = 0
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
_, c = sess.run([optimizer, cost], feed_dict={x: batch_xs, y: batch_ys})
avg_cost += c / total_batch
if(epoch + 1) % display_step == 0:
print("epoch:", "%04d" % (epoch + 1), "cost = ", "{:.9f}".format(avg_cost))
print("Train Finished!")
5.测试模型
# 测试模型
correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("正确率:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))
6.保存模型
# 保存模型
save_path = saver.save(sess, model_path)
print("Model保存路径:", save_path)
7.读取、使用模型
with tf.Session() as sess:
# 初始化全局变量
sess.run(tf.global_variables_initializer())
saver.restore(sess, model_path)
# 测试model
output = tf.argmax(prediction, 1)
batch_xs, batch_ys = mnist.train.next_batch(2)
output_value, prediction_value = sess.run([output, prediction], feed_dict={x: batch_xs})
print("识别结果:", output_value, "\n", prediction_value, "\n", batch_ys)
# 打印图片
im = batch_xs[0]
im = im.reshape(-1, 28)
pylab.imshow(im)
pylab.show()
im = batch_xs[1]
im = im.reshape(-1, 28)
pylab.imshow(im)
pylab.show()