Hey guys, I want to make a simple classification neural network with pytorch’s autograd package. I have gone through some resources that helped me create the code. Problem is the code is not working I have tried some solutions but it does not work for me.
I am trying to classify mnist dataset, I am building simple 4 layer network using matrix multiplications. I am using sigmoid as activation and cross entropy loss function. Network processes code in batches. My request to guys is please have look at code and tell me what i am doing wrong in code? Code the network class and training loop in given below. Link to colab file also given. Thanks in advance https://colab.research.google.com/drive/1V4mfk4Fbc7x4uCIehmSohEVEXwJAnlOn? 1usp=sharing
class network():
def __init__(self,num_layer,layer_size ): 

    self.W1=torch.randn(layer_size[0],layer_size[1],dtype=torch.float32,requires_grad=True)

    self.b1=torch.randn(layer_size[1],dtype=torch.float32,requires_grad=True)

    self.W2=torch.randn(layer_size[1],layer_size[2],dtype=torch.float32,requires_grad=True)

    self.b2=torch.randn(layer_size[2],dtype=torch.float32,requires_grad=True)

    

    self.W3=torch.randn(layer_size[2],layer_size[3],dtype=torch.float32,requires_grad=True)

    self.b3=torch.randn(layer_size[3],dtype=torch.float32,requires_grad=True)

    self.W4=torch.randn(layer_size[3],layer_size[4],dtype=torch.float32,requires_grad=True)

    self.b4=torch.randn(layer_size[4],dtype=torch.float32,requires_grad=True)

    self.act=torch.nn.ReLU()

    self.act1=torch.nn.Sigmoid()

    self.act_last=torch.nn.Softmax(dim=1)

    self.loss1 = torch.nn.CrossEntropyLoss()

def forward(self,x):

    return self.act1(torch.matmul(self.act1(torch.matmul(self.act1(torch.matmul(self.act1(torch.matmul(x,self.W1)+self.b1),self.W2) +self.b2),self.W3) +self.b3),self.W4)+self.b4)



def loss(self,pred,Y):

    return -1*(torch.sum(Y*(torch.log(pred))))/20

    # return l

model=network(5,[784,392,196,98,10])
lr=0.00001
n_iters=100
for epoch in range(n_iters):
for i in range (3000):

    value = randint(0, train_X.shape[0]-50)

    

    X1=X[value:value+20]

    Y1=Y[value:value+20]

    y_pred=model.forward(X1)

    # print(Y1)

    # print(y_pred)

    l=model.loss(y_pred,Y1)

    

    l.backward()

    with torch.no_grad():

        model.W1-=lr*model.W1.grad

        model.W2-=lr*model.W2.grad

        model.W3-=lr*model.W3.grad

    # value_W4=self.W4-lr*self.W4.grad

        model.b1-=lr*model.b1.grad

        model.b2-=lr*model.b2.grad

        model.b3-=lr*model.b3.grad

    model.W1.grad.zero_()

    model.W2.grad.zero_()

    model.W3.grad.zero_()

    model.b1.grad.zero_()

    model.b2.grad.zero_()

    model.b3.grad.zero_()

print(f'epoch {epoch+1},loss={l:.8f}')