1 概述
本程序所用数据集为MNIST,通过softmax激活函数训练样本,选择得到的标签数据one-hot verctor中的最大值作为结果。在学习率为0.01,batchsize为100,迭代次数为25次时,在测试集上的精确度到达了0.8316。
2 代码
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# 解决版本不兼容问题
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
# 导入读取MNIST数据包
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# 导入pylab绘图包
import pylab
# 重定义默认图
tf.reset_default_graph()
# 定义占位符
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
# 定义学习参数
W = tf.Variable(tf.random_normal([784, 10]))
b = tf.Variable(tf.zeros([10]))
# 定义预测值
# softmax用于多分类过程中,它将多个神经元的输出,映射到(0,1)区间内,可以看成概率来理解,从而来进行多分类
pred = tf.nn.softmax(tf.matmul(x, W) + b)
# 损失函数
cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices=1))
# 定义学习率参数
learning_rate = 0.01
# 梯度下降优化器
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# 定义迭代次数以及batch大小
training_epochs = 25
batch_size = 100
display_step = 1
# 启动session
with tf.Session() as sess:
# 全局变量初始化
sess.run(tf.global_variables_initializer())
# 开始训练
for epoch in range(training_epochs):
avg_cost = 0
# 求batch数量
total_batch = int(mnist.train.num_examples / batch_size)
# 对每一个batch进行训练
for i in range(total_batch):
# 获取batch中的样本值
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
# 一个batch里的梯度下降
_, c = sess.run([optimizer, cost], feed_dict={x: batch_xs, y: batch_ys})
# 平均损失
avg_cost += c / total_batch
# 所有batch完成一次梯度下降
# 展示迭代信息
if (epoch + 1) % display_step == 0:
print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(avg_cost))
# 完成训练
print("finished!")
# 测试model
correct_predoction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_predoction, tf.float32))
print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))
# 保存模型
saver = tf.train.Saver()
model_path = "log/521model.cpkt"
save_path = saver.save(sess, model_path)
print("Model saved in file: %s " % save_path)
# 读取模型
print("new session start!")
with tf.Session() as sess:
# 全局变量初始化
sess.run(tf.global_variables_initializer())
# 模型加载
saver.restore(sess, model_path)
# 测试model
correct_predoction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
# 计算准确率
accuracy = tf.reduce_mean(tf.cast(correct_predoction, tf.float32))
print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))
output = tf.argmax(pred, 1)
batch_xs, batch_ys = mnist.train.next_batch(2)
outputval, predv = sess.run([output, pred], feed_dict={x: batch_xs})
print(outputval, predv, 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()