# Sizes of tensors must match except in dimension 1

**URL:** <https://discuss.pytorch.org/t/sizes-of-tensors-must-match-except-in-dimension-1/147785>\
**Category:** nlp\
**Created:** [March 29, 2022, 8:55pm UTC](https://discuss.pytorch.org/t/sizes-of-tensors-must-match-except-in-dimension-1/147785 "2022-03-29T20:55:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gorgen](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/f14d63/32.png) [@Gorgen](https://discuss.pytorch.org/u/Gorgen)\
**Post date:** [March 29, 2022, 8:55pm UTC](https://discuss.pytorch.org/t/sizes-of-tensors-must-match-except-in-dimension-1/147785/1 "2022-03-29T20:55:09Z")

</div>

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.  
Could you please help me to fix this issue?  
Thanks, best wishes

Traceback (most recent call last):  
File “train\_meld.py”, line 238, in   
test\_loss, test\_acc, test\_label, test\_pred, test\_mask, test\_fscore, attentions = train\_or\_eval\_model(model, loss\_function, test\_loader, e)  
File “train\_meld.py”, line 83, in train\_or\_eval\_model  
log\_prob, \_, alpha, alpha\_f, alpha\_b, \_ = model(r1, r2, r3, r4, audio\_feature, x5, x6, x1, o2,  
File “F:\Anaconda3\lib\site-packages\torch\nn\modules\module.py”, line 1110, in \_call\_impl  
return forward\_call(\*input, \*\*kwargs)  
File “F:\Projects\declare\Cosmic\_mfcc\_rnn\CommonsenseGRUModel.py”, line 94, in forward  
mixed\_feature = torch.cat((r, audio\_feature), dim=-1) # [seq\_len, batch\_size, dim\_mixed\_feature]  
RuntimeError: Sizes of tensors must match except in dimension 2. Expected size 7 but got size 5 for tensor number 1 in the list.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [March 29, 2022, 11:00pm UTC](https://discuss.pytorch.org/t/sizes-of-tensors-must-match-except-in-dimension-1/147785/2 "2022-03-29T23:00:43Z")

</div>

The shape mismatch is raised in:

```python
mixed_feature = torch.cat((r, audio_feature), dim=-1) 

```

which seems to try to concatenate two 3-dimensional tensors in `dim2`.  
Based on this all dimensions should have the same size besides `dim2` which is not the case as seen in this small examples:

```python
r = torch.randn(2, 7, 7)
audio_feature = torch.randn(2, 5, 1)
mixed_feature = torch.cat((r, audio_feature), dim=-1) 
> RuntimeError: Sizes of tensors must match except in dimension 2. Expected size 7 but got size 5 for tensor number 1 in the list.

```

As you can see, the size in `dim1` is different and set to `7` and `5` in these tensors.

---

<div class="post-metadata">

**Author:** ![Gorgen](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/f14d63/32.png) [@Gorgen](https://discuss.pytorch.org/u/Gorgen)\
**Post date:** [March 30, 2022, 8:38am UTC](https://discuss.pytorch.org/t/sizes-of-tensors-must-match-except-in-dimension-1/147785/3 "2022-03-30T08:38:55Z")

</div>

Dear @ptrblck : thank you for your suggestions. To be honest. I follow your other answer about the similar issues. [RuntimeError: Sizes of tensors must match except in dimension 1](https://discuss.pytorch.org/t/runtimeerror-sizes-of-tensors-must-match-except-in-dimension-1/140651).

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?

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.

Thanks. best wishes

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [March 30, 2022, 9:37am UTC](https://discuss.pytorch.org/t/sizes-of-tensors-must-match-except-in-dimension-1/147785/4 "2022-03-30T09:37:48Z")

</div>

I would suggest to add a debug print statement right before the failing `torch.cat` operation as:

```python
print(r.shape)
print(audio_features.shape)
mixed_features = torch.cat((r, audio_features), dim=-1)

```

and check if the shape might change in a specific iteration.

---

<div class="post-metadata">

**Author:** ![Gorgen](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/f14d63/32.png) [@Gorgen](https://discuss.pytorch.org/u/Gorgen)\
**Post date:** [March 30, 2022, 10:17am UTC](https://discuss.pytorch.org/t/sizes-of-tensors-must-match-except-in-dimension-1/147785/5 "2022-03-30T10:17:55Z")

</div>

I appreciate your detailed advice. Thanks. Best wishes

---

<div class="post-metadata">

**Author:** ![Hassan\_Ghavidel](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/hassan_ghavidel/32/71175_2.png) [@Hassan\_Ghavidel](https://discuss.pytorch.org/u/Hassan_Ghavidel)\
**Post date:** [August 3, 2024, 10:15pm UTC](https://discuss.pytorch.org/t/sizes-of-tensors-must-match-except-in-dimension-1/147785/6 "2024-08-03T22:15:35Z")

</div>

Hi @ptrblck  
I HAVE the same issue. how can we fix it? i receive the error  
RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 48 but got size 47 for tensor number 1 in the list.

here:  
def forward(self, x, skip=None):  
x = self.up(x)  
if skip is not None:  
print(f"Shape of x : {x.shape}“)  
print(f"Shape of skip: {skip.shape}”)  
x = torch.cat([x, skip], dim=1)  
x = self.conv1(x)  
x = self.conv2(x)  
return x  
the shape of x and skip are different  
Shape of x : torch.Size([2, 768, 24, 24])  
Shape of skip: torch.Size([2, 384, 24, 24])  
Shape of x : torch.Size([2, 384, 48, 48])  
Shape of skip: torch.Size([2, 192, 47, 47])

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [August 3, 2024, 10:26pm UTC](https://discuss.pytorch.org/t/sizes-of-tensors-must-match-except-in-dimension-1/147785/7 "2024-08-03T22:26:09Z")

</div>

The shape mismatch error is raised in the `torch.cat` operation since `x` and `skip` have different spatial sizes. Pad one of these tensors or slice the other one to create matching spatial sizes.
