AttributeError: 'tuple' object has no attribute 'dim'

Nicholas_Wickman: <p>Hi all, I’m modifying the tutorial at <a href="http://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html" rel="nofollow noopener">http://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html</a> to use a model I’m defining myself with nn.Sequential like so:</p>
<pre><code>model = nn.Sequential(
## Encoder Network
nn.Conv2d(3, 64, kernel_size=4),
nn.ReLU(True),
nn.Conv2d(64, 128, kernel_size=4),
nn.ReLU(True),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0),
.
.
.
nn.ConvTranspose2d(1, 1, 5),
nn.ReLU(True),
nn.Upsample(scale_factor=2, mode='bilinear'),
#
nn.AdaptiveMaxPool2d(1, 5),
nn.Linear(1, 1, 5)
)
</code></pre>
<p>and in the training function:</p>
<pre><code>    # Iterate over data.
        for data in dataloaders[phase]:
            # get the inputs
            inputs, labels = data

            # wrap them in Variable
            if use_gpu:
                inputs = Variable(inputs.cuda())
                labels = Variable(labels.cuda())
            else:
                inputs, labels = Variable(inputs), Variable(labels)

            # zero the parameter gradients
            optimizer.zero_grad()

            # forward
            print(inputs)
            outputs = model(inputs)
            _, preds = torch.max(outputs.data, 1)
            loss = criterion(outputs, labels)
</code></pre>
<p>But “outputs=model(inputs)” returns a tuple. How can I get the right output from the model?</p>
<p>The Traceback is:</p>
<pre><code>Traceback (most recent call last):
  File "smallEDNetwork.py", line 198, in &lt;module&gt;
num_epochs=25)
  File "smallEDNetwork.py", line 123, in train_model
outputs = model(inputs)
  File "/home/nic/.conda/envs/my_root/lib/python3.6/site-packages/torch/nn/modules/module.py", line 325, in __call__
result = self.forward(*input, **kwargs)
  File "/home/nic/.conda/envs/my_root/lib/python3.6/site-packages/torch/nn/modules/container.py", line 67, in forward
input = module(input)
  File "/home/nic/.conda/envs/my_root/lib/python3.6/site-packages/torch/nn/modules/module.py", line 325, in __call__
result = self.forward(*input, **kwargs)
  File "/home/nic/.conda/envs/my_root/lib/python3.6/site-packages/torch/nn/modules/linear.py", line 55, in forward
return F.linear(input, self.weight, self.bias)
  File "/home/nic/.conda/envs/my_root/lib/python3.6/site-packages/torch/nn/functional.py", line 833, in linear
if input.dim() == 2 and bias is not None:
AttributeError: 'tuple' object has no attribute 'dim'</code></pre>

SimonW: <p>Could you post the trace?</p>

Nicholas_Wickman: <p>Updated with the trace</p>

SimonW: <p>You used <code>AdaptiveMaxPool2d</code> wrong, it should be <code>nn.AdaptiveMaxPool2d((1, 5))</code>. By setting the 2nd arg as 5, you are setting <code>return_indices</code> to a <code>True</code> value. <a href="http://pytorch.org/docs/master/nn.html#torch.nn.AdaptiveMaxPool2d">http://pytorch.org/docs/master/nn.html#torch.nn.AdaptiveMaxPool2d</a></p>

Nicholas_Wickman: <p>Ahh that makes sense, but would have been tough for me to catch! Thanks a lot.</p>

SimonW: <p>Yeah it’s a tricky bug. Usually this happens when something returned a tuple in <code>Sequential</code>.</p>

Jayan-K-Duggal: <p>How to solve this :</p>
<p>‘AdaptiveAvgPool2d’ object has no attribute ‘dim’</p>