2020-12-14 12:45:55 +08:00
|
|
|
import os
|
|
|
|
import numpy as np
|
|
|
|
from tqdm import trange
|
|
|
|
from extra.utils import get_parameters
|
2020-12-16 15:44:08 +08:00
|
|
|
from tinygrad.tensor import Tensor, GPU, Device
|
2020-12-14 12:45:55 +08:00
|
|
|
|
2020-12-28 07:35:56 +08:00
|
|
|
def sparse_categorical_crossentropy(out, Y):
|
|
|
|
num_classes = out.shape[-1]
|
|
|
|
YY = Y.flatten()
|
|
|
|
y = np.zeros((YY.shape[0], num_classes), np.float32)
|
|
|
|
# correct loss for NLL, torch NLL loss returns one per row
|
|
|
|
y[range(y.shape[0]),YY] = -1.0*num_classes
|
|
|
|
y = y.reshape(list(Y.shape)+[num_classes])
|
2021-01-01 22:19:03 +08:00
|
|
|
y = Tensor(y)
|
2020-12-28 07:35:56 +08:00
|
|
|
return out.mul(y).mean()
|
|
|
|
|
2021-01-01 22:19:03 +08:00
|
|
|
def train(model, X_train, Y_train, optim, steps, BS=128, lossfn=sparse_categorical_crossentropy):
|
2020-12-29 11:45:46 +08:00
|
|
|
Tensor.training = True
|
2020-12-14 12:45:55 +08:00
|
|
|
losses, accuracies = [], []
|
|
|
|
for i in (t := trange(steps, disable=os.getenv('CI') is not None)):
|
|
|
|
samp = np.random.randint(0, X_train.shape[0], size=(BS))
|
|
|
|
|
2021-01-01 22:19:03 +08:00
|
|
|
x = Tensor(X_train[samp])
|
2020-12-28 07:35:56 +08:00
|
|
|
y = Y_train[samp]
|
2020-12-14 12:45:55 +08:00
|
|
|
|
|
|
|
# network
|
|
|
|
out = model.forward(x)
|
|
|
|
|
2020-12-18 06:37:31 +08:00
|
|
|
loss = lossfn(out, y)
|
2020-12-14 12:45:55 +08:00
|
|
|
optim.zero_grad()
|
|
|
|
loss.backward()
|
|
|
|
optim.step()
|
|
|
|
|
2020-12-28 07:46:32 +08:00
|
|
|
cat = np.argmax(out.cpu().data, axis=-1)
|
2020-12-28 07:35:56 +08:00
|
|
|
accuracy = (cat == y).mean()
|
2020-12-14 12:45:55 +08:00
|
|
|
|
|
|
|
# printing
|
|
|
|
loss = loss.cpu().data
|
|
|
|
losses.append(loss)
|
|
|
|
accuracies.append(accuracy)
|
|
|
|
t.set_description("loss %.2f accuracy %.2f" % (loss, accuracy))
|
|
|
|
|
2021-01-01 22:19:03 +08:00
|
|
|
def evaluate(model, X_test, Y_test, num_classes=None, BS=128):
|
2020-12-29 11:45:46 +08:00
|
|
|
Tensor.training = False
|
2020-12-14 12:45:55 +08:00
|
|
|
def numpy_eval(num_classes):
|
2020-12-28 22:24:51 +08:00
|
|
|
Y_test_preds_out = np.zeros(list(Y_test.shape)+[num_classes])
|
2020-12-14 12:45:55 +08:00
|
|
|
for i in trange(len(Y_test)//BS, disable=os.getenv('CI') is not None):
|
2021-01-01 22:19:03 +08:00
|
|
|
Y_test_preds_out[i*BS:(i+1)*BS] = model.forward(Tensor(X_test[i*BS:(i+1)*BS])).cpu().data
|
2020-12-29 23:40:11 +08:00
|
|
|
Y_test_preds = np.argmax(Y_test_preds_out, axis=-1)
|
2020-12-14 12:45:55 +08:00
|
|
|
return (Y_test == Y_test_preds).mean()
|
|
|
|
|
|
|
|
if num_classes is None: num_classes = Y_test.max().astype(int)+1
|
|
|
|
accuracy = numpy_eval(num_classes)
|
|
|
|
print("test set accuracy is %f" % accuracy)
|
2020-12-18 06:37:31 +08:00
|
|
|
return accuracy
|
2020-12-29 11:45:46 +08:00
|
|
|
|