【Tensorflow】MNIST解析

2020-02-27  本文已影响0人  紫晶葫芦

1.准备数据
2.搭建模型
3.训练模型
4.保存模型
5.测试模型
6.查看模型

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# one_hot 的编码格式:1就是 1000000000
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

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分类
r = tf.matmul(x, W) + b
pred = tf.nn.softmax(r)

# 反向传播,将生成的pred与样本标签y进行一次交叉熵运算最小化cost,reduction_indices=1按列合计
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)

init = tf.global_variables_initializer()
training_epochs = 25
batch_size = 100
display_step = 1
saver = tf.train.Saver(max_to_keep=1)
savedir = "mini/6-3.ckpt"

with tf.Session() as sess:
    sess.run(init)
    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(mnist.train.num_examples/batch_size)
        # 遍历全部数据集
        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("Finished!")
    # 测试
    correct_prediction = tf.equal(tf.arg_max(pred, 1), tf.arg_max(y, 1))
    # 计算准确率
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.int32))
    print("accuracy:", accuracy.eval({x: mnist.test.images, y:mnist.test.labels}))

    # 储存模型
    save_path = saver.save(sess,savedir)
    print("model saved in file :%s" % save_path)


import  pylab
# 读取模型
print("Startin 2nd session...")
with tf.Session() as sess2:
    #初始化所有变量
    sess2.run(init)
    saver.restore(sess2,savedir)
    # 测试
    correct_prediction = tf.equal(tf.arg_max(pred, 1), tf.arg_max(y, 1))
    # 计算准确率
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.int32))
    print("accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

    output = tf.arg_max(pred, 1)
    batch_xs, batch_ys = mnist.train.next_batch(2)
    outputval, predv = sess2.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()
上一篇下一篇

猜你喜欢

热点阅读