Linear forward (Wx + b)
Background
The linear (a.k.a. dense, fully-connected, or PyTorch's nn.Linear) layer is the bedrock of every feed-forward network: it applies a learned affine transform — a matrix multiply plus a bias — to each input vector in a batch. It is the very first building block of this series; you will combine it with non-linearities and a backward pass over the next problems until you are training a 2-layer MLP on XOR.
Problem statement
Implement linear_forward(x, W, b): the forward pass of a linear layer,
with , , and bias broadcast across the batch. The result is the pre-activation output.
Input
x—np.ndarrayof shape(B, in_features): a batch of input vectors.W—np.ndarrayof shape(in_features, out_features): the weight matrix.b—np.ndarrayof shape(out_features,): the bias vector.
Output
Returns an np.ndarray of shape (B, out_features).
Examples
Example 1 — bias broadcasts across the batch
Input: x = np.zeros((3, 4)), W = np.ones((4, 2)), b = [7.0, -2.0]
Output: [[ 7.0, -2.0],
[ 7.0, -2.0],
[ 7.0, -2.0]]
Explanation: with a zero input, x @ W is all zeros, so the output is just the bias repeated across all 3 rows — the (out,) bias broadcasts over the batch dimension with no reshaping.
Example 2 — identity weight returns the input
Input: x = [[1.0, 2.0, 3.0]], W = np.eye(3), b = np.zeros(3)
Output: [[1.0, 2.0, 3.0]]
Explanation: , so an identity weight matrix with zero bias passes the input straight through.
Constraints
- Use matrix multiply
@(not elementwise*), then add the bias. - The bias is shape
(out_features,)and broadcasts across the batch — no reshape needed. - Shapes thread as
(B, in) · (in, out) → (B, out); the batch dimension is preserved for anyB. - Tests compare against
x @ W + bwithatol=1e-7.
Notes
- Convention. This uses the row-vector convention (inputs are rows,
Wis(in, out)) — the same layout asnn.Linearoperating on a(batch, features)tensor. - Series. This is step 1 of build-nn;
build-nn-05-linear-backwardderives the gradients for this same layer once the forward pass is solid.
This problem ships 5 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.
- •Output shape is (B, out_features)
- •Matches x @ W + b reference
- •Bias broadcasts across the batch dimension
- •Single example (B=1) works
- •Larger batch shape is preserved