'''
sess
prdict y=softmax(wx+b)
label y_
cross-entropy
train
initial
accuracy
test
save
restore
'''
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
file = './MNIST/MNIST_data/'
mnist = input_data.read_data_sets(file,one_hot=True)
sess = tf.InteractiveSession()
x = tf.placeholder(dtype=tf.float32,shape=[None,784],name='x')
W = tf.Variable(tf.zeros(shape=[784,10]))
b = tf.Variable(tf.zeros(shape=[10]))
y = tf.nn.softmax(tf.matmul(x,W) + b)
y_ = tf.placeholder(dtype=tf.float32,shape=[None,10],name='y_')
cross_entropy = tf.reduce_mean(-tf.reduce_sum(input_tensor=y_*tf.log(y),reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
saver = tf.train.Saver()
tf.global_variables_initializer().run()
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
train_step.run(feed_dict={x:batch_xs, y_:batch_ys})
correct_prediction = tf.equal(tf.argmax(input=y, axis=1),tf.argmax(input=y_, axis=1))
accuracy = tf.reduce_mean(tf.cast(x=correct_prediction,dtype=tf.float32))
save_path = saver.save(sess=sess, save_path='./model_mnist.ckpt')
test_accuracy = accuracy.eval({x:mnist.test.images, y_:mnist.test.labels})
print(test_accuracy)
saver.restore(sess,'./model_mnist.ckpt')
result = accuracy.eval({x:mnist.test.images, y_:mnist.test.labels})
print(result)
92%