Calculating the loss with pytorch is easy using the reduction feature:
criterion = nn.CrossEntropyLoss(weight=cw, reduc