-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
37 lines (26 loc) · 873 Bytes
/
train.py
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
import numpy as np
from tinygrad.nn.optim import SGD
from tinygrad.nn.state import get_state_dict, safe_save
from tinygrad.tensor import Tensor
from net import TinyNet
Tensor.training = True
def train_model(
X_train: np.ndarray,
Y_train: np.ndarray,
net: TinyNet,
opt: SGD
):
for step in range(1000):
samp = np.random.randint(0, X_train.shape[0], size=(64))
batch = Tensor(X_train[samp], requires_grad=False)
labels = Tensor(Y_train[samp])
out = net(batch)
loss = Tensor.sparse_categorical_crossentropy(out, labels)
opt.zero_grad()
loss.backward()
opt.step()
pred = out.argmax(axis=-1)
acc = (pred == labels).mean()
if step % 100 == 0:
print(
f"Step {step+1} | Loss: {loss.numpy()} | Accuracy: {acc.numpy()}")