Pytorch嵌入RuntimeError:类型为torch.LongTensor的预期对象,但为参数#3'index'找到类型为torch.cuda.LongTensor的对象

时间:2018-08-03 23:43:08

标签: python typeerror pytorch word-embedding

我收到此错误消息

RuntimeError: Expected object of type torch.LongTensor but found type torch.cuda.LongTensor for argument #3 'index'

但是argument #3 "index"是什么意思?我在index中找不到“ torch.embedding”自变量(此处来源:https://pytorch.org/docs/stable/_modules/torch/nn/modules/sparse.html#Embedding) 似乎我正在传递嵌入错误的参数。

我什至如下更改了输入的数据类型,但错误仍然存​​在。

batch['doc_tok'] = batch['doc_tok'].long()
batch['query_tok'] = batch['query_tok'].long()

任何评论(即使很短!)或仅列出要查看的关键字都将受到高度赞赏!


这里是完整的追溯。

Traceback (most recent call last):
  File "train_v2.py", line 110, in <module>
    main()
  File "train_v2.py", line 81, in main
    model.update(batch)
  File "/home/aerin/Desktop/squad_vteam/src/model.py", line 129, in update
    loss_adv = self.adversarial_loss(batch, loss, self.network.lexicon_encoder.embedding.weight, y)
  File "/home/aerin/Desktop/squad_vteam/src/model.py", line 104, in adversarial_loss
    start, end, _ = self.network(batch)
  File "/home/aerin/anaconda3/envs/san/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/aerin/Desktop/squad_vteam/src/dreader.py", line 78, in forward
    doc_mask, query_mask = self.lexicon_encoder(batch)
  File "/home/aerin/anaconda3/envs/san/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/aerin/Desktop/squad_vteam/src/encoder.py", line 116, in forward
    doc_emb, query_emb = emb(doc_tok), emb(query_tok)
  File "/home/aerin/anaconda3/envs/san/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/aerin/anaconda3/envs/san/lib/python3.6/site-packages/torch/nn/modules/sparse.py", line 108, in forward
    self.norm_type, self.scale_grad_by_freq, self.sparse)
  File "/home/aerin/anaconda3/envs/san/lib/python3.6/site-packages/torch/nn/functional.py", line 1076, in embedding
    return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
RuntimeError: Expected object of type torch.LongTensor but found type torch.cuda.LongTensor for argument #3 'index'

更新:我什至将整个model.network发送给cpu,但仍然收到相同的错误。

batch['doc_tok']=batch['doc_tok'].long().cpu()
batch['query_tok']=batch['query_tok'].long().cpu()
self.network.cpu()

print(batch['doc_tok'].dtype,  batch['query_tok'].dtype) # They are both torch.int64 torch.int64

start, end, _ = self.network(batch)

在这一点上,我怀疑这可能是一个错误...

model.py代码:https://github.com/byorxyz/san_mrc/blob/master/src/model.py

定义的网络:https://github.com/byorxyz/san_mrc/blob/master/src/dreader.py

2 个答案:

答案 0 :(得分:3)

在我看来,您的输入/目标张量(createStore等)以及您的网络及其变量(我相信batch['doc_tok']index层的内部张量)在不同的设备上(用于数据的CPU,用于模型的GPU?)。

如果您希望所有内容都在GPU上运行,则都需要:

  • 将数据加载到此处,例如Embedding
  • 在此处加载模型,例如batch['doc_tok'].cuda()

如果要在CPU上运行,则用model.network.cuda()代替。

答案 1 :(得分:1)

尝试一下:

batch['doc_tok']=batch['doc_tok'].long().cpu()
batch['query_tok']=batch['query_tok'].long().cpu()