import numpy as np
from tinygrad.nn import Linear
from tinygrad.tensor import Tensor
from tinygrad.nn.optim import SGD
HIDDEN_LAYERS=1
HIDDEN_WIDTH=128
BIAS=True
class DotCounter:
def __init__(self):
self.layers = [Linear(HIDDEN_WIDTH, HIDDEN_WIDTH, bias=BIAS) for _ in range(HIDDEN_LAYERS)]
self.layers = [Linear(784, HIDDEN_WIDTH)] + self.layers + [Linear(HIDDEN_WIDTH, 6, bias=True)]
def __call__(self, x):
for layer in self.layers[:-1]:
x = layer(x).leakyrelu()
return self.layers[-1](x).softmax()
def getData():
trainData = np.load("dataset/trainData.npy").astype(np.float32)
trainLabels = np.load("dataset/trainLabels.npy").astype(np.float32)
testData = np.load("dataset/testData.npy").astype(np.float32)
testLabels = np.load("dataset/testLabels.npy").astype(np.float32)
return trainData, trainLabels, testData, testLabels
trainData, trainLabels, testData, testLabels = getData()
def trained():
Tensor.training = True
net = DotCounter()
opt = SGD([layer.weight for layer in net.layers], lr=3e-4)
for im, num in zip(trainData, trainLabels):
data = Tensor(im.flatten())
guess = net(data)
# error = mse(guess, Tensor([num-1]))
error = guess.sparse_categorical_crossentropy(Tensor([num-1])) #minus one as index zero corresponds to one circle
opt.zero_grad()
error.backward()
opt.step()
Tensor.training = False
return net
def eval_accuracy(net):
correct = 0
total = testData.shape[0]
for im, num in zip(testData, testLabels):
data = Tensor(im.flatten())
guess = net(data).argmax().numpy().item()
actual = num-1
if guess == actual:
correct += 1
return correct / total
for layers in range(0, 8, 2):
for width in [32, 64, 128, 256]:
HIDDEN_LAYERS=layers
HIDDEN_WIDTH=width
net = trained()
accuracy = eval_accuracy(net)
with open("layers-by-width-by-accuracy.data", "a") as file:
file.write("{}\t{}\t{}\n".format(layers, width, accuracy))