RuntimeError:后端CUDA的预期对象,但参数#3'索引'

时间:2019-08-17 20:55:15

标签: pytorch word-embedding

我正在使用Google CoLab上的项目“ lda2vec-pytorch”, 奔宁pytorch 1.1.0

https://github.com/TropComplique/lda2vec-pytorch

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
cuda:0

我在前进方法中添加了“噪声”的情况下出现异常 class negative_sampling_loss(nn.Module):

        noise = self.multinomial.draw(batch_size*window_size*self.num_sampled)
        noise = Variable(noise).view(batch_size, window_size*self.num_sampled)

        device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self.embedding = self.embedding.to(device)
        #print("negative_sampling_loss::forward() self.embedding", self.embedding.is_cuda)  This line get's an error.

        # shape: [batch_size, window_size*num_sampled, embedding_dim]
        noise = self.embedding(noise)  # Exception HERE

这是堆栈跟踪:

Traceback (most recent call last):
  File "train.py", line 36, in <module>
    main()
  File "train.py", line 32, in main
    save_every=20, grad_clip=5.0
  File "../utils/training.py", line 138, in train
    neg_loss, dirichlet_loss = model(doc_indices, pivot_words, target_words)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "../utils/lda2vec_loss.py", line 82, in forward
    neg_loss = self.neg(pivot_words, target_words, doc_vectors, w)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "../utils/lda2vec_loss.py", line 167, in forward
    noise = self.embedding(noise)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/sparse.py", line 117, in forward
    self.norm_type, self.scale_grad_by_freq, self.sparse)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py", line 1506, in embedding
    return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
RuntimeError: Expected object of backend CUDA but got backend CPU for argument #3 'index'

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

变量noise在CPU上可用,而self.embedding在GPU上可用。我们也可以将noise发送到GPU:

noise = noise.to(device)