#163Forward pass — arbitrary depthMedium

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:

a()=σ(W()a(1)+b()),a(0)=x\mathbf{a}^{(\ell)} = \sigma\bigl(W^{(\ell)}\mathbf{a}^{(\ell-1)} + \mathbf{b}^{(\ell)}\bigr), \qquad \mathbf{a}^{(0)} = \mathbf{x}

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. Each W has shape (n_out, n_in) and b has shape (n_out,); consecutive shapes are compatible.

Output

Returns an np.ndarray: the output of the last layer, σ\sigma 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: σ([0,0])=[0.5,0.5]\sigma([0,0])=[0.5,0.5], then σ(0.5+0.5)=σ(1)0.7311\sigma(0.5+0.5)=\sigma(1)\approx0.7311.

Example 2 — one layer

Input:  x = [1, 1], layers = [([[1, 2]], [-1])]
Output: [0.88079708]

Explanation: σ(11+211)=σ(2)0.8808\sigma(1\cdot1 + 2\cdot1 - 1) = \sigma(2)\approx0.8808.

Constraints

  • Start from a = x; for each (W, b) set a = 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