I am working on implementing my own diceloss gradient, I have coded the dice loss gradient equation using python/numpy but it doesn't match the gradient computed by pyTorch
can someone help me?
The derivate equation I am using is :
def dice_loss_grad(y, label):
sum_squares = np.sum(y**2)+np.sum(label**2)
num = (label * sum_squares) - (2*y*np.sum(y*label))
dem = (sum_squares)**2
res = -2*(num/dem)
return res
c = DiceLoss()
loss = c(input, output)
loss.backward(retain_graph=True)
result_torch = grad(outputs=loss, inputs=input, retain_graph=True)[0].detach().numpy()
loss_j = dice_loss_grad(input.detach().numpy(), output.detach().numpy())
print(result_torch)
print(loss_j)
class DiceLoss(nn.Module):
def __init__(self, weight=None, size_average=True):
super(DiceLoss, self).__init__()
def forward(self, inputs, targets, smooth=0):
#comment out if your model contains a sigmoid or equivalent activation layer
# inputs = F.sigmoid(inputs)
#flatten label and prediction tensors
intersection = (inputs * targets).sum()
dice = (2.*intersection + smooth)/(inputs.sum() + targets.sum() + smooth)
return 1-dice