Training Systems · Local CPULaptop CPULesson 4 of 5 in Phase 05

Phase 05 / Neural network training

Train a CNN on a small dataset

Revision v0.18.1 ·

Lesson · about 110 minutes · CPU practice

Train a classifier with a fixed data split and report held-out metrics.

Phase study guide: preparation, milestones and project evidence

Before you start

Before this: A compiled train and evaluation step
Hardware: CPU

Setup help: Python, notebook, or Cloud TPU VM

Use the tested CPU packages (the recorded environment uses Python 3.14.3). Other Python versions require separate checks. Hardware receipts apply only to their recorded source, environment, hardware, and cases; they do not qualify every lesson or TPU configuration. See the TPU performance and hardware evidence guide. The optional Cloud TPU specialization explains target-specific execution boundaries. Logical CPU devices practice partitioning; they do not simulate TPU performance.

Download the repository ZIP or use your existing checkout. Extract it and open a terminal in the folder containing requirements-cpu.txt. The beginner ZIP is separate; these lessons use the full repository folder.

macOS / Linux terminal · repository folder
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements-cpu.txt

PowerShell · repository folder
py -3 -m venv .venv
.\.venv\Scripts\python.exe -m pip install -r requirements-cpu.txt

After activation, run the complete worked example from that same folder:

macOS / Linux terminal · activated environment
python phases/05-networks/04-train-a-cnn-on-a-small-dataset/code/main.py

PowerShell command · repository folder
.\.venv\Scripts\python.exe phases/05-networks/04-train-a-cnn-on-a-small-dataset/code/main.py

For an incremental build, create main.py in your own practice folder, append each build block in order, and run python main.py with this environment active. Keep that file separate from the downloaded reference solution.

Check: Held-out accuracy is at least 0.90.9 for the fixed fixture; confusion counts total 2424. The runnable script prints the actual measured loss and counts.

Revisit the guided CPU setup · Open 19: TPU Setup. Package instructions are CPU instructions; Windows and accelerator environments need their own validation.

Stage 1 of 5 · Understand the MechanismCore mental model, equations & visual walkthrough

What you will be able to do

  • Generate images with an explicit label rule
  • Derive the convolution output shape
  • Pool spatial responses and retain class scores
  • Read a confusion matrix rather than a success label

The problem

Can a small model tell horizontal bars from vertical bars in noisy images? We’ll create the images locally and train a convolutional classifier on CPU. You’ll then test images from a different random seed and read a confusion matrix. The task is small enough to inspect without downloading a dataset.

The idea

A convolution combines a local input patch with weights reused across spatial positions. Layout, stride and padding decide which pixels contribute to each output. Trace one position first; the full feature map repeats that same rule.

Trace one convolution output

For one output cell, take the matching 3×33\times3 image patch, multiply element by element by one filter, sum over the patch's height, width, and input channels, then add that filter's bias. The same filter values are reused at the next location; only the selected pixels move. The independent NumPy dot product checks one interior cell before training.

The 5-by-5 diagram is a separate shape example: with stride one and no padding, the top-left patch can move to three positions across and three down, giving a 3-by-3 output map. The trained images are 8-by-8, so the same kernel yields 6-by-6 positions. Use the confusion matrix afterward to see which held-out orientations were mistaken.

The highlighted purple square is one 3×33\times3 receptive field on a 5×55\times5 input. Its nine selected cells contribute, with their corresponding filter weights, to one output location. Move the square one cell at a time: at stride one and no padding it has three valid positions across and three down, producing a 3×33\times3 map. The filter is reused at each position. Then compare this shape example with the trained 8×88\times8 inputs, which produce 6×66\times6 spatial outputs; the diagram shows the indexing rule, not learned filter values.

Open full-size diagram
Hout=⌊Hin+2P−KS⌋+1,Wout=⌊Win+2P−KS⌋+1H_{\text{out}} = \left\lfloor \frac{H_{\text{in}} + 2P - K}{S} \right\rfloor + 1, \qquad W_{\text{out}} = \left\lfloor \frac{W_{\text{in}} + 2P - K}{S} \right\rfloor + 1

Generate images with an explicit label rule

Each example is an 8×8×18\times8\times1 image. Horizontal bars are class zero and vertical bars are class one; the bar's interior position varies, and Gaussian noise perturbs every pixel. Alternating labels keeps the generated split balanced. A different seed creates new held-out images without changing the label rule.

In the retained held-out image, a bright horizontal band appears around row 3, while other pixels remain near zero with noise. Inspecting an image and its label makes the task concrete before the CNN is fit. This generator has no perspective, occlusion, or natural objects, so its result says nothing about photographs.

Derive the convolution output shape

The input layout is (B,H,W,C)(B,H,W,C). With an 8×88\times8 image and a 3×33\times3 filter, VALID padding and stride one allow 8−3+1=68-3+1=6 positions along each spatial axis. Four output channels mean four learned filters, so each image produces a 6×6×46\times6\times4 activation array. The kernel shape is (3,3,1,4)(3,3,1,4) with four bias values.

The patch reference uses cross-correlation, the operation used by this convolution API: it does not flip the filter. Matching the NumPy result at the selected output checks channel order, patch indexing, and bias addition before the optimizer can obscure a layout error.

(B,8,8,1) → Conv3×3/VALID → (B,6,6,4) → ReLU → spatial mean → (B,4) → Linear → (B,2)

Pool spatial responses and retain class scores

After ReLU, averaging spatial axes 11 and 22 turns each 6×66\times6 map into one value per filter. The result retains four features per image, regardless of where the bar appears. That invariance fits orientation classification, but it discards location information that a localization task would need.

The head produces two raw class scores. Pass those logits and integer labels directly to the stable cross-entropy; applying softmax first is unnecessary and changes the expected input. The NumPy reference subtracts the largest score before log-sum-exp and averages over images.

Read a confusion matrix rather than a success label

Rows in the matrix are true classes and columns are predicted classes. The retained counts are [[12,0],[0,12]][[12,0],[0,12]]: all 12 horizontal and all 12 vertical held-out images land on the diagonal. The matrix totals 24, and its trace divided by 24 equals the reported accuracy 1.01.0; held-out mean loss is 0.0157006250.015700625.

A horizontal-only baseline would get 12 of 24 correct, or 0.50.5, because the set is balanced. If an off-diagonal count appears after a change, its row tells you the true orientation and its column the predicted one; inspect examples from that cell. Zero observed errors is not a guarantee on other images.

Read the figure

The top heatmap has pixel columns on the horizontal axis and pixel rows on the vertical axis; its color bar is pixel value. This held-out example shows a bright horizontal band around row 3. In the lower heatmap, row labels are actual orientation and columns are predicted orientation; the color bar counts examples. Its observed cells are ([[12,0],[0,12]]), with all 24 images on the diagonal.

Connect it to the computation

The image panel explains one input, while the confusion panel compares all 24 held-out labels with predictions: 12 horizontal and 12 vertical are correct, with no observed off-diagonal errors. Check that the matrix sum is 24 and trace/24 is the reported accuracy 1.01.0. If a changed run has a nonzero off-diagonal cell, use its row and column to select examples for inspection. The zero errors apply only to this synthetic split.

Figure data and execution details

Recorded environment: JAX 0.9.2 · 1 CPU device

The notebook contains the data experiment, plotting code, and recorded figure output. Rerun it to test your changes.

Stage 2 of 5 · Build & Run the ReferenceStep-by-step code construction & verified execution output

Your build: Create a separate main.py in your practice folder. Append each block in order and run python main.py in the lesson environment. The downloaded script is the complete reference, so keep it separate from your attempt.

1 Generate two disjoint synthetic splits

Create main.py. Print a training image as an 8×88\times8 array if you want to inspect the task before fitting.

main.py · append build step 1
# Step 1 — 1. Generate two disjoint synthetic splits: Two seeds produce separate examples.
from flax import nnx
import jax
import jax.numpy as jnp
import numpy as np
import optax
def bars(seed,n,noise=.08):
    rng=np.random.default_rng(seed)
    images=np.zeros((n,8,8,1),np.float32)
    labels=np.arange(n,dtype=np.int32)%2
    for i,label in enumerate(labels):
        position=int(rng.integers(2,6))
        if label==0:images[i,position,:,0]=1.
        else:images[i,:,position,0]=1.
    # Accumulate the next contribution into `images`.
    images+=rng.normal(0,noise,images.shape).astype(np.float32)
    return jnp.array(images),jnp.array(labels)
train_x,train_y=bars(20,48)
test_x,test_y=bars(21,24)
# Check tensor shape invariant: `train_x.shape==(48,8,8,1) and test_x.shape==(24,8,8,1)`
assert train_x.shape==(48,8,8,1) and test_x.shape==(24,8,8,1)
assert not np.array_equal(np.asarray(train_x[:24]),np.asarray(test_x))

Two seeds produce separate examples. Both labels are balanced by construction; noise can make orientation harder as its scale grows.

Explain the APIs in this step
  • jnp.array(values, dtype=...): Constructs a JAX array from Python/NumPy values.

2 Build and independently inspect the convolution

Append the small CNN and the patch dot-product check before defining an optimizer.

main.py · append build step 2
# Step 2 — 2. Build and independently inspect the convolution
# Define `BarCNN` module / container with explicit state and forward pass:
class BarCNN(nnx.Module):
    def __init__(self):
        rngs=nnx.Rngs(0)
        self.conv=nnx.Conv(1,4,(3,3),padding='VALID',rngs=rngs)
        self.head=nnx.Linear(4,2,rngs=rngs)
    def __call__(self,x):
        features=jnp.mean(nnx.relu(self.conv(x)),axis=(1,2))
        return self.head(features)
model=BarCNN()
raw=model.conv(train_x[:1])
# Check tensor shape invariant: `raw.shape==(1,6,6,4)`
assert raw.shape==(1,6,6,4)
# Transfer the device result to the host for an independent NumPy comparison.
patch=np.asarray(train_x[0,:3,:3,:])
# Transfer the device result to the host for an independent NumPy comparison.
kernel=np.asarray(model.conv.kernel[...])
expected=np.sum(patch*kernel[:,:,:,0])+np.asarray(model.conv.bias[...])[0]
np.testing.assert_allclose(raw[0,0,0,0],expected,rtol=1e-5,atol=1e-6)
def loss(m,x,y):return jnp.mean(optax.softmax_cross_entropy_with_integer_labels(m(x),y))
optimizer=nnx.Optimizer(model,optax.adam(.03),wrt=nnx.Param)
@nnx.jit
def train_step(m,o,x,y):
    value,grads=nnx.value_and_grad(loss)(m,x,y)
    # Apply the parameter gradients to the mutable Flax model and optimizer state.
    o.update(m,grads)
    return value

The first output at (0,0)(0,0) consumes exactly the top-left 3×33\times3 patch. The NumPy result validates filter orientation and layout.

Explain the APIs in this step
  • nnx.Rngs(seed): Provides named random streams used to initialize and operate on NNX modules.
  • nnx.Linear(in_features, out_features, ...): Creates a learned affine layer in Flax NNX.
  • jnp.mean(x, axis=..., keepdims=...): Averages JAX array values along the selected axis.
  • np.testing.assert_allclose(actual, desired, rtol=..., atol=...): Checks a host-side numerical expectation and raises AssertionError when values differ beyond tolerance.

3 Fit, then measure the held-out split once

Append 8080 full-batch updates, stable NumPy loss, and confusion accounting. Every update uses only train_x/train_y.

main.py · append build step 3
# Step 3 — 3. Fit, then measure the held-out split once: The output is a measured CPU result for synthetic bars.
initial=float(loss(model,train_x,train_y))
for _ in range(80):train_step(model,optimizer,train_x,train_y)
# Transfer the device result to the host for an independent NumPy comparison.
# Transfer the device result to the host for an independent NumPy comparison.
scores=np.asarray(model(test_x))
labels=np.asarray(test_y)
predictions=scores.argmax(axis=-1)
# Reduce along axis=-1 to compute `shifted`.
shifted=scores-scores.max(axis=-1,keepdims=True)
# Reduce along axis=-1 to compute `reference_loss`.
reference_loss=np.mean(np.log(np.exp(shifted).sum(axis=-1))-shifted[np.arange(len(labels)),labels])
np.testing.assert_allclose(loss(model,test_x,test_y),reference_loss,rtol=1e-5,atol=1e-6)
confusion=np.zeros((2,2),dtype=int)
np.add.at(confusion,(labels,predictions),1)
accuracy=np.mean(predictions==labels)
assert confusion.sum()==24 and np.isclose(np.trace(confusion)/24,accuracy)
assert accuracy>=.9 and float(loss(model,train_x,train_y))<initial*.3
print('Held-out loss:',reference_loss,'accuracy:',accuracy,'confusion:\n',confusion)

The output is a measured CPU result for synthetic bars. No MNIST, TPU, or throughput claim is implied.

Explain the APIs in this step
  • array.sum(axis=...): Adds values along the selected axis; that axis is removed unless keepdims=True.
  • np.testing.assert_allclose(actual, desired, rtol=..., atol=...): Checks a host-side numerical expectation and raises AssertionError when values differ beyond tolerance.
View the cumulative build in main.py

These blocks append in order to your separate practice file.

main.py · cumulative Python build
# Step 1 — 1. Generate two disjoint synthetic splits: Two seeds produce separate examples.
from flax import nnx
import jax
import jax.numpy as jnp
import numpy as np
import optax
def bars(seed,n,noise=.08):
    rng=np.random.default_rng(seed)
    images=np.zeros((n,8,8,1),np.float32)
    labels=np.arange(n,dtype=np.int32)%2
    for i,label in enumerate(labels):
        position=int(rng.integers(2,6))
        if label==0:images[i,position,:,0]=1.
        else:images[i,:,position,0]=1.
    # Accumulate the next contribution into `images`.
    images+=rng.normal(0,noise,images.shape).astype(np.float32)
    return jnp.array(images),jnp.array(labels)
train_x,train_y=bars(20,48)
test_x,test_y=bars(21,24)
# Check tensor shape invariant: `train_x.shape==(48,8,8,1) and test_x.shape==(24,8,8,1)`
assert train_x.shape==(48,8,8,1) and test_x.shape==(24,8,8,1)
assert not np.array_equal(np.asarray(train_x[:24]),np.asarray(test_x))

# Step 2 — 2. Build and independently inspect the convolution
# Define `BarCNN` module / container with explicit state and forward pass:
class BarCNN(nnx.Module):
    def __init__(self):
        rngs=nnx.Rngs(0)
        self.conv=nnx.Conv(1,4,(3,3),padding='VALID',rngs=rngs)
        self.head=nnx.Linear(4,2,rngs=rngs)
    def __call__(self,x):
        features=jnp.mean(nnx.relu(self.conv(x)),axis=(1,2))
        return self.head(features)
model=BarCNN()
raw=model.conv(train_x[:1])
# Check tensor shape invariant: `raw.shape==(1,6,6,4)`
assert raw.shape==(1,6,6,4)
# Transfer the device result to the host for an independent NumPy comparison.
patch=np.asarray(train_x[0,:3,:3,:])
# Transfer the device result to the host for an independent NumPy comparison.
kernel=np.asarray(model.conv.kernel[...])
expected=np.sum(patch*kernel[:,:,:,0])+np.asarray(model.conv.bias[...])[0]
np.testing.assert_allclose(raw[0,0,0,0],expected,rtol=1e-5,atol=1e-6)
def loss(m,x,y):return jnp.mean(optax.softmax_cross_entropy_with_integer_labels(m(x),y))
optimizer=nnx.Optimizer(model,optax.adam(.03),wrt=nnx.Param)
@nnx.jit
def train_step(m,o,x,y):
    value,grads=nnx.value_and_grad(loss)(m,x,y)
    # Apply the parameter gradients to the mutable Flax model and optimizer state.
    o.update(m,grads)
    return value

# Step 3 — 3. Fit, then measure the held-out split once: The output is a measured CPU result for synthetic bars.
initial=float(loss(model,train_x,train_y))
for _ in range(80):train_step(model,optimizer,train_x,train_y)
# Transfer the device result to the host for an independent NumPy comparison.
# Transfer the device result to the host for an independent NumPy comparison.
scores=np.asarray(model(test_x))
labels=np.asarray(test_y)
predictions=scores.argmax(axis=-1)
# Reduce along axis=-1 to compute `shifted`.
shifted=scores-scores.max(axis=-1,keepdims=True)
# Reduce along axis=-1 to compute `reference_loss`.
reference_loss=np.mean(np.log(np.exp(shifted).sum(axis=-1))-shifted[np.arange(len(labels)),labels])
np.testing.assert_allclose(loss(model,test_x,test_y),reference_loss,rtol=1e-5,atol=1e-6)
confusion=np.zeros((2,2),dtype=int)
np.add.at(confusion,(labels,predictions),1)
accuracy=np.mean(predictions==labels)
assert confusion.sum()==24 and np.isclose(np.trace(confusion)/24,accuracy)
assert accuracy>=.9 and float(loss(model,train_x,train_y))<initial*.3
print('Held-out loss:',reference_loss,'accuracy:',accuracy,'confusion:\n',confusion)

Run the example

python3 phases/05-networks/04-train-a-cnn-on-a-small-dataset/code/main.py
About this command

This shortcut uses an activated macOS/Linux course environment in the repository folder. For Windows, use the explicit interpreter command in “Setup help” above. The script includes worked examples and reference solutions.

Inspect the complete assembled example
main.py · complete Python example
# Step 1 — 1. Generate two disjoint synthetic splits: Two seeds produce separate examples.
from flax import nnx
import jax
import jax.numpy as jnp
import numpy as np
import optax
def bars(seed,n,noise=.08):
    rng=np.random.default_rng(seed)
    images=np.zeros((n,8,8,1),np.float32)
    labels=np.arange(n,dtype=np.int32)%2
    for i,label in enumerate(labels):
        position=int(rng.integers(2,6))
        if label==0:images[i,position,:,0]=1.
        else:images[i,:,position,0]=1.
    # Accumulate the next contribution into `images`.
    images+=rng.normal(0,noise,images.shape).astype(np.float32)
    return jnp.array(images),jnp.array(labels)
train_x,train_y=bars(20,48)
test_x,test_y=bars(21,24)
# Check tensor shape invariant: `train_x.shape==(48,8,8,1) and test_x.shape==(24,8,8,1)`
assert train_x.shape==(48,8,8,1) and test_x.shape==(24,8,8,1)
assert not np.array_equal(np.asarray(train_x[:24]),np.asarray(test_x))

# Step 2 — 2. Build and independently inspect the convolution
# Define `BarCNN` module / container with explicit state and forward pass:
class BarCNN(nnx.Module):
    def __init__(self):
        rngs=nnx.Rngs(0)
        self.conv=nnx.Conv(1,4,(3,3),padding='VALID',rngs=rngs)
        self.head=nnx.Linear(4,2,rngs=rngs)
    def __call__(self,x):
        features=jnp.mean(nnx.relu(self.conv(x)),axis=(1,2))
        return self.head(features)
model=BarCNN()
raw=model.conv(train_x[:1])
# Check tensor shape invariant: `raw.shape==(1,6,6,4)`
assert raw.shape==(1,6,6,4)
# Transfer the device result to the host for an independent NumPy comparison.
patch=np.asarray(train_x[0,:3,:3,:])
# Transfer the device result to the host for an independent NumPy comparison.
kernel=np.asarray(model.conv.kernel[...])
expected=np.sum(patch*kernel[:,:,:,0])+np.asarray(model.conv.bias[...])[0]
np.testing.assert_allclose(raw[0,0,0,0],expected,rtol=1e-5,atol=1e-6)
def loss(m,x,y):return jnp.mean(optax.softmax_cross_entropy_with_integer_labels(m(x),y))
optimizer=nnx.Optimizer(model,optax.adam(.03),wrt=nnx.Param)
@nnx.jit
def train_step(m,o,x,y):
    value,grads=nnx.value_and_grad(loss)(m,x,y)
    # Apply the parameter gradients to the mutable Flax model and optimizer state.
    o.update(m,grads)
    return value

# Step 3 — 3. Fit, then measure the held-out split once: The output is a measured CPU result for synthetic bars.
initial=float(loss(model,train_x,train_y))
for _ in range(80):train_step(model,optimizer,train_x,train_y)
# Transfer the device result to the host for an independent NumPy comparison.
# Transfer the device result to the host for an independent NumPy comparison.
scores=np.asarray(model(test_x))
labels=np.asarray(test_y)
predictions=scores.argmax(axis=-1)
# Subtract each example’s largest class logit before exponentiation for numerical stability.
shifted=scores-scores.max(axis=-1,keepdims=True)
# The log-sum-exp term normalizes over classes; the indexed term selects each true class.
reference_loss=np.mean(np.log(np.exp(shifted).sum(axis=-1))-shifted[np.arange(len(labels)),labels])
np.testing.assert_allclose(loss(model,test_x,test_y),reference_loss,rtol=1e-5,atol=1e-6)
confusion=np.zeros((2,2),dtype=int)
np.add.at(confusion,(labels,predictions),1)
accuracy=np.mean(predictions==labels)
assert confusion.sum()==24 and np.isclose(np.trace(confusion)/24,accuracy)
assert accuracy>=.9 and float(loss(model,train_x,train_y))<initial*.3
print('Held-out loss:',reference_loss,'accuracy:',accuracy,'confusion:\n',confusion)

Expected result: Held-out accuracy is at least 0.90.9 for the fixed fixture; confusion counts total 2424. The runnable script prints the actual measured loss and counts.

Recorded output and runtime details

Reference output captured from executing the complete lesson script (code/main.py) and its assertions in the course CPU environment.

2026-10-27T17:26:39.317194+00:00 · Python 3.14.3 · JAX 0.9.2 · 1 CPU device

Held-out loss: 0.015700625 accuracy: 1.0 confusion:
 [[12  0]
 [ 0 12]]
Held-out loss: 0.015700625 accuracy: 1.0 confusion:
 [[12  0]
 [ 0 12]]
True-class counts: [12 12] row-normalized confusion: [[1. 0.]
 [0. 1.]]
Absent class recall: undefined, not zero.
PASS: networks-04

Download execution record · The notebook includes executed cells and figure outputs. Compare your run with these values; timings depend on the machine.

Stage 3 of 5 · Controlled ExperimentsPredict before running & test boundary conditions

Trace a second patch and a second filter

Trace a second patch and a second filter · Python experiment
# Experiment — Trace a second patch and a second filter
patch=np.asarray(test_x[0,2:5,3:6,:])
# Transfer the device result to the host for an independent NumPy comparison.
weights=np.asarray(model.conv.kernel[...])[:,:,:,2]
expected=np.sum(patch*weights)+np.asarray(model.conv.bias[...])[2]
np.testing.assert_allclose(model.conv(test_x[:1])[0,2,3,2],expected,rtol=1e-5,atol=1e-6)

Expected: The output uses rows 22–44 and columns 33–55 of the input.

The same learned filter is reused at each valid location; changing the output coordinate changes the patch, not the filter.

Predict the chance classifier

Predict the chance classifier · Python experiment
# Experiment — Predict the chance classifier: A majority or constant-label baseline is part of evaluation.
always_zero=np.zeros_like(labels)
baseline=np.mean(always_zero==labels)
assert baseline==.5
baseline_confusion=np.zeros((2,2),dtype=int)
np.add.at(baseline_confusion,(labels,always_zero),1)
# Transfer the device result to the host for an independent NumPy comparison.
np.testing.assert_array_equal(baseline_confusion,np.array([[12,0],[12,0]]))

Expected: Accuracy is 0.50.5 with confusion [[12,0][[12,0],[12,0]][12,0]].

A majority or constant-label baseline is part of evaluation. A good-looking class-specific count alone can hide a useless predictor.

Stage 4 of 5 · Hands-On Exercise & CheckpointModify the code yourself, solve transfer tasks & test your understanding
Hands-On Exercise · Modify & Verify Run in the declared lesson environment

Make it yours

Where to put your code: Modify your own main.py, or use the Make it yours cell in your downloaded notebook. Run your attempt in the declared environment and verify your prediction before opening the reference solution.

Apply the trained model to one 10×1010\times10 image. Predict the intermediate activation shape and total parameter count before executing, then verify both assertions.

Show a writing guide and starter scaffold

How to write this exercise code

Learner-only inputs, outputs, checks and hints

Inputs

  • The trained convolutional classifier and one image with shape (1, 10, 10, channels).

Outputs

  • The intermediate activation shape for that image, the trainable parameter count, and the prediction shape.

Invariants to check

  • The observed activation dimensions follow the convolution and pooling sequence; parameter count is independent of input batch size.

Hints

  • Trace spatial dimensions through each convolution and pooling operation.
  • Count model parameter leaves once; do not count activations as parameters.
Your attempt · complete the TODOs
import jax
import jax.numpy as jnp
from flax import nnx

def inspect_single_image(model, image):
    # TODO: Run one image, capture the requested intermediate shape, and count trainable parameter scalars.
    raise NotImplementedError
Reveal the reference solution
Reference solution
# Exercise solution: Apply the trained model to one 10\times10 image.
larger=jnp.zeros((1,10,10,1))
larger=larger.at[0,4,:,0].set(1.)
# Check tensor shape invariant: `model.conv(larger).shape==(1,8,8,4)`
assert model.conv(larger).shape==(1,8,8,4)
# Check tensor shape invariant: `model(larger).shape==(1,2)`
assert model(larger).shape==(1,2)
assert sum(a.size for a in jax.tree.leaves(nnx.state(model,nnx.Param)))==50

Read a confusion matrix as conditional rates

Transfer

Normalize each confusion-matrix row by its true-class count. Explain the diagonal and verify that weighting class recalls by class counts reproduces overall accuracy. Then remove a class in a constructed case and report its recall as undefined.

Show a writing guide and starter scaffold

How to write this practice solution

Learner-only inputs, outputs, checks and hints

Inputs

  • Normalize each confusion-matrix row by its true-class count. Explain the diagonal and verify that weighting class recalls by class counts reproduces overall accuracy. Then remove a class in a constructed case and report its recall as undefined.

Outputs

  • Per-class recalls, undefined absent-class recall, and reconstructed accuracy.

Invariants to check

  • Rows are conditioned on true labels; classes with zero support are not assigned a numeric recall.

Hints

  • Keep the integer counts; a normalized row without a count hides how much evidence supports it.
Your attempt · complete the TODOs
import jax.numpy as jnp

def practice_read_a_confusion_matrix_as_conditional_rates(confusion, class_counts):
    """Implement the read a confusion matrix as conditional rates check."""
    # TODO: Divide each true-class row by its support, mask zero-support recall, and reconstruct accuracy by support weighting.
    raise NotImplementedError
Show a hint

Keep the integer counts; a normalized row without a count hides how much evidence supports it.

Reveal reference solution and reasoning
Read a confusion matrix as conditional rates · Python solution
# Read a confusion matrix as conditional rates (Transfer): Each row asks where examples of one true class went.
# Reduce along axis=1 to compute `counts`.
counts=confusion.sum(axis=1)
rates=np.divide(confusion,counts[:,None],out=np.full(confusion.shape,np.nan),where=counts[:,None]!=0)
np.testing.assert_allclose(rates.sum(axis=1),np.ones(2))
np.testing.assert_allclose((np.diag(rates)*counts).sum()/counts.sum(),accuracy)
missing=np.array([[3,1],[0,0]])
# Reduce along axis=1 to compute `n`.
n=missing.sum(axis=1)
missing_rates=np.divide(missing,n[:,None],out=np.full(missing.shape,np.nan),where=n[:,None]!=0)
# Ensure all array elements remain finite: `np.isnan(missing_rates[1]).all()`
assert np.isnan(missing_rates[1]).all()
print('True-class counts:',counts,'row-normalized confusion:',rates)
print('Absent class recall: undefined, not zero.')

Each row asks where examples of one true class went. A missing class has no observed recall; assigning zero confuses missing evidence with demonstrated failure.

Catch a channel-order mistake

Intermediate

Transpose NHWC images to NCHW and show why this convolution rejects them. Repair the layout and compare predictions.

Show a writing guide and starter scaffold

How to write this practice solution

Learner-only inputs, outputs, checks and hints

Inputs

  • Transpose NHWC images to NCHW and show why this convolution rejects them. Repair the layout and compare predictions.

Outputs

  • The failure case and repaired NHWC output/prediction comparison.

Invariants to check

  • The input batch follows the convolution module’s declared spatial/channel axis order.

Hints

  • The final axis is the input-channel dimension for NNX Conv.
Your attempt · complete the TODOs
import jax.numpy as jnp

def practice_catch_a_channel_order_mistake(model, images):
    """Implement the catch a channel-order mistake check."""
    # TODO: Transpose the declared NHWC batch to NCHW to reproduce the shape rejection, then restore NHWC and compare predictions.
    raise NotImplementedError
Show a hint

The final axis is the input-channel dimension for NNX Conv.

Reveal reference solution and reasoning
Catch a channel-order mistake · Python solution
# Catch a channel-order mistake (Intermediate)
wrong=jnp.transpose(test_x[:2],(0,3,1,2))
caught=False
try:model(wrong)
except (ValueError,TypeError):caught=True
assert caught
# Rearrange tensor axes to match the required layout for `repaired`.
repaired=jnp.transpose(wrong,(0,2,3,1))
np.testing.assert_allclose(model(repaired),model(test_x[:2]),rtol=1e-6)

The mistaken array has eight channels where the layer expects one. Explicit layout conversion is the repair; changing the declared channel count would silently change the model task.

Check your understanding

What does spatial mean pooling discard in this classifier?

Choose one answer

Diagnose the result

Before tuning, inspect sample labels and input layout. Validate a convolution patch numerically, inspect prediction counts, and compare the constant baseline. If all predictions choose one class, examine class balance, score ranges, and per-class errors. If held-out samples equal training samples, repair the split before reporting metrics.

Stage 5 of 5 · Keep Your EvidenceRecord your verification receipt & carry takeaways forward

Carry forward

  • Generate images with an explicit label rule
  • Derive the convolution output shape
  • Pool spatial responses and retain class scores
  • Read a confusion matrix rather than a success label

Keep your evidence

Download evidence template

Keep the 8-by-8-to-6-by-6 shape diagram, NumPy patch dot product, seeds 20/21, independent held-out cross-entropy, 24-observation confusion matrix, constant-class baseline, and layout failure repair.

Save your modified code, its output, and an explanation of your prediction. Checkpoint answers and exercise evidence are tracked separately.

Saved in this browser. Export or back up your portfolio anytime in My learning.

Save work for this lesson

Reference execution: CPU validation only. This worked reference is validated on CPU. Accelerator compatibility requires a separate source review and target execution receipt. Use the execution matrix to review device selection and data ownership before adapting a separate target program. Execution matrix and target qualification.

Primary references