'tuple' object has no attribute 'dim'

I want to use the inception_v3 framework that comes with torchversion ,but it nake a mistake.
model.fc = nn.Sequential(nn.Linear(2048,512),

for epoch in range(epochs):
for inputs,labels in train_loader:
inputs,labels = inputs.to(device),labels.to(device)
out = model(inputs)
loss = criterion(out,labels)
running_loss +=loss.item()
steps +=1

    if (steps+1)%5 == 0:
        test_loss = 0
        accuracy = 0
        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()


        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

Traceback (most recent call last):
File “F:/csdncollection/train.py”, line 81, in
loss = criterion(out,labels)
File “D:\Anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 493, in call
result = self.forward(*input, **kwargs)
File “D:\Anaconda3\lib\site-packages\torch\nn\modules\loss.py”, line 209, in forward
return F.nll_loss(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction)
File “D:\Anaconda3\lib\site-packages\torch\nn\functional.py”, line 1863, in nll_loss
dim = input.dim()
AttributeError: ‘tuple’ object has no attribute ‘dim’

I hope someone could help me .

By default the inception model returns two outputs, the output of the last linear layer and the aux_logits.
If you don’t want to use the aux_logits for your training, just index out at 0:

loss = criterion(out[0], labels)

If you are training from scratch, you might just disable it the aux_logits: model = models.inception_v3(aux_logits=False).

1 Like

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.

1 Like

hello,when I use
model = models.inception_v3(aux_logits=False)
It has error.
Traceback (most recent call last):
File “F:/csdncollection/train.py”, line 49, in
model = models.inception_v3(pretrained=True,aux_logits=False)
File “D:\Anaconda3\lib\site-packages\torchvision\models\inception.py”, line 31, in inception_v3
File “D:\Anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 777, in load_state_dict
self.class.name, “\n\t”.join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for Inception3:
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”.

when I use
model = models.inception_v3(pretrained=True,aux_logits=True)
this model can run,but it has another error.
Epoch 1/200Train loss: 1.629 Test loss: 1.306 Test accuracy: 0.620
Epoch 1/200Train loss: 1.686 Test loss: 1.883 Test accuracy: 0.377
Epoch 1/200Train loss: 1.320 Test loss: 1.058 Test accuracy: 0.611

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

Hello, it seems that I am mistaken, he can run correctly, thank you.