PythiloLesson 4 of 15 · progress kept in this browser

Batches of vectors

Networks process many examples at once. A batch is a matrix X of shape (n, d): one row per example. To apply a layer to every row, multiply by the transpose: X @ W.T + b. The result has shape (n, m), and b of shape (m,) is broadcast, added to every row.

import numpy as np

X = np.array([[1.0, 0.0],
              [0.0, 1.0],
              [1.0, 1.0]])
W = np.array([[2.0, 0.0],
              [0.0, 3.0],
              [1.0, -1.0]])
b = np.array([0.0, 1.0, 0.0])
print(X @ W.T + b)  # shape (3, 3)
Your task

Write batch_layer(X, W, b) applying the layer W @ x + b to every row of X, returning shape (n, m). Write pairwise_sq_distances(A, B) returning the (n, m) matrix whose entry [i, j] is the squared L2 distance between row i of A and row j of B, without Python loops.

The first run loads NumPy into this browser.

Next: Derivatives