I’m trying to implement scatter_ in my custom loss function:
encoded_target = input.data.clone().zero_()
encoded_target.scatter_(1, target.unsqueeze(1), 1)

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:
encoded_target.scatter_(2, target.unsqueeze(1), 1)
But this is not what i want, why is working for dimension 2 and failing for 1?
Thanks