Logpt = logpt.gather(1,target) IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

Thanks for the links. I took a quick look at the posts and they seem right for nn.BCELoss. However, I’m not a huge fan of single dimension inputs to nn.BCELoss (but this might be my personal pet peeve :wink: ).
In any case, your focal loss implementation expects the same inputs as nn.CrossEntropyLoss if I’m not misinterpreting the posted code.
I.e. input should contain logits (remove the sigmoid) and should have the shape [batch_size, nb_classes, *] while the target should contain class indices in [0, nb_classes-1] and have the shape [batch_size, *]. Note that * denotes additional dimensions (e.g. for a segmentation use case).

1 Like