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
- 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. - Output:
1 / (1 + e^(-z)), the sigmoid of the score. - 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
| What | Calculation | Value |
|---|---|---|
| Weighted inputs | 2×1 and 1×(-1) | 2 and -1 |
| Score z | 2 - 1 + 1 | 2 |
| Output | 1 / (1 + e^-2) | 0.8808 |
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.
Hint 1: Where to start
Do it in two separate lines: first the score, one number; then the sigmoid of that number.
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.Hint 3: The outline
Set
ztob. For each pair fromzip(x, w), addvalue * weighttoz. Return1 / (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
- The score: each input times its weight, plus the bias. Starting the total at
badds the bias once. With no inputs the loop does nothing and the score is the bias. - 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.