断言`THIndexTensor_(size)(target,0)== batch_size'失败

时间:2017-09-28 16:49:45

标签: python machine-learning torch pytorch language-model

当我用PyTorch编写Python代码时发生了。我正在尝试使用CrossEntropyLoss构建一个简单的语言模型,但是出现了这个错误。实际上,我根据this编写了这段代码。这是我的代码。

import numpy as np
import torch
from torch.autograd import Variable
import torch.nn as nn

data = '...'
words = list(set(data))
word2ind = {word: i for i, word in enumerate(words)}
ind2word = {i: word for i, word in enumerate(words)}

class RNN(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(RNN, self).__init__()
        self.hidden_size = hidden_size
        self.in2h = nn.Linear(input_size-1+hidden_size, hidden_size)
        self.in2o = nn.Linear(input_size-1+hidden_size, output_size)
        self.o2o = nn.Linear(hidden_size+output_size, output_size)
        self.softmax = nn.LogSoftmax()

    def forward(self, inputs, hidden):
        input_combined = torch.cat((inputs.float(), hidden.float()), 1)
        print(type(input_combined.data))
        hidden = self.in2h(input_combined)
        output = self.in2o(input_combined)
        output_combined = torch.cat((hidden, output), 1)
        output = self.o2o(output_combined)
        output = self.softmax(output)
        print(output)
        return output, hidden

    def init_hidden(self):
        return Variable(torch.from_numpy(np.zeros((1, self.hidden_size))).type(torch.LongTensor))

def form_onehot(sent):
    one_hot = np.zeros((len(data), len(words)), dtype=np.int64)
    for i, word in enumerate(sent):
        one_hot[i, word2ind[word]] = 1
    return torch.LongTensor(one_hot)

def random_choice(vec):
    return np.random.choice(range(len(words)), p=vec)

def train(rnn, learning_rate, optimizer, criterion, input_tensor, target_tensor):
    hidden = rnn.init_hidden()
    optimizer.zero_grad()
    for i in range(input_tensor.size(1)):
        output, hidden = rnn(input_tensor[i, :].unsqueeze(0), hidden)
        loss = criterion(output, target_tensor[i])
        loss.backward()
        optimizer.step()
    return output, loss.data[0] / input_tensor.size()[0]

onehot_data = form_onehot(data)
rnn = RNN(len(words), 10, len(words))
learning_rate = 0.1
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(rnn.parameters(), lr=learning_rate)
input_tensor = Variable(onehot_data[:, :-1].type(torch.FloatTensor))
print(type(input_tensor.data))
target_tensor = Variable(onehot_data[:, 1:])
int_target_tensor = Variable(onehot_data[1:, :].type(torch.LongTensor))
output, loss = train(rnn, learning_rate, optimizer, criterion, input_tensor, int_target_tensor)

这是错误:

    ---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-449-8abc91b616c7> in <module>()
----> 1 output, loss = train(rnn, learning_rate, optimizer, criterion, input_tensor, int_target_tensor)

<ipython-input-445-72363097fc21> in train(rnn, learning_rate, optimizer, criterion, input_tensor, target_tensor)
     52         output, hidden = rnn(input_tensor[i, :].unsqueeze(0), hidden)
     53         print(output.size(), target_tensor[i].size())
---> 54         loss = criterion(output, target_tensor[i])
     55         print('aaaaaaaaaaa')
     56         loss.backward()

D:\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    204 
    205     def __call__(self, *input, **kwargs):
--> 206         result = self.forward(*input, **kwargs)
    207         for hook in self._forward_hooks.values():
    208             hook_result = hook(self, input, result)

D:\Anaconda3\lib\site-packages\torch\nn\modules\loss.py in forward(self, input, target)
    319         _assert_no_grad(target)
    320         return F.cross_entropy(input, target,
--> 321                                self.weight, self.size_average)
    322 
    323 

D:\Anaconda3\lib\site-packages\torch\nn\functional.py in cross_entropy(input, target, weight, size_average)
    535                 for each minibatch.
    536     """
--> 537     return nll_loss(log_softmax(input), target, weight, size_average)
    538 
    539 

D:\Anaconda3\lib\site-packages\torch\nn\functional.py in nll_loss(input, target, weight, size_average)
    503     else:
    504         raise ValueError('Expected 2 or 4 dimensions (got {})'.format(dim))
--> 505     return f(input, target)
    506 
    507 

D:\Anaconda3\lib\site-packages\torch\nn\_functions\thnn\auto.py in forward(self, input, target)
     39         output = input.new(1)
     40         getattr(self._backend, update_output.name)(self._backend.library_state, input, target,
---> 41                                                    output, *self.additional_args)
     42         return output
     43 

RuntimeError: Assertion `THIndexTensor_(size)(target, 0) == batch_size' failed.  at d:\downloads\pytorch-master-1\torch\lib\thnn\generic/ClassNLLCriterion.c:50

我现在真的被困在这里,所以任何回复都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

发现问题。这是由于target_tensor的形状。它应该是Variable(torch.LongTensor)的列表。