With class weights wcw_cwc, PyTorch's weighted cross-entropy is a weighted mean, not a plain mean:
Implement weighted_ce(logits, targets, weights) and weighted_ce_grad(logits, targets, weights) (gradient with respect to the logits).
weighted_ce(logits, targets, weights)
weighted_ce_grad(logits, targets, weights)