#188Network — the two-layer forward passMediumNeural Networks
Network — the two-layer forward pass
Background
The canonical network chains two dense+sigmoid layers. The hidden activation becomes the input to the output layer:
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 .
Input
x— input vector, lengthi.W1— shape(h, i),b1— lengthh(input → hidden).W2— shape(o, h),b2— lengtho(hidden → output).
Output
Returns an np.ndarray of shape (o,): the prediction .
Examples
Example 1
Input: x = [0, 0], W1 = [[1, 0], [0, 1]], b1 = [0, 0], W2 = [[1, 1]], b2 = [0]
Output: [0.73105858]
Explanation: ; ; .
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.ndarrayof shape(o,).
Notes
- Add another
W, bpair 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