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

Hi all, I’m modifying the tutorial at http://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html to use a model I’m defining myself with nn.Sequential like so:

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)
)

and in the training function:

    # 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)

But “outputs=model(inputs)” returns a tuple. How can I get the right output from the model?

The Traceback is:

Traceback (most recent call last):
  File "smallEDNetwork.py", line 198, in <module>
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'

Could you post the trace?

Updated with the trace

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

1 Like

Ahh that makes sense, but would have been tough for me to catch! Thanks a lot.

Yeah it’s a tricky bug. Usually this happens when something returned a tuple in Sequential.

How to solve this :

‘AdaptiveAvgPool2d’ object has no attribute ‘dim’

1 Like