# Pytorch: tensor multiplication

**URL:** <https://discuss.pytorch.org/t/pytorch-tensor-multiplication/81443>\
**Category:** Uncategorized\
**Created:** [May 15, 2020, 10:33pm UTC](https://discuss.pytorch.org/t/pytorch-tensor-multiplication/81443 "2020-05-15T22:33:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lifeblack](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lifeblack/32/18210_2.png) [@lifeblack](https://discuss.pytorch.org/u/lifeblack)\
**Post date:** [May 15, 2020, 10:33pm UTC](https://discuss.pytorch.org/t/pytorch-tensor-multiplication/81443/1 "2020-05-15T22:33:42Z")

</div>

Hi!  
I want to code this matrix multiplication:

![image](https://discuss.pytorch.org/uploads/default/original/3X/d/3/d3ce46b9cab7be3b9b7bc6f5a93da9bfd8a9e565.png)  
B=16, h=32, w=56, b\*b=64, n=25  
my code:

```auto
torch.mul(torch.rand([16, 32, 56, 64]),torch.rand([16, 32, 56, 64,25]))

```

but I encountered in with this error:

```auto
The size of tensor a (64) must match the size of tensor b (25) at non-singleton dimension 4

```

Would you mind helping me to fix this problem?

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [May 15, 2020, 10:36pm UTC](https://discuss.pytorch.org/t/pytorch-tensor-multiplication/81443/2 "2020-05-15T22:36:09Z")

</div>

Hi,

I think the simplest is to write it as a matrix matrix multiplication. And so add a temporary dimension of size 1 on the first Tensor:

```auto
torch.matmul(torch.rand([16, 32, 56, 1, 64]),torch.rand([16, 32, 56, 64,25])).squeeze(-2)

```

---

<div class="post-metadata">

**Author:** ![lifeblack](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lifeblack/32/18210_2.png) [@lifeblack](https://discuss.pytorch.org/u/lifeblack)\
**Post date:** [May 15, 2020, 10:40pm UTC](https://discuss.pytorch.org/t/pytorch-tensor-multiplication/81443/3 "2020-05-15T22:40:17Z")

</div>

Thanks for your reply. The problem solved.  
Would you mind explaining it? I do not understand it.

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [May 15, 2020, 10:44pm UTC](https://discuss.pytorch.org/t/pytorch-tensor-multiplication/81443/4 "2020-05-15T22:44:41Z")

</div>

It consider `Bxhxw` as batch dimensions. And `1x(bb)` and `(bb)xn` for the other dimensions of each other dimensions.  
Doing the matrix multiply between the two, will give you a Tensor with `1xn` and with the same batch size.  
The squeeze removes the dimension of size 1.

---

<div class="post-metadata">

**Author:** ![lifeblack](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lifeblack/32/18210_2.png) [@lifeblack](https://discuss.pytorch.org/u/lifeblack)\
**Post date:** [May 15, 2020, 10:47pm UTC](https://discuss.pytorch.org/t/pytorch-tensor-multiplication/81443/5 "2020-05-15T22:47:23Z")

</div>

Thanks for your help dear @albanD 🙂
