Sizes of tensors must match except in dimension 1

Gorgen: <p>Dear everyone: when i train the model. it reported  Expected size 7 but got size 5 for tensor number 1 in the list. I don’t know what expected object is needed to be 7.<br>
Could you please help me to fix this issue?<br>
Thanks, best wishes</p>
<p>Traceback (most recent call last):<br>
File “train_meld.py”, line 238, in <br>
test_loss, test_acc, test_label, test_pred, test_mask, test_fscore, attentions = train_or_eval_model(model, loss_function, test_loader, e)<br>
File “train_meld.py”, line 83, in train_or_eval_model<br>
log_prob, _, alpha, alpha_f, alpha_b, _ = model(r1, r2, r3, r4, audio_feature, x5, x6, x1, o2,<br>
File “F:\Anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 1110, in _call_impl<br>
return forward_call(*input, **kwargs)<br>
File “F:\Projects\declare\Cosmic_mfcc_rnn\CommonsenseGRUModel.py”, line 94, in forward<br>
mixed_feature = torch.cat((r, audio_feature), dim=-1)  # [seq_len, batch_size, dim_mixed_feature]<br>
RuntimeError: Sizes of tensors must match except in dimension 2. Expected size 7 but got size 5 for tensor number 1 in the list.</p>

ptrblck: <p>The shape mismatch is raised in:</p>
<pre><code class="lang-python">mixed_feature = torch.cat((r, audio_feature), dim=-1) 
</code></pre>
<p>which seems to try to concatenate two 3-dimensional tensors in <code>dim2</code>.<br>
Based on this all dimensions should have the same size besides <code>dim2</code> which is not the case as seen in this small examples:</p>
<pre><code class="lang-python">r = torch.randn(2, 7, 7)
audio_feature = torch.randn(2, 5, 1)
mixed_feature = torch.cat((r, audio_feature), dim=-1) 
&gt; RuntimeError: Sizes of tensors must match except in dimension 2. Expected size 7 but got size 5 for tensor number 1 in the list.
</code></pre>
<p>As you can see, the size in <code>dim1</code> is different and set to <code>7</code> and <code>5</code> in these tensors.</p>

Gorgen: <p>Dear <a class="mention" href="/u/ptrblck">@ptrblck</a> : thank you for your suggestions. To be honest. I follow your other answer about the similar issues. <a href="https://discuss.pytorch.org/t/runtimeerror-sizes-of-tensors-must-match-except-in-dimension-1/140651" class="inline-onebox">RuntimeError: Sizes of tensors must match except in dimension 1</a>.</p>
<p>I know the issue. However,none of the samples on r have that dimension is 7. Since any sample without r does not have a dimension of 7, why is there a prompt for a dimension of 7?</p>
<p>I’m sorry, I’m constantly trying to debug to find the problem. Can you give me some advice, where should I place breakpoints to effectively identify the problem. Thank you for answering this really stupid sounding question.</p>
<p>Thanks. best wishes</p>

ptrblck: <p>I would suggest to add a debug print statement right before the failing <code>torch.cat</code> operation as:</p>
<pre><code class="lang-python">print(r.shape)
print(audio_features.shape)
mixed_features = torch.cat((r, audio_features), dim=-1)
</code></pre>
<p>and check if the shape might change in a specific iteration.</p>

Gorgen: <p>I appreciate your detailed advice. Thanks. Best wishes</p>

Hassan_Ghavidel: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a><br>
I HAVE the same issue. how can we fix it? i receive the error<br>
RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 48 but got size 47 for tensor number 1 in the list.</p>
<p>here:<br>
def forward(self, x, skip=None):<br>
x = self.up(x)<br>
if skip is not None:<br>
print(f"Shape of x : {x.shape}“)<br>
print(f"Shape of skip: {skip.shape}”)<br>
x = torch.cat([x, skip], dim=1)<br>
x = self.conv1(x)<br>
x = self.conv2(x)<br>
return x<br>
the shape of x and skip are different<br>
Shape of x : torch.Size([2, 768, 24, 24])<br>
Shape of skip: torch.Size([2, 384, 24, 24])<br>
Shape of x : torch.Size([2, 384, 48, 48])<br>
Shape of skip: torch.Size([2, 192, 47, 47])</p>

ptrblck: <p>The shape mismatch error is raised in the <code>torch.cat</code> operation since <code>x</code> and <code>skip</code> have different spatial sizes. Pad one of these tensors or slice the other one to create matching spatial sizes.</p>