Custom loss function: what I do wrong?

Hello! I am trying to add a Gaussian penalty to the MSE loss function:

def g_p(inp_, mu, sigma):
    norm= 1/(sigma*torch.sqrt(2*pi))
    exp_= torch.pow((inp_-mu)/sigma, 2)
    return norm*exp

def GP_loss(pred, target):
    return F.mse_loss(pred, target) + g_p(pred, torch.Tensor([37]), torch.Tensor([0.04]))

As response, while training the model I get the error:

TypeError: unsupported format string passed to Tensor.__format__

What am I doing wrong?

Thank you guys!

1 Like

Hi i not sure what is wrong but would suggest that you use typed tensor (FloatTensor, LongTensor etc) in order to make it easier to pinpoint the problem

I found the problem. It’s a bug in, class Recorder, function ‘on_backward_begin’.

Tensors are not ready to be used in the f-string Python format. So changing:

#######self.pbar.child.comment = f'{smooth_loss:.4f}'
self.pbar.child.comment = "$2.3f"%smooth_loss

Solves the issue. Thank you anyway, @Kaspar !


Pytorch Tensors do support f-strings.

Your loss function doesn’t return a scalar (zero dimensional) tensor and therefore cannot be formatted with the format expression: .4f.

You might find it helpful to check out the FlattenedLoss class to understand the library’s approach to implementing loss functions.