# Stack expects each equal size

**URL:** <https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012>\
**Category:** Uncategorized\
**Created:** [February 1, 2022, 1:37pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012 "2022-02-01T13:37:33Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim5](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/e9c0ed/32.png) [@Tim5](https://discuss.pytorch.org/u/Tim5)\
**Post date:** [February 1, 2022, 1:37pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/1 "2022-02-01T13:37:33Z")

</div>

`stack expects each tensor to be equal size but got [7, 768] at entry 0 and [8, 768] at entry 1`

```
    ca_embedding = torch.stack(tns_embedding) 
    embeddings.append(ca_embedding)

```

the shape for the first is `ca_embedding shape is torch.Size([7, 768])`  
the shape for the second is `ca_embedding shape is torch.Size([8, 768])`

---

<div class="post-metadata">

**Author:** ![anantguptadbl](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/anantguptadbl/32/17784_2.png) [@anantguptadbl](https://discuss.pytorch.org/u/anantguptadbl)\
**Post date:** [February 1, 2022, 2:15pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/2 "2022-02-01T14:15:27Z")

</div>

@Tim5

Recreating the problem

```auto
a=torch.rand(7, 768)
b=torch.rand(8, 768)
torch.stack([a,b])

```

```auto
RuntimeError: stack expects each tensor to be equal size, but got [7, 768] at entry 0 and [8, 768] at entry 1

```

Solution

```auto
torch.concat([a,b])

```

---

<div class="post-metadata">

**Author:** ![Tim5](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/e9c0ed/32.png) [@Tim5](https://discuss.pytorch.org/u/Tim5)\
**Post date:** [February 1, 2022, 2:23pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/3 "2022-02-01T14:23:51Z")

</div>

Thanks a lot , i will try but excuse me does concat work like stack ?

---

<div class="post-metadata">

**Author:** ![Tim5](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/e9c0ed/32.png) [@Tim5](https://discuss.pytorch.org/u/Tim5)\
**Post date:** [February 1, 2022, 2:26pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/4 "2022-02-01T14:26:33Z")

</div>

i got `AttributeError: module 'torch' has no attribute 'concat'`

---

<div class="post-metadata">

**Author:** ![anantguptadbl](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/anantguptadbl/32/17784_2.png) [@anantguptadbl](https://discuss.pytorch.org/u/anantguptadbl)\
**Post date:** [February 1, 2022, 2:35pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/5 "2022-02-01T14:35:08Z")

</div>

Looks like you might be using a slightly older version of pytorch  
You can use `torch.cat` in its stead

---

<div class="post-metadata">

**Author:** ![Tim5](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/e9c0ed/32.png) [@Tim5](https://discuss.pytorch.org/u/Tim5)\
**Post date:** [February 1, 2022, 3:49pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/6 "2022-02-01T15:49:25Z")

</div>

it works but excuse me does cat works as stack ? as the shape for both is different … i mean the shape of cat is one but for stack is two

---

<div class="post-metadata">

**Author:** ![anantguptadbl](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/anantguptadbl/32/17784_2.png) [@anantguptadbl](https://discuss.pytorch.org/u/anantguptadbl)\
**Post date:** [February 1, 2022, 3:57pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/7 "2022-02-01T15:57:07Z")

</div>

@Tim5 there ware different ways of solving a problem

```auto
a=[torch.rand(1,10), torch.rand(1,10), torch.rand(1,10)]
print(torch.cat(a, axis=0).size())
print(torch.cat(a, axis=1).size())
print(torch.stack(a).size())

```

```auto
torch.Size([3, 10])
torch.Size([1, 30])
torch.Size([3, 1, 10])

```

Generally if you use stack in a loop then it can give problems

---

<div class="post-metadata">

**Author:** ![Tim5](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/e9c0ed/32.png) [@Tim5](https://discuss.pytorch.org/u/Tim5)\
**Post date:** [February 1, 2022, 5:29pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/8 "2022-02-01T17:29:50Z")

</div>

> [@anantguptadbl](#):
>
> `ch.Size([3, 10])`

if my tensor gave me like that  
`_embedding shape is torch.Size([7, 768])`  
`_embedding shape is torch.Size([8, 768])` … and so on  
and i need to make it to sum all different number like` 7 , 8 , ....` so i need to use this line  
`torch.cat(_embedding, axis=0)` ,right ? as i got the summation only without `768`

---

<div class="post-metadata">

**Author:** ![AlphaBetaGamma96](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@AlphaBetaGamma96](https://discuss.pytorch.org/u/AlphaBetaGamma96)\
**Post date:** [February 1, 2022, 5:44pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/9 "2022-02-01T17:44:56Z")

</div>

`torch.stack` and `torch.cat` both ‘concatenate’ Tensors but the **key** different is that `torch.stack` applies this concatenation along a new dimenion, thereby _stacking_ the Tensors whereas `torch.cat` does a direct concatenation along already existing dimensions

For example,

```auto
a = torch.randn(7,768)
b = torch.randn(8,768)

torch.cat((a,b)).shape #returns torch.Size([15, 768])

```

Trying `torch.stack` results in the error,

```auto
RuntimeError: stack expects each tensor to be equal size, but got [7, 768] at entry 0 and [8, 768] at entry 1

```

because `torch.stack` takes Tensors of the **same** shape and _stacks_ them into a larger Tensor. You can think of having a `B` number of matrices of size `[N, N]` and _stacking_ them together to get a Tensor `[B,N,N]`.

I’ll repeat your original problem with two new tensors that should explain the difference between `torch.stack` and `torch.cat`.

For example,

```auto
aa = torch.randn(4,768)
bb = torch.randn(4,768)
 
torch.stack((aa,bb)).shape #returns torch.Size([2, 4, 768]) #stacked along new dimension
torch.cat((aa,bb)).shape #returns torch.Size([8, 768]) #concatenated along existing dimension

```

This example should clearly show the difference between `torch.stack` and `torch.cat`. 🙂

---

<div class="post-metadata">

**Author:** ![Tim5](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/e9c0ed/32.png) [@Tim5](https://discuss.pytorch.org/u/Tim5)\
**Post date:** [February 1, 2022, 5:58pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/10 "2022-02-01T17:58:18Z")

</div>

really thanks for your help and the great illustration of the difference but i got the summation only … why 768 didn’t appear? it returns with me `torch.Size([6144])`

---

<div class="post-metadata">

**Author:** ![AlphaBetaGamma96](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@AlphaBetaGamma96](https://discuss.pytorch.org/u/AlphaBetaGamma96)\
**Post date:** [February 1, 2022, 6:01pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/11 "2022-02-01T18:01:17Z")

</div>

Which command did you use that returned `torch.Size([6144])`? (Also, what version of pytorch are you using?)

---

<div class="post-metadata">

**Author:** ![Tim5](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/e9c0ed/32.png) [@Tim5](https://discuss.pytorch.org/u/Tim5)\
**Post date:** [February 1, 2022, 6:04pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/12 "2022-02-01T18:04:28Z")

</div>

the version is `1.8.1+cu111`  
the command is  
` _embedding = torch.cat(tokens_embedding)` as tokens\_embedding hold different values every time from another loop

---

<div class="post-metadata">

**Author:** ![AlphaBetaGamma96](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@AlphaBetaGamma96](https://discuss.pytorch.org/u/AlphaBetaGamma96)\
**Post date:** [February 1, 2022, 6:08pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/13 "2022-02-01T18:08:35Z")

</div>

what size is `tokens_embedding`?

If all your token embeddings are of size `torch.Size([768])` and say you have four of them. You’ll want to use `torch.stack` in this example,

```auto
tokens_embedding = [torch.randn(768) for _ in range(4)] #list of 4 Tensors of shape [768]

_embedding = torch.cat(tokens_embedding)
_embedding.shape #returns torch.Size([3072])
 
_embedding = torch.stack(tokens_embedding)
_embedding.shape #returns torch.Size([4, 768])

```

---

<div class="post-metadata">

**Author:** ![Tim5](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/e9c0ed/32.png) [@Tim5](https://discuss.pytorch.org/u/Tim5)\
**Post date:** [February 1, 2022, 6:12pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/14 "2022-02-01T18:12:00Z")

</div>

i already used stack before but got different shape for each time and that made a problem when i tried to implement this line after that

```auto
    embeddings.append(_embedding)

```

---

<div class="post-metadata">

**Author:** ![AlphaBetaGamma96](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@AlphaBetaGamma96](https://discuss.pytorch.org/u/AlphaBetaGamma96)\
**Post date:** [February 1, 2022, 6:19pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/15 "2022-02-01T18:19:19Z")

</div>

Can you post a minimal reproducible example? Because it’s hard to visualize what your error is without the broader context.

If it’s purely the original error, it’s because your Tensors are of different sizes and `torch.stack` won’t work, and you might need to use `torch.cat` instead.

---

<div class="post-metadata">

**Author:** ![Tim5](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/t/e9c0ed/32.png) [@Tim5](https://discuss.pytorch.org/u/Tim5)\
**Post date:** [February 1, 2022, 6:24pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/16 "2022-02-01T18:24:33Z")

</div>

the code is very huge to post as for loops and the file that i have also … i will describe my problem in the following lines

i implemented this line ` _embedding = torch.stack(tokens_embedding)` inside loop every time it read a sentences and got from it the following results

```auto
size is torch.Size([8, 768])
size is torch.Size([9, 768])
size is torch.Size([10, 768])
size is torch.Size([13, 768])
size is torch.Size([7, 768])
and so on

```

after it finished, i should use this line  
` embeddings.append(_embedding)`  
but got

```auto
RuntimeError: stack expects each tensor to be equal size, but got [7, 768] at entry 0 and [8, 768] at entry 1

```

---

<div class="post-metadata">

**Author:** ![AlphaBetaGamma96](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@AlphaBetaGamma96](https://discuss.pytorch.org/u/AlphaBetaGamma96)\
**Post date:** [February 1, 2022, 6:31pm UTC](https://discuss.pytorch.org/t/stack-expects-each-equal-size/143012/17 "2022-02-01T18:31:57Z")

</div>

> [@Tim5](#):
>
> ```auto
> size is torch.Size([8, 768])
> size is torch.Size([9, 768])
> size is torch.Size([10, 768])
> size is torch.Size([13, 768])
> size is torch.Size([7, 768])
> and so on
> 
> ```

And you want to stack these Tensors here? That won’t work because of the original problem, you could use `torch.cat` instead. Although, you’ll loose the information about which sets of rows correspond to what original Tensors.
