tensorflow官方教程 - MNIST for ML Beginers - 代码及注释

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# Implementing the Regresson
x = tf.placeholder(tf.float32, [None, 784]) # None means a dimension can be of any length
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)

# Training
y_ = tf.placeholder(tf.float32, [None, 10])
    # The shape argument is optional, but it allows TensorFlow to automatically catch bugs.
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
    #tf.reduce_mean computes the mean over all the examples in the batch.
    # the function below are more stable:
    # cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_,logits=tf.matmul(x, W) + b))

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSession() # launch the model
    # allows you to interleave operations which build a computation graph with ones that run the graph.
tf.global_variables_initializer().run()
    # Before Variables can be used within a session, they must be initialized using that session.
for _ in range(1000): # run the training step 1000 times
    # each step of the loop, we get a "batch" of 100 random data points from our training set.
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x:batch_xs, y_:batch_ys}) # feeding in the batches data to replace the placeholder s
    # equal to: train_step.run(feed_dict={x: batch[0], y_: batch[1]})

# Evaluation our model
    # tf.argmax is an extremely useful function which gives you the index of the highest entry in a tensor along some axis.
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_:mnist.test.labels}))
    # equal to: print(accuracy.eval(feed_dict={x: mnist.test.images, y_:mnist.test.labels}))

>> 91.92%

# ======================================
# ==> notes here:
#
# using small batches of random data is called stochastic training - in this case: stachastic gradient descent.Ideally,
# we'd like to use all our data for every step of training because that would give us a better sense of what we should
# be doing, but that's expensive. So, instead, we use a different subset every time. Doing this is cheap and has much
# of the same benefit.
# =======================================


猜你喜欢

转载自blog.csdn.net/tsinghuahui/article/details/72989142