Forward propagation and loss
Quick answer
Forward propagation maps inputs to predictions layer by layer. A loss measures prediction error against a target. For binary classification, work with logits (raw scores) in the loss and convert them to probabilities only when needed. Static examples verified locally with Python 3.13.5 and NumPy 2.3.5 where imported. ByteMind execution is unverified.
Forward trace through ReLU
import numpy as npX=np.array([[1,0],[0,1]])W1=np.array([[2,-1],[-1,2]])A=np.maximum(0,X@W1)z=A@np.array([[1],[-1]])print(A.tolist(), z.tolist())Expected output: [[2, 0], [0, 2]] [[2], [-2]]. The logits are real scores, not probabilities.
How it works
For a batch X, hidden preactivations Z1=X@W1+b1, hidden values A=tanh(Z1), logits Z2=A@W2+b2. If binary labels Y and logits are both (N,1), mean binary cross-entropy is mean(softplus(Z2)-YZ2). More stable at extreme logits: max(z,0)-yz+log1p(exp(-abs(z))). NumPy logaddexp(0,z) is softplus, but subtracting y*z can lose tiny positive terms for large positive logits; the max/log1p form avoids that cancellation. For a binary label 0 or 1, probability sigmoid(z) is monotone in z; probability 0.5 corresponds to z=0. For regression, mean squared error = sum((prediction-target)^2)/N for a scalar output per row. Use compatible shapes; (N,) and (N,1) can accidentally broadcast into (N,N). Loss reduction determines gradient scaling.
Stable binary cross-entropy
from math import exp,log1pdef loss(z,y): return max(z,0)-y*z+log1p(exp(-abs(z)))print([round(loss(z,y),4) for z,y in [(0,1),(1000,1),(-1000,1)]])Expected output: [0.6931, 0.0, 1000.0]. Confident wrong predictions incur a large finite loss without exponent overflow.
Common mistakes
Do not apply sigmoid twice to logits. Do not take log of rounded probabilities 0 or 1. Do not report training loss as held-out performance. Verify target shape before subtracting.
Practice and next steps
Trace two rows through a one-hidden-layer network and compare per-row loss with the mean. Inspect loss at logits +1000 and -1000.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Forward propagation and loss — knowledge check
Apply forward propagation and loss to small examples and debugging scenarios.
What is the binary probability at logit zero?
Why use the logits form of BCE?
What can happen when subtracting (N,) from (N,1)?
For squared errors 4 and 1, what is the mean?
For y=1 and z=-1000, what is stable BCE approximately?