This is the definition of my model:
class MyClassifier(nn.Module):
def __init__(self,args):
super(MyClassifier, self).__init__()
self.avgpool = nn.AdaptiveAvgPool2d(output_size=(1, 1))
self.fc1= nn.Linear(in_features= args.input_features, out_features=args.hidden_size, bias=True)
if args.classifier_act == 'Tanh':
self.act = nn.Tanh()
else:
self.act = nn.Sigmoid()
self.fc2 = nn.Linear(args.hidden_size,args.num_classes)
def forward(self, input):
x= self.avgpool(input)
x = x.view(x.size(0), -1)
x= self.act(self.fc1(x))
x= self.fc2(x)
return x
for batch, (lr, hr,rcan,hran,srfbn,csnln,gmfn,drln,rnan,mardn,label,filename,idx_scale) in enumerate(self.loader_train):
lr= self.prepare([lr])
self.optimizer.zero_grad()
self.scores = self.model(input, idx_scale)
self.scores = torch.sigmoid(self.scores)
loss = self.loss(self.scores , label)
` loss = Variable(loss, requires_grad=True)`
loss.backward()
self.optimizer.step()
print(loss.grad)
for name, param in self.model.named_parameters():
if param.grad is not None:
print(name, param.grad.sum())
else:
print(name, param.grad)
The loss and output of the model required_grad are false.
scores required true: False
loss required true: False
I put the loss in Variable as follow:
loss = Variable(loss, requires_grad=True)
But still, the grad is non and my model is not training.
tensor(1., device=‘cuda:0’)
model.fc1.weight None
model.fc1.bias None
model.fc2.weight None
model.fc2.bias None
I have no idea why this happens.