# -rw-r--r-- 2.1 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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))