i += 1
np.random.seed(i)
x = Tensor(np.random.randn(32 , 28 * 28 ), "X" )
# Create a 1-hot encoded tensor with 1 random 1
y = np.zeros((32 , 10 ))
y[np.arange(32 ), np.random.choice(10 , 32 )] = 1
y = Tensor(y, "y" )
w1 = Tensor(np.random.randn(28 * 28 , 100 ) * 0.1 , "w1" , requires_grad= True )
b1 = Tensor(np.random.randn(100 ), "b1" , requires_grad= True )
w2 = Tensor(np.random.randn(100 , 10 ) * 0.1 , "w2" , requires_grad= True )
def NN(inputs, params: tuple ):
x, y = inputs
w1, b1, w2 = params
z1 = x.mmul(w1, "tmp" ).add(b1, "z1" )
a1 = tg.sigmoid(z1)
z2 = a1.mmul(w2)
loss = - tg.BCE_loss(z2, y).sum ("loss" )
return loss
debug = []
loss = NN(inputs= (x, y), params= (w1, b1, w2))
loss.backward()
# grad_check(NN, (x, y), (w1, b1, w2))