# Why can a 1\*n and a n\*1 tensor be added together?

**URL:** <https://discuss.pytorch.org/t/why-can-a-1-n-and-a-n-1-tensor-be-added-together/71666>\
**Category:** Uncategorized\
**Created:** [March 2, 2020, 3:56am UTC](https://discuss.pytorch.org/t/why-can-a-1-n-and-a-n-1-tensor-be-added-together/71666 "2020-03-02T03:56:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikings](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/a87d85/32.png) [@Vikings](https://discuss.pytorch.org/u/Vikings)\
**Post date:** [March 2, 2020, 3:56am UTC](https://discuss.pytorch.org/t/why-can-a-1-n-and-a-n-1-tensor-be-added-together/71666/1 "2020-03-02T03:56:34Z")

</div>

I’m a beginner of PyTorch and when I trying to run the following code:

> x=torch.tensor([[5,3],[5,3]])  
> z1=x.view(1,4)  
> z2=x.view(4,1)  
> print(z1+z2)

I got:

> tensor([[10, 8, 10, 8],  
> [8, 6, 8, 6],  
> [10, 8, 10, 8],  
> [8, 6, 8, 6]])

Why doesn’t the compiler throw an error? Why two tensors with different dimensions can be added together?

Thank you!

---

<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 2, 2020, 4:08am UTC](https://discuss.pytorch.org/t/why-can-a-1-n-and-a-n-1-tensor-be-added-together/71666/2 "2020-03-02T04:08:41Z")

</div>

This behavior is called broadcasting and tries to stay as close as possible to numpy’s implementation, which is described [here](https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html).

---

<div class="post-metadata">

**Author:** ![Vikings](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/a87d85/32.png) [@Vikings](https://discuss.pytorch.org/u/Vikings)\
**Post date:** [March 2, 2020, 4:31am UTC](https://discuss.pytorch.org/t/why-can-a-1-n-and-a-n-1-tensor-be-added-together/71666/3 "2020-03-02T04:31:49Z")

</div>

Got it.  
Thank you for your help!
