# Batch matrix multiplication

**URL:** <https://discourse.pymc.io/t/batch-matrix-multiplication/13085>\
**Category:** General\
**Created:** [October 11, 2023, 11:18am UTC](https://discourse.pymc.io/t/batch-matrix-multiplication/13085 "2023-10-11T11:18:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marco\_Forgione](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/marco_forgione/32/7269_2.png) [@Marco\_Forgione](https://discourse.pymc.io/u/Marco_Forgione)\
**Post date:** [October 11, 2023, 11:18am UTC](https://discourse.pymc.io/t/batch-matrix-multiplication/13085/1 "2023-10-11T11:18:10Z")

</div>

Is there a way to do a batch matrix multiplication in pymc? The prototype operation in numpy  
is:

```python
B, M, N, P = 32, 100, 5, 2
w1_np = np.random.randn(B, M, N)
w2_np = np.random.randn(B, N, P)
data = w1_np.dot(w2_np) # shape: (B, M, P)

```

where I wanna carry out the B matrix multiplies (M, N), (N,P) → (M, P) in one shot and store them in  
a 3D structure (B, M, P). My tentative pymc code is:

```auto
with pm.Model() as bmm:

    w1 = pm.Normal('w1', 0, sigma=1, shape=(B, M, N))
    w2 = pm.Normal('w2', 0, sigma=1, shape=(B, N, P))

    #ym = pm.math.dot(w1, w2)
    ym = w1 @ w2
    out = pm.Normal('out', mu=ym)#, observed=data)
    prior_data = pm.sample_prior_predictive()

```

but it does not work, while the version with `ym = pm.math.dot(w1, w2)` returns a result with different dimensions.

Thanks,  
Marco

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [October 11, 2023, 2:16pm UTC](https://discourse.pymc.io/t/batch-matrix-multiplication/13085/2 "2023-10-11T14:16:58Z")

</div>

> [@Marco\_Forgione](#):
>
> `data = w1_np.dot(w2_np) # shape: (B, M, P)`

This is not what happens.

```auto
w1_np.dot(w2_np).shape == (B, M, B, P)

```

What you want is `matmul`:

```auto
np.matmul(w1_np, w2_np).shape == (B, M, P)

```

Which is what you get when you use the `@` operator in numpy and in PyMC (but only the latest version, there was a mistake before).

To be safe you can use `pytensor.tensor.matmul(w1, w2)`

---

<div class="post-metadata">

**Author:** ![Marco\_Forgione](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/marco_forgione/32/7269_2.png) [@Marco\_Forgione](https://discourse.pymc.io/u/Marco_Forgione)\
**Post date:** [October 13, 2023, 2:05pm UTC](https://discourse.pymc.io/t/batch-matrix-multiplication/13085/3 "2023-10-13T14:05:19Z")

</div>

Thanks Ricardo,

Thanks for the reply. Updating to version 5.9, the @ operator in pymc works. Previously I had issues in 5.7.2

> This is not what happens.
> 
> ```auto
> w1_np.dot(w2_np).shape == (B, M, B, P)
> 
> ```

Right, it was a leftover of previous attempts I meant @ here.
