RuntimeError: Sizes of tensors must match except in dimension 1

username: Samah_Abu_saleem
cooked: <p>I am trying to train GCN model on my custom dataset and I have resized all the values but I am getting error:</p>
<blockquote>
<p>RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 7 but got size 14515200 for tensor number 1 in the list.</p>
</blockquote>
<p>This is the GCN code:</p>
<blockquote>
<p>class Net(torch.nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Net, self).<strong>init</strong>()<br>
self.conv1 = GCNConv(3, 64)<br>
self.conv2 = GCNConv(64, 64)<br>
self.conv3 = GCNConv(64, 64)<br>
self.conv4 = GCNConv(48, 64)<br>
self.conv5 = GCNConv(64, 96)<br>
self.conv6 = GCNConv(96, 128)<br>
self.linear1 = torch.nn.Linear(64,2)<br>
self.linear2 = torch.nn.Linear(64,8)</p>
<pre><code>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
</code></pre>
<p>def main():<br>
data_list = load_data_graph(data_size=data_size)<br>
device = torch.device(‘cuda’)<br>
model = Net().to(device)</p>
<pre><code>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="  ")
</code></pre>
</blockquote>
<p>The error:</p>
<blockquote>
<p>RuntimeError                              Traceback (most recent call last)<br>
/tmp/ipykernel_58/874853799.py in <br>
126<br>
127 if <strong>name</strong>==“<strong>main</strong>”:<br>
 → 128     main()</p>
<p>/tmp/ipykernel_58/874853799.py in main()<br>
76             batch = batch.to(“cuda”)<br>
77             optimizer.zero_grad()<br>
—&gt; 78             outputs = model(batch)<br>
79             loss = criterion(outputs,batch.tensor)<br>
80             loss.backward()</p>
<p>/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1101                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1102             return forward_call(*input, **kwargs)<br>
1103         # Do not call functions when jit is used<br>
1104         full_backward_hooks, non_full_backward_hooks = <span class="chcklst-box fa fa-square-o fa-fw"></span>, <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>/tmp/ipykernel_58/874853799.py in forward(self, data)<br>
28         print(data)<br>
29         pixel_values = data.pixel_values.view(-1, 3)<br>
—&gt; 30         x = torch.cat([data.pos, pixel_values], dim=-1)<br>
31         x, edge_index = x, data.edge_index<br>
32         x = self.conv1(x, edge_index)</p>
<p>RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 7 but got size 14515200 for tensor number 1 in the list.</p>
</blockquote>

username: ptrblck
cooked: <p><code>torch.cat</code> 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 <code>data.pos</code> and <code>pixel_values</code>. 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: <code>pixel_values = data.pixel_values.view(-1, 3)</code> as you are reshaping the <code>pixel_values</code> tensor right before this op so check what shape is expected.</p>

username: Samah_Abu_saleem
cooked: <p>Sir, the data looks like this:</p>
<blockquote>
<p>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])</p>
</blockquote>
<p>I have resized all the pixel values and they have the same dimension:</p>
<blockquote>
<p>(1080,1920,3)</p>
</blockquote>

username: ptrblck
cooked: <p>I don’t believe the used tensors have the same shape, as <code>pos</code>’ shape is given as <code>[7, 2]</code> and <code>pixel_values</code>’ as <code>[7, 1080, 1920, 3]</code>. If you reshape the latter one you’ll get the previously posted error message:</p>
<pre><code class="lang-python">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)
# &gt;RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 7 but got size 14515200 for tensor number 1 in the list.
</code></pre>
<p>As explained before, you can’t concatenate two tensors with different sizes in more than a single dimension. In your case it’s <code>[7, 2]</code> and <code>[14515200, 3]</code>.</p>

username: Samah_Abu_saleem
cooked: <p>ok Sir, I want to define the node features as a combination of <code>pos</code> and <code>pixel_values</code>. How I can do it? Any suggestion please,</p>

username: ptrblck
cooked: <p>You can’t concatenate both tensors in their current shape together so would need to flatten them in another way.<br>
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:</p>
<pre><code class="lang-python">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)
</code></pre>
<p>However, while this works technically, you would have to check if this concatenation and reshaping makes sense in your use case.</p>

username: Samah_Abu_saleem
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="6" data-topic="140651">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<pre><code class="lang-auto">pixel_values = pixel_values.view(pixel_values.size(0), -1)
x = torch.cat([pos, pixel_values], dim=1)
</code></pre>
</blockquote>
</aside>
<blockquote>
<hr>
<p>RuntimeError                              Traceback (most recent call last)<br>
/tmp/ipykernel_58/426753045.py in <br>
126<br>
127 if <strong>name</strong>==“<strong>main</strong>”:<br>
 → 128     main()</p>
<p>/tmp/ipykernel_58/426753045.py in main()<br>
76             batch = batch.to(“cuda”)<br>
77             optimizer.zero_grad()<br>
—&gt; 78             outputs = model(batch)<br>
79             loss = criterion(outputs,batch.tensor)<br>
80             loss.backward()</p>
<p>/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1101                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1102             return forward_call(*input, **kwargs)<br>
1103         # Do not call functions when jit is used<br>
1104         full_backward_hooks, non_full_backward_hooks = <span class="chcklst-box fa fa-square-o fa-fw"></span>, <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>/tmp/ipykernel_58/426753045.py in forward(self, data)<br>
30         x = torch.cat([data.pos, pixel_values], dim=1)<br>
31         x, edge_index = x, data.edge_index<br>
—&gt; 32         x = self.conv1(x, edge_index)<br>
33         x = F.relu(x)<br>
34         x = self.conv2(x, edge_index)</p>
<p>/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1101                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1102             return forward_call(*input, **kwargs)<br>
1103         # Do not call functions when jit is used<br>
1104         full_backward_hooks, non_full_backward_hooks = <span class="chcklst-box fa fa-square-o fa-fw"></span>, <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>/usr/local/lib/python3.8/dist-packages/torch_geometric/nn/conv/gcn_conv.py in forward(self, x, edge_index, edge_weight)<br>
180                     edge_index = cache<br>
181<br>
 → 182         x = self.lin(x)<br>
183<br>
184         # propagate_type: (x: Tensor, edge_weight: OptTensor)</p>
<p>/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
1100         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1101                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1102             return forward_call(*input, **kwargs)<br>
1103         # Do not call functions when jit is used<br>
1104         full_backward_hooks, non_full_backward_hooks = <span class="chcklst-box fa fa-square-o fa-fw"></span>, <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>/usr/local/lib/python3.8/dist-packages/torch_geometric/nn/dense/linear.py in forward(self, x)<br>
107     def forward(self, x: Tensor) → Tensor:<br>
108         “”“”“”<br>
 → 109         return F.linear(x, self.weight, self.bias)<br>
110<br>
111     <span class="mention">@torch.no_grad</span>()</p>
<p>/usr/local/lib/python3.8/dist-packages/torch/nn/functional.py in linear(input, weight, bias)<br>
1846     if has_torch_function_variadic(input, weight, bias):<br>
1847         return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)<br>
 → 1848     return torch._C._nn.linear(input, weight, bias)<br>
1849<br>
1850</p>
<p>RuntimeError: mat1 and mat2 shapes cannot be multiplied (8x6220802 and 3x64)</p>
</blockquote>

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

username: Samah_Abu_saleem
cooked: <p>ok, thank you Sir. I got it.<br>
but I got another error:</p>
<blockquote>
<p>RuntimeError                              Traceback (most recent call last)<br>
/tmp/ipykernel_59/2520602840.py in <br>
128<br>
129 if <strong>name</strong>==“<strong>main</strong>”:<br>
 → 130     main()</p>
<p>/tmp/ipykernel_59/2520602840.py in main()<br>
78             batch = batch.to(“cuda”)<br>
79             optimizer.zero_grad()<br>
—&gt; 80             outputs = model(batch)<br>
81             loss = criterion(outputs,batch.tensor)<br>
82             loss.backward()</p>
</blockquote>
<blockquote>
<p>RuntimeError: “addmm_cuda” not implemented for ‘Byte’</p>
</blockquote>

username: ptrblck
cooked: <p>It seems you are passing <code>ByteTensor</code>s to the model while floating point tensor are expected. Call <code>input = input.float()</code> before passing the tensor to the model and it should work.</p>

username: MERYEM
cooked: <p>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?</p>

username: ptrblck
cooked: <p>Could you post a minimal and executable code snippet to reproduce the issue, as it’s unclear which operation fails.<br>
Generally, these errors are raised of you are trying to use tensors with an unsupported shape mismatch as seen here:</p>
<pre><code class="lang-python">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.
</code></pre>

username: MERYEM
cooked: <p>Here’s the code snippet where the error occurs:<br>
import torch</p>
<h1><a name="assuming-model_features1-and-model_features2-are-my-tensors-1" class="anchor" href="#assuming-model_features1-and-model_features2-are-my-tensors-1"></a>Assuming model_features1 and model_features2 are my tensors</h1>
<h1><a name="model_features1-has-a-size-of-batch_size-2000-2" class="anchor" href="#model_features1-has-a-size-of-batch_size-2000-2"></a>model_features1 has a size of (batch_size, 2000)</h1>
<h1><a name="model_features2-has-a-size-of-batch_size-400-3" class="anchor" href="#model_features2-has-a-size-of-batch_size-400-3"></a>model_features2 has a size of (batch_size, 400)</h1>
<p>concatenated_features_train = torch.cat((model_features1, model_features2), dim=1)</p>
<p>I need the concatenation in dim1 because I want to concatenate features from the fully connected (FC) layers after the training phase</p>

username: ptrblck
cooked: <p>This code snippet won’t fail:</p>
<pre><code class="lang-python">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])
</code></pre>
<p>since the same <code>batch_size</code> is used in <code>dim0</code>.<br>
Are you using <code>torch.cat((...), dim=0)</code> by mistake?</p>