Forward pass, activations, losses, and initialization
A trustworthy forward pass makes shapes, activation semantics, initialization, and loss expectations explicit.
A nonlinear sensor classifier defeats every classical baseline.
This chapter moves from write every shape to match outputs to the loss. We will make the idea concrete in a small code example before placing it back inside a production system with a declared latency, cost, and operator-capacity budget.
Pass raw logits to stable combined losses, verify target encoding and reduction, and test that a deliberately better logit lowers loss.
Verify the behavior illustrated in forward_pass.py, then measure decision utility, slice reliability, and system latency.
An excessive learning rate causes NaNs; dead ReLUs and a BatchNorm train/eval mismatch hide the cause. An unsafe release must degrade to a named baseline or the last known-good version.
Build the idea one layer at a time.
A forward pass is a chain of contracts: tensor shapes enter an affine layer, activations transform its pre-activations, and a loss interprets the final output. Initialization decides whether useful signal survives that chain before learning even begins.
For a two-layer classifier, the computation is z1 = XW1 + b1, h = ReLU(z1), and logits = hW2 + b2. Logits are unrestricted scores. A combined logits loss applies the probability transform internally with stable arithmetic, so adding sigmoid or softmax beforehand both changes the objective and can lose numerical precision.
Weights that are too large cause activations or probabilities to saturate; weights that are too small make signals vanish. He initialization scales ReLU-layer weights by the inverse square root of fan-in. Validate shapes, finite outputs, activation variance, and a tiny-batch overfit before scaling training.
- ReLU/GELU
- cross-entropy
- chain rule
- initialization
Follow the reasoning before touching the code.
- 01
Write every shape
Annotate batch, input, hidden, and output dimensions so broadcasting cannot silently turn a wrong tensor into a plausible result.
- 02
Initialize for signal flow
Choose a variance rule compatible with the activation, set biases deliberately, and inspect pre-activation and activation distributions on real batches.
- 03
Match outputs to the loss
Pass raw logits to stable combined losses, verify target encoding and reduction, and test that a deliberately better logit lowers loss.
import numpy as np
rng = np.random.default_rng(3)
X = np.array([[1., -1.], [0.5, 2.]])
y = np.array([[1.], [0.]])
W1 = rng.normal(0, np.sqrt(2 / 2), size=(2, 4))
W2 = rng.normal(0, np.sqrt(2 / 4), size=(4, 1))
hidden = np.maximum(0, X @ W1)
logits = hidden @ W2
loss = np.mean(np.logaddexp(0, logits) - y * logits)
print(hidden.shape, logits.shape, round(float(loss), 4))Run a shape-explicit ReLU network with stable binary logits loss.
What to notice
- The [2, 2] batch becomes [2, 4] hidden activations and [2, 1] logits, matching one binary target per row.
- logaddexp implements the stable softplus term directly from logits, so no explicit sigmoid is required for training loss.
YOUR TURN Pause and predict before revealing the answer.
Set every W1 value to zero. What happens to hidden activations and to the gradient symmetry among those units?
Need a hint?
All hidden units receive the same input, output, and downstream signal.
Check your reasoning
Every hidden activation is identical (zero here), and units remain symmetric instead of learning distinct features; random weight initialization breaks that symmetry.
Lessons 1 in this course.
Connect the chapter to the course’s quantitative spine.
A trustworthy forward pass makes shapes, activation semantics, initialization, and loss expectations explicit. The backpropagated error below is the shared course-level lens that helps quantify the surrounding system. Name each symbol, check its units or shape, and use only values that were available at the production cutoff.
Backpropagated error
| Symbol | Meaning / shape / unit |
|---|---|
δ_l | loss gradient at layer l |
W | weight matrix |
φ′ | activation derivative |
Open derivation and numerical substitution
Start from the production quantity being optimized, substitute the observed values with their declared units, then isolate the model-controlled term. Preserve shape annotations at each step so broadcasting or aggregation cannot silently change the result.
- Write the named inputs: δ_l, W, φ′.
- Substitute one small, hand-checkable batch before vectorizing.
- Calculate an independent reference value and compare within a declared tolerance.
# equation → code contract
inputs = validate_shapes_and_units(batch)
value = compute_c09(inputs)
assert is_finite(value)Change one assumption and make the tradeoff visible.
Use the worked chapter as your prediction, then change one input in the lab and explain why the output moves. The lab runs predefined TypeScript and never executes learner code, so the focus stays on the concept rather than setup.
Optimizer stability lab
Sweep learning rate over a deterministic convex-plus-curvature loss.
Assumption: Twenty gradient steps from w=4 on L(w)=w²/2.
Open nonvisual data table
| Item | Computed state | Interpretation |
|---|---|---|
| step 1 | 3.5200 | 6.19520 |
| step 5 | 2.1109 | 2.22801 |
| step 10 | 1.1140 | 0.62050 |
| step 15 | 0.5879 | 0.17281 |
| step 20 | 0.3103 | 0.04813 |
Trace the complete operating path.
Carry the chapter’s central idea into the system: A trustworthy forward pass makes shapes, activation semantics, initialization, and loss expectations explicit. The path below shows where that reasoning needs an artifact, a test, an owner, or a fallback.
- 01
Turn “Write every shape” into a written invariant and a failing test.
- 02
Promote forward_pass.py from illustration to a versioned implementation with representative fixtures.
- 03
Use “Match outputs to the loss” as the release checkpoint: Pass raw logits to stable combined losses, verify target encoding and reduction, and test that a deliberately better logit lowers loss.
- 04
Serve or materialize behind explicit a declared latency, cost, and operator-capacity budget.
- 05
Join telemetry to mature outcomes and retain a rollback path.
Observability
Join service health, input quality, prediction distributions, slice behavior, and mature outcomes by exact version.
Cost
Measure storage, preprocessing, compute, queueing, and human review under a representative arrival pattern.
Failure modes
An excessive learning rate causes NaNs; dead ReLUs and a BatchNorm train/eval mismatch hide the cause. Add a detector, owner, mitigation, and stop condition for this class of failure.
Alternatives
Compare a rule, a simpler statistical baseline, and a different system boundary before adding model complexity.
Explain the contract, not just the vocabulary.
Network from first principles
Specify a NumPy MLP, finite-difference checks, PyTorch parity, calibrated inference, and debugging report.
- Forward/backward derivation
- Gradient check plan
- Optimizer/regularization ablations
- Export and batch-one parity
Read primary material with a purpose.
Return to the opening failure.
Package preprocessing, vocabularies, weights, calibration, output semantics, and batch-one parity as one artifact.
For this lesson, the release evidence is the worked code example, a hand-checked formal result, deterministic simulation output, a ≥80% checkpoint, the production rubric, and a named fallback. The course resolves when the system can produce a small network built from forward pass through backpropagation and optimization.