practical-pytorch icon indicating copy to clipboard operation
practical-pytorch copied to clipboard

TypeError: torch.index_select when move to gpu

Open nijianmo opened this issue 7 years ago • 4 comments

Hi, thanks for your clear code :-) I have one question about moving the model to gpu. It'd be appreciated if you could share some of your suggestion.

I am running the char-generation model. I use model.cuda() to move it to gpu and meet a TypeError in torch.index_select error. The index is set to long in char_tensor function while the gpu failed to recognize it as long type. The code works on cpu. Anyone has any idea?

Traceback (most recent call last):
  File "train-beer.py", line 100, in <module>
    loss = train(*random_training_set(args.chunk_len))
  File "train-beer.py", line 84, in train
    output, hidden = decoder(inp[c], hidden)
  File "/home/anaconda2/envs/venv35/lib/python3.5/site-packages/torch/nn/modules/module.py", line 224, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/pytorch/practical-pytorch/char-rnn-generation/model.py", line 20, in forward
    input = self.encoder(input.view(1, -1))
  File "/home/anaconda2/envs/venv35/lib/python3.5/site-packages/torch/nn/modules/module.py", line 224, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/anaconda2/envs/venv35/lib/python3.5/site-packages/torch/nn/modules/sparse.py", line 94, in forward
    self.scale_grad_by_freq, self.sparse
  File "/home/anaconda2/envs/venv35/lib/python3.5/site-packages/torch/nn/_functions/thnn/sparse.py", line 53, in forward
    output = torch.index_select(weight, 0, indices.view(-1))
TypeError: torch.index_select received an invalid combination of arguments - got (torch.cuda.FloatTensor, int, !torch.LongTensor!), but expected (torch.cuda.FloatTensor source, int dim, torch.cuda.LongTensor index)

nijianmo avatar Sep 09 '17 03:09 nijianmo

Getting the same error while using nn.Embedding. During init I do: self.embedding = nn.Embedding(output_size, hidden_size) And during the forward pass: word_embedded = self.embedding(word_input)

I am getting similar error: TypeError: torch.index_select received an invalid combination of arguments - got (torch.cuda.FloatTensor, int, !torch.LongTensor!), but expected (torch.cuda.FloatTensor source, int dim, torch.cuda.LongTensor index)

Thanks

parajain avatar Sep 12 '17 05:09 parajain

if you forget

    if use_cuda:
        model.cuda()

this error will occur

askender avatar Sep 12 '17 10:09 askender

I was able to fix it, word_input was not converted to cuda in this case. My bad. Thanks for the help.

parajain avatar Sep 12 '17 10:09 parajain

@pj-parag How did you solve the problem

ls4408 avatar Nov 22 '17 08:11 ls4408