Dear,

I’m using the code from: Semantic models PyTorch but I need to create a custom loss with a weight assigned to each class.

I would like to understand how to do this.

Can anyone help me please?

Dear,

I’m using the code from: Semantic models PyTorch but I need to create a custom loss with a weight assigned to each class.

I would like to understand how to do this.

Can anyone help me please?

```
def SoftCrossEntropy(inputs, target, weight_id=None, reduction='sum'):
"""
inputs are performed by log_softmax
"""
log_likelihood = -inputs
batch = inputs.shape[0]
if weight_id is None:
if reduction == 'average':
loss = torch.sum(torch.mul(log_likelihood, target)) / batch
else:
loss = torch.sum(torch.mul(log_likelihood, target))
return loss
else:
log_likelihood = log_likelihood * weight_id
target = target * weight_id
if reduction == 'average':
loss = torch.sum(torch.mul(log_likelihood, target)) / batch
else:
loss = torch.sum(torch.mul(log_likelihood, target))
return loss
```