In loss.item() getting error: ValueError: only one element tensors can be converted to Python scalars

class FocalLoss(nn.Module):

def __init__(self, weight=None, 
             gamma=2., reduction='none'):
    nn.Module.__init__(self)
    self.weight = weight
    self.gamma = gamma
    self.reduction = reduction
    
def forward(self, input_tensor, target_tensor):
    target_tensor = torch.argmax(target_tensor ,axis=1)
    log_prob = F.log_softmax(input_tensor, dim=-1)
    prob = torch.exp(log_prob)
    return F.nll_loss(
        ((1 - prob) ** self.gamma) * log_prob, 
        target_tensor, 
        weight=self.weight,
        reduction = self.reduction
    )

—> 33 batch_loss += loss.item()
34 total_loss += loss.item()
35

ValueError: only one element tensors can be converted to Python scalars

Hi Garry!

Because you use reduction = 'none' in nll_loss(), it will (most likely)
return a batch of loss values and therefore loss will be a tensor that
contains more than one element.

The purpose of .item() is to convert a single-element tensor into a regular
python scalar, hence the error. Try using reduction = 'mean' (the default)
and loss should now be a single-element tensor for which .item() will work.

Best.

K. Frank

Thank you very much, Frank that fixed my problem!