John1231983:

loss =(loss * sample_weight / sample_weight.sum()).sum() loss.mean().backward()


OMG, it worked. But I think
loss =(loss * sample_weight / sample_weight.sum()).sum()
loss.mean().backward()

Should be
loss =torch.mean(loss * sample_weight)
loss.backward()

By removing .sum() in the last and use torch.mean(). Am I correct?
To verify it, I used same_weight is 1 then it should be same as reduction=‘mean’
import torch
import numpy as np
import torch.nn as nn
num_class =2
b,h,w =4,8,8
input = torch.randn((b, 1, h, w), requires_grad=True)
target = torch.empty((b, h, w), dtype=torch.long).random_(num_class)
pred = torch.rand((b, num_class, h, w), dtype=torch.float, requires_grad=True)
criterion = nn.CrossEntropyLoss(reduction='none')
loss = criterion(pred, target)

sample_weight = torch.from_numpy(np.asarray([1, 1, 1, 1])).float()
sample_weight = sample_weight.view(-1, 1, 1)
loss1 =torch.mean((loss * sample_weight))
print (loss1)

loss2 =(loss * sample_weight / sample_weight.sum()).sum()
loss2 = loss2.mean()
print (loss2)

criterion = nn.CrossEntropyLoss()
loss = criterion(pred, target)
print (loss)

Output
tensor(0.7202, grad_fn=<MeanBackward0>)
tensor(46.0946, grad_fn=<MeanBackward0>)
tensor(0.7202, grad_fn=<NllLoss2DBackward>)