#168Forward pass — cache the activationsEasyNeural Networks
Forward pass — cache the activations
Background
The forward pass through the canonical net computes two activations:
For inference you only need . But to train (Lesson 6, backprop) you must keep the hidden activation too — the backward pass reuses it. So a training-ready forward pass returns both.
Problem statement
Implement forward_cache(x, W1, b1, W2, b2) returning the tuple (a1, a2).
Input
x— input vector, lengthi.W1(h, i),b1(h,)— input → hidden.W2(o, h),b2(o,)— hidden → output.
Output
Returns (a1, a2): the hidden activation (np.ndarray shape (h,)) and the prediction (np.ndarray shape (o,)), both in .
Examples
Example 1
Input: x = [0, 0], W1 = [[1, 0], [0, 1]], b1 = [0, 0], W2 = [[1, 1]], b2 = [0]
Output: a1 = [0.5, 0.5], a2 = [0.73105858]
Explanation: ; ; .
Constraints
- Apply sigmoid after each linear step.
- Return
a1anda2in that order.
Notes
- Caching
a1(and the pre-activations) during the forward pass is what makes the backward pass cheap — it avoids recomputing them.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •example: identity layer, summed output
- •reference: hidden activation of zero pre-activations
- •sample: activations lie strictly in (0,1)