ValueError: Expected target size (32, 7), got torch.Size([32])

username: salma_kasraoui
cooked: <p>Hi everyone,<br>
I know this topic was previously discussed, however, the proposed solutions didn’t work for me.<br>
I am trying to perform classification of precomputed features into 7 categories using logistic regression.<br>
I got the following error when training the classifier:</p>
<pre><code class="lang-python">ValueError: Expected target size (32, 7), got torch.Size([32])
</code></pre>
<p>My target shape was ([768,1]) and squeezing it didn’t solve the problem.<br>
The input shape also is <code>torch.Size([768, 1, 221])</code></p>
<p>By squeezing it, I got this error:</p>
<pre><code class="lang-python">RuntimeError: Expected object of scalar type Long but got scalar type Int for argument #2 'target'
</code></pre>
<p>To train the logistic regression model, I used this piece of code which works steadily with another dataset:</p>
<pre><code class="lang-auto">#define classifier
num_input = trainingData.shape[-1]
num_classes = trainingLabels.cpu().unique().numel()
model = Sequential(Linear(num_input, num_classes), LogSoftmax(dim=1))
optimizer = Adam(model.parameters())
criterion = NLLLoss()

batch_size = 32
num_epochs = 50
#learning rate
lr = 1e-4

nsamples = trainingData.shape[0]
nbatches = nsamples // batch_size

for e in range(num_epochs):
    perm = torch.randperm(nsamples)

    
    for i in range(nbatches):
        idx = perm[i * batch_size : (i+1) * batch_size]
        model.zero_grad()
        resp = model.forward(trainingData[idx])
        trainingLabels = trainingLabels.squeeze()
        loss = criterion(resp, trainingLabels[idx])
        loss.backward()
        optimizer.step()
    
    resp = model.forward(trainingData)
    avg_loss = criterion(resp, trainingLabels)
</code></pre>
<p>Obviously, my problem is in the data shape but I can not fix it may be because I am new to pytorch.</p>
<p>Any help will be appreciated.</p>

username: ptrblck
cooked: <p>Hi Salma,</p>
<p>Based on the error message, it seems you have an additional dimension in your model output.<br>
If you just would like to classify the data in 7 classes (not-pixel wise classification etc.), your output should have the shape <code>[batch_size, nb_classes]</code>, while your target should be a <code>torch.LongTensor</code> containing the class indices in the shape <code>[batch_size]</code>.</p>
<p>Try to squeeze the model output and convert your target as:</p>
<pre><code class="lang-python">target = target.long()
</code></pre>
<p>If that doesn’t help, could you post the input, output and target shapes, so that we could debug this issue further?</p>
<p>Also a small issue in your code:<br>
You should call the model directly (<code>output = model(data)</code>), since this will make sure to register all hooks etc. Currently you are using <code>model.forward(data)</code> which might yield some issues in the future.</p>
<p>While your code should work fine using your manual batching approach, you could use a <code>Dataset</code> and <code>DataLoader</code> instead, which will make shuffling, preprocessing the data etc. a bit easier.</p>

username: salma_kasraoui
cooked: <p>Yes there was an additional dimension in the input and the target.<br>
I squeezed the input and I converted the target to <code>target.long</code></p>
<p>And it worked. Thank you!</p>

username: Naveen
cooked: <p>I have a similar error I am posting it here because it is similar and<br>
I tried this method and its not working.<br>
I was trying to do cross entropy loss and I got this error “Expected target size (1, 5), got torch.Size([1, 17451])”<br>
My cls_preds.size()= torch.Size([1, 17451, 5]) and cls_targets.size()= torch.Size([1, 17451])<br>
and I was trying to calculate Loss by following code<br>
loss = nn.CrossEntropyLoss()<br>
output = loss(cls_preds, cls_targets)</p>
<p>Please provide some explanation, thank you.</p>

username: ptrblck
cooked: <p><code>nn.CrossEntrolyLoss</code> expects a model output in the shape <code>[batch_size, nb_classes, *additional_dims]</code> and a target in the shape <code>[batch_size, *additional_dims]</code> containing the class indices in the range <code>[0, nb_classes-1]</code>.</p>
<p>Based on your output shape it seems you are dealing with 17451 classes and a temporal dimension of 5. The target should thus have a shape of <code>[1, 5]</code> (note the missing channel dimension).</p>
<p>Also, it seems your target might be one-hot encoded, as it’s using the class dimension in dim1, but is missing the temp. dim?</p>
<p>Maybe your use case is vice versa and you are dealing with 5 classes.<br>
In this case, you would have to permute the output so that it has the shape <code>[1, 5, 17451]</code>.</p>

username: Naveen
cooked: <p>Yes you are right my class_num = 5 and number of batches = 1 and the other dimension is number of anchors.<br>
and I did cls_preds.permute(0,2,1) and my shape is now [1, 5, 17451].</p>
<p>Now I am getting an error saying “RuntimeError: Assertion `cur_target &gt;= 0 &amp;&amp; cur_target &lt; n_classes’ failed.  at /pytorch/aten/src/THNN/generic/SpatialClassNLLCriterion.c:111” in nn.nll_loss2d.</p>
<p>any help?<br>
Thank you.</p>

username: ptrblck
cooked: <p>If you are dealing with the shape <code>[1, 5, 17451]</code>, I assume that each “anchor” belongs to one of the 5 classes?<br>
If so, then the target should contain class indices in the range <code>[0, 4]</code>.</p>

username: Naveen
cooked: <p>Actually I tried number of anchor boxes as batch number and it got solved. It make sense? Thanks anyway.</p>

username: dbld_blqq
cooked: <p>Hi, I face the same problem as Naveen,  shape of my output is torch.Size([1, 20, 4]), where 20 is the time sequence, 4 is the number of classes, softmax was used as the final activitation function, and loss is nn.CrossEntropyLoss(), I use the follow expression:<br>
loss = loss_func(output, torch.max(label, 2)[1])<br>
I got this error:<br>
ValueError: Expected target size (2, 4), got torch.Size([2, 20])<br>
But if I changed my time sequence to 4, this error disappeared, why？</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="dbld_blqq" data-post="9" data-topic="42409">
<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/dbld_blqq/48/38516_2.png" class="avatar"> dbld_blqq:</div>
<blockquote>
<p>Hi, I face the same problem as Naveen, shape of my output is torch.Size([1, 20, 4]), where 20 is the time sequence, 4 is the number of classes</p>
</blockquote>
</aside>
<p>That would be wrong as explained here:</p>
<aside class="quote no-group" data-username="ptrblck" data-post="5" data-topic="42409">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p><code>nn.CrossEntrolyLoss</code> expects a model output in the shape <code>[batch_size, nb_classes, *additional_dims]</code> and a target in the shape <code>[batch_size, *additional_dims]</code> containing the class indices in the range <code>[0, nb_classes-1]</code>.</p>
</blockquote>
</aside>
<p>so you would have to <code>permute</code> the model output such that the class dimension is in <code>dim1</code>.</p>
<aside class="quote no-group" data-username="dbld_blqq" data-post="9" data-topic="42409">
<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/dbld_blqq/48/38516_2.png" class="avatar"> dbld_blqq:</div>
<blockquote>
<p>softmax was used as the final activitation function, and loss is nn.CrossEntropyLoss()</p>
</blockquote>
</aside>
<p>That is unfortunately also wrong, since <code>nn.CrossEntropyLoss</code> expects logits, so you would have to remove the <code>softmax</code>.</p>

username: dbld_blqq
cooked: <p>You are right, I added transpose op before output and it works. Thanks a lot.</p>

username: Faisal_T
cooked: <p>I have a similar problem I’m getting:<br>
“RuntimeError: Expected target size [2, 30000], got [2]”. when calling “torch.nn.CrossEntropyLoss()”.</p>
<p>I saw your comment where you explained that : " <code>nn.CrossEntrolyLoss</code> expects a model output in the shape <code>[batch_size, nb_classes, *additional_dims]</code> and a target in the shape <code>[batch_size, *additional_dims]</code>"</p>
<p>I’m not sure why my targets/labels dimensions are only the batch size which is [2].<br>
Thank you so much</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Faisal_T" data-post="12" data-topic="42409">
<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/faisal_t/48/56500_2.png" class="avatar"> Faisal_T:</div>
<blockquote>
<p>I’m not sure why my targets/labels dimensions are only the batch size which is [2].</p>
</blockquote>
</aside>
<p>This would indicate your targets are representing a single class for each sample, so a multi-class classification while your model outputs indicate an additional temporal dimension.<br>
Could you describe your use case a bit more, as the target shape seems to be unexpected?</p>

username: Faisal_T
cooked: <p>I’ve created a post that contains the code of the model definition and the training loop: <a href="https://discuss.pytorch.org/t/runtimeerror-expected-target-size-2-30000-got-2/170465" class="inline-onebox">RuntimeError: Expected target size [2, 30000], got [2]</a></p>
<p>My task is to classify Arabic poetry texts based on category. I have 26 different categories. each poetry text sample has one category.</p>

username: Faisal_T
cooked: <p>Hello ptrblck, do you need me to provide any more info? Thank you so much</p>

username: ptrblck
cooked: <p>No, as I think you’ve already posted the important issues in the cross-post. Let me highlight them there.</p>