Problem with the NLLLoss() in a pytorch tutorial

Hello fellow coders!

I’m just getting started with pytorch because I want to use it for my bachelor thesis. I wanted to learn more about classification with RNNs. So I started this tutorial:
http://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html#sphx-glr-intermediate-char-rnn-classification-tutorial-py

Under “Training/Training the Network” it implements the loss function. I wrote it exactly the same way as it is written there, but I get a RuntimeError:

Traceback (most recent call last):
  File "/home/erika/PycharmProjects/lstm_test/training.py", line 80, in <module>
    network.output, loss = train(category_tensor, line_tensor)
  File "/home/erika/PycharmProjects/lstm_test/training.py", line 50, in train
    loss = criterion(network.output, category_tensor)
  File "/home/erika/.local/lib/python3.5/site-packages/torch/nn/modules/module.py", line 357, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/erika/.local/lib/python3.5/site-packages/torch/nn/modules/loss.py", line 170, in forward
    self.ignore_index, self.reduce)
  File "/home/erika/.local/lib/python3.5/site-packages/torch/nn/functional.py", line 1052, in nll_loss
    return torch._C._nn.nll_loss(input, target, weight, size_average, ignore_index, reduce)
RuntimeError: Expected object of type Variable[torch.LongTensor] but found type Variable[torch.FloatTensor] for argument #1 'target'

I already tried to change “category_tensor” to a LongTensor, but this won’t work either.
Did anyone have the same problem?

Thank you very much for helping me!

Target must be of type torch.LongTensor. You can cast it as target = target.long()

1 Like

Alright thank you! But know I got another error…
I resized the target tensor to a 1D tensor. The output tensor is a 2D one, but I still get this:

RuntimeError: AssertionTHIndexTensor_(size)(target, 0) == batch_size’ failed. at /pytorch/torch/lib/THNN/generic/ClassNLLCriterion.c:79`

Does anyone know, what I did wrong?

Here is a fraction of the code:

def train(category_tensor, line_tensor):
    hidden = network.rnn.initHidden()

    network.rnn.zero_grad()

    for i in range(line_tensor.size()[0]):
        network.output, hidden = network.rnn(line_tensor[i], hidden)

    category_tensor = category_tensor.long()
    category_tensor = category_tensor.resize(57)

    loss = criterion(network.output, category_tensor)
    loss.backward()

    # Update values of parameters, gradients * learning rate
    for p in network.rnn.parameters():
        p.data.add_(-learning_rate, p.grad.data)

    return network.output, loss.data[0]


# ------------------------------- Track Losses -------------------------------

n_iters = 100000
print_every = 5000
plot_every = 1000

current_loss = 0
all_losses = []

def timeSince(since):
    now = time.time()
    s = now - since
    m = math.floor(s / 60)
    s -= m * 60
    return '%dm %ds' % (m, s)

start = time.time()

for iter in range(1, n_iters + 1):
    preprocessing.category, line, category_tensor, line_tensor = randomTrainingExample()
    network.output, loss = train(category_tensor, line_tensor)
    current_loss += loss

This error means that your input and target in your loss function do not have the same sizes at dimension 0, i.e. the mini-batch sizes are different.

1 Like