Softmax source code

Here we report the complete source code for the softmax classifier:

import tensorflow as tf 
import mnist_data
import matplotlib.pyplot as plt
from random import randint
import numpy as np

logs_path = 'log_mnist_softmax'
batch_size = 100
learning_rate = 0.5
training_epochs = 10

mnist = mnist_data.read_data_sets("data")


X = tf.placeholder(tf.float32, [None, 28, 28, 1],name="input")
Y_ = tf.placeholder(tf.float32, [None, 10])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
XX = tf.reshape(X, [-1, 784])

Y = tf.nn.softmax(tf.matmul(XX, W) + b,name="output")
cross_entropy = -tf.reduce_mean(Y_ * tf.log(Y)) * 1000.0
correct_prediction = tf.equal(tf.argmax(Y, 1),
tf.argmax(Y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,
tf.float32))
train_step = tf.train.GradientDescentOptimizer
(0.005).minimize(cross_entropy)

tf.summary.scalar("cost", cross_entropy)tf.summary.scalar("accuracy", accuracy)summary_op = tf.summary.merge_all()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())

writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph())
for epoch in range(training_epochs):
batch_count = int(mnist.train.num_examples/batch_size)
for i in range(batch_count):
batch_x, batch_y = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={X: batch_x,
Y_: batch_y})

print "Epoch: ", epoch

print "Accuracy: ", accuracy.eval
(feed_dict={X: mnist.test.images,
Y_: mnist.test.labels})
print "done"

num = randint(0, mnist.test.images.shape[0])
img = mnist.test.images[num]

classification = sess.run(tf.argmax(Y, 1),
feed_dict={X: [img]})
print 'Neural Network predicted', classification[0]
print 'Real label is:', np.argmax(mnist.test.labels[num])

saver = tf.train.Saver()
save_path = saver.save(sess, "saved_mnist_cnn.ckpt")
print("Model saved to %s" % save_path)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset