#165Dense layer — sigmoid forward passEasy

Dense layer — sigmoid forward pass

Background

One dense layer's forward pass is a linear step followed by an activation. With the canonical sigmoid:

z=Wx+b,a=σ(z)\mathbf{z} = W\mathbf{x} + \mathbf{b}, \qquad \mathbf{a} = \sigma(\mathbf{z})

Here W has shape (n_out, n_in), so W @ x produces one score per output neuron, the bias shifts each, and the sigmoid squashes each into (0,1)(0, 1).

Problem statement

Implement layer_forward(x, W, b) returning σ(Wx+b)\sigma(W\mathbf{x} + \mathbf{b}).

Input

  • x — array-like input vector of length n_in.
  • W — array-like weight matrix of shape (n_out, n_in).
  • b — array-like bias vector of length n_out.

Output

Returns an np.ndarray of shape (n_out,): the layer's activation.

Examples

Example 1 — zero score is 0.5

Input:  x = [0, 0], W = [[1, 0], [0, 1]], b = [0, 0]
Output: [0.5 0.5]

Explanation: z=[0,0]\mathbf{z} = [0, 0], and σ(0)=0.5\sigma(0) = 0.5 per neuron.

Example 2

Input:  x = [1, 1], W = [[1, 2]], b = [-1]
Output: [0.88079708]

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

Constraints

  • Compute W @ x + b, then apply the sigmoid elementwise.
  • Return an np.ndarray of shape (n_out,).

Notes

  • This single layer is the building block of the whole stack — feeding its output into another layer_forward is exactly what "depth" means.
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: zero score gives one half per neuron
  • Reference: single output neuron with z equal to two
  • Sample: three-neuron layer output