input is the output from my CNN network and is of the shape [1,1,32,64,64] and target is the ground truth used to calculate the loss, it has the shape [1,32,64,64], hence the unsqueeze(1) to bring it to the same shape as input.

input is of type float32 and target is of type long.

I get the following error at scatter_:

RuntimeError: Invalid index in scatter at c:\new-builder_2\win-wheel\pytorch\aten\src\th\generic/THTensorMath.cpp:703

Ive checked the documentation on this and my implementation is correct, Is there any reason this is not working?

P.S. I noticed that if I change the dimension in scatter_ it works:

This works and gives the loss output but I’m not sure if this is the preferred method.
Is what what I am doing equivalent to the output produced by scatter_?
I read in one of the docs that for target is is advised to use the scatter_ method as this was the tensor is differentiable and autograd can be utilized. Or was this the case for previous versions of pytorch?

Sorry, I found this code online for DiceLoss and wanted to use it in my code, below is the full code for DiceLoss:

def forward(self, input, target):
weights = 1
encoded_target = input.data.clone().zero_()
encoded_target.scatter_(1, target.unsqueeze(1), 1)
# encoded_target = Variable(encoded_target)
assert input.size() == encoded_target.size(), "Input sizes must be equal."
assert input.dim() == 5, "Input must be a 4D Tensor."
num = (input * encoded_target).sum(dim=4).sum(dim=3).sum(dim=2)
den1 = input.pow(2).sum(dim=4).sum(dim=3).sum(dim=2)
den2 = encoded_target.pow(2).sum(dim=4).sum(dim=3).sum(dim=2)
dice = (2 * num / (den1 + den2)) * weights
return ((1-dice.sum()) / dice.size(1)) / 5

I want to create a target tensor to calculate the loss against. Thats why I thought if I create it in the same was as in my second post will it be the same thing as scatter?

I assume scatter here is trying to do the same thing…