Hello,
I am writing a custom loss function including the backward pass by myself. Here is a basic sketch of my issue with explanation.
from torch.autograd import Variable, Function
class Loss2(torch.nn.Module):
def init(self):
def forward(self,Input1,Input2):
class Loss1(Function):
def init(self):
def forward(ctx,Input1,Input2):
def backward(ctx,gradOutput):
%The problem occurs below:
a=Variable(a,requires_grad=True)
b=Variable(b,requires_grad=False)
criterion=Loss2()
LossVal2=criterion(a,b)
LossVal2.backward()
print(a.grad) %This is always None
I want to call another loss function(i.e Loss2) in the backward of Loss1, which is also a custom torch.nn.Module but all the functions have autograd. So, I only implemented forward pass for Loss2. However, I can not get the gradients of Loss2 with respect to its inputs. But I always have None as the gradient wrt a parameter.
By the way I previously used Loss2 as an independent loss function. So, it worked by itself previously. Maybe I am missing a fundemental issue for implementation in pytorch. Waiting for help.