DDC-52: Asymmetry of matrix-vector multiplication
A data challenge a day helps you master machine learning
About these daily data challenges
Each post is an exercise that helps you learn about data in Python.
Try to solve the exercise before checking my solution at the bottom of the post 🤓
You can share your solution or visualization in the comments!
Today’s challenge
Create a 5x5 matrix and a 5x1 vector of random numbers. Calculate and print vM and Mv to show that both orders of multiplication are valid, but the results are different.
.
.
.
.
Scroll down for the solution…
.
.
.
.

.
.
.
.
keep scrolling!
.
.
.
.
import numpy as np
M = np.random.randn(5,5)
v = np.random.randn(5,1)
vM = v.T@M
Mv = M@v
print(vM,’\n’,Mv.T)
This dimensionality consept is crucial for understanding neural network operations. The fact that vM gives a 1x5 result (row vector) while Mv gives a 5x1 result (column vector) really highlits how matrix algebra enforces consistent shapes through computation. It reminds me how attention mechanisms in transformers rely heavily on getting these multiplicatons right. Woudl be intresting to see this extended to batched operations too.
It is interesting to see the CPU batching behavior!!