title: PyTorch: Variable data has to be a tensor, but got int
body: <p>I'm new to PyTorch and I'm doing some NLP works. I tried to wrap a Variable for LongTensor, but it shows that:</p>
  
  <pre><code>Variable data has to be a tensor, but got int
  </code></pre>
  
  <p>My code looks like this:</p>
  
  <pre><code>def trainIters(encoder, decoder, n_iters, abstract, article, print_every=1000, plot_every=100, learning_rate=0.01):
  start = time.time()
  plot_losses = []
  print_loss_total = 0  # Reset every print_every
  plot_loss_total = 0  # Reset every plot_every
  
  encoder_optimizer = optim.SGD(encoder.parameters(), lr=learning_rate)
  decoder_optimizer = optim.SGD(decoder.parameters(), lr=learning_rate)
  
  print(abstract)
  
  abstract = Variable(abstract)
  article = Variable(article)
  criterion = nn.NLLLoss()
  
  for iter in range(1, n_iters + 1):
      input_variable = article[iter - 1]
      target_variable = abstract[iter - 1]
  
      loss = train(input_variable, target_variable, encoder, decoder, encoder_optimizer, decoder_optimizer, criterion)
      print_loss_total += loss
      plot_loss_total += loss
  
      if iter % print_every == 0:
          print_loss_avg = print_loss_total / print_every
          print_loss_total = 0
          print('%s (%d %d%%) %.4f' % (timeSince(start, iter / n_iters),
                                       iter, iter / n_iters * 100, print_loss_avg))
  
      if iter % plot_every == 0:
          plot_loss_avg = plot_loss_total / plot_every
          plot_losses.append(plot_loss_avg)
          plot_loss_total = 0
  
  showPlot(plot_losses)
  
  if __name__ == '__main__':
      hidden_size = 1024
      data = Data()
      vocab = data.load_dict('/users/cheng/NLP/Data/cnn/abstracts/train_set', '/users/cheng/NLP/Data/cnn/articles/train_set')
      abs_train, art_train = data.load_matrix('/users/cheng/NLP/Data/cnn/abstracts/train_set',
                                       '/users/cheng/NLP/Data/cnn/articles/train_set')
      encoder1 = model.EncoderRNN(len(vocab.idx2word), hidden_size)
      decoder1 = model.DecoderRNN(hidden_size, len(vocab.idx2word))
  
      if torch.cuda.is_available():
          encoder1 = encoder1.cuda()
          decoder1 = decoder1.cuda()
  
      trainIters(encoder1, decoder1, abs_train, art_train, 1000, print_every=100)
  </code></pre>
  
  <p>The error happens at "abstract = Variable(abstract)". I printed it and it looks like this:</p>
  
  <pre><code>2.0000e+00  2.6000e+01  4.0000e+00  ...   0.0000e+00  0.0000e+00  0.0000e+00
  2.0000e+00  5.1000e+01  4.9000e+01  ...   0.0000e+00  0.0000e+00  0.0000e+00
  2.0000e+00  5.1000e+01  4.9000e+01  ...   0.0000e+00  0.0000e+00  0.0000e+00
              ...                   ⋱                   ...                
  2.0000e+00  4.5500e+02  5.1000e+01  ...   0.0000e+00  0.0000e+00  0.0000e+00
  2.0000e+00  1.3560e+03  5.1000e+01  ...   0.0000e+00  0.0000e+00  0.0000e+00
  2.0000e+00  2.0100e+02  1.4000e+01  ...   0.0000e+00  0.0000e+00  0.0000e+00
  [torch.LongTensor of size 55547x2552]
  </code></pre>
  
  <p>I tried to wrap a LongTensor in the interactive programming mode and it works fine, but I don't know why this is wrong. </p>