Multi-target not supported at ..\aten\src\THNN/generic/ClassNLLCriterion.c:20

username: Samuel_Adu
cooked: <p>Hello People,</p>
<p>I am newbie, working with adult dataset.</p>
<p>I would please appreciate any help here on how to fix this.</p>
<p>iter = 0<br>
for epoch in range(num_epochs):<br>
for i, (inputs, labels) in enumerate(train_loader):</p>
<pre><code>    # Load inputs as Variable
    inputs = Variable(X_trainTensor)
    labels = Variable(y_trainTensor).type(torch.LongTensor)
    
    #y_tensor = torch.tensor(y_train, dtype=torch.long, device=device)
    # Clear gradients w.r.t. parameters
    optimizer.zero_grad()
    
    # Forward pass to get output/logits
    outputs = model(inputs.float())
    
    # Calclate Loss: softmax --&gt; cross entropy loss
    loss = criterion(outputs, labels)
    
    # Getting gradients w.r.t. parameters
    loss.backward()
    
    # Updating parameters
    optimizer.step()
</code></pre>
<p>Error:</p>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
72<br>
73         # Calclate Loss: softmax --&gt; cross entropy loss<br>
—&gt; 74         loss = criterion(outputs, labels)<br>
75<br>
76         # Getting gradients w.r.t. parameters<br>
.<br>
.<br>
.<br>
.<br>
RuntimeError: multi-target not supported at …\aten\src\THNN/generic/ClassNLLCriterion.c:20</p>

username: mailcorahul
cooked: <p>What criterion are you using?<br>
Can you check if the <code>labels</code> tensor is of size <code>(N,)</code> and <code>outputs</code> tensor is of size <code>(N, C)</code>?<br>
<code>N=batch_size, C=number of classes</code></p>

username: Samuel_Adu
cooked: <aside class="quote no-group" data-username="Samuel_Adu" data-post="1" data-topic="58891">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/samuel_adu/48/12389_2.png" class="avatar"> Samuel_Adu:</div>
<blockquote>
<p>labels</p>
</blockquote>
</aside>
<p>Thank you very much Raghul.</p>
<p>I have the following:</p>
<p>]:</p>
<p>​<br>
print(inputs.shape)<br>
print(labels.shape)<br>
print(outputs.shape)</p>
<p>torch.Size([100, 4])<br>
torch.Size([100, 1])<br>
torch.Size([100, 1])</p>
<p>Please any suggestion on how to for about this?</p>
<p>input_dim = 4<br>
hidden_dim = 10<br>
output_dim = 1</p>
<p>model = FeedforwardNeuralNetModel(input_dim, hidden_dim, output_dim)</p>
<p>criterion = nn.CrossEntropyLoss()</p>
<p>learning_rate = 0.01</p>
<p>optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)</p>
<pre><code>#return model, loss_fnc, optimizer
</code></pre>
<p><span class="hashtag-raw">#load_model</span>(0.01)</p>
<p>for epoch in range(num_epochs):<br>
for i, (inputs, labels) in enumerate(train_loader):<br>
# Load inputs as Variable<br>
inputs = Variable(torch.Tensor(inputs.view(-1, 4).float()))<br>
labels = Variable(torch.Tensor(labels.float()))#.type(torch.LongTensor)</p>
<pre><code>    # Clear gradients w.r.t. parameters
    optimizer.zero_grad()
    
    # Forward pass to get output/logits
    outputs = model(inputs.float())
    
    # Calclate Loss: softmax --&gt; cross entropy loss
    
    loss = criterion(outputs.view(-1, 1), labels.type(torch.LongTensor))
    
    # Getting gradients w.r.t. parameters
    loss.backward()
    #loss.backward()
    
    # Updating parameters
    optimizer.step()
</code></pre>
<hr>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
97         # Calclate Loss: softmax → cross entropy loss<br>
98<br>
—&gt; 99         loss = criterion(outputs.view(-1, 1), labels.type(torch.LongTensor))<br>
100<br>
101         # Getting gradients w.r.t. parameters</p>
<p>~\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
491             result = self._slow_forward(*input, **kwargs)<br>
492         else:<br>
 → 493             result = self.forward(*input, **kwargs)<br>
494         for hook in self._forward_hooks.values():<br>
495             hook_result = hook(self, input, result)</p>
<p>~\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\nn\modules\loss.py in forward(self, input, target)<br>
940     def forward(self, input, target):<br>
941         return F.cross_entropy(input, target, weight=self.weight,<br>
 → 942                                ignore_index=self.ignore_index, reduction=self.reduction)<br>
943<br>
944</p>
<p>~\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\nn\functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)<br>
2054     if size_average is not None or reduce is not None:<br>
2055         reduction = _Reduction.legacy_get_string(size_average, reduce)<br>
 → 2056     return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)<br>
2057<br>
2058</p>
<p>~\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\nn\functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)<br>
1869                          .format(input.size(0), target.size(0)))<br>
1870     if dim == 2:<br>
 → 1871         ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)<br>
1872     elif dim == 4:<br>
1873         ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)</p>
<p>RuntimeError: multi-target not supported at …\aten\src\THNN/generic/ClassNLLCriterion.c:20</p>

username: albanD
cooked: <p>Hi,</p>
<p>The labels should be of size (N,) not (N, 1). You can use <code>labels.squeeze_(-1)</code> to remove the extra dimension of size 1.</p>

username: Samuel_Adu
cooked: <aside class="quote no-group" data-username="albanD" data-post="4" data-topic="58891">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/48/215_2.png" class="avatar"> albanD:</div>
<blockquote>
<p>labels.squeeze_(-1)</p>
</blockquote>
</aside>
<p>Hi AlbanD,</p>
<p>Thank you very much for the suggestion.</p>
<p>But this:</p>
<p>RuntimeError: Assertion `cur_target &gt;= 0 &amp;&amp; cur_target &lt; n_classes’ failed.  at …\aten\src\THNN/generic/ClassNLLCriterion.c:92</p>
<p>Any idea please?</p>

username: mailcorahul
cooked: <p><a class="mention" href="/u/samuel_adu">@Samuel_Adu</a> I think since the size of <code>outputs</code> is <code>(N, 1)</code> where <code>n_classes or C </code>= 1 here, whereas the <code>labels</code> tensor contains values like for eg. <code>[1, 0, 1, 0, 0] i.e labels for class 0 and 1</code>, this error is thrown.<br>
The variable <code>output_dim</code> should be 2 and size of <code>outputs</code> tensor should be <code>(N, 2)</code>.<br>
Just to make sure, is this a binary classification task?</p>

username: albanD
cooked: <p>This is a different error. This one says that the values contained in your target tensor are larger than the number of classes (or negative).</p>

username: Samuel_Adu
cooked: <p>Hi Mailcorahul,</p>
<p>Thank you very much for your kind response.Yes the labels tensor is like this [1, 0, 1, 0, 0] but the outputs tensor size is (N, 2) and Yes, this is a binary classification task…please below…</p>
<p>print(inputs.shape)<br>
print(labels.shape)<br>
print(outputs.shape)<br>
print(labels)<br>
print(outputs)</p>
<p>torch.Size([100, 4])<br>
torch.Size([100])<br>
torch.Size([100, 1])<br>
tensor([1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,<br>
0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,<br>
0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,<br>
0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0,<br>
0, 0, 1, 0])<br>
tensor([[  -0.6877],<br>
[ -79.4583],<br>
[  -0.6877],<br>
[  -0.6877],<br>
[  -0.6877],<br>
[-231.3748],</p>

username: Samuel_Adu
cooked: <p>Hi AlbanD,</p>
<p>Thank you very much for your kind response.</p>
<p>Yes it is. It came up after I used labels.squeeze_(-1)…still trying to figure out the solution.</p>
<p>I’d appreciate any further suggestion.</p>

username: albanD
cooked: <p>Hi,</p>
<p>So the problem is that your output is of size (100, 1), meaning that C = 1 for your loss: you have a single class. I think this should be 2 in your case no?</p>

username: Samuel_Adu
cooked: <p>Wow…Thank you very much Raghul</p>
<p>This solves it.</p>

username: Samuel_Adu
cooked: <p>Wow…Thank you very much AlbanD for your suggestions</p>
<p>This is resolved.</p>