In [1]:
import numpy as np
from miniflow import *

In [2]:
# load MNIST
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=False)


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

In [3]:
# getting input and label data
X_batch, y_batch = mnist.train.next_batch(128)
print(X_batch.shape, y_batch.shape)


(128, 784) (128,)

In [4]:
X_val = mnist.validation.images
y_val = mnist.validation.labels.astype(np.int8)
X_test = mnist.test.images
y_test = mnist.test.labels.astype(np.int8)

In [5]:
# weights initialization
W = 0.01 * np.random.randn(784,10)
b = np.zeros((1,10))

In [6]:
# Neural Network
X_in = Input()
W_in = Input()
b_in = Input()
y_in = Input()
f = Linear(X_in, W_in, b_in)
f = CrossEntropyWithSoftmax(f, y_in)

learning_rate = 1e-2
batch_size = 128
for i in range(10000):
    X_batch, y_batch = mnist.train.next_batch(batch_size)
    y_batch = y_batch.astype(np.int8)
    feed_dict = {X_in: X_batch, y_in: y_batch, W_in: W, b_in: b}
    loss, grad = value_and_grad(f, feed_dict, [W_in, b_in])

    if i % 1000 == 0:
        acc = accuracy(f, {X_in: X_val, y_in: y_val, W_in: W, b_in: b})
        print("Epoch: {}, Loss: {:.3f}, Val_Acc: {}".format(i+1, loss, acc))

    # SGD
    dW, db = grad
    W -= learning_rate * dW
    b -= learning_rate * db

acc = accuracy(f, {X_in: X_test, y_in: y_test, W_in: W, b_in: b})
print('----------------')
print("Testing Accuracy = {}".format(acc))


Epoch: 1, Loss: 2.292, Val_Acc: 0.125
Epoch: 1001, Loss: 0.540, Val_Acc: 0.8706
Epoch: 2001, Loss: 0.507, Val_Acc: 0.8832
Epoch: 3001, Loss: 0.406, Val_Acc: 0.891
Epoch: 4001, Loss: 0.298, Val_Acc: 0.8966
Epoch: 5001, Loss: 0.489, Val_Acc: 0.8998
Epoch: 6001, Loss: 0.378, Val_Acc: 0.9026
Epoch: 7001, Loss: 0.312, Val_Acc: 0.9042
Epoch: 8001, Loss: 0.473, Val_Acc: 0.9074
Epoch: 9001, Loss: 0.251, Val_Acc: 0.9088
----------------
Testing Accuracy = 0.9107

In [ ]: