#175Neuron — one unit, three activationsEasy

Neuron — one unit, three activations

Background

The lesson's one-picture summary: features → linear score z → activation f(z) → output. Change only the activation ff and the same neuron becomes a different model:

activationf(z)f(z)model
identityzzlinear regression
sigmoid1/(1+ez)1/(1+e^{-z})logistic regression
step1[z0]\mathbb{1}[z \ge 0]perceptron

Problem statement

Implement neuron(x, w, b, activation) that computes z=wx+bz = \mathbf{w}^\top\mathbf{x}+b and applies the named activation.

Input

  • x, w — array-like vectors of equal length.
  • bfloat.
  • activation — one of "identity", "sigmoid", "step".

Output

Returns a Python float: f(z)f(z).

Examples

Example 1

Input:  x = [1, 1], w = [1, 2], b = -1, activation = "identity"
Output: 2.0

Explanation: z=2z = 2; identity returns zz.

Example 2

Input:  x = [1, 1], w = [1, 2], b = -1, activation = "sigmoid"
Output: 0.8807970779...

Explanation: σ(2)0.8808\sigma(2) \approx 0.8808.

Constraints

  • Compute z once, then branch on activation.
  • "step" returns 1.0 when z0z \ge 0 else 0.0.
  • Return a plain float.

Notes

  • This is the heart of the "neuron" abstraction: one linear core, a pluggable nonlinearity. Stacking many such units with smooth activations is what makes deep networks trainable.
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 identity
  • reference sigmoid
  • sample step at zero