Skip to content
Neural networks · 1 of 5 · Python

Build a single neuron

A neuron is the building block of a neural network, and it does two things you already know. First it takes a weighted sum of its inputs and adds a bias, exactly like the linear model. Then it passes that one number through an activation function. With sigmoid as the activation, the output is a number between 0 and 1 that can be read as how strongly the neuron says yes.

Learn it

Words to know

neuron
The basic unit of a network: a weighted sum of its inputs plus a bias, passed through an activation function.
dot product
Multiply two lists of numbers position by position and add up the results. It gives one number.
sigmoid
The function 1 / (1 + e^(-x)). It squeezes any number into the range 0 to 1.

How it works

  1. Score: z = x[0] * w[0] + x[1] * w[1] + ... + b. This is the dot product of the inputs and the weights, plus the bias.
  2. Output: 1 / (1 + e^(-z)), the sigmoid of the score.
  3. A score of 0 gives exactly 0.5. Positive scores give more than 0.5, negative scores less.

Worked example

Input
x = [2, 1]
w = [1, -1]
b = 1

Output
0.880797
WhatCalculationValue
Weighted inputs2×1 and 1×(-1)2 and -1
Score z2 - 1 + 12
Output1 / (1 + e^-2)0.8808
output00.51-6-3036score zz = 0: 0.5z = 0: 0.5z = 2: 0.88z = 2: 0.88
The sigmoid of the score z. Far to the left the neuron says no, far to the right it says yes, and near 0 a small change in z moves the output most.

A score of 2 becomes 0.88: the neuron leans clearly toward yes. A score of -2 would give 0.12, the same distance on the other side of 0.5.

Why it matters

A layer is many neurons reading the same inputs, each with its own weights. The next two lessons work out how a neuron's weights should change when its answer is wrong.

Your turn

Compute the output of one neuron with a sigmoid activation.

x
the inputs, a list of numbers; may be empty
w
one weight per input
b
the bias
Returns
The sigmoid of dot(x, w) + b. The score stays between -50 and 50, so the plain sigmoid formula is safe.

Example 1

Input
x = [2, 1]
w = [1, -1]
b = 1

Output
0.880797

The score is 2 - 1 + 1 = 2, and the sigmoid of 2 is about 0.88.

Example 2

Input
x = [1, 2]
w = [2, -1]
b = 0

Output
0.5

The weighted inputs are 2 and -2. They cancel, the score is 0, and the sigmoid of 0 is 0.5.

Example 3

Input
x = [0]
w = [7]
b = 0

Output
0.5

An input of 0 contributes nothing, however large its weight.

Example 4

Input
x = []
w = []
b = 0

Output
0.5

With no inputs the score is the bias alone, here 0.

Hints

Each hint gives away a little more. Open one only when you need it.

  1. Hint 1: Where to start

    Do it in two separate lines: first the score, one number; then the sigmoid of that number.

  2. Hint 2: The key step

    The score is the same weighted sum as in the linear model lesson: pair inputs with weights, multiply, add, then add b.

  3. Hint 3: The outline

    Set z to b. For each pair from zip(x, w), add value * weight to z. Return 1 / (1 + math.exp(-z)).

Solution

From here on the page gives the method away. If you are still solving, go back to the hints.

Step by step

  1. The score: each input times its weight, plus the bias. Starting the total at b adds the bias once. With no inputs the loop does nothing and the score is the bias.
  2. Sigmoid squeezes the score into the range 0 to 1. The score can be any size. The activation turns it into an output that later layers, or a loss, can use.

The same numbered steps are the comments in the solution.

Python
import math


def solve(x, w, b):
    # 1. The score: each input times its weight, plus the bias
    z = b
    for value, weight in zip(x, w):
        z += value * weight
    # 2. Sigmoid squeezes the score into the range 0 to 1
    return 1 / (1 + math.exp(-z))

Watch out

  • Apply sigmoid once, to the finished score. Applying it to each input before adding gives a different and wrong result.
  • Do not forget the bias. A neuron without one always outputs 0.5 when its inputs are 0.

Check yourself

What are the two steps inside a neuron?

A weighted sum of the inputs plus a bias, then an activation function applied to that sum.

What does a neuron with a sigmoid output when its score is 0?

Exactly 0.5, the middle of the range.