tinygrad/test/test_mnist.py

104 lines
3.0 KiB
Python
Raw Normal View History

2020-10-19 01:16:01 +08:00
#!/usr/bin/env python
import os
import unittest
2020-10-19 01:16:01 +08:00
import numpy as np
2020-10-19 03:48:17 +08:00
from tinygrad.tensor import Tensor
2020-10-19 05:36:29 +08:00
from tinygrad.utils import layer_init_uniform, fetch_mnist
2020-10-23 20:46:45 +08:00
import tinygrad.optim as optim
2020-10-19 01:16:01 +08:00
from tqdm import trange
# load the mnist dataset
2020-10-19 04:30:25 +08:00
X_train, Y_train, X_test, Y_test = fetch_mnist()
2020-10-19 01:16:01 +08:00
2020-10-19 05:55:20 +08:00
# create a model
2020-10-19 04:08:14 +08:00
class TinyBobNet:
def __init__(self):
2020-10-19 05:36:29 +08:00
self.l1 = Tensor(layer_init_uniform(784, 128))
self.l2 = Tensor(layer_init_uniform(128, 10))
2020-10-19 04:08:14 +08:00
def forward(self, x):
return x.dot(self.l1).relu().dot(self.l2).logsoftmax()
# create a model with a conv layer
class TinyConvNet:
def __init__(self):
2020-10-26 04:48:44 +08:00
# https://keras.io/examples/vision/mnist_convnet/
conv = 3
#inter_chan, out_chan = 32, 64
inter_chan, out_chan = 8, 16 # for speed
self.c1 = Tensor(layer_init_uniform(inter_chan,1,conv,conv))
self.c2 = Tensor(layer_init_uniform(out_chan,inter_chan,conv,conv))
self.l1 = Tensor(layer_init_uniform(out_chan*5*5, 10))
def forward(self, x):
x.data = x.data.reshape((-1, 1, 28, 28)) # hacks
x = x.conv2d(self.c1).relu().max_pool2d()
x = x.conv2d(self.c2).relu().max_pool2d()
2020-10-23 12:49:14 +08:00
x = x.reshape(Tensor(np.array((x.shape[0], -1))))
2020-10-26 04:48:44 +08:00
return x.dot(self.l1).logsoftmax()
2020-10-23 21:11:38 +08:00
def train(model, optim, steps, BS=128):
losses, accuracies = [], []
2020-10-26 07:40:37 +08:00
for i in (t := trange(steps, disable=os.getenv('CI') is not None)):
2020-10-23 21:11:38 +08:00
samp = np.random.randint(0, X_train.shape[0], size=(BS))
x = Tensor(X_train[samp].reshape((-1, 28*28)).astype(np.float32))
Y = Y_train[samp]
y = np.zeros((len(samp),10), np.float32)
# correct loss for NLL, torch NLL loss returns one per row
y[range(y.shape[0]),Y] = -10.0
y = Tensor(y)
# network
out = model.forward(x)
2020-10-19 01:16:01 +08:00
2020-10-23 21:11:38 +08:00
# NLL loss function
loss = out.mul(y).mean()
loss.backward()
optim.step()
cat = np.argmax(out.data, axis=1)
accuracy = (cat == Y).mean()
# printing
loss = loss.data
losses.append(loss)
accuracies.append(accuracy)
t.set_description("loss %.2f accuracy %.2f" % (loss, accuracy))
2020-10-19 01:16:01 +08:00
2020-10-23 21:11:38 +08:00
def evaluate(model):
def numpy_eval():
Y_test_preds_out = model.forward(Tensor(X_test.reshape((-1, 28*28)).astype(np.float32)))
Y_test_preds = np.argmax(Y_test_preds_out.data, axis=1)
return (Y_test == Y_test_preds).mean()
2020-10-19 03:48:17 +08:00
2020-10-23 21:11:38 +08:00
accuracy = numpy_eval()
print("test set accuracy is %f" % accuracy)
assert accuracy > 0.95
2020-10-23 21:11:38 +08:00
class TestMNIST(unittest.TestCase):
2020-10-23 21:33:18 +08:00
def test_conv(self):
2020-10-23 21:11:38 +08:00
np.random.seed(1337)
2020-10-23 17:53:01 +08:00
model = TinyConvNet()
2020-10-26 04:48:44 +08:00
optimizer = optim.Adam([model.c1, model.c2, model.l1], lr=0.001)
2020-10-23 21:11:38 +08:00
train(model, optimizer, steps=400)
2020-10-23 17:53:01 +08:00
evaluate(model)
2020-10-23 21:33:18 +08:00
def test_sgd(self):
2020-10-23 21:11:38 +08:00
np.random.seed(1337)
2020-10-23 17:53:01 +08:00
model = TinyBobNet()
2020-10-23 20:46:45 +08:00
optimizer = optim.SGD([model.l1, model.l2], lr=0.001)
2020-10-23 21:11:38 +08:00
train(model, optimizer, steps=1000)
2020-10-23 17:53:01 +08:00
evaluate(model)
2020-10-23 21:33:18 +08:00
def test_rmsprop(self):
2020-10-23 21:11:38 +08:00
np.random.seed(1337)
2020-10-23 20:46:45 +08:00
model = TinyBobNet()
2020-10-23 21:22:32 +08:00
optimizer = optim.RMSprop([model.l1, model.l2], lr=0.0002)
2020-10-23 21:11:38 +08:00
train(model, optimizer, steps=1000)
2020-10-23 17:53:01 +08:00
evaluate(model)
2020-10-22 02:21:44 +08:00
if __name__ == '__main__':
unittest.main()
2020-10-23 21:11:38 +08:00