2020-10-19 01:16:01 +08:00
|
|
|
#!/usr/bin/env python
|
2020-10-22 00:30:08 +08:00
|
|
|
import unittest
|
2020-10-19 01:16:01 +08:00
|
|
|
import numpy as np
|
2023-07-03 06:07:30 +08:00
|
|
|
from tinygrad.state import get_parameters
|
2022-07-18 03:55:26 +08:00
|
|
|
from tinygrad.tensor import Tensor, Device
|
2023-02-28 02:19:54 +08:00
|
|
|
from tinygrad.nn import optim, BatchNorm2d
|
2020-12-14 12:45:55 +08:00
|
|
|
from extra.training import train, evaluate
|
2021-10-31 10:55:50 +08:00
|
|
|
from datasets import fetch_mnist
|
2020-11-11 07:37:39 +08:00
|
|
|
|
2020-10-19 01:16:01 +08:00
|
|
|
# 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):
|
2022-09-26 04:46:33 +08:00
|
|
|
self.l1 = Tensor.scaled_uniform(784, 128)
|
|
|
|
self.l2 = Tensor.scaled_uniform(128, 10)
|
2020-10-19 04:08:14 +08:00
|
|
|
|
2020-10-27 23:53:35 +08:00
|
|
|
def parameters(self):
|
2023-07-03 06:07:30 +08:00
|
|
|
return get_parameters(self)
|
2020-10-27 23:53:35 +08:00
|
|
|
|
2020-10-19 04:08:14 +08:00
|
|
|
def forward(self, x):
|
2023-02-25 02:11:24 +08:00
|
|
|
return x.dot(self.l1).relu().dot(self.l2).log_softmax()
|
2020-10-19 04:08:14 +08:00
|
|
|
|
2020-10-22 00:12:19 +08:00
|
|
|
# create a model with a conv layer
|
|
|
|
class TinyConvNet:
|
2023-02-28 02:19:54 +08:00
|
|
|
def __init__(self, has_batchnorm=False):
|
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
|
2022-09-26 04:46:33 +08:00
|
|
|
self.c1 = Tensor.scaled_uniform(inter_chan,1,conv,conv)
|
|
|
|
self.c2 = Tensor.scaled_uniform(out_chan,inter_chan,conv,conv)
|
|
|
|
self.l1 = Tensor.scaled_uniform(out_chan*5*5, 10)
|
2023-02-28 02:19:54 +08:00
|
|
|
if has_batchnorm:
|
|
|
|
self.bn1 = BatchNorm2d(inter_chan)
|
|
|
|
self.bn2 = BatchNorm2d(out_chan)
|
|
|
|
else:
|
|
|
|
self.bn1, self.bn2 = lambda x: x, lambda x: x
|
2020-10-22 00:12:19 +08:00
|
|
|
|
2020-10-27 23:53:35 +08:00
|
|
|
def parameters(self):
|
2023-07-03 06:07:30 +08:00
|
|
|
return get_parameters(self)
|
2020-10-27 23:53:35 +08:00
|
|
|
|
2023-02-28 02:39:47 +08:00
|
|
|
def forward(self, x:Tensor):
|
2020-11-08 01:17:57 +08:00
|
|
|
x = x.reshape(shape=(-1, 1, 28, 28)) # hacks
|
2023-02-28 02:19:54 +08:00
|
|
|
x = self.bn1(x.conv2d(self.c1)).relu().max_pool2d()
|
|
|
|
x = self.bn2(x.conv2d(self.c2)).relu().max_pool2d()
|
2020-10-29 23:13:05 +08:00
|
|
|
x = x.reshape(shape=[x.shape[0], -1])
|
2023-02-25 02:11:24 +08:00
|
|
|
return x.dot(self.l1).log_softmax()
|
2020-10-22 00:12:19 +08:00
|
|
|
|
2020-10-23 21:11:38 +08:00
|
|
|
class TestMNIST(unittest.TestCase):
|
2022-06-06 03:40:12 +08:00
|
|
|
def test_sgd_onestep(self):
|
2022-06-06 03:13:05 +08:00
|
|
|
np.random.seed(1337)
|
2022-06-06 03:40:12 +08:00
|
|
|
model = TinyBobNet()
|
|
|
|
optimizer = optim.SGD(model.parameters(), lr=0.001)
|
|
|
|
train(model, X_train, Y_train, optimizer, BS=69, steps=1)
|
2022-07-03 06:47:10 +08:00
|
|
|
for p in model.parameters(): p.realize()
|
2022-06-07 00:25:31 +08:00
|
|
|
|
2022-06-07 00:45:37 +08:00
|
|
|
def test_sgd_threestep(self):
|
|
|
|
np.random.seed(1337)
|
|
|
|
model = TinyBobNet()
|
|
|
|
optimizer = optim.SGD(model.parameters(), lr=0.001)
|
2022-07-18 06:38:43 +08:00
|
|
|
train(model, X_train, Y_train, optimizer, BS=69, steps=3)
|
|
|
|
|
|
|
|
def test_sgd_sixstep(self):
|
|
|
|
np.random.seed(1337)
|
|
|
|
model = TinyBobNet()
|
|
|
|
optimizer = optim.SGD(model.parameters(), lr=0.001)
|
|
|
|
train(model, X_train, Y_train, optimizer, BS=69, steps=6, noloss=True)
|
2022-06-07 00:45:37 +08:00
|
|
|
|
2022-06-07 00:25:31 +08:00
|
|
|
def test_adam_onestep(self):
|
|
|
|
np.random.seed(1337)
|
|
|
|
model = TinyBobNet()
|
|
|
|
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
|
|
|
train(model, X_train, Y_train, optimizer, BS=69, steps=1)
|
2022-07-03 06:47:10 +08:00
|
|
|
for p in model.parameters(): p.realize()
|
2020-11-08 01:17:57 +08:00
|
|
|
|
2022-06-07 00:38:28 +08:00
|
|
|
def test_adam_threestep(self):
|
|
|
|
np.random.seed(1337)
|
|
|
|
model = TinyBobNet()
|
|
|
|
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
|
|
|
train(model, X_train, Y_train, optimizer, BS=69, steps=3)
|
|
|
|
|
2022-06-06 07:31:20 +08:00
|
|
|
def test_conv_onestep(self):
|
|
|
|
np.random.seed(1337)
|
|
|
|
model = TinyConvNet()
|
|
|
|
optimizer = optim.SGD(model.parameters(), lr=0.001)
|
2022-07-16 23:32:42 +08:00
|
|
|
train(model, X_train, Y_train, optimizer, BS=69, steps=1, noloss=True)
|
|
|
|
for p in model.parameters(): p.realize()
|
2022-06-06 07:31:20 +08:00
|
|
|
|
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-27 23:53:35 +08:00
|
|
|
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
2022-07-04 06:18:00 +08:00
|
|
|
train(model, X_train, Y_train, optimizer, steps=100)
|
2022-09-07 22:52:05 +08:00
|
|
|
assert evaluate(model, X_test, Y_test) > 0.94 # torch gets 0.9415 sometimes
|
2020-11-11 23:58:43 +08:00
|
|
|
|
2023-02-28 02:19:54 +08:00
|
|
|
def test_conv_with_bn(self):
|
|
|
|
np.random.seed(1337)
|
|
|
|
model = TinyConvNet(has_batchnorm=True)
|
2023-03-20 14:53:56 +08:00
|
|
|
optimizer = optim.AdamW(model.parameters(), lr=0.003)
|
|
|
|
train(model, X_train, Y_train, optimizer, steps=200)
|
|
|
|
assert evaluate(model, X_test, Y_test) > 0.94
|
2023-02-28 02:19:54 +08:00
|
|
|
|
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-27 23:53:35 +08:00
|
|
|
optimizer = optim.SGD(model.parameters(), lr=0.001)
|
2022-07-04 06:18:00 +08:00
|
|
|
train(model, X_train, Y_train, optimizer, steps=600)
|
2023-02-20 05:05:37 +08:00
|
|
|
assert evaluate(model, X_test, Y_test) > 0.94 # CPU gets 0.9494 sometimes
|
2020-11-17 07:07:49 +08:00
|
|
|
|
2020-10-22 00:30:08 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|