Phase 05 / Neural network training
Build a tiny multilayer perceptron
Revision v0.18.1 · Updated since your last visit. Review the changes; your progress is saved.
Lesson · about 85 minutes · CPU practice
Implement a small model with explicit parameters before adding a model library.
Phase study guide: preparation, milestones and project evidence
Before you start
Before this: Math & optimization
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.
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements-cpu.txtpy -3 -m venv .venv
.\.venv\Scripts\python.exe -m pip install -r requirements-cpu.txtAfter activation, run the complete worked example from that same folder:
python phases/05-networks/01-build-a-tiny-multilayer-perceptron/code/main.py.\.venv\Scripts\python.exe phases/05-networks/01-build-a-tiny-multilayer-perceptron/code/main.pyFor 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.
Download this lesson’s notebook. Open it with a Python kernel using the course environment. Run cells from the top after restarting the kernel; experiments depend on earlier definitions.
For local Jupyter, first install the course requirements using the Local Python steps, then use your environment’s Python to start Jupyter:
python -m pip install jupyterlab
python -m jupyter lab.\.venv\Scripts\python.exe -m pip install jupyterlab
.\.venv\Scripts\python.exe -m jupyter labSelect the downloaded networks-01.ipynb file in JupyterLab. The notebook contains reference solutions; make your prediction and attempt each practice task before opening them. Restart the kernel before changing logical device setup.
The worked reference for this lesson is CPU-validated. Some references explicitly configure logical CPU devices; setting JAX_PLATFORMS does not adapt them to a TPU. Follow the execution matrix and reviewed multi-host example before attempting a target run.
For accelerator preparation, study TPU setup, observed runtime receipts, checkpointed jobs and precision and retained traces. Current receipts cover bounded v5e1 launch, resume, and precision checks; four single-device Pallas cases; and a four-device v5e collective fixture. Each receipt is limited to its recorded source, environment, cases, and hardware. It does not qualify all lessons or TPU configurations. Cloud provisioning is optional for CPU learning.
Check: Loss falls below and nearby held-out accuracy is at least . Exact initialization and losses may vary across backend/version.
Revisit the guided CPU setup · Open 19: TPU Setup. Package instructions are CPU instructions; Windows and accelerator environments need their own validation.
What you will be able to do
- Trace the shapes from observations to logits
- Explain why the hidden activation matters
- Use stable binary cross-entropy
- Separate learning from held-out measurement
The problem
Four points can give us a useful puzzle: how do we separate the two diagonal pairs in XOR? A single straight line cannot do it. We’ll add a small hidden layer and a nonlinear activation, check the shapes, and train a classifier. Then we’ll test nearby points to see what it has learned beyond the four training examples.
The idea
A multilayer perceptron combines learned linear maps with nonlinear activations. The hidden layer builds features that the output layer can combine. The activation lets the composition represent boundaries that a single linear map cannot.
Why hidden layers need nonlinear operations
The four training points alternate labels around the square: and are class one, while the other corners are class zero. A straight boundary puts each class on one side, so it cannot separate both diagonals. The trained field later shows two separated class-one regions, one around each triangle.
If you remove tanh, the two affine maps combine into one: . Increasing width then adds parameters but still leaves a straight decision boundary. Keep the activation when you want the hidden layer to build nonlinear features.
The four corner labels are the only observed labels in the training set. The grid between them is the model's extension of that pattern, so use held-out points to check nearby behavior instead of treating every colored pixel as evidence.
Trace the shapes from observations to logits
Follow one batch row through the actual matrix shapes. The input has two features, so multiplied by of shape gives . The bias is added to each row, and tanh keeps the same shape. Multiplying by of shape gives ; squeezing only the last axis yields one score per example, .
For a single example, the result is still , not a scalar and not an eight-example batch. A positive score favors class one because sigmoid maps it above (0.5). Keep labels in the matching shape so each score is paired with one target.
(B, 2) → affine → (B, 8) → tanh → (B, 8) → affine → (B, 1) → (B,)Explain why the hidden activation matters
The activation is the operation that stops the two affine layers from collapsing into one line. With tanh, each of the eight hidden coordinates can respond differently to an input; the output combines those responses to form the two separated XOR regions. The width-three NNX model in the next lesson changes the number of features, but not this role of a nonlinearity.
This run used 33 trainable values and ended at training loss (0.0015879), with accuracy (1.0) on 32 nearby points from held-out seed 12. Those measurements show that this initialization fit this small task. They do not establish that width eight is optimal or that every random initialization will converge.
Use stable binary cross-entropy
For score and binary label , the per-example loss is . At , either label gives , which the zero-parameter check confirms. The script also computes the same mean with NumPy, independently of JAX autodiff and Optax.
The score vector and label vector both have shape , so the loss has one value per example before the mean. If labels instead have shape , broadcasting against scores of shape creates : every score is paired with every label. Inspect the unreduced shape before averaging, because a plausible scalar can hide this changed objective. Repeating every row three times leaves the mean loss and gradient unchanged.
Separate learning from held-out measurement
The optimizer sees only the four exact corners for 200 updates. Afterward, the script creates 32 perturbed points with NumPy seed 12 and labels them from the original sign rule. The run reports final training loss (0.0015879) and held-out accuracy (1.0); the held-out points are near the corners, so this tests local transfer on the same simple rule.
Keep the seed, width, rate, update count, and split together when recording the result. Use held-out data for a declared evaluation, not repeated tuning. Neither the four-point fit nor nearby accuracy tests arbitrary points across the plane or a real population.
Read the classification loss one example at a time
A score (z) is not a probability. For label (y=1), positive (z) reduces loss; for (y=0), it increases loss. At (z=0), sigmoid gives (0.5) and both labels have loss .
logaddexp(0, z) computes the softplus term without first rounding a sigmoid to zero or one. That stable per-example value is then averaged across examples; it is the same quantity the NumPy reference checks.
A tiny MLP separates the XOR regions
Predict before looking: Can a single straight boundary separate these labels?
Read the figure
The horizontal axis is feature 0 and the vertical axis is feature 1; color is predicted probability of class one, from pale near 0 to dark near 1. The four markers are the only training observations: triangles at opposite-sign corners are label 1, and circles at same-sign corners are label 0. In the retained run the corresponding class-one corners sit in dark regions and class-zero corners in pale regions.
Connect it to the computation
The alternating corners cannot be divided by one straight line, while this learned field bends into two class-one regions. Use the (0.5) color transition to inspect where the model changes its decision, then compare predictions at the separately generated nearby points. Grid colors are model queries, not new labeled examples or a calibration measurement.
Figure data and execution details
Open full-size figureDownload data and provenance
The notebook contains the data experiment, plotting code, and recorded figure output. Rerun it to test your changes.
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 Define the fixture and parameter tree
Create main.py and copy this block. Before running, write the four XOR labels and the expected shape of each parameter.
# Step 1 — 1. Define the fixture and parameter tree: The parameter count is 16+8+8+1=33.
import jax
import jax.numpy as jnp
import numpy as np
import optax
x = jnp.array([[-1.,-1.],[-1.,1.],[1.,-1.],[1.,1.]])
y = jnp.array([0.,1.,1.,0.])
# Create or split explicit PRNG key(s) (`(k1, k2)`) for reproducible randomness.
k1, k2 = jax.random.split(jax.random.key(0))
# Sample deterministic random values into `params` using an explicit PRNG key.
params = {'w1':jax.random.normal(k1,(2,8))*.4, 'b1':jnp.zeros(8),
'w2':jax.random.normal(k2,(8,1))*.4, 'b2':jnp.zeros(1)}
assert sum(a.size for a in jax.tree.leaves(params)) == 33The parameter count is . Independent keys initialize the two layers; biases start at zero.
Explain the APIs in this step
jnp.array(values, dtype=...): Constructs a JAX array from Python/NumPy values.jax.random.split(key, num=...): Derives new keys for independent random operations; using the same key again repeats its random stream.jax.random.key(seed): Creates a typed explicit PRNG key from a seed.jax.random.normal(key, shape, dtype=...): Draws normal random values using an explicit PRNG key.
2 Write prediction and verify the loss
Append these functions. The NumPy reference has no autodiff or optimizer and tests the objective itself.
# Step 2 — 2. Write prediction and verify the loss: The zero-score baseline is about 0.693147.
def logits(p, batch):
return (jnp.tanh(batch@p['w1']+p['b1'])@p['w2']+p['b2']).squeeze(-1)
def loss(p, batch, labels):
return jnp.mean(optax.sigmoid_binary_cross_entropy(logits(p,batch),labels))
# Transfer the device result to the host for an independent NumPy comparison.
scores = np.asarray(logits(params,x))
expected = np.mean(np.logaddexp(0.,scores)-np.asarray(y)*scores)
np.testing.assert_allclose(loss(params,x,y), expected, rtol=1e-6)
zeros = jax.tree.map(jnp.zeros_like, params)
np.testing.assert_allclose(loss(zeros,x,y),np.log(2.),rtol=1e-6)
# Check tensor shape invariant: `logits(params,jnp.ones((1,2))).shape == (1,)`
assert logits(params,jnp.ones((1,2))).shape == (1,)The zero-score baseline is about . A loss below it alone does not prove held-out skill.
Explain the APIs in this step
jnp.mean(x, axis=..., keepdims=...): Averages JAX array values along the selected axis.optax.sigmoid_binary_cross_entropy(logits, labels): Computes elementwise binary cross entropy from logits and labels; output has the same shape and needs an explicit reduction.np.testing.assert_allclose(actual, desired, rtol=..., atol=...): Checks a host-side numerical expectation and raisesAssertionErrorwhen values differ beyond tolerance.jax.tree.map(fn, tree, ...): Applies a function leaf-by-leaf across matching PyTrees.
3 Train and evaluate untouched points
Append the compiled step and loop. Prediction: can the nonlinear model beat chance on points near each training corner?
# Step 3 — 3. Train and evaluate untouched points: The independent held-out seed changes input coordinates.
tx = optax.adam(.03)
state = tx.init(params)
# Define and JIT-compile `step(p, s)` so XLA traces and fuses the operations:
@jax.jit
def step(p,s):
value,grads = jax.value_and_grad(loss)(p,x,y)
updates,s = tx.update(grads,s,p)
return optax.apply_updates(p,updates),s,value
initial = float(loss(params,x,y))
for _ in range(200): params,state,_ = step(params,state)
rng = np.random.default_rng(12)
# Transfer the device result to the host for an independent NumPy comparison.
held_x = np.repeat(np.asarray(x),8,axis=0)+rng.normal(0,.12,(32,2))
held_y = (held_x[:,0]*held_x[:,1]<0).astype(np.float32)
held_scores = np.asarray(logits(params,jnp.array(held_x)))
accuracy = np.mean((held_scores>0)==held_y)
final = float(loss(params,x,y))
assert final < .03 and accuracy >= .95
print('Training loss:', initial, '->', final, 'held-out accuracy:', accuracy)The independent held-out seed changes input coordinates. Passing this check establishes behavior on this fixture only.
Explain the APIs in this step
optax.adam(learning_rate, ...): Creates Adam’s gradient transformation; call itsupdateand apply updates to the parameters.jax.jit(fn) / @jax.jit: Traces a function for its input signatures and compiles it; changed shapes, dtypes, or static arguments can trigger another compilation.jax.value_and_grad(loss_fn)(params, ...): Transforms a pure function to return its value and gradient(value, grads)from the same evaluation.jnp.array(values, dtype=...): Constructs a JAX array from Python/NumPy values.
View the cumulative build in main.py
These blocks append in order to your separate practice file.
# Step 1 — 1. Define the fixture and parameter tree: The parameter count is 16+8+8+1=33.
import jax
import jax.numpy as jnp
import numpy as np
import optax
x = jnp.array([[-1.,-1.],[-1.,1.],[1.,-1.],[1.,1.]])
y = jnp.array([0.,1.,1.,0.])
# Create or split explicit PRNG key(s) (`(k1, k2)`) for reproducible randomness.
k1, k2 = jax.random.split(jax.random.key(0))
# Sample deterministic random values into `params` using an explicit PRNG key.
params = {'w1':jax.random.normal(k1,(2,8))*.4, 'b1':jnp.zeros(8),
'w2':jax.random.normal(k2,(8,1))*.4, 'b2':jnp.zeros(1)}
assert sum(a.size for a in jax.tree.leaves(params)) == 33
# Step 2 — 2. Write prediction and verify the loss: The zero-score baseline is about 0.693147.
def logits(p, batch):
return (jnp.tanh(batch@p['w1']+p['b1'])@p['w2']+p['b2']).squeeze(-1)
def loss(p, batch, labels):
return jnp.mean(optax.sigmoid_binary_cross_entropy(logits(p,batch),labels))
# Transfer the device result to the host for an independent NumPy comparison.
scores = np.asarray(logits(params,x))
expected = np.mean(np.logaddexp(0.,scores)-np.asarray(y)*scores)
np.testing.assert_allclose(loss(params,x,y), expected, rtol=1e-6)
zeros = jax.tree.map(jnp.zeros_like, params)
np.testing.assert_allclose(loss(zeros,x,y),np.log(2.),rtol=1e-6)
# Check tensor shape invariant: `logits(params,jnp.ones((1,2))).shape == (1,)`
assert logits(params,jnp.ones((1,2))).shape == (1,)
# Step 3 — 3. Train and evaluate untouched points: The independent held-out seed changes input coordinates.
tx = optax.adam(.03)
state = tx.init(params)
# Define and JIT-compile `step(p, s)` so XLA traces and fuses the operations:
@jax.jit
def step(p,s):
value,grads = jax.value_and_grad(loss)(p,x,y)
updates,s = tx.update(grads,s,p)
return optax.apply_updates(p,updates),s,value
initial = float(loss(params,x,y))
for _ in range(200): params,state,_ = step(params,state)
rng = np.random.default_rng(12)
# Transfer the device result to the host for an independent NumPy comparison.
held_x = np.repeat(np.asarray(x),8,axis=0)+rng.normal(0,.12,(32,2))
held_y = (held_x[:,0]*held_x[:,1]<0).astype(np.float32)
held_scores = np.asarray(logits(params,jnp.array(held_x)))
accuracy = np.mean((held_scores>0)==held_y)
final = float(loss(params,x,y))
assert final < .03 and accuracy >= .95
print('Training loss:', initial, '->', final, 'held-out accuracy:', accuracy)Run the example
python3 phases/05-networks/01-build-a-tiny-multilayer-perceptron/code/main.pyAbout 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
# Step 1 — 1. Define the fixture and parameter tree: The parameter count is 16+8+8+1=33.
import jax
import jax.numpy as jnp
import numpy as np
import optax
x = jnp.array([[-1.,-1.],[-1.,1.],[1.,-1.],[1.,1.]])
y = jnp.array([0.,1.,1.,0.])
# Create or split explicit PRNG key(s) (`(k1, k2)`) for reproducible randomness.
k1, k2 = jax.random.split(jax.random.key(0))
# Sample deterministic random values into `params` using an explicit PRNG key.
params = {'w1':jax.random.normal(k1,(2,8))*.4, 'b1':jnp.zeros(8),
'w2':jax.random.normal(k2,(8,1))*.4, 'b2':jnp.zeros(1)}
assert sum(a.size for a in jax.tree.leaves(params)) == 33
# Step 2 — 2. Write prediction and verify the loss: The zero-score baseline is about 0.693147.
def logits(p, batch):
return (jnp.tanh(batch@p['w1']+p['b1'])@p['w2']+p['b2']).squeeze(-1)
def loss(p, batch, labels):
return jnp.mean(optax.sigmoid_binary_cross_entropy(logits(p,batch),labels))
# Transfer the device result to the host for an independent NumPy comparison.
scores = np.asarray(logits(params,x))
expected = np.mean(np.logaddexp(0.,scores)-np.asarray(y)*scores)
np.testing.assert_allclose(loss(params,x,y), expected, rtol=1e-6)
zeros = jax.tree.map(jnp.zeros_like, params)
np.testing.assert_allclose(loss(zeros,x,y),np.log(2.),rtol=1e-6)
# Check tensor shape invariant: `logits(params,jnp.ones((1,2))).shape == (1,)`
assert logits(params,jnp.ones((1,2))).shape == (1,)
# Step 3 — 3. Train and evaluate untouched points: The independent held-out seed changes input coordinates.
tx = optax.adam(.03)
state = tx.init(params)
# Define and JIT-compile `step(p, s)` so XLA traces and fuses the operations:
@jax.jit
def step(p,s):
value,grads = jax.value_and_grad(loss)(p,x,y)
updates,s = tx.update(grads,s,p)
return optax.apply_updates(p,updates),s,value
initial = float(loss(params,x,y))
for _ in range(200): params,state,_ = step(params,state)
rng = np.random.default_rng(12)
# Transfer the device result to the host for an independent NumPy comparison.
held_x = np.repeat(np.asarray(x),8,axis=0)+rng.normal(0,.12,(32,2))
held_y = (held_x[:,0]*held_x[:,1]<0).astype(np.float32)
held_scores = np.asarray(logits(params,jnp.array(held_x)))
accuracy = np.mean((held_scores>0)==held_y)
final = float(loss(params,x,y))
assert final < .03 and accuracy >= .95
print('Training loss:', initial, '->', final, 'held-out accuracy:', accuracy)Expected result: Loss falls below and nearby held-out accuracy is at least . Exact initialization and losses may vary across backend/version.
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.
Training loss: 0.7047799825668335 -> 0.0015878621488809586 held-out accuracy: 1.0
Training loss: 0.7047799825668335 -> 0.0015878621488809586 held-out accuracy: 1.0
Consistent hidden permutation preserves logits; one-sided permutation changes them.
PASS: networks-01
Download execution record · The notebook includes executed cells and figure outputs. Compare your run with these values; timings depend on the machine.
Duplication preserves a mean objective
# Experiment — Duplication preserves a mean objective: Averaging normalizes repeated identical data.
repeated_x=jnp.repeat(x,3,axis=0)
repeated_y=jnp.repeat(y,3)
np.testing.assert_allclose(loss(params,repeated_x,repeated_y),loss(params,x,y),rtol=1e-5)
original_g=jax.grad(loss)(params,x,y)
repeated_g=jax.grad(loss)(params,repeated_x,repeated_y)
for a,b in zip(jax.tree.leaves(original_g),jax.tree.leaves(repeated_g)):
np.testing.assert_allclose(a,b,rtol=1e-4,atol=1e-7)Expected: The losses and corresponding gradient leaves agree within tolerance.
Averaging normalizes repeated identical data. Summing would change the learning-rate interpretation.
An affine stack stays affine
# Experiment — An affine stack stays affine
def affine_stack(batch):return (batch@params['w1']+params['b1'])@params['w2']+params['b2']
midpoint=jnp.array([[.2,-.3]])
delta=jnp.array([[.4,.1]])
np.testing.assert_allclose(affine_stack(midpoint),.5*(affine_stack(midpoint+delta)+affine_stack(midpoint-delta)),atol=1e-6)Expected: The midpoint identity holds for the affine stack.
This exact affine identity explains why removing the nonlinearity limits the class of functions, regardless of width.
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.
Swap class zero and one. Transform only the trained output layer to preserve the boundary and verify predictions on new coordinates.
Show a writing guide and starter scaffold
How to write this exercise code
Learner-only inputs, outputs, checks and hints
Inputs
- An MLP parameter pytree and a permutation of its output classes.
Outputs
- Transformed output-layer parameters and logits on held-out coordinates.
Invariants to check
- The transformed model preserves or intentionally permutes class scores as specified; hidden-unit changes must be paired.
Hints
- Inspect which parameter axis represents classes.
- Test a new coordinate batch, not only the training points.
import jax.numpy as jnp
def permute_output_classes(params, class_permutation):
"""Transformed output-layer parameters and logits on held-out coordinates."""
# TODO: Transform the output-layer class axis and verify logits on new input coordinates.
raise NotImplementedErrorReveal the reference solution
# Exercise solution: Swap class zero and one.
complemented=dict(params,w2=-params['w2'],b2=-params['b2'])
changed_x=jnp.array([[-.8,.7],[.7,.9],[-1.1,-.8]])
np.testing.assert_allclose(logits(complemented,changed_x),-logits(params,changed_x),atol=1e-6)
# Transfer the device result to the host for an independent NumPy comparison.
expected_labels=(np.asarray(changed_x)[:,0]*np.asarray(changed_x)[:,1]>0)
assert np.array_equal(np.asarray(logits(complemented,changed_x)>0),expected_labels)Reorder hidden units without changing predictions
Transfer
Permute the hidden units and make the matching change to the output layer. Predict whether the logits change. Test the result on new coordinates, then show why permuting only one side breaks the contract.
Show a writing guide and starter scaffold
How to write this practice solution
Learner-only inputs, outputs, checks and hints
Inputs
- Permute the hidden units and make the matching change to the output layer. Predict whether the logits change. Test the result on new coordinates, then show why permuting only one side breaks the contract.
Outputs
- Logit parity on new inputs plus a one-sided-permutation counterexample.
Invariants to check
- Incoming and outgoing parameter axes use the same hidden-unit permutation.
Hints
- Columns of the first kernel and rows of the second kernel describe the same hidden units.
import jax.numpy as jnp
def practice_reorder_hidden_units_without_changing_predictions(hidden_permutation, hidden_weights, output_weights):
"""Implement the reorder hidden units without changing predictions check."""
# TODO: Permute the hidden-unit axis and apply the matching inverse permutation to outgoing weights and biases.
raise NotImplementedErrorShow a hint
Columns of the first kernel and rows of the second kernel describe the same hidden units.
Reveal reference solution and reasoning
# Reorder hidden units without changing predictions (Transfer): A hidden unit has no intrinsic index.
order=jnp.array([7,0,6,1,5,2,4,3])
probe=jnp.array([[.2,-.9],[-.4,.6],[.8,.1]])
permuted=dict(params,w1=params['w1'][:,order],b1=params['b1'][order],w2=params['w2'][order,:])
np.testing.assert_allclose(logits(permuted,probe),logits(params,probe),rtol=1e-5,atol=1e-5)
broken=dict(params,w1=params['w1'][:,order],b1=params['b1'][order])
assert not np.allclose(logits(broken,probe),logits(params,probe),atol=1e-5)
print('Consistent hidden permutation preserves logits; one-sided permutation changes them.')A hidden unit has no intrinsic index. Reordering its incoming weights, bias and outgoing weights together preserves the function. This is a parameter-layout invariant, not another training run.
Diagnose accidental pairwise broadcasting
Intermediate
Demonstrate how labels shaped and scores shaped create a loss. Repair the contract before averaging.
Show a writing guide and starter scaffold
How to write this practice solution
Learner-only inputs, outputs, checks and hints
Inputs
- Demonstrate how labels shaped and scores shaped create a loss. Repair the contract before averaging.
Outputs
- Rejected mismatched shape and a per-example loss after alignment.
Invariants to check
- One score is paired with one label; the reduction is over examples only.
Hints
- Inspect the unreduced loss shape; a scalar mean can hide the error.
import jax.numpy as jnp
def practice_diagnose_accidental_pairwise_broadcasting(scores, labels):
"""Implement the diagnose accidental pairwise broadcasting check."""
# TODO: Assert score and label shapes match before subtraction, then show the (B,1) by (B,) broadcast shape and repair it.
raise NotImplementedErrorShow a hint
Inspect the unreduced loss shape; a scalar mean can hide the error.
Reveal reference solution and reasoning
# Diagnose accidental pairwise broadcasting (Intermediate)
bad=optax.sigmoid_binary_cross_entropy(logits(params,x),y[:,None])
# Check tensor shape invariant: `bad.shape==(4,4)`
assert bad.shape==(4,4)
good=optax.sigmoid_binary_cross_entropy(logits(params,x),y)
# Check tensor shape invariant: `good.shape==(4,)`
assert good.shape==(4,)
np.testing.assert_allclose(jnp.mean(good),loss(params,x,y),rtol=1e-6)The incorrect scalar still looks plausible, but it compares each score with every label. A shape check catches the silent objective change.
Diagnose the result
Inspect unreduced loss shapes before tuning learning rate. Compare the loss with NumPy, check finite gradients, and test . If fit succeeds but perturbed held-out points fail, investigate the learned boundary rather than claiming generalization from four exact predictions.
Carry forward
- Trace the shapes from observations to logits
- Explain why the hidden activation matters
- Use stable binary cross-entropy
- Separate learning from held-out measurement
Keep your evidence
Keep the 33-parameter shape calculation, independent NumPy cross-entropy, train and held-out seeds, initial/final loss, nearby held-out accuracy, and one repaired score/label shape failure.
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.
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.