Firstly, I’d say remove this line as the loss should have a gradient by default, and if it doesn’t before this line, then the gradient will be 0 for all parameters as there’s no grad_fn connecting your loss to your parameters.



 Anjali_Raj:

    loss.requires_grad=True



The next thing I’d do is check to see if all your weights/biases have requires_grad eqaul to True. You can check this via,
for name, param in net.named_parameters():
  print(name, param.requires_grad)

Also, as @gphillip mentioned,  is it necessary to keep changing the outputs to float? Could you perhaps try your model without using .float on your output a and your labels! (This could be a problem with your gradients)