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

In [2]:
def batches(batch_size, features, labels):
    assert len(features) == len(labels)
    output = []
    sample_size = len(features)
    for start in range(0, sample_size, batch_size):
        end = start + batch_size
        output.append([features[start:end], labels[start:end]])
    return output

In [3]:
def print_epoch_stats(epoch_i, sess, last_features, last_labels):
    current_cost = sess.run(
        cost, feed_dict = {features: last_features, labels: last_labels}
    )
    valid_accuracy = sess.run(
        accuracy, feed_dict = {features: valid_features, labels: valid_labels}
    )
    print('Epoch: {:<4} - Cost: {:<8.3} Valid Accuracy: {:<5.3}'.format(epoch_i, current_cost, valid_accuracy))

In [9]:
n_input = 784
n_classes = 10

mnist = input_data.read_data_sets('datasets/ud730/mnist', one_hot=True)

train_features = mnist.train.images
valid_features = mnist.validation.images
test_features = mnist.test.images

train_labels = mnist.train.labels.astype(np.float32)
valid_labels = mnist.validation.labels.astype(np.float32)
test_labels = mnist.test.labels.astype(np.float32)

features = tf.placeholder(tf.float32, [None, n_input])
labels = tf.placeholder(tf.float32, [None, n_classes])

weights = tf.Variable(tf.random_normal([n_input, n_classes]))
bias = tf.Variable(tf.random_normal([n_classes]))

logits = tf.add(tf.matmul(features, weights), bias)

learning_rate = tf.placeholder(tf.float32)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)

correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

batch_size = 128
epochs = 100
learn_rate = 0.01


Extracting datasets/ud730/mnist/train-images-idx3-ubyte.gz
Extracting datasets/ud730/mnist/train-labels-idx1-ubyte.gz
Extracting datasets/ud730/mnist/t10k-images-idx3-ubyte.gz
Extracting datasets/ud730/mnist/t10k-labels-idx1-ubyte.gz

In [10]:
init = tf.global_variables_initializer()
train_batches = batches(batch_size, train_features, train_labels)
with tf.Session() as sess:
    sess.run(init)
    for epoch_i in range(epochs):
        for batch_features, batch_labels in train_batches:
            train_feed_dict = {
                features: batch_features,
                labels: batch_labels,
                learning_rate: learn_rate
            }
            sess.run(optimizer, feed_dict = train_feed_dict)
            
        print_epoch_stats(epoch_i, sess, batch_features, batch_labels)
    test_accuracy = sess.run(
        accuracy,
        feed_dict = {
            features: test_features,
            labels: test_labels
        }
    )
print('Test Accuracy: {}'.format(test_accuracy))


Epoch: 0    - Cost: 6.92     Valid Accuracy: 0.264
Epoch: 1    - Cost: 4.89     Valid Accuracy: 0.373
Epoch: 2    - Cost: 3.75     Valid Accuracy: 0.475
Epoch: 3    - Cost: 3.04     Valid Accuracy: 0.545
Epoch: 4    - Cost: 2.57     Valid Accuracy: 0.598
Epoch: 5    - Cost: 2.24     Valid Accuracy: 0.636
Epoch: 6    - Cost: 1.98     Valid Accuracy: 0.665
Epoch: 7    - Cost: 1.79     Valid Accuracy: 0.686
Epoch: 8    - Cost: 1.63     Valid Accuracy: 0.702
Epoch: 9    - Cost: 1.5      Valid Accuracy: 0.715
Epoch: 10   - Cost: 1.4      Valid Accuracy: 0.726
Epoch: 11   - Cost: 1.31     Valid Accuracy: 0.737
Epoch: 12   - Cost: 1.24     Valid Accuracy: 0.745
Epoch: 13   - Cost: 1.18     Valid Accuracy: 0.752
Epoch: 14   - Cost: 1.13     Valid Accuracy: 0.761
Epoch: 15   - Cost: 1.08     Valid Accuracy: 0.768
Epoch: 16   - Cost: 1.04     Valid Accuracy: 0.774
Epoch: 17   - Cost: 1.0      Valid Accuracy: 0.781
Epoch: 18   - Cost: 0.972    Valid Accuracy: 0.786
Epoch: 19   - Cost: 0.943    Valid Accuracy: 0.792
Epoch: 20   - Cost: 0.917    Valid Accuracy: 0.797
Epoch: 21   - Cost: 0.894    Valid Accuracy: 0.801
Epoch: 22   - Cost: 0.872    Valid Accuracy: 0.804
Epoch: 23   - Cost: 0.852    Valid Accuracy: 0.808
Epoch: 24   - Cost: 0.834    Valid Accuracy: 0.811
Epoch: 25   - Cost: 0.818    Valid Accuracy: 0.813
Epoch: 26   - Cost: 0.802    Valid Accuracy: 0.816
Epoch: 27   - Cost: 0.788    Valid Accuracy: 0.82 
Epoch: 28   - Cost: 0.775    Valid Accuracy: 0.824
Epoch: 29   - Cost: 0.763    Valid Accuracy: 0.827
Epoch: 30   - Cost: 0.751    Valid Accuracy: 0.828
Epoch: 31   - Cost: 0.74     Valid Accuracy: 0.829
Epoch: 32   - Cost: 0.73     Valid Accuracy: 0.832
Epoch: 33   - Cost: 0.721    Valid Accuracy: 0.835
Epoch: 34   - Cost: 0.712    Valid Accuracy: 0.836
Epoch: 35   - Cost: 0.704    Valid Accuracy: 0.838
Epoch: 36   - Cost: 0.696    Valid Accuracy: 0.839
Epoch: 37   - Cost: 0.689    Valid Accuracy: 0.841
Epoch: 38   - Cost: 0.682    Valid Accuracy: 0.843
Epoch: 39   - Cost: 0.675    Valid Accuracy: 0.844
Epoch: 40   - Cost: 0.669    Valid Accuracy: 0.846
Epoch: 41   - Cost: 0.663    Valid Accuracy: 0.847
Epoch: 42   - Cost: 0.658    Valid Accuracy: 0.849
Epoch: 43   - Cost: 0.653    Valid Accuracy: 0.85 
Epoch: 44   - Cost: 0.648    Valid Accuracy: 0.851
Epoch: 45   - Cost: 0.643    Valid Accuracy: 0.852
Epoch: 46   - Cost: 0.639    Valid Accuracy: 0.852
Epoch: 47   - Cost: 0.635    Valid Accuracy: 0.854
Epoch: 48   - Cost: 0.631    Valid Accuracy: 0.854
Epoch: 49   - Cost: 0.627    Valid Accuracy: 0.855
Epoch: 50   - Cost: 0.623    Valid Accuracy: 0.856
Epoch: 51   - Cost: 0.62     Valid Accuracy: 0.857
Epoch: 52   - Cost: 0.616    Valid Accuracy: 0.858
Epoch: 53   - Cost: 0.613    Valid Accuracy: 0.859
Epoch: 54   - Cost: 0.61     Valid Accuracy: 0.86 
Epoch: 55   - Cost: 0.607    Valid Accuracy: 0.861
Epoch: 56   - Cost: 0.604    Valid Accuracy: 0.862
Epoch: 57   - Cost: 0.601    Valid Accuracy: 0.862
Epoch: 58   - Cost: 0.599    Valid Accuracy: 0.864
Epoch: 59   - Cost: 0.596    Valid Accuracy: 0.864
Epoch: 60   - Cost: 0.594    Valid Accuracy: 0.865
Epoch: 61   - Cost: 0.591    Valid Accuracy: 0.865
Epoch: 62   - Cost: 0.589    Valid Accuracy: 0.866
Epoch: 63   - Cost: 0.587    Valid Accuracy: 0.867
Epoch: 64   - Cost: 0.585    Valid Accuracy: 0.867
Epoch: 65   - Cost: 0.582    Valid Accuracy: 0.867
Epoch: 66   - Cost: 0.58     Valid Accuracy: 0.868
Epoch: 67   - Cost: 0.578    Valid Accuracy: 0.868
Epoch: 68   - Cost: 0.576    Valid Accuracy: 0.869
Epoch: 69   - Cost: 0.575    Valid Accuracy: 0.87 
Epoch: 70   - Cost: 0.573    Valid Accuracy: 0.871
Epoch: 71   - Cost: 0.571    Valid Accuracy: 0.871
Epoch: 72   - Cost: 0.569    Valid Accuracy: 0.872
Epoch: 73   - Cost: 0.567    Valid Accuracy: 0.872
Epoch: 74   - Cost: 0.566    Valid Accuracy: 0.872
Epoch: 75   - Cost: 0.564    Valid Accuracy: 0.872
Epoch: 76   - Cost: 0.563    Valid Accuracy: 0.873
Epoch: 77   - Cost: 0.561    Valid Accuracy: 0.873
Epoch: 78   - Cost: 0.56     Valid Accuracy: 0.873
Epoch: 79   - Cost: 0.558    Valid Accuracy: 0.874
Epoch: 80   - Cost: 0.557    Valid Accuracy: 0.875
Epoch: 81   - Cost: 0.556    Valid Accuracy: 0.875
Epoch: 82   - Cost: 0.554    Valid Accuracy: 0.875
Epoch: 83   - Cost: 0.553    Valid Accuracy: 0.876
Epoch: 84   - Cost: 0.552    Valid Accuracy: 0.876
Epoch: 85   - Cost: 0.55     Valid Accuracy: 0.876
Epoch: 86   - Cost: 0.549    Valid Accuracy: 0.876
Epoch: 87   - Cost: 0.548    Valid Accuracy: 0.878
Epoch: 88   - Cost: 0.547    Valid Accuracy: 0.878
Epoch: 89   - Cost: 0.546    Valid Accuracy: 0.878
Epoch: 90   - Cost: 0.545    Valid Accuracy: 0.879
Epoch: 91   - Cost: 0.544    Valid Accuracy: 0.88 
Epoch: 92   - Cost: 0.543    Valid Accuracy: 0.88 
Epoch: 93   - Cost: 0.541    Valid Accuracy: 0.88 
Epoch: 94   - Cost: 0.54     Valid Accuracy: 0.88 
Epoch: 95   - Cost: 0.539    Valid Accuracy: 0.881
Epoch: 96   - Cost: 0.539    Valid Accuracy: 0.881
Epoch: 97   - Cost: 0.538    Valid Accuracy: 0.881
Epoch: 98   - Cost: 0.537    Valid Accuracy: 0.881
Epoch: 99   - Cost: 0.536    Valid Accuracy: 0.881
Test Accuracy: 0.8773999810218811

In [ ]: