CodeBlanks Logo Icon CodeBlanks

Matrix multiplication

Matrix multiplication is done with np.dot() or the @ operator.

Matrix multiplication

Multiplying A of shape (m, n) by B of shape (n, p) gives a matrix of shape (m, p). Each element of the result is the dot product of a row of A and a column of B.

A = np.array([[1, 2],
              [3, 4]])
B = np.array([[5, 6],
              [7, 8]])
print(A @ B)   # [[19 22] [43 50]]
# 19 = 1*5 + 2*7

A * B is element-wise multiplication and gives a completely different result: [[5 12] [21 32]].

Batch prediction

Matrix multiplication gives the predictions for all examples in one operation. In a linear model you write X @ w + b, in a neural network layer X @ W + b:

X = np.array([[1, 2],
              [3, 4],
              [5, 6]])   # 3 examples, 2 features
w = np.array([0.5, 1.0])
print(X @ w + 1)         # [3.5 6.5 9.5]

The speed of deep learning comes from GPUs being able to do these multiplications highly in parallel.

Practice Predict all examples

Compute all predictions with X @ w + b from X (4 examples, 2 features) and w. Print the predictions and their shape.

Starter code

model.py
import numpy as np

X = np.array([[1, 1], [2, 0], [0, 3], [4, 2]])
w = np.array([3, -2])
b = 5

# buraya yaz

Sample solution

model.py
import numpy as np

X = np.array([[1, 1], [2, 0], [0, 3], [4, 2]])
w = np.array([3, -2])
b = 5

tahmin = X @ w + b
print(tahmin)
print(tahmin.shape)

Expected output

[ 6 11 -1 13]
(4,)

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 If X of shape (4, 3) is multiplied by W of shape (3, 2), what is the shape of <code>X @ W</code>?

  • (4, 3)
  • An error
  • (4, 2) Correct answer
  • (3, 3)

Explanation: The inner dimensions (3) match and disappear; the outer ones (4, 2) remain.

2 Do the matrix multiplication.

Question
C = A ____ B
Answer
C = A @ B

Explanation: @ is matrix multiplication; * is element-wise.

3 Match the shapes.

Term Meaning
(2, 3) @ (3, 4) (2, 4)
(5, 2) @ (2,) (5,)
(3, 3) @ (3, 1) (3, 1)
(2, 3) @ (2, 3) Error

All lessons in Beginner

13 Project: Classification

  1. Preparing data
  2. Training model
  3. Prediction
  4. Evaluation