from __future__ import print_function
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) #下载文件放到MNIST_data目录下
print("train_img:",mnist.train.images.shape) #55000张训练图
print("train_lbl:",mnist.train.labels.shape)
print("test_img:",mnist.test.images.shape) #10000张测试图
print("test_lbl:",mnist.test.labels.shape)
learning_rate = 0.01
training_epochs = 50
batch_size = 100
display_step = 1
x = tf.placeholder(tf.float32, [None, 784]) # mnist中的训练图是28*28=784的图片
y = tf.placeholder(tf.float32, [None, 10]) # 0-9 的数字 => 10 classes
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
predict = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax逻辑回归模型
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(predict), reduction_indices=1))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
init = tf.global_variables_initializer()
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})
#上面表达式等价于下面方式:
# sess.run(optimizer, {x: batch_xs, y: batch_ys})
# c = sess.run(cost, {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("Optimization Finished!")
correct_prediction = tf.equal(tf.argmax(predict, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))
结果:
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
train_img: (55000, 784)
train_lbl: (55000, 10)
test_img: (10000, 784)
test_lbl: (10000, 10)
Epoch: 0001 cost= 1.176489531
Epoch: 0002 cost= 0.662428102
Epoch: 0003 cost= 0.550669022
Epoch: 0004 cost= 0.496786994
Epoch: 0005 cost= 0.463793346
Epoch: 0006 cost= 0.440911430
Epoch: 0007 cost= 0.423949281
Epoch: 0008 cost= 0.410678503
Epoch: 0009 cost= 0.399907770
Epoch: 0010 cost= 0.390889884
Epoch: 0011 cost= 0.383404499
Epoch: 0012 cost= 0.376811127
Epoch: 0013 cost= 0.371049813
Epoch: 0014 cost= 0.365936547
Epoch: 0015 cost= 0.361368383
Epoch: 0016 cost= 0.357302430
Epoch: 0017 cost= 0.353572087
Epoch: 0018 cost= 0.350146192
Epoch: 0019 cost= 0.347043392
Epoch: 0020 cost= 0.344097957
Epoch: 0021 cost= 0.341507307
Epoch: 0022 cost= 0.339022189
Epoch: 0023 cost= 0.336690402
Epoch: 0024 cost= 0.334497478
Epoch: 0025 cost= 0.332458832
Epoch: 0026 cost= 0.330518939
Epoch: 0027 cost= 0.328740775
Epoch: 0028 cost= 0.326980677
Epoch: 0029 cost= 0.325429160
Epoch: 0030 cost= 0.323856879
Epoch: 0031 cost= 0.322389204
Epoch: 0032 cost= 0.321006620
Epoch: 0033 cost= 0.319638948
Epoch: 0034 cost= 0.318378163
Epoch: 0035 cost= 0.317121298
Epoch: 0036 cost= 0.315938809
Epoch: 0037 cost= 0.314795836
Epoch: 0038 cost= 0.313735532
Epoch: 0039 cost= 0.312705021
Epoch: 0040 cost= 0.311682237
Epoch: 0041 cost= 0.310774488
Epoch: 0042 cost= 0.309808256
Epoch: 0043 cost= 0.308927144
Epoch: 0044 cost= 0.308017197
Epoch: 0045 cost= 0.307202091
Epoch: 0046 cost= 0.306348794
Epoch: 0047 cost= 0.305591306
Epoch: 0048 cost= 0.304780430
Epoch: 0049 cost= 0.304073534
Epoch: 0050 cost= 0.303377406
Optimization Finished!
Accuracy: 0.9192