Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytorch, Unable to get repr for <class 'torch.Tensor'>

Tags:

pytorch

mse

I'm implementing some RL in PyTorch and had to write my own mse_loss function (which I found on Stackoverflow ;) ). The loss function is:

def mse_loss(input_, target_):    
    return torch.sum(
        (input_ - target_) * (input_ - target_)) / input_.data.nelement()

Now, in my training loop, the first input is something like:

tensor([-1.7610e+10]), tensor([-6.5097e+10])

Input tensor

With this input I'll get the error:

Unable to get repr for <class 'torch.Tensor'>

Computing a = (input_ - target_) works fine, while b = a * a respectively b = torch.pow(a, 2) will fail with the error metioned above.

Does anyone know a fix for this?

Thanks a lot!

Update: I just tried using torch.nn.functional.mse_loss which will result in the same error..

like image 787
bene Avatar asked Jun 24 '18 11:06

bene


1 Answers

I had the same error,when I use the below code

criterion = torch.nn.CrossEntropyLoss().cuda()
output=output.cuda()
target=target.cuda()
loss=criterion(output, target)

but I finally found my wrong:output is like tensor([[0.5746,0.4254]]) and target is like tensor([2]),the number 2 is out of indice of output

when I not use GPU,this error message is:

RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed.  at /opt/conda/conda-bld/pytorch-nightly_1547458468907/work/aten/src/THNN/generic/ClassNLLCriterion.c:93
like image 183
Steve H Avatar answered Oct 16 '22 02:10

Steve H