PyTorch: RuntimeError: Function MulBackward0 returned an invalid gradient at index 0 - expected type torch.cuda.FloatTensor but got torch.FloatTensor

喜欢而已 提交于 2020-01-04 05:42:13

问题


I don't understand what this error is telling me. In a different post the same problem was also addressed but there was no useful solution for this.

Traceback (most recent call last):
  File "train.py", line 252, in <module>
    main()
  File "train.py", line 231, in main
    train(net, training_dataset, targets, device, criterion, optimizer, epoch, args.epochs)
  File "train.py", line 103, in train
    loss.backward()
  File "/home/hb119056/.local/lib/python3.6/site-packages/torch/tensor.py", line 107, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "/home/hb119056/.local/lib/python3.6/site-packages/torch/autograd/__init__.py", line 93, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: Function MulBackward0 returned an invalid gradient at index 0 - expected type torch.cuda.FloatTensor but got torch.FloatTensor

This is the corresponding segment from my code.

outputs = net(x, indices)
outputs = outputs.transpose(0, 1)
prob = F.normalize(outputs, p=1, dim=1).detach()
target = torch.from_numpy(np.load(file_dir + '/points/points{:03}.npy'.format(i))).to(device)
rv = torch.zeros(12 * outputs.shape[0])

for j in [x for x in range(10) if x != i]:
    source = torch.from_numpy(np.load(file_dir + '/points/points{:03}.npy'.format(j))).to(device)
    rv = factor.ransac(source, target, prob, n_iter, tol, device) # self-written

predicted = factor.predict(source, rv, outputs, device) # self-written
loss = criterion(predicted, target.type(torch.FloatTensor).to(device))
loss.backward() # error occurs here
optimizer.step()

Any help is greatly appreciated, thank you in advance!


回答1:


try changing loss = criterion(predicted, target.type(torch.FloatTensor).to(device)) to loss = criterion(predicted, target.to(device).float())




回答2:


change this line:

loss = criterion(predicted, target.type(torch.FloatTensor).to(device))

to

predicted = predicted.to(device)
target=target.type(predicted.type()).to(predicted.device)
loss = criterion(predicted, target)


来源:https://stackoverflow.com/questions/57725608/pytorch-runtimeerror-function-mulbackward0-returned-an-invalid-gradient-at-ind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!