Hi guys,
met some problem with the Dice loss function:
class DiceLoss(nn.Module):
def init(self, weight=None, size_average=True):
super(DiceLoss, self).init()
def forward(self, inputs, targets, smooth=1):
inputs = functional.sigmoid(inputs)
inputs = inputs.view(-1)
targets = targets.view(-1)
intersection = (inputs*targets).sum()
dice = (2.*intersection + smooth)/(inputs.sum() + targets.sum() + smooth)
    return 1-dice

def main():
…
loss_ = loss_fn(pred, label)
…
loss_fn in main() is DiceLoss()
and
pred shape: torch.Size([16, 4, 240, 240])  （batchsize, num_classes, width, height）
label shape: torch.Size([16, 240, 240])  (batchsize, width, height)(the element of tensor is in [0, 1, 2, 3], represent pixel class)
After flatten op:
pred  shape: torch.Size([3686400])
label shape: torch.Size([921600])
THE BUG :RuntimeError: The size of tensor a (3686400) must match the size of tensor b (921600) at non-singleton dimension 0
what I am curious about is that there is no bug when I am using nn.CrossEntropyLoss() as my loss_fn,but when I used DiceLoss() , it turned to have a bug above.
Please help me why this bug occurs and how to achieve DiceLoss to get it worked.
Thank you so MUCH!!