ValueError: Expected target size different

username: Bill_Ye
cooked: <p>Hi , everyone. I try so hard to solve my issue but it seems that I am wasting 4 hours and can not get a result. I try to train a RNN model using GRU to classify three different language in wav file. I still can’t find the reason for the error.</p>
<pre><code class="lang-auto">loss_func = nn.NLLLoss()
learning_rate = 0.01
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

num_epochs = 25
model.train()

for epoch in range(num_epochs):
    train_loss = 0
    
    for x, y in train_dataloader:
        x = x.to(device)
        y = y.to(device)
        
        yhat = model(x)
        print(yhat.shape)
        
        loss = loss_func(yhat,y)
        
        model.zero_grad()
        loss.backward()
        optimizer.step()
        
        train_loss += loss
    
    if not (epoch % 1):
        print(f'Epoch: {epoch+1:02d}, ' +
              f'Loss: {train_loss / len(train_dataloader.dataset):.4f}')

print('Finished Training')
</code></pre>
<p>The shape of the output yhat is (60,1000,3) and using the softmax activation. The shape of the target y is (60,1000,1).</p>
<p>It shows that the error is <code>ValueError: Expected target size (60, 3), got torch.Size([60, 1000, 1])</code></p>
<p>Is there something wrong with my labels?</p>

username: ptrblck
cooked: <p>I’m not familiar with your use case and thus don’t know what the dimensions mean in your output.<br>
However, <code>nn.NLLLoss</code> (and <code>nn.CrossEntropyLoss</code>) expect a model output in the shape <code>[batch_size, nb_classes, *]</code> and a target in <code>[batch_size, *]</code> containing class indices in the range <code>[0, nb_classes-1]</code>.<br>
Note that the <code>*</code> stands for additional dimensions.</p>
<p>In your case it seems that you are using <code>[batch_size=60, nb_classes=1000, seq_len=3]</code> in your output.<br>
If that’s the case, the target should have the shape <code>[60, 3]</code> and contain values in <code>[0, 999]</code>.</p>
<aside class="quote no-group" data-username="Bill_Ye" data-post="1" data-topic="102840">
<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/bill_ye/48/30599_2.png" class="avatar"> Bill_Ye:</div>
<blockquote>
<p>and using the softmax activation</p>
</blockquote>
</aside>
<p><code>nn.NLLLoss</code> expects log probabilities, so use <code>F.log_softmax</code> instead.</p>

username: Bill_Ye
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="102840">
<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>However, <code>nn.NLLLoss</code> (and <code>nn.CrossEntropyLoss</code>) expect a model output in the shape <code>[batch_size, nb_classes, *]</code> and a target in <code>[batch_size, *]</code> containing class indices in the range <code>[0, nb_classes-1]</code>.</p>
</blockquote>
</aside>
<p>Thank you for the response, it helps a lot.</p>