#163Forward pass — arbitrary depthMediumNeural Networks
Forward pass — arbitrary depth
Background
"Deep" just means stacking the same linear → σ block more times. Each layer's output is the next layer's input:
The whole forward pass is a loop over the layers.
Problem statement
Implement deep_forward(x, layers) that runs the input through every layer in order and returns the final activation.
Input
x— input vector.layers— list of(W, b)pairs, in order. EachWhas shape(n_out, n_in)andbhas shape(n_out,); consecutive shapes are compatible.
Output
Returns an np.ndarray: the output of the last layer, applied at every layer.
Examples
Example 1 — two layers
Input: x = [0, 0], layers = [([[1, 0], [0, 1]], [0, 0]), ([[1, 1]], [0])]
Output: [0.73105858]
Explanation: , then .
Example 2 — one layer
Input: x = [1, 1], layers = [([[1, 2]], [-1])]
Output: [0.88079708]
Explanation: .
Constraints
- Start from
a = x; for each(W, b)seta = sigmoid(W @ a + b). - Apply the sigmoid at every layer (including the last).
- Return the final activation as an
np.ndarray.
Notes
- This single loop is the entire forward pass for a network of any depth — the i→h→o net is just the two-layer case.
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: canonical two-layer net
- •Reference: single layer with bias
- •Sample: three all-zero layers hold one half