VLavorini
(Vincenzo Lavorini)
November 22, 2018, 3:12pm
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!
1 Like
Kaspar
(Kaspar Lund)
November 22, 2018, 6:29pm
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
VLavorini
(Vincenzo Lavorini)
November 23, 2018, 4:03pm
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 !
jc8478
(Jamie Cox)
December 14, 2018, 11:42am
4
@VLavorini
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 fast.ai library’s approach to implementing loss functions.
2 Likes