我不明白此错误告诉我什么。在a different post中,同样的问题也得到解决,但是对此没有有用的解决方案。
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
这是我代码中的对应段。
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()
非常感谢您的帮助,在此先感谢您!
答案 0 :(得分:0)
更改此行:
loss = criterion(predicted, target.type(torch.FloatTensor).to(device))
到
predicted = predicted.to(device)
target=target.type(predicted.type()).to(predicted.device)
loss = criterion(predicted, target)
答案 1 :(得分:0)
尝试更改
loss = criterion(predicted, target.type(torch.FloatTensor).to(device))
至
loss = criterion(predicted, target.to(device).float())