title: In Pytorch F.nll_loss() Expected object of type torch.LongTensor but found type torch.FloatTensor for argument #2 'target'
body: <p>Why does this error occur. </p>
  
  <p>I am trying to write a custom loss function, that finally has a negative log likelihood. </p>
  
  <p>As per my understanding the NLL is calculated between two probability values? </p>
  
  <pre><code>&gt;&gt;&gt; loss = F.nll_loss(sigm, trg_, ignore_index=250, weight=None, size_average=True)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/home//lib/python3.5/site-packages/torch/nn/functional.py", line 1332, in nll_loss
      return torch._C._nn.nll_loss(input, target, weight, size_average, ignore_index, reduce)
  RuntimeError: Expected object of type torch.LongTensor but found type torch.FloatTensor for argument #2 'target'
  </code></pre>
  
  <p>The inputs here being the following: </p>
  
  <pre><code>&gt;&gt;&gt; sigm.size()
  torch.Size([151414, 80])
  &gt;&gt;&gt; sigm
  tensor([[ 0.3283,  0.6472,  0.8278,  ...,  0.6756,  0.2168,  0.5659],
          [ 0.6603,  0.5957,  0.8375,  ...,  0.2274,  0.4523,  0.4665],
          [ 0.5262,  0.4223,  0.5009,  ...,  0.5734,  0.3151,  0.2076],
          ...,
          [ 0.4083,  0.2479,  0.5996,  ...,  0.8355,  0.6681,  0.7900],
          [ 0.6373,  0.3771,  0.6568,  ...,  0.4356,  0.8143,  0.4704],
          [ 0.5888,  0.4365,  0.8587,  ...,  0.2233,  0.8264,  0.5411]])
  </code></pre>
  
  <p>And my target tensor is:     </p>
  
  <pre><code>&gt;&gt;&gt; trg_.size()
  torch.Size([151414])
  &gt;&gt;&gt; trg_
  tensor([-7.4693e-01,  3.5152e+00,  2.9679e-02,  ...,  1.6316e-01,
           3.6594e+00,  1.3366e-01])
  </code></pre>
  
  <p>If I convert this to long I loose all data:      </p>
  
  <pre><code>&gt;&gt;&gt; sigm.long()
  tensor([[ 0,  0,  0,  ...,  0,  0,  0],
          [ 0,  0,  0,  ...,  0,  0,  0],
          [ 0,  0,  0,  ...,  0,  0,  0],
          ...,
          [ 0,  0,  0,  ...,  0,  0,  0],
          [ 0,  0,  0,  ...,  0,  0,  0],
          [ 0,  0,  0,  ...,  0,  0,  0]])
  &gt;&gt;&gt; trg_.long()
  tensor([ 0,  3,  0,  ...,  0,  3,  0])
  </code></pre>
  
  <p>If I convert the raw values of target tensor to <code>sigmoid</code> too: </p>
  
  <pre><code>&gt;&gt;&gt; F.sigmoid(trg_)
  tensor([ 0.3215,  0.9711,  0.5074,  ...,  0.5407,  0.9749,  0.5334])
  &gt;&gt;&gt; loss = F.nll_loss(sigm, F.sigmoid(trg_), ignore_index=250, weight=None, size_average=True)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/home/lib/python3.5/site-packages/torch/nn/functional.py", line 1332, in nll_loss
      return torch._C._nn.nll_loss(input, target, weight, size_average, ignore_index, reduce)
  RuntimeError: Expected object of type torch.LongTensor but found type torch.FloatTensor for argument #2 'target'
  </code></pre>
  
  <p>This does calculate the loss happily, but it is again just make belief as I have lost data in the long conversion: </p>
  
  <pre><code>&gt;&gt;&gt; loss = F.nll_loss(sigm, F.sigmoid(trg_).long(), ignore_index=250, weight=None, size_average=True)
  &gt;&gt;&gt; loss 
  tensor(-0.5010)
  
  &gt;&gt;&gt; F.sigmoid(trg_).long()
  tensor([ 0,  0,  0,  ...,  0,  0,  0])
  </code></pre>