Python 如何调试索引0处的无效渐变?-皮托克
我试图训练一个演员-评论家模型,但当我到达评论家的后支柱时,我得到了以下错误:Python 如何调试索引0处的无效渐变?-皮托克,python,pytorch,Python,Pytorch,我试图训练一个演员-评论家模型,但当我到达评论家的后支柱时,我得到了以下错误: 运行时错误:索引0处的梯度无效-预期类型为torch.cuda.FloatTensor,但获得torch.FloatTensor 我无法识别错误所指的梯度。有人能帮忙吗 以下是堆栈跟踪: Traceback (most recent call last): File "train.py", line 338, in <module> main() File "train.py", line
运行时错误:索引0处的梯度无效-预期类型为torch.cuda.FloatTensor,但获得torch.FloatTensor
我无法识别错误所指的梯度。有人能帮忙吗
以下是堆栈跟踪:
Traceback (most recent call last):
File "train.py", line 338, in <module>
main()
File "train.py", line 327, in main
reinforce_trainer.train(opt.start_reinforce, opt.start_reinforce + opt.critic_pretrain_epochs - 1, True, start_time)
File "/home/fbommfim/init-tests/treeLSTM/lib/train/reinforce_trainer.py", line 56, in train
train_reward, critic_loss = self.train_epoch(epoch, pretrain_critic, no_update)
File "/home/fbommfim/init-tests/treeLSTM/lib/train/reinforce_trainer.py", line 153, in train_epoch
critic_loss = self.critic.backward(baselines.cuda(), rewards, critic_weights.cuda(), num_words, self.critic_loss_func, regression=True)
File "/home/fbommfim/init-tests/treeLSTM/lib/model/encoder_decoder/hybrid2seq_model.py", line 67, in backward
outputs.backward(grad_output)
File "/home/linuxbrew/.linuxbrew/Cellar/python/3.7.6_1/lib/python3.7/site-packages/torch/tensor.py", line 195, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "/home/linuxbrew/.linuxbrew/Cellar/python/3.7.6_1/lib/python3.7/site-packages/torch/autograd/__init__.py", line 99, in backward
allow_unreachable=True) # allow_unreachable flag
RuntimeError: invalid gradient at index 0 - expected type torch.cuda.FloatTensor but got torch.FloatTensor
对于hybrid2seq\u model.py
,和backward
:
def backward(self, outputs, targets, weights, normalizer, criterion, regression=False):
grad_output, loss = self.generator.backward(outputs, targets, weights, normalizer, criterion, regression)
outputs.cuda()
grad_output.cuda()
outputs.backward(grad_output)
return loss
错误表明它期望一个cuda张量,而得到一个非cuda张量,所以这就是我要寻找的 像
grad\u output.cuda()这样的调用将返回一个cuda张量。这不是就地操作。您可能想要grad\u output=grad\u output.cuda()
,所以我首先要修复这样的调用
def backward(self, outputs, targets, weights, normalizer, criterion, regression=False):
grad_output, loss = self.generator.backward(outputs, targets, weights, normalizer, criterion, regression)
outputs.cuda()
grad_output.cuda()
outputs.backward(grad_output)
return loss