Hi, I’m wondering how to use the ignore_index for handling void targets in segmentation task. Should I manually create some fake label maps for those data? i.e., a map full of -1, then set ignore_index to -1? Thanks!
It seems to work:
criterion = nn.CrossEntropyLoss(ignore_index=-1)
output = torch.randn(2, 10, requires_grad=True)
target = torch.tensor([-1, 1])
loss = criterion(output, target)
loss.backward()
print(loss, output.grad)
> tensor(1.4096, grad_fn=<NllLossBackward>)
tensor([[ 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
0.0000, 0.0000, 0.0000],
[ 0.1207, -0.7557, 0.0068, 0.0153, 0.0806, 0.0683, 0.0791, 0.1964,
0.0741, 0.0229, 0.0915]])
I would assume that only positive indices would work, so based on this assumption I would create an additional dummy class:
criterion = nn.CrossEntropyLoss(ignore_index=10)
output = torch.randn(2, 11, requires_grad=True)
target = torch.tensor([10, 1])
loss = criterion(output, target)
loss.backward()
print(loss, output.grad)
but apparently -1
just works fine.
Thank you very much!!