I am trying to pass soft targets to mobilenet v2 (with the only change that I am using 2 classes instead of 1000).
According to CrossEntropyLoss — PyTorch 1.13 documentation and various similar posts, since ~2022 float targets are accepted by CrossEntropyLoss. However, for me it still fails.
The Last layer is:
nn.Linear(in_features=1280, out_features=2, bias=True)
It crashes though, if my labels are floats. I have prepared the minimal failing example using values returned by my network (batch_size = 4):
activations = torch.FloatTensor([[-0.3139, -0.0486], [-0.0510, 0.0470], [ 0.0963, 0.0143], [-0.2151, -0.0576]]) targets_float = torch.FloatTensor([1.0, 1.0, 0.0, 0.5]) targets_long = torch.LongTensor([1, 1, 0, 0]) crit = nn.CrossEntropyLoss() crit(activations,targets_long) # returns tensor(0.6606) as expected crit(activations,targets_float) # return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing) #crashes with # RuntimeError: expected scalar type Long but found Float