今天在用pytorch训练模型遇到一个错误:
RuntimeError: ‘lengths’ argument should be a 1D CPU int64 tensor, but got 1D cuda:0 Long tensor
解决:
参考https://github.com/pytorch/pytorch/releases
inp = torch.rand(10, 2, 3, device=“cuda”)
lengths = torch.tensor([10, 7], device=“cuda”)
torch.nn.utils.rnn.pack_padded_sequence(inp, lengths)
RuntimeError: ‘lengths’ argument should be a 1D CPU int64 tensor,
but got 1D cuda:0 Long tensorEnsure the lenghts is already on the right device
lengths = lengths.cpu()
torch.nn.utils.rnn.pack_padded_sequence(inp, lengths)Runs fine with no implicit move across device
在torch\nn\utils\rnn.py 第244行附近
_VF._pack_padded_sequence(input, lengths, batch_first)
改为 _VF._pack_padded_sequence(input, lengths.cpu(), batch_first)