nn.CrossEntropyLoss
normalizes with the used weights, so you would have to change the loss2
calculation to:
loss2 = F.cross_entropy(pred, label, reduction='none', weight=weight).sum() / weight[label].sum()
loss2
> tensor(1.5594)
This post also describes it using another example.