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)
In [3]:
# getting input and label data
X_batch, y_batch = mnist.train.next_batch(128)
print(X_batch.shape, y_batch.shape)
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))
In [ ]: