RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 1. Got 8 and 4 in dimension 2 at /Users/soumith/code/builder/wheel/pytorch-src/aten/src/TH/generic/THTensorMath.cpp:3616

I’m receiving this error:

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
File "/project_cpu/layers.py", line 22, in forward
    return torch.cat((input1, input2), dim=1)
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 1. Got 8 and 4 in dimension 2 at /Users/soumith/code/builder/wheel/pytorch-src/aten/src/TH/generic/THTensorMath.cpp:3616

I want to concatenate two layers if their shape match in dimension 2 by a factor. If the layer1 has larger shape there is not a problem at all, but if the layer2 is larger, then I have the error.

('layer1: ', (128, 64, 8, 8))
('layer2: ', (128, 64, 16, 16))
('factor: ', 2)

This is where I check the matching layers:

if model.layerdic[x].size()[2] == factor * model.layerdic[y].size()[2]:
                        matching.append([x, y, factor])

I have a class like below:

class Concatenate(torch.nn.Module):
    def __init__(self):
        super(Concatenate, self).__init__()
        
    def forward(self, input1, input2):
        return torch.cat((input1, input2), dim=1)

Then I create this new merge layer as following:

if old_model.layerdic[layer1_id].size()[2] < old_model.layerdic[layer2_id].size()[2]:
            pool_layer = {'type': 'pool', 'params': {'poolsize': downsampling_factor, 'pooltype': 'max'}, 'id': new_id_pool,
                          'input': [layer2_id]}
            new_model_descriptor['layers'].append(pool_layer)

            merge_layer = {'type': 'merge', 'params': {'mergetype': 'concat'}, 'id': new_id,
                           'input': [layer1_id, new_id_pool]}
            new_model_descriptor['layers'].append(merge_layer)
else:
            pool_layer = {'type': 'pool', 'params': {'poolsize': downsampling_factor, 'pooltype': 'max'}, 'id': new_id_pool,
                          'input': [layer1_id]}
            new_model_descriptor['layers'].append(pool_layer)

            merge_layer = {'type': 'merge', 'params': {'mergetype': 'concat'}, 'id': new_id,
                           'input': [new_id_pool, layer2_id]}
            new_model_descriptor['layers'].append(merge_layer)

I don’t see a mismatch and I don’t understand why I have this error. I even don’t have 4 in dimension 2…

I’m not sure how exactly your code works, but as far as I understand you are checking if there is a size mismatch and if so you use factor to downsample the larger tensor to finally concat both.

This should generally work. However just by looking at your code snippets there seems to be a mismatch between both codes in the conditions.
While you are using merge_layer = {..., 'input': [layer1_id, new_id_pool]} in the first case, the arguments are switched for the second case merge_layer = {..., 'input': [new_id_pool, layer2_id]}.

Could this be the issue? If not, could you provide a small executable code snippet so that we could try to reproduce this problem?

No, it is not the issue. I received the same error when it wasn’t switched so I wanted to check if that was the issue…

I don’t think I can provide a code snippet - it’s connected to many other methods.

Do you have an idea why I receive this error message? I mean why it is not Got 8 and 16 in dimension 2 , but 8 and 4?

I’m not sure and can just speculate without seeing the code.
Could you add a print statement before the downsampling and concatenating and after it to see the shapes of both tensors?

1 Like