Custom loss function: what I do wrong?


(Vincenzo Lavorini) #1

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

def g_p(inp_, mu, sigma):
    pi=torch.acos(torch.Tensor([-1]))
    norm= 1/(sigma*torch.sqrt(2*pi))
    exp_= torch.pow((inp_-mu)/sigma, 2)
    exp=torch.exp(-exp_/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!


(Kaspar Lund) #2

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


(Vincenzo Lavorini) #3

I found the problem. It’s a bug in basic_train.py, 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 !