Matrix Multiplication
Matrix multiplication is a mathematical operation between 2 matrices that returns a matrix.
For each row in the first matrix, take the Dot Productf each column in the second matrix. Place the results onto the corresponding row in a new matrix.
The size of the new matrix will be the row count from the first matrix by the column count from the second.
It is only defined when the number of columns from the first matrix equals the number of rows in the second.
In Numpy the @
operator is used for matrix multiplication between 2 multi-dimensional arrays (matrices):
In [2]:
from numpy import array
matrix1 = array([
[2, 4],
[1, 3]
])
matrix2 = array([
[3, 1, 5],
[-2, 1, 3]
])
matrix1 @ matrix2
Out[2]:
array([[-2, 6, 22], [-3, 4, 14]])
The same operator works in PyTorch:
In [4]:
from torch import tensor
tensor(matrix1) @ tensor(matrix2)
Out[4]:
tensor([[-2, 6, 22], [-3, 4, 14]])