In [6]:
'''Softmax linear regression'''
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
In [7]:
mnist = input_data.read_data_sets("MNIST_data/", one_hot = True)
In [8]:
#will hold the input data
x = tf.placeholder(tf.float32, [None, 784])
In [10]:
#Parameters (Variables) to be tuned
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
In [12]:
#create the model
y = tf.nn.softmax(tf.matmul(x, W) + b)
In [13]:
#placeholder will hold the input data
y_ = tf.placeholder(tf.float32, [None, 10])
In [19]:
#define the cross entroy cost function
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
In [21]:
# define the optimization algorithm to be used
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
In [24]:
# launch the model in interactive session
sess = tf.InteractiveSession()
In [25]:
tf.global_variables_initializer().run()
In [38]:
# run the training step 1000 times!
for _ in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
In [30]:
#define the predictions tensor
correct_predictions = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
#define the accuracy
accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32))
In [39]:
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
In [ ]: