got torch.cuda.IntTensor instead (while checking arguments for embedding)

pytorch做nn.Embedding时报错,完整报错信息如下:

File "D:\workspace\ncz-python-algo\com\ncz\algo\transformer_study\transformer.py", line 28, in forward
    embedds = self.lut(x)
  File "D:\programs\python37\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "D:\programs\python37\lib\site-packages\torch\nn\modules\sparse.py", line 126, in forward
    self.norm_type, self.scale_grad_by_freq, self.sparse)
  File "D:\programs\python37\lib\site-packages\torch\nn\functional.py", line 1852, in embedding
    return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got torch.cuda.IntTensor instead (while checking arguments for embedding)

主要意思是:

embedding时输入的值为int类型,而要求的是long类型。

反观相关示例代码:

data = torch.from_numpy(np.random.randint(2, 11, size=(2, 2)))
lut = nn.Embedding(11, 20)
embeddings = lut(data)
print(embeddings)
 

而data数据就是torch.IntTensor代码。

修改方法为:

data = torch.from_numpy(np.random.randint(2, 11, size=(2, 2))).long()

问题修复!

你可能感兴趣的:(神经网络,深度学习,python,人工智能)