Logistic Regression with MNIST


In [1]:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data

print ("Packages are loaded!!!")


Packages are loaded!!!

Download and Extract MNIST DataSet


In [2]:
mnist = input_data.read_data_sets('data/', one_hot=True)
trainimg = mnist.train.images
trainlabel = mnist.train.labels
testimg = mnist.test.images
testlabel = mnist.test.labels

print ("MNIST loaded!!!")


Extracting data/train-images-idx3-ubyte.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
MNIST loaded!!!

Create Tensor Graph for Logistic Regression


In [3]:
x = tf.placeholder("float", [None, 784])
y = tf.placeholder("float", [None, 10])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
# Logistic Regression Model
actvation = tf.nn.softmax(tf.matmul(x,W) + b)
# Cost Fuction
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(actvation), reduction_indices=1))
# Optimizer
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

Prediction and Accuracy


In [4]:
# Prediction
pred = tf.equal(tf.argmax(actvation, 1), tf.argmax(y, 1))
# Accuracy
accr = tf.reduce_mean(tf.cast(pred, "float"))
# Initializer
init = tf.initialize_all_variables()

Train Model


In [8]:
training_epochs = 50
batch_size = 100
display_step = 5
# Session
sess = tf.Session()
sess.run(init)
# Mini-Batch Learning
for epoch in range(training_epochs):
    avg_cost = 0.
    num_batch = int(mnist.train.num_examples/batch_size)
    for i in range(num_batch):
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys})
        feeds = {x: batch_xs, y: batch_ys}
        avg_cost += sess.run(cost, feed_dict=feeds)/num_batch
    # Display
    if epoch % display_step == 0:
        feeds_train = {x: batch_xs, y: batch_ys}
        feeds_test = {x: mnist.test.images, y: mnist.test.labels}
        train_acc = sess.run(accr, feeds_train)
        test_acc = sess.run(accr, feeds_test)
        print ("Epoch : %03d/%03d cost: %.9f train_acc: %.3f test_acc: %.3f" 
               % (epoch, training_epochs, avg_cost, train_acc, test_acc))
print ("Done!!!")


Epoch : 000/050 cost: 1.177067931 train_acc: 0.840 test_acc: 0.854
Epoch : 005/050 cost: 0.441086413 train_acc: 0.870 test_acc: 0.896
Epoch : 010/050 cost: 0.383993804 train_acc: 0.890 test_acc: 0.904
Epoch : 015/050 cost: 0.356193866 train_acc: 0.990 test_acc: 0.909
Epoch : 020/050 cost: 0.341433665 train_acc: 0.890 test_acc: 0.912
Epoch : 025/050 cost: 0.330003295 train_acc: 0.890 test_acc: 0.915
Epoch : 030/050 cost: 0.322628716 train_acc: 0.930 test_acc: 0.916
Epoch : 035/050 cost: 0.317337896 train_acc: 0.880 test_acc: 0.917
Epoch : 040/050 cost: 0.309249855 train_acc: 0.890 test_acc: 0.918
Epoch : 045/050 cost: 0.306366050 train_acc: 0.890 test_acc: 0.918
Done!!!

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: