I am using a custom loss function in which I have defined using Class(nn.Module). I have written only the forward() in my Loss function as I am using all  the tensors and torch operations. But After loss.backward when i try to print the parameters gradients, I am getting no gradients. This is my loss function
import pdb
import torch

class my_Loss(torch.nn.Module):
    
    def __init__(self):
       super(my_Loss,self).__init__()
       #self.X = X
       #self.Y = Y
       
    def forward(self,Y1,X1):
      
      cuda = torch.device("cuda:1")
      tensor = torch.tensor((), dtype=torch.float64,device=cuda)
      #F = tensor.new_zeros((Y1.size(0),1),requires_grad=True)
      N = Y1.size(0)
      for i in range (N): 

         #Z = (Y/(2*torch.max(X,1-X)))+((torch.max(X,1-X)-X)/(2*torch.max(X,1-X)))
         Y = Y1.clone()[i,0]
         X = X1.clone()[i,0]
         X_comp = 1-X
         Z_num1 = torch.max(X,X_comp)
         Z_den = 2*torch.max(X,X_comp)
         Z_1 = Y/Z_den
         Z_2 = (Z_num1-X)/Z_den
         Z = Z_1+Z_2
         Z_comp = 1-Z
         #F = X*(Z*torch.log(Z)+(1-Z)*torch.log(1-Z))
         F = -X*(Z*torch.log(Z)+Z_comp*torch.log(Z_comp))
      loss = (1-(torch.sum(F)/N))
      #pdb.set_trace()
      return loss

Now have some questions

Do I need to define backward() also?
2)If not then why all the gradients are ‘None’ type?
How to define backward() for this loss function?