ml.lab
Python sleeps until you run code

Practice

Reps until it is automatic

Practice sets generate a fresh problem every time, check your answer, and show the worked solution. Review brings back the exercises you have missed so far.

Each set deals a fresh problem every time. Get enough right in a row to complete it, then keep going for as long as it helps.

Practice setDot products

Multiply matching entries and add. Vectors with 2 to 4 entries and small integers.

5 correct in a row completes the set. A miss starts the count again.

Compute a⋅b\mathbf{a}\cdot\mathbf{b} for a=(5,−1)\mathbf{a} = (5,\allowbreak -1) and b=(6,−2)\mathbf{b} = (6,\allowbreak -2).

a · b

Type a number: 0.25, -2, 3/4 and sqrt(2) all work. Enter checks.

Practice setProjections

Project a onto the line through b with the formula (a·b / b·b) b.

5 correct in a row completes the set. A miss starts the count again.

Find proj⁡ba\operatorname{proj}_{\mathbf{b}}\mathbf{a} for a=(0,−4)\mathbf{a} = (0,\allowbreak -4) and b=(−1,−1)\mathbf{b} = (-1,\allowbreak -1).

proj

One entry per box, top to bottom. 0.25, -2, 3/4 and sqrt(2) all work. Enter moves to the next empty box and checks once all are filled.

Practice setVector arithmetic

Scale vectors and add them entry by entry: linear combinations of two or three vectors, and solving for an unknown vector.

5 correct in a row completes the set. A miss starts the count again.

Find the vector x\mathbf{x} that satisfies 3x+a=b3\mathbf{x} + \mathbf{a} = \mathbf{b}, where a=(−1,−3,−5,−4)\mathbf{a} = (-1,\allowbreak -3,\allowbreak -5,\allowbreak -4) and b=(8,−12,−5,−4)\mathbf{b} = (8,\allowbreak -12,\allowbreak -5,\allowbreak -4). Enter x\mathbf{x} as a column.

x

One entry per box, top to bottom. 0.25, -2, 3/4 and sqrt(2) all work. Enter moves to the next empty box and checks once all are filled.

Practice setLengths, distances and cosine similarity

Norms from Pythagoras, distances between points, unit vectors, the L1 norm, and cosine similarity.

5 correct in a row completes the set. A miss starts the count again.

How far apart are the points a=(−1,−2)\mathbf{a} = (-1,\allowbreak -2) and b=(7,−8)\mathbf{b} = (7,\allowbreak -8)? Find the distance ∣a−b∣|\mathbf{a} - \mathbf{b}|.

|a − b|

Type a number: 0.25, -2, 3/4 and sqrt(2) all work. Enter checks.

Practice setMatrix times vector

Compute Ax with the row view (dot products), then check it with the column view (a combination of columns).

5 correct in a row completes the set. A miss starts the count again.

Compute Ax\mathbf{A}\mathbf{x} for A=[−341−2]\mathbf{A} = \begin{bmatrix}-3 & 4 \\ 1 & -2\end{bmatrix} and x=[20]\mathbf{x} = \begin{bmatrix}2 \\ 0\end{bmatrix}.

Ax

One entry per box, top to bottom. 0.25, -2, 3/4 and sqrt(2) all work. Enter moves to the next empty box and checks once all are filled.

Practice setMatrix multiplication

Multiply matrices entry by entry: each entry is a row of the left matrix dotted with a column of the right one.

5 correct in a row completes the set. A miss starts the count again.

Compute AB\mathbf{A}\mathbf{B} for A=[31−2−2−30]\mathbf{A} = \begin{bmatrix}3 & 1 \\ -2 & -2 \\ -3 & 0\end{bmatrix} and B=[−223−2−21]\mathbf{B} = \begin{bmatrix}-2 & 2 & 3 \\ -2 & -2 & 1\end{bmatrix}.

AB

One entry per box, row by row (Tab moves along a row). 0.25, -2, 3/4 and sqrt(2) all work. Enter moves to the next empty box and checks once all are filled.

Practice setShapes and batches

Predict shapes before you compute: products, transposes, dense layers, numpy broadcasting, parameter counts and multiply-add counts.

5 correct in a row completes the set. A miss starts the count again.

A\mathbf{A} has shape 8×208 \times 20 and B\mathbf{B} has shape 20×520 \times 5. What is the shape of AB\mathbf{A}\mathbf{B}?

Practice setDeterminants

2x2 determinants as ad - bc, 3x3 by cofactor expansion, and the rules for products, transposes, inverses and scaling.

5 correct in a row completes the set. A miss starts the count again.

Find det⁡A\det\mathbf{A} for A=[01−2−313−31−2]\mathbf{A} = \begin{bmatrix}0 & 1 & -2 \\ -3 & 1 & 3 \\ -3 & 1 & -2\end{bmatrix} by cofactor expansion along the first row.

det A

Type a number: 0.25, -2, 3/4 and sqrt(2) all work. Enter checks.

Practice setInverses and solving

Invert 2x2 matrices with the swap-negate-divide formula, and use the inverse to find which input lands on a given output.

5 correct in a row completes the set. A miss starts the count again.

Solve Ax=y\mathbf{A}\mathbf{x} = \mathbf{y} for A=[1011]\mathbf{A} = \begin{bmatrix}1 & 0 \\ 1 & 1\end{bmatrix} and y=[−4−4]\mathbf{y} = \begin{bmatrix}-4 \\ -4\end{bmatrix}. In words: which input x\mathbf{x} does A\mathbf{A} send to y\mathbf{y}?

x

One entry per box, top to bottom. 0.25, -2, 3/4 and sqrt(2) all work. Enter moves to the next empty box and checks once all are filled.

Practice setEigenvalues by hand

Solve det(A - λI) = 0 for a 2x2 matrix, check with the trace and determinant, and see the eigenvectors that go with each eigenvalue.

5 correct in a row completes the set. A miss starts the count again.

Find the eigenvalues of A=[010−1]\mathbf{A} = \begin{bmatrix}0 & 1 \\ 0 & -1\end{bmatrix}. Enter them as a column, largest first.

λ₁, λ₂

One entry per box, top to bottom. 0.25, -2, 3/4 and sqrt(2) all work. Enter moves to the next empty box and checks once all are filled.

Practice setPowers through eigenvectors

Compute A^k v by writing v in the eigenbasis, and predict which eigenvalue takes over after many steps.

5 correct in a row completes the set. A miss starts the count again.

The matrix A=[1−60−2]\mathbf{A} = \begin{bmatrix}1 & -6 \\ 0 & -2\end{bmatrix} has eigenvectors p1=(1,0)\mathbf{p}_1 = (1,\allowbreak 0) with λ1=1\lambda_1 = 1 and p2=(2,1)\mathbf{p}_2 = (2,\allowbreak 1) with λ2=−2\lambda_2 = -2. Use them to compute A3v\mathbf{A}^{3}\mathbf{v} for v=[−1−1]\mathbf{v} = \begin{bmatrix}-1 \\ -1\end{bmatrix}.

A^3 v

One entry per box, top to bottom. 0.25, -2, 3/4 and sqrt(2) all work. Enter moves to the next empty box and checks once all are filled.

Practice setDerivatives at a point

Power, product and chain rules, and the derivatives of exp, ln, sigmoid, tanh and ReLU, evaluated at a point.

5 correct in a row completes the set. A miss starts the count again.

Let f(x)=2x3−3x2+x+2f(x) = 2x^3 - 3x^2 + x + 2. Find f′(−1)f'(-1).

f′(-1)

Type a number: 0.25, -2, 3/4 and sqrt(2) all work. Enter checks.

Practice setGradients

Partial derivatives collected into a gradient: polynomials, exponentials, quadratic forms, linear-regression loss, and directional derivatives.

5 correct in a row completes the set. A miss starts the count again.

Let f(x,y)=2x2+xy+2y2+x−yf(x,\allowbreak y) = 2x^2 + xy + 2y^2 + x - y. Find the gradient ∇f\nabla f at (x,y)=(−2,−2)(x,\allowbreak y) = (-2,\allowbreak -2).

∇f

One entry per box, top to bottom. 0.25, -2, 3/4 and sqrt(2) all work. Enter moves to the next empty box and checks once all are filled.

Practice setJacobians

Matrices of partial derivatives: polynomial maps, linear layers, elementwise activations, softmax, and the chain rule as a matrix product.

5 correct in a row completes the set. A miss starts the count again.

Let f(x,y)=(3y2+3x, 3x+2x2)\mathbf{f}(x,\allowbreak y) = \big(3y^2 + 3x,\allowbreak \ 3x + 2x^2\big). Find the Jacobian J\mathbf{J} at (x,y)=(0,−2)(x,\allowbreak y) = (0,\allowbreak -2).

J

One entry per box, row by row (Tab moves along a row). 0.25, -2, 3/4 and sqrt(2) all work. Enter moves to the next empty box and checks once all are filled.

Practice setForward passes

Push numbers through small networks: a ReLU hidden layer, a linear or sigmoid output, and a batch in the row convention used by code.

5 correct in a row completes the set. A miss starts the count again.

In code, a layer processes a batch with one example per row: H=ReLU⁡(XW+b)\mathbf{H} = \operatorname{ReLU}(\mathbf{X}\mathbf{W} + \mathbf{b}), where the bias row b\mathbf{b} is added to every row. Compute H\mathbf{H} for X=[1−2−21]\mathbf{X} = \begin{bmatrix}1 & -2 \\ -2 & 1\end{bmatrix}, W=[1−111]\mathbf{W} = \begin{bmatrix}1 & -1 \\ 1 & 1\end{bmatrix} and b=[−21]\mathbf{b} = \begin{bmatrix}-2 & 1\end{bmatrix}.

H

One entry per box, row by row (Tab moves along a row). 0.25, -2, 3/4 and sqrt(2) all work. Enter moves to the next empty box and checks once all are filled.

Practice setSoftmax and cross-entropy

Turn logits into probabilities the stable way, with and without temperature, then compute the cross-entropy loss and its gradient p - y.

5 correct in a row completes the set. A miss starts the count again.

A freshly initialized classifier gives all K=65K = 65 classes the same logit. What cross-entropy loss (natural log) should the first training step report? Give the answer to 3 decimal places.

loss

Type a number: 0.25, -2, 3/4 and sqrt(2) all work. Enter checks.

Practice setBackprop by hand

Gradients through a linear layer with a batch (dL/dW, dL/dX, dL/db), through a ReLU, and where a value is used twice.

5 correct in a row completes the set. A miss starts the count again.

A single ReLU unit and a squared-error loss: z=wx+bz = wx + b, a=ReLU⁡(z)a = \operatorname{ReLU}(z), L=(a−t)2L = (a - t)^2. With x=3x = 3, w=−1w = -1, b=−2b = -2 and target t=−1t = -1, find (∂L∂w,∂L∂b)\left(\dfrac{\partial L}{\partial w},\allowbreak \dfrac{\partial L}{\partial b}\right).

(∂L/∂w, ∂L/∂b)

One entry per box, top to bottom. 0.25, -2, 3/4 and sqrt(2) all work. Enter moves to the next empty box and checks once all are filled.

Practice setRNN steps

Run a recurrent network forward by hand: two steps of a scalar RNN, or one step of a two-unit RNN with weight matrices.

5 correct in a row completes the set. A miss starts the count again.

A two-unit RNN computes ht=tanh⁡(Whhht−1+wxhxt+b)\mathbf{h}_t = \tanh(\mathbf{W}_{hh}\mathbf{h}_{t-1} + \mathbf{w}_{xh}x_t + \mathbf{b}) with tanh⁡\tanh applied to each entry. Use Whh=[1−10.50.5]\mathbf{W}_{hh} = \begin{bmatrix}1 & -1 \\ 0.5 & 0.5\end{bmatrix}, wxh=[02]\mathbf{w}_{xh} = \begin{bmatrix}0 \\ 2\end{bmatrix}, b=[−0.50]\mathbf{b} = \begin{bmatrix}-0.5 \\ 0\end{bmatrix}, the previous state ht−1=[−0.51]\mathbf{h}_{t-1} = \begin{bmatrix}-0.5 \\ 1\end{bmatrix} and the input xt=1x_t = 1. Find ht\mathbf{h}_t to 3 decimal places.

hₜ

One entry per box, top to bottom. 0.25, -2, 3/4 and sqrt(2) all work. Enter moves to the next empty box and checks once all are filled.

Practice setOne LSTM step

Compute the four gates of a scalar LSTM, then the new cell state and hidden state, by hand.

5 correct in a row completes the set. A miss starts the count again.

A scalar LSTM (hidden size 1) has these weights:

gateww (times xtx_t)uu (times ht−1h_{t-1})bb (bias)
forget ftf_t-100
input iti_t101
candidate gtg_t200
output oto_t2-11
ft=σ(wfxt+ufht−1+bf),it=σ(wixt+uiht−1+bi),gt=tanh⁡(wgxt+ught−1+bg),ot=σ(woxt+uoht−1+bo),ct=ft ct−1+it gt,ht=ottanh⁡(ct).\begin{aligned} f_t &= \sigma(w_f x_t + u_f h_{t-1} + b_f), & i_t &= \sigma(w_i x_t + u_i h_{t-1} + b_i), \\ g_t &= \tanh(w_g x_t + u_g h_{t-1} + b_g), & o_t &= \sigma(w_o x_t + u_o h_{t-1} + b_o), \\ c_t &= f_t\,c_{t-1} + i_t\,g_t, & h_t &= o_t\tanh(c_t). \end{aligned}

With xt=1x_t = 1, ht−1=−0.5h_{t-1} = -0.5 and ct−1=2c_{t-1} = 2, find ctc_t and hth_t to 3 decimal places. Enter them as a column, ctc_t first.

c_t, h_t

One entry per box, top to bottom. 0.25, -2, 3/4 and sqrt(2) all work. Enter moves to the next empty box and checks once all are filled.

Practice setCounting LSTM parameters

Count the weights of LSTM layers, compare with a plain RNN, and count whole character-level models.

5 correct in a row completes the set. A miss starts the count again.

An LSTM layer has hidden size n=2n = 2 and input size m=50m = 50, with one bias vector per gate. How many parameters does it have?

parameters

Type a number: 0.25, -2, 3/4 and sqrt(2) all work. Enter checks.

Practice setAttention weights

Scaled dot-product attention for one query and three keys: scores, softmax weights, the weighted average of values, and a causal mask.

5 correct in a row completes the set. A miss starts the count again.

A query q=(1,0,−1,0)\mathbf{q} = (1,\allowbreak 0,\allowbreak -1,\allowbreak 0) attends over three keys k1=(2,2,2,−1)\mathbf{k}_1 = (2,\allowbreak 2,\allowbreak 2,\allowbreak -1), k2=(1,0,0,−1)\mathbf{k}_2 = (1,\allowbreak 0,\allowbreak 0,\allowbreak -1), k3=(2,1,1,1)\mathbf{k}_3 = (2,\allowbreak 1,\allowbreak 1,\allowbreak 1), in d=4d = 4 dimensions. Find the attention weights aj=softmax⁡j(q⋅kj/d)a_j = \operatorname{softmax}_j(\mathbf{q}\cdot\mathbf{k}_j/\sqrt{d}). Give each to 3 decimal places.

a

One entry per box, top to bottom. 0.25, -2, 3/4 and sqrt(2) all work. Enter moves to the next empty box and checks once all are filled.

Practice set2D convolution

Slide a small kernel over a small image by hand, with stride 1 or 2, and predict output sizes with padding and stride.

5 correct in a row completes the set. A miss starts the count again.

Convolve the image X=[2012222100233030]\mathbf{X} = \begin{bmatrix}2 & 0 & 1 & 2 \\ 2 & 2 & 2 & 1 \\ 0 & 0 & 2 & 3 \\ 3 & 0 & 3 & 0\end{bmatrix} with the kernel K=[10−110−110−1]\mathbf{K} = \begin{bmatrix}1 & 0 & -1 \\ 1 & 0 & -1 \\ 1 & 0 & -1\end{bmatrix}, stride 1, no padding. As in deep-learning libraries, slide the kernel without flipping it: each output is the sum of the kernel times the patch under it, entry by entry.

Y

One entry per box, row by row (Tab moves along a row). 0.25, -2, 3/4 and sqrt(2) all work. Enter moves to the next empty box and checks once all are filled.