RuntimeError: Sizes of tensors must match except in dimension 1

I am trying to train GCN model on my custom dataset and I have resized all the values but I am getting error:

RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 7 but got size 14515200 for tensor number 1 in the list.

This is the GCN code:

class Net(torch.nn.Module):
def init(self):
super(Net, self).init()
self.conv1 = GCNConv(3, 64)
self.conv2 = GCNConv(64, 64)
self.conv3 = GCNConv(64, 64)
self.conv4 = GCNConv(48, 64)
self.conv5 = GCNConv(64, 96)
self.conv6 = GCNConv(96, 128)
self.linear1 = torch.nn.Linear(64,2)
self.linear2 = torch.nn.Linear(64,8)

def forward(self, data):
    print(data)
    pixel_values = data.pixel_values.view(-1, 3)
    x = torch.cat([data.pos, pixel_values], dim=-1)
    x, edge_index = x, data.edge_index
    x = self.conv1(x, edge_index)
    x = F.relu(x)
    x = self.conv2(x, edge_index)
    x = F.relu(x)
    x = self.conv3(x, edge_index)
    x = F.relu(x)
    x = self.conv4(x, edge_index)
    x = F.relu(x)
    x = self.conv5(x, edge_index)
    x = F.relu(x)
    x = self.conv6(x, edge_index)
    x = F.relu(x)
    x, _ = scatter_max(x, data.batch, dim=0)
    x = self.linear1(x)
    x = F.relu(x)
    x = self.linear2(x)
    return x

def main():
data_list = load_data_graph(data_size=data_size)
device = torch.device(‘cuda’)
model = Net().to(device)

trainset = data_list[:train_size]
optimizer = torch.optim.Adam(model.parameters())
train_loader = DataLoader(trainset, batch_size=batch_size, shuffle=True)
testset = data_list[train_size:]
test_loader = DataLoader(testset, batch_size=batch_size)
criterion = nn.CrossEntropyLoss()
history = {
    "train_loss": [],
    "test_loss": [],
    "test_acc": []
}

print("Start Train")

model.train()
for epoch in range(epoch_num):
    train_loss = 0.0
    for i, batch in enumerate(train_loader):
        batch = batch.to("cuda")
        optimizer.zero_grad()
        outputs = model(batch)
        loss = criterion(outputs,batch.tensor)
        loss.backward()
        optimizer.step()
        
        train_loss += loss.cpu().item()
        if i % 10 == 9:
            progress_bar = '['+('='*((i+1)//10))+(' '*((train_size//100-(i+1))//10))+']'
            print('\repoch: {:d} loss: {:.3f}  {}'
                    .format(epoch + 1, loss.cpu().item(), progress_bar), end="  ")

The error:

RuntimeError Traceback (most recent call last)
/tmp/ipykernel_58/874853799.py in
126
127 if name==“main”:
→ 128 main()

/tmp/ipykernel_58/874853799.py in main()
76 batch = batch.to(“cuda”)
77 optimizer.zero_grad()
—> 78 outputs = model(batch)
79 loss = criterion(outputs,batch.tensor)
80 loss.backward()

/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = ,

/tmp/ipykernel_58/874853799.py in forward(self, data)
28 print(data)
29 pixel_values = data.pixel_values.view(-1, 3)
—> 30 x = torch.cat([data.pos, pixel_values], dim=-1)
31 x, edge_index = x, data.edge_index
32 x = self.conv1(x, edge_index)

RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 7 but got size 14515200 for tensor number 1 in the list.

torch.cat expects all dimension to have the same shape besides the one which is used to concatenate the tensors. Based on the error message you are hitting a shape mismatch in data.pos and pixel_values. Based on the code I guess the mismatch is caused in dim0 and you are trying to concatenate the tensors in dim1. This line of code looks related: pixel_values = data.pixel_values.view(-1, 3) as you are reshaping the pixel_values tensor right before this op so check what shape is expected.

Sir, the data looks like this:

DataBatch(pos=[7, 2], pixel_values=[7, 1080, 1920, 3], detection_score=[7], edge_index=[2, 21], edge_attr=[21, 1], batch=[7], ptr=[2])

I have resized all the pixel values and they have the same dimension:

(1080,1920,3)

I don’t believe the used tensors have the same shape, as pos’ shape is given as [7, 2] and pixel_values’ as [7, 1080, 1920, 3]. If you reshape the latter one you’ll get the previously posted error message:

pos = torch.randn(7, 2)
pixel_values = torch.randn(7, 1080, 1920, 3)
pixel_values = pixel_values.view(-1, 3)
x = torch.cat([pos, pixel_values], dim=-1)
# >RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 7 but got size 14515200 for tensor number 1 in the list.

As explained before, you can’t concatenate two tensors with different sizes in more than a single dimension. In your case it’s [7, 2] and [14515200, 3].

ok Sir, I want to define the node features as a combination of pos and pixel_values. How I can do it? Any suggestion please,

You can’t concatenate both tensors in their current shape together so would need to flatten them in another way.
Assuming that dim0 is the batch size and you are dealing with 7 samples, you could flatten the tensors in their “feature dimension” and concatenate them afterwards:

pos = torch.randn(7, 2)
pixel_values = torch.randn(7, 1080, 1920, 3)
pixel_values = pixel_values.view(pixel_values.size(0), -1)
x = torch.cat([pos, pixel_values], dim=1)

However, while this works technically, you would have to check if this concatenation and reshaping makes sense in your use case.


RuntimeError Traceback (most recent call last)
/tmp/ipykernel_58/426753045.py in
126
127 if name==“main”:
→ 128 main()

/tmp/ipykernel_58/426753045.py in main()
76 batch = batch.to(“cuda”)
77 optimizer.zero_grad()
—> 78 outputs = model(batch)
79 loss = criterion(outputs,batch.tensor)
80 loss.backward()

/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = ,

/tmp/ipykernel_58/426753045.py in forward(self, data)
30 x = torch.cat([data.pos, pixel_values], dim=1)
31 x, edge_index = x, data.edge_index
—> 32 x = self.conv1(x, edge_index)
33 x = F.relu(x)
34 x = self.conv2(x, edge_index)

/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = ,

/usr/local/lib/python3.8/dist-packages/torch_geometric/nn/conv/gcn_conv.py in forward(self, x, edge_index, edge_weight)
180 edge_index = cache
181
→ 182 x = self.lin(x)
183
184 # propagate_type: (x: Tensor, edge_weight: OptTensor)

/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = ,

/usr/local/lib/python3.8/dist-packages/torch_geometric/nn/dense/linear.py in forward(self, x)
107 def forward(self, x: Tensor) → Tensor:
108 “”“”“”
→ 109 return F.linear(x, self.weight, self.bias)
110
111 @torch.no_grad()

/usr/local/lib/python3.8/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
1846 if has_torch_function_variadic(input, weight, bias):
1847 return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)
→ 1848 return torch._C._nn.linear(input, weight, bias)
1849
1850

RuntimeError: mat1 and mat2 shapes cannot be multiplied (8x6220802 and 3x64)

My code snippet creates a tensor in the shape of [batch_size, 6220802], which now doesn’t match the expected input features to the next linear layer, so you should either change the in_features to the new value (which would be quite large) or change the logic of how the tensors should be concatenated.

ok, thank you Sir. I got it.
but I got another error:

RuntimeError Traceback (most recent call last)
/tmp/ipykernel_59/2520602840.py in
128
129 if name==“main”:
→ 130 main()

/tmp/ipykernel_59/2520602840.py in main()
78 batch = batch.to(“cuda”)
79 optimizer.zero_grad()
—> 80 outputs = model(batch)
81 loss = criterion(outputs,batch.tensor)
82 loss.backward()

RuntimeError: “addmm_cuda” not implemented for ‘Byte’

It seems you are passing ByteTensors to the model while floating point tensor are expected. Call input = input.float() before passing the tensor to the model and it should work.

Hello, I encountered a PyTorch RuntimeError: Sizes of tensors must match except in dimension 1 - Expected size of 2000 but received size of 400 for tensor number 1 . Could you please assist me in resolving this issue without compromising the integrity of the data?

Could you post a minimal and executable code snippet to reproduce the issue, as it’s unclear which operation fails.
Generally, these errors are raised of you are trying to use tensors with an unsupported shape mismatch as seen here:

a = torch.randn(2, 2000)
b = torch.randn(3, 400)

c = torch.cat((a, b), dim=0)
# RuntimeError: Sizes of tensors must match except in dimension 0. Expected size 2000 but got size 400 for tensor number 1 in the list.

Here’s the code snippet where the error occurs:
import torch

Assuming model_features1 and model_features2 are my tensors

model_features1 has a size of (batch_size, 2000)

model_features2 has a size of (batch_size, 400)

concatenated_features_train = torch.cat((model_features1, model_features2), dim=1)

I need the concatenation in dim1 because I want to concatenate features from the fully connected (FC) layers after the training phase

This code snippet won’t fail:

batch_size = 10

model_features1 = torch.randn(batch_size, 2000)
model_features2 = torch.randn(batch_size, 400)

concatenated_features_train = torch.cat((model_features1, model_features2), dim=1)
print(concatenated_features_train.shape)
# torch.Size([10, 2400])

since the same batch_size is used in dim0.
Are you using torch.cat((...), dim=0) by mistake?