Pytorch FloatClassNLLCriterion_updateOutput error
I am getting the following error message while computing the loss of my neural net: TypeError: FloatClassNLLCriterion_updateOutput received an invalid combination of arguments - got (int, torch.FloatTensor, !torch.FloatTensor!, torch.FloatTensor, bool, NoneType, torch.FloatTensor, int), but expected (int state, torch.FloatTensor input, torch.LongTensor target, torch.FloatTensor output, bool sizeAverage, [torch.FloatTensor weights or None], torch.FloatTensor total_weight, int ignore_index) on this line: loss = criterion(outputs,one_hot_target). 

I have tried several things and searched the web, but I can't seem to find my mistake. Does someone have an idea?

The code used:

class Net(nn.Module):
def _init_(self,input_size,hidden_size, num_classes):
    super(Net, self)._init_()
    self.l1 = nn.Linear(input_size,hidden_size)
    self.l2 = nn.Linear(hidden_size,num_classes)

def forward(self,x):
    x = self.l1(x)
    x = F.tanh(x)
    x = self.l2(x)
    x = F.softmax(x)
    return x

mlp = Net(input_size,hidden_size,num_classes)

criterion = nn.NLLLoss()
optimizer = torch.optim.SGD(mlp.parameters(), lr=learning_rate)

for i in range(N-1,len(word_list)):

  # Define input vector x
  x = None


  for j in range(0,N-1):
      try:
          x = np.r_[x,w2v[word_list[j]]]
      except:
          x = w2v[word_list[j-N+1]]

  # Done with defining  x


  np.reshape(x,(len(x),1))
  x = autograd.Variable(torch.FloatTensor(x))
  optimizer.zero_grad()

  outputs = mlp(x)
  outputs = outputs.unsqueeze(0)
  outputs = outputs.transpose(0,1)

  index = w2i[word_list[i]]
  one_hot_target = np.zeros([num_classes,1],dtype=float)

  one_hot_target[index] = float(1)

  one_hot_target = autograd.Variable(torch.Tensor(one_hot_target))
  print (one_hot_target)
  loss = criterion(outputs,one_hot_target)
  #loss.backward()
  #optimizer.step()


The target tensor you are passing to your loss function is of type Float Tensor. That's happening here:
one_hot_target = autograd.Variable(torch.Tensor(one_hot_target)) 

That is, because the default Tensor type in PyTorch is FloatTensor.
However, the NLLLoss() function is expecting a LongTensor as target. You can double check in the example in the documentation, which you can find here: NLLLoss Pytorch docs.

You can simply convert your target Tensor to a LongTensor like this:

one_hot_target = autograd.Variable(torch.Tensor(one_hot_target)).long()
