Chapter 9: Matrix Multiplication
Matrix multiplication is a fundamental operation in linear algebra, and it plays a crucial role in deep learning. In this chapter, we will explore what matrix multiplication is, provide examples to illustrate the concept, and demonstrate how to perform matrix multiplication using NumPy, a popular Python library for numerical computations.
Definition:
Matrix multiplication is a binary operation that takes two matrices and produces another matrix. It is also known as matrix product. Given two matrices A and B, the product AB is defined only if the number of columns in matrix A is equal to the number of rows in matrix B. The resulting matrix has dimensions (number of rows in A) x (number of columns in B).
Mathematical Notation:
Matrix multiplication is often denoted using the dot product symbol or by simply placing the matrices next to each other without an operator. The dot product symbol is represented as follows:
$$
A \cdot B
$$
Example:
Consider two matrices A and B:
Matrix A:
$$
A = \begin{bmatrix}
2 & 3 \\
1 & 4 \\
\end{bmatrix}
$$
Matrix B:
$$
B = \begin{bmatrix}
5 & 6 \\
7 & 8 \\
\end{bmatrix}
$$
To compute the product AB, we perform the following calculations:
$$
(2 \cdot 5) + (3 \cdot 7) = 10 + 21 = 31 \quad \text{(Result at position (1,1))}
$$
$$
(2 \cdot 6) + (3 \cdot 8) = 12 + 24 = 36 \quad \text{(Result at position (1,2))}
$$
$$
(1 \cdot 5) + (4 \cdot 7) = 5 + 28 = 33 \quad \text{(Result at position (2,1))}
$$
$$
(1 \cdot 6) + (4 \cdot 8) = 6 + 32 = 38 \quad \text{(Result at position (2,2))}
$$
So, the product of matrices A and B, denoted as AB, is:
$$
AB = \begin{bmatrix}
31 & 36 \\
33 & 38 \\
\end{bmatrix}
$$
NumPy Code:
You can perform matrix multiplication using NumPy in Python. Here’s how you can do it:
import numpy as np
# Define matrices A and B
A = np.array([[2, 3], [1, 4]])
B = np.array([[5, 6], [7, 8]])
# Perform matrix multiplication
result = np.dot(A, B)
# Print the result
print(result)This code will output the following result:
[[31 36]
[33 38]]NumPy’s np.dot function efficiently computes the matrix product of A and B.
In summary, matrix multiplication is a fundamental operation in linear algebra and deep learning. It involves multiplying corresponding elements of rows and columns to produce a new matrix. NumPy provides a convenient way to perform matrix multiplication in Python, making it a valuable tool in deep learning and other numerical applications.