#188Network — the two-layer forward passMedium

Network — the two-layer forward pass

Background

The canonical ihoi \to h \to o network chains two dense+sigmoid layers. The hidden activation becomes the input to the output layer:

a1=σ(W1x+b1),a2=σ(W2a1+b2)\mathbf{a}_1 = \sigma(W_1\mathbf{x} + \mathbf{b}_1), \qquad \mathbf{a}_2 = \sigma(W_2\mathbf{a}_1 + \mathbf{b}_2)

That chaining — output of one layer feeding the next — is exactly what "stacking layers" means. The sigmoid between layers is essential: without it the two linear maps would collapse into one.

Problem statement

Implement forward(x, W1, b1, W2, b2) returning the network output a2\mathbf{a}_2.

Input

  • x — input vector, length i.
  • W1 — shape (h, i), b1 — length h (input → hidden).
  • W2 — shape (o, h), b2 — length o (hidden → output).

Output

Returns an np.ndarray of shape (o,): the prediction a2(0,1)o\mathbf{a}_2 \in (0,1)^o.

Examples

Example 1

Input:  x = [0, 0], W1 = [[1, 0], [0, 1]], b1 = [0, 0], W2 = [[1, 1]], b2 = [0]
Output: [0.73105858]

Explanation: a1=σ([0,0])=[0.5,0.5]\mathbf{a}_1 = \sigma([0,0]) = [0.5, 0.5]; z2=0.5+0.5=1z_2 = 0.5 + 0.5 = 1; σ(1)0.7311\sigma(1)\approx0.7311.

Example 2 — all-zero weights

Input:  x = [1, 2, 3], W1 = zeros(4×3), b1 = zeros(4), W2 = zeros(2×4), b2 = zeros(2)
Output: [0.5 0.5]

Explanation: every score is 0, so both layers output 0.5 regardless of the input.

Constraints

  • Apply layer 1 (W1 @ x + b1, then sigmoid), feed its output into layer 2, sigmoid again.
  • Return an np.ndarray of shape (o,).

Notes

  • Add another W, b pair and another sigmoid and you have a 3-layer net — the pattern repeats unchanged for arbitrary depth.
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: 2-2-1 net with identity W1
  • Reference: all-zero weights give 0.5 per output
  • Sample: 2-2-1 net with mixed weights