I can across this implementation of Dice:
from torch.autograd import Function, Variable
“”“Dice coeff for individual examples”""
def forward(self, input, target): self.save_for_backward(input, target) eps = 0.0001 self.inter = torch.dot(input.view(-1), target.view(-1)) self.union = torch.sum(input) + torch.sum(target) + eps t = (2 * self.inter.float() + eps) / self.union.float() return t # This function has only a single output, so it gets only one gradient def backward(self, grad_output): input, target = self.saved_variables grad_input = grad_target = None if self.needs_input_grad: grad_input = grad_output * 2 * (target * self.union - self.inter) \ / (self.union * self.union) if self.needs_input_grad: grad_target = None return grad_input, grad_target
Uptill now, my dice score had no backward method, it was just the regular view(-1) and intersection over union. Does that mean that nothing was being learnt as backward method was not implemented?