import os
import time
import numpy as np
from efficientnet import EfficientNet
from tinygrad.tensor import Tensor

if __name__ == "__main__":
    Tensor.default_gpu = os.getenv("GPU") is not None
    model = EfficientNet()

    BS = 4

    img = np.zeros((BS, 3, 224, 224), dtype=np.float32)

    st = time.time()
    out = model.forward(Tensor(img))
    et = time.time()
    print("forward %.2f s" % (et - st))

    Y = [0] * BS

    y = np.zeros((BS, 1000), np.float32)
    y[range(y.shape[0]), Y] = -1000.0
    y = Tensor(y)
    loss = out.logsoftmax().mul(y).mean()

    st = time.time()
    loss.backward()
    et = time.time()
    print("backward %.2f s" % (et - st))
Esempio n. 2
0
 def __init__(self):
     self.l1 = Tensor.uniform(784, 128)
     self.l2 = Tensor.uniform(128, 10)
Esempio n. 3
0
 def __init__(self, classes=10):
     conv = 3
     inter_chan, out_chan = 8, 16  # for speed
     self.c1 = Tensor.uniform(inter_chan, 3, conv, conv)
     self.c2 = Tensor.uniform(out_chan, inter_chan, conv, conv)
     self.l1 = Tensor.uniform(out_chan * 6 * 6, classes)
Esempio n. 4
0
 def numpy_eval():
     Y_test_preds_out = model.forward(
         Tensor(X_test.reshape((-1, 28 * 28)).astype(np.float32),
                gpu=gpu)).cpu()
     Y_test_preds = np.argmax(Y_test_preds_out.data, axis=1)
     return (Y_test == Y_test_preds).mean()
Esempio n. 5
0
 def __init__(self):
     self.l1 = Tensor(layer_init_uniform(784, 128))
     self.l2 = Tensor(layer_init_uniform(128, 10))