2020-10-19 01:16:01 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import numpy as np
|
2020-10-19 03:48:17 +08:00
|
|
|
from tinygrad.tensor import Tensor
|
2020-10-19 04:30:25 +08:00
|
|
|
from tinygrad.utils import fetch_mnist
|
2020-10-19 04:33:02 +08:00
|
|
|
import tinygrad.optim as optim
|
2020-10-19 04:30:25 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
# train a model
|
|
|
|
|
2020-10-19 05:27:29 +08:00
|
|
|
np.random.seed(1337)
|
2020-10-19 04:33:02 +08:00
|
|
|
def layer_init(m, h):
|
|
|
|
ret = np.random.uniform(-1., 1., size=(m,h))/np.sqrt(m*h)
|
|
|
|
return ret.astype(np.float32)
|
|
|
|
|
2020-10-19 04:08:14 +08:00
|
|
|
class TinyBobNet:
|
|
|
|
def __init__(self):
|
|
|
|
self.l1 = Tensor(layer_init(784, 128))
|
|
|
|
self.l2 = Tensor(layer_init(128, 10))
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
return x.dot(self.l1).relu().dot(self.l2).logsoftmax()
|
|
|
|
|
2020-10-19 04:27:59 +08:00
|
|
|
# optimizer
|
|
|
|
|
2020-10-19 04:08:14 +08:00
|
|
|
model = TinyBobNet()
|
2020-10-19 05:27:29 +08:00
|
|
|
optim = optim.SGD([model.l1, model.l2], lr=0.001)
|
2020-10-19 04:50:23 +08:00
|
|
|
#optim = optim.Adam([model.l1, model.l2], lr=0.001)
|
2020-10-19 01:16:01 +08:00
|
|
|
|
|
|
|
BS = 128
|
|
|
|
losses, accuracies = [], []
|
|
|
|
for i in (t := trange(1000)):
|
|
|
|
samp = np.random.randint(0, X_train.shape[0], size=(BS))
|
|
|
|
|
|
|
|
x = Tensor(X_train[samp].reshape((-1, 28*28)))
|
|
|
|
Y = Y_train[samp]
|
|
|
|
y = np.zeros((len(samp),10), np.float32)
|
2020-10-19 05:27:29 +08:00
|
|
|
# correct loss for NLL, torch NLL loss returns one per row
|
|
|
|
y[range(y.shape[0]),Y] = -10.0
|
2020-10-19 01:16:01 +08:00
|
|
|
y = Tensor(y)
|
|
|
|
|
2020-10-19 04:08:14 +08:00
|
|
|
# network
|
|
|
|
outs = model.forward(x)
|
|
|
|
|
|
|
|
# NLL loss function
|
|
|
|
loss = outs.mul(y).mean()
|
|
|
|
loss.backward()
|
2020-10-19 04:27:59 +08:00
|
|
|
optim.step()
|
2020-10-19 01:16:01 +08:00
|
|
|
|
2020-10-19 04:08:14 +08:00
|
|
|
cat = np.argmax(outs.data, axis=1)
|
2020-10-19 01:16:01 +08:00
|
|
|
accuracy = (cat == Y).mean()
|
|
|
|
|
2020-10-19 04:08:14 +08:00
|
|
|
# printing
|
|
|
|
loss = loss.data
|
2020-10-19 01:16:01 +08:00
|
|
|
losses.append(loss)
|
|
|
|
accuracies.append(accuracy)
|
|
|
|
t.set_description("loss %.2f accuracy %.2f" % (loss, accuracy))
|
|
|
|
|
2020-10-19 04:08:14 +08:00
|
|
|
# evaluate
|
2020-10-19 01:16:01 +08:00
|
|
|
def numpy_eval():
|
2020-10-19 04:08:14 +08:00
|
|
|
Y_test_preds_out = model.forward(Tensor(X_test.reshape((-1, 28*28))))
|
|
|
|
Y_test_preds = np.argmax(Y_test_preds_out.data, axis=1)
|
2020-10-19 01:16:01 +08:00
|
|
|
return (Y_test == Y_test_preds).mean()
|
|
|
|
|
2020-10-19 03:48:17 +08:00
|
|
|
accuracy = numpy_eval()
|
|
|
|
print("test set accuracy is %f" % accuracy)
|
|
|
|
assert accuracy > 0.95
|
|
|
|
|