'tuple' object has no attribute 'dim'

fyy: <p>I want to use the inception_v3 framework that comes with torchversion ,but  it nake a mistake.<br>
model.fc = nn.Sequential(nn.Linear(2048,512),<br>
nn.ReLU(),<br>
nn.Dropout(0.2),<br>
nn.Linear(512,5),<br>
nn.LogSoftmax(dim=1))</p>
<p>for epoch in range(epochs):<br>
for inputs,labels in train_loader:<br>
inputs,labels = inputs.to(device),labels.to(device)<br>
optimizer.zero_grad()<br>
out = model(inputs)<br>
loss = criterion(out,labels)<br>
loss.backward()<br>
optimizer.step()<br>
running_loss +=loss.item()<br>
steps +=1</p>
<pre><code>    if (steps+1)%5 == 0:
        test_loss = 0
        accuracy = 0
        model.eval()
        with torch.no_grad():
            for inputs,labels in test_loader:
                inputs, labels = inputs.to(device), labels.to(device)
                out2 = model(inputs)
                batch_loss = criterion(out2,labels)
                test_loss +=batch_loss.item()

                ps = torch.exp(out2)
                top_pred,top_class = ps.topk(1,dim=1)
                equals = top_class == labels.view(*top_class.shape)
                accuracy += torch.mean(equals.type(torch.FloatTensor)).item()

        train_losses.append(running_loss/len(train_loader))
        test_losses.append(test_loss/len(test_loader))

        print(f"Epoch {epoch+1}/{epochs}"
              f"Train loss: {running_loss/5:.3f}",
              f"Test loss: {test_loss/len(test_loader):.3f} "
              f"Test accuracy: {accuracy/len(test_loader):.3f}")
        running_loss = 0
        model.train()
</code></pre>
<p>Traceback (most recent call last):<br>
File “F:/csdncollection/train.py”, line 81, in <br>
loss = criterion(out,labels)<br>
File “D:\Anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 493, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “D:\Anaconda3\lib\site-packages\torch\nn\modules\loss.py”, line 209, in forward<br>
return F.nll_loss(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction)<br>
File “D:\Anaconda3\lib\site-packages\torch\nn\functional.py”, line 1863, in nll_loss<br>
dim = input.dim()<br>
AttributeError: ‘tuple’ object has no attribute ‘dim’</p>
<p>I hope someone could help me .</p>

ptrblck: <p>By default the inception model returns two outputs, the output of the last linear layer and the <code>aux_logits</code>.<br>
If you don’t want to use the <code>aux_logits</code> for your training, just index <code>out</code> at 0:</p>
<pre><code class="lang-python">loss = criterion(out[0], labels)
</code></pre>
<p>If you are training from scratch, you might just disable it the <code>aux_logits</code>: <code>model = models.inception_v3(aux_logits=False)</code>.</p>

fyy: <p>Thank you, I just read your answer to help others on this question. He has run successfully. You are really a helpful person, thank you.</p>

fyy: <p>hello，when I use<br>
<code>model = models.inception_v3(aux_logits=False)</code><br>
It has error.<br>
Traceback (most recent call last):<br>
File “F:/csdncollection/train.py”, line 49, in <br>
model = models.inception_v3(pretrained=True,aux_logits=False)<br>
File “D:\Anaconda3\lib\site-packages\torchvision\models\inception.py”, line 31, in inception_v3<br>
model.load_state_dict(model_zoo.load_url(model_urls[‘inception_v3_google’]))<br>
File “D:\Anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 777, in load_state_dict<br>
self.<strong>class</strong>.<strong>name</strong>, “\n\t”.join(error_msgs)))<br>
RuntimeError: Error(s) in loading state_dict for Inception3:<br>
Unexpected key(s) in state_dict: “AuxLogits.conv0.conv.weight”, “AuxLogits.conv0.bn.weight”, “AuxLogits.conv0.bn.bias”, “AuxLogits.conv0.bn.running_mean”, “AuxLogits.conv0.bn.running_var”, “AuxLogits.conv1.conv.weight”, “AuxLogits.conv1.bn.weight”, “AuxLogits.conv1.bn.bias”, “AuxLogits.conv1.bn.running_mean”, “AuxLogits.conv1.bn.running_var”, “AuxLogits.fc.weight”, “AuxLogits.fc.bias”.</p>
<p>when I use<br>
model = models.inception_v3(pretrained=True,aux_logits=True)<br>
this model can run,but it has another error.<br>
Epoch 1/200Train loss: 1.629 Test loss: 1.306 Test accuracy: 0.620<br>
Epoch 1/200Train loss: 1.686 Test loss: 1.883 Test accuracy: 0.377<br>
Epoch 1/200Train loss: 1.320 Test loss: 1.058 Test accuracy: 0.611</p>

ptrblck: <p>If you load the pretrained parameters, you need to specify <code>aux_logits=True</code> (or just leave the default).<br>
In that case, slice the output as suggested in the other post.<br>
Which kind of error are you getting then?</p>

fyy: <p>Hello, it seems that I am mistaken, he can run correctly, thank you.</p>