PyTorch:变量数据必须是张量,但得到int

时间:2018-02-15 17:15:49

标签: python pytorch

我是PyTorch的新手,我正在做一些NLP工作。我试图为LongTensor包装一个变量,但它显示:

Variable data has to be a tensor, but got int

我的代码如下所示:

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)

错误发生在" abstract = Variable(abstract)"。我打印它看起来像这样:

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]

我尝试将LongTensor包装在交互式编程模式中并且工作正常,但我不知道为什么这是错误的。

1 个答案:

答案 0 :(得分:0)

trainIters参数的顺序错误。

你打电话

trainIters(encoder1, decoder1, abs_train, art_train, 1000, print_every=100)

但该功能定义为

def trainIters(encoder, decoder, n_iters, abstract, article, print_every=1000, plot_every=100, learning_rate=0.01):

请注意,article是一个整数值(1000)。

如果您的参数列表太长以避免混淆,我建议您使用命名参数,例如:

trainIters(
    encoder=encoder1, 
    decoder=decoder1, 
    abstract=abs_train, 
    article=art_train, 
    n_iters=1000, 
    print_every=100,
)