Hi there,
The problem is the gradient is None on my custom loss (Dice loss).
- to verify the setup is correct, I used the
criterion = nn.BCEWithLogitsLoss(size_average=False, reduce=True).
The setup is indeed correct as I can retrieve gradients for my parameters using
grad_of_params = {}
for name, parameter in model.named_parameters():
grad_of_params[name] = parameter.grad
print (name, parameter.grad)
- when I used my own criterion, gradients become None.
criterion = Dice_Loss()
class Dice_Loss(torch.nn.Module):
def init(self):
super().init()
def forward(self, output, target):pred_mask = nn.functional.sigmoid(output) pred_mask = torch.autograd.Variable(pred_mask.gt(0.5),requires_grad=True) pred_mask = pred_mask.type(torch.cuda.FloatTensor) true_pos = torch.sum(torch.mul(pred_mask, target)) true_pos = true_pos / 88 flipped_mask = torch_flip(target) false_pos = torch.sum(torch.mul(flipped_mask, pred_mask)) false_pos = false_pos / 88 flipped_pred = torch_flip(pred_mask) false_neg = torch.sum(torch.mul(target, flipped_pred)) false_neg = false_neg/ 88 n = torch.mul(true_pos,2) dice = torch.div(n, (n+false_neg+false_pos)) return 1- dice
def torch_flip(arr):
return torch.mul(arr, -1) + 1
As I could not see where the code has gone wrong, any suggestion or recommendation what to debug is very welcome.
Many thanks