I think the major steps are:

calculate the cross entropy for each sample in a batch
calculate the weight for each sample, which is like a lookup table in a for loop
loss = sum(cross_entropy_tensor * weight_tensor) / batch_size

Now I can get softmax tensor with shape batch_size * num_class by using nn.LogSoftmax. Then I’m a little confused about how to implement 1 and 2.
nn.NLLLoss seems combine 1 and 3 with no per sample weight.