7. cross entropy

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# Loading data set 
MNIST = input_data.read_data_sets ( " MNIST_data " , one_hot = True) 

# batch size 
the batch_size = 64 # calculates a total period of a number of batches 
n_batch mnist.train.num_examples // = the batch_size # define two placeholder 
X = tf.placeholder (tf.float32, [None, 784 ]) 
Y = tf.placeholder (tf.float32, [None, 10 ]) # Create a simple neural network: 784-10 
W is = tf.Variable ( tf.truncated_normal ([784,10], STDDEV = 0.1 )) 
B = tf.Variable (tf.zeros ([10]) + 0.1 ) 
Prediction = tf.nn.softmax (tf.matmul (X, W is) + B ) #






Quadratic cost function 
# Loss = tf.losses.mean_squared_error (Y, Prediction) 
# Cross Entropy 
Loss = tf.losses.softmax_cross_entropy (Y, Prediction) 

# using a gradient descent method 
Train tf.train.GradientDescentOptimizer = (0.3 ) .minimize ( Loss) 

# store the result in a Boolean list 
correct_prediction = tf.equal (tf.argmax (Y,. 1), tf.argmax (Prediction,. 1 ))
 # required accuracy 
accuracy = tf.reduce_mean (tf.cast (correct_prediction , tf.float32)) 

with tf.Session () AS sess: 
    # variable initialization 
    sess.run (tf.global_variables_initializer ())
     # cycles epoch: trained once all the data is a cycle 
    for Epoch in the Range (21):
         For BATCH in Range (n_batch):
             # access to data and a tag batch 
            batch_xs, batch_ys = mnist.train.next_batch (the batch_size) 
            sess.run (Train, feed_dict = {X: batch_xs, Y: batch_ys})
         # each training cycle do a test 
        ACC = sess.run (Accuracy, feed_dict = {X: mnist.test.images, Y: mnist.test.labels})
         Print ( " Iter " + STR (Epoch) + " , the Accuracy testing " + str (the ACC))
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
Iter 0,Testing Accuracy 0.7473
Iter 1,Testing Accuracy 0.8413
Iter 2,Testing Accuracy 0.9066
Iter 3,Testing Accuracy 0.9113
Iter 4,Testing Accuracy 0.9143
Iter 5,Testing Accuracy 0.9168
Iter 6,Testing Accuracy 0.9199
Iter 7,Testing Accuracy 0.9201
Iter 8,Testing Accuracy 0.9202
Iter 9,Testing Accuracy 0.9213
Iter 10,Testing Accuracy 0.921
Iter 11,Testing Accuracy 0.9205
Iter 12,Testing Accuracy 0.9214
Iter 13,Testing Accuracy 0.923
Iter 14,Testing Accuracy 0.9237
Iter 15,Testing Accuracy 0.9238
Iter 16,Testing Accuracy 0.924
Iter 17,Testing Accuracy 0.9231
Iter 18,Testing Accuracy 0.9246
Iter 19,Testing Accuracy 0.925
Iter 20,Testing Accuracy 0.9253

Guess you like

Origin www.cnblogs.com/liuwenhua/p/11605466.html