Level 6 · BuilderLesson 6.1 36 min read 10 question quiz 11 marks of written practice

The Artificial Neuron: Weights, Bias and a Decision

From brain cells to maths

A biological neuron receives signals from other neurons through its dendrites. If the combined signal in the cell body is strong enough, the neuron fires, sending a pulse down its axon to the next neurons. The junctions, called synapses, have different strengths, and learning changes those strengths.

A biological neuron and an artificial neuronTwo panels. On the left a biological neuron: branching dendrites in blue receive signals, the cell body in pink combines them, and the axon in orange carries a pulse to synapses at its tips; the cell fires when the combined signal is strong enough. On the right an artificial neuron: three inputs x1, x2 and x3 in blue are joined by lines labelled with weights w1, w2 and w3 to a pink summing node, which also adds a bias b; the sum passes through an activation function f to give the output a equals f of w1 x1 plus w2 x2 plus w3 x3 plus b. Matching colours show dendrites as inputs, synapses as weights, the cell body as the sum and firing as the activation.The brain cell, and the maths it inspiredBiological neurondendritescell bodyaxonsynapsesfires when the combined signal is strong enoughArtificial neuronw₁x₁w₂x₂w₃x₃Σ+ bfoutputinputsweightssum + biasactivationa = f(w₁x₁ + w₂x₂ + w₃x₃ + b)
The artificial neuron keeps the idea, not the biology.

An artificial neuron keeps only that skeleton:

BiologicalArtificial
Signals arriving at the dendritesInputs x₁, x₂, … xₙ
Synapse strengthsWeights w₁, w₂, … wₙ
How easily the cell firesBias b
Adding up the signals in the cell bodyWeighted sum z = w₁x₁ + w₂x₂ + … + wₙxₙ + b
Firing or staying quietActivation function a = f(z)
Inspired by, not a copy of

Real neurons communicate with timed pulses, chemistry and thousands of connections each, and are far more complicated than this. The artificial neuron is a convenient mathematical unit that happens to have been inspired by biology.

Inside an artificial neuron

Inside an artificial neuronA neuron drawn left to right. Three input nodes hold the film's action 0.9, comedy 0.2 and length 2.5 hours. Lines join them to a glowing summing node, labelled with Sam's weights: plus 1.5 for action, giving plus 1.35; plus 0.8 for comedy, giving plus 0.16; and minus 0.6 for hours, giving minus 1.50. Positive weights are pink and negative blue, and thicker lines are larger weights. A bias of minus 0.5 joins from above. The sum z is -0.49. It passes through a small sigmoid curve to give an output of 0.38, a 38 percent chance: probably not.One neuron, worked through: will Sam enjoy this film?0.9actionw = +1.5x × w = +1.350.2comedyw = +0.8x × w = +0.162.5hoursw = −0.6x × w = −1.50bias −0.5Σz = −0.49sigmoid0.3838%probablynotz = 0.9 × 1.5 + 0.2 × 0.8 + 2.5 × (−0.6) − 0.5 = −0.49, σ(z) = 0.38
Multiply, add, squash. Every neuron in every network does exactly this.

A worked example: will Sam enjoy this film?

The inputs describe the film. The weights describe Sam's taste. The bias describes how keen Sam is on films in general.

InputValue xWeight ww × x
How much action (0 to 1)0.91.51.35
How much comedy (0 to 1)0.20.80.16
Length in hours2.5−0.6−1.50
Bias−0.50
Weighted sum z−0.49

Passing z through the sigmoid (Level 4) gives σ(−0.49) ≈ 0.38: a 38% chance Sam enjoys it. The action helps, but at two and a half hours it is just too long.

Read the weights

A positive weight means "more of this input makes a yes more likely", a negative weight means the opposite, and a large weight means the input matters a lot. Sam loves action (1.5), quite likes comedy (0.8) and dislikes long films (−0.6).

The weighted sum is a dot product (Level 3) of the input vector and the weight vector, plus the bias: z = w · x + b. That one line of maths, repeated billions of times, is most of what a neural network computes.

A neuron draws a line

With two inputs, the neuron says yes when w₁x₁ + w₂x₂ + b > 0 and no when it is below 0. The boundary, where w₁x₁ + w₂x₂ + b = 0, is a straight line.

How weights and bias move the boundaryA square map of two inputs, shaded from blue where the neuron says no to pink where it says yes, with the boundary line x1 plus 2 x2 minus 3 equals 0 drawn in black, where the output is 0.5. Two dashed teal lines parallel to it show the same weights with biases minus 1.5 and minus 4.5: the line slides without turning. A dotted orange line shows weights 2 and 1 with the same bias: the line turns. A violet arrow, the weight vector 1, 2, points at right angles to the boundary towards the yes side.A neuron's decision boundary: weights turn it, the bias slides itw = (1, 2)yes sideno sideinput x₁boundary: x₁ + 2x₂ − 3 = 0where z = 0 and the output is 0.5same weights, bias −1.5 and −4.5the line slides, its angle unchangedweights (2, 1), same biasthe line turnsthe weight vector wat right angles to the line, towards yes
One neuron, one straight line: that is all a single neuron can draw.
  • The weights set the angle of the line. The weight vector (w₁, w₂) points at right angles to the line, towards the "yes" side.
  • The bias slides the line without turning it.
  • With three inputs the boundary is a flat plane, and with more it is a hyperplane: a single neuron can only ever separate classes with something flat.

Neuron lab

Turn one neuron's weights and bias and watch its decision boundary turn and slide; then let the perceptron rule learn. This tool needs JavaScript, so turn it on to use it. Everything you need to answer the exam question is explained in the text above.

Move the sliders and watch the line turn and slide. Then press Train and the perceptron rule below moves them for you.

Neurons as logic gates

In 1943 McCulloch and Pitts showed that simple threshold units can compute logic. With a step activation (output 1 if z > 0, otherwise 0):

Gatew₁w₂bWhy it works
AND11−1.5Only 1 + 1 = 2 beats 1.5
OR11−0.5Any single 1 beats 0.5
NAND−1−11.5The opposite of AND
NOT (one input)−10.5Input 0 gives 0.5 > 0; input 1 gives −0.5
Logic gates as single neuronsThree panels, each showing the four input pairs 00, 01, 10 and 11 as points on a square, with the neuron's boundary line and its output-1 side shaded pink. AND, with weights 1 and 1 and bias minus 1.5: only 11 is on the pink side. OR, with weights 1 and 1 and bias minus 0.5: everything except 00 is on the pink side. NAND, with weights minus 1 and minus 1 and bias 1.5: everything except 11 is on the pink side. Filled pink points output 1 and hollow points output 0.Three logic gates, each a single neuron with a step activationANDw = (1, 1), b = -1.500011011pink: output 1 · hollow: output 0ORw = (1, 1), b = -0.500011011pink: output 1 · hollow: output 0NANDw = (-1, -1), b = 1.500011011pink: output 1 · hollow: output 0
Each gate is a straight line that puts the 1s on one side.

Because NAND alone can build any logic circuit, a network of neurons can in principle compute anything a digital computer can. The interesting question is not whether networks can compute, but whether they can learn the right weights.

The perceptron learning rule

Frank Rosenblatt's 1958 perceptron learned its weights from examples with a beautifully simple rule. For each training example:

  1. Predict: output = 1 if w · x + b > 0, otherwise 0.
  2. Error = target − output, which is +1, 0 or −1.
  3. Update every weight: wᵢ ← wᵢ + learning rate × error × xᵢ, and the bias: b ← b + learning rate × error.

If the prediction is right, the error is 0 and nothing changes. If it said 0 but should have said 1, each weight grows in proportion to its input, pushing z up next time. If it said 1 but should have said 0, the weights shrink.

The perceptron convergence theorem

If a straight line can separate the two classes perfectly, the perceptron rule is guaranteed to find one in a finite number of steps.

That guarantee is also the catch. If no straight line can separate the classes, the perceptron never settles.

The XOR problem

XOR (exclusive or) outputs 1 when exactly one input is 1.

x₁x₂XOR
000
011
101
110
The XOR problemTwo panels showing the four XOR points: 01 and 10 output 1 and are pink; 00 and 11 output 0 and are hollow blue, so the pink points lie on one diagonal and the hollow points on the other. In the first panel, three dashed attempts at a single straight line each leave a point on the wrong side. In the second panel, two parallel lines from two hidden neurons, an OR neuron at x1 plus x2 greater than 0.5 and an AND neuron at x1 plus x2 greater than 1.5, enclose a pink band containing exactly the two pink points: the output is OR and not AND.XOR: one line always fails, two lines succeedOne neuron: one straight lineevery line leaves a point on the wrong sideA hidden layer: two linesoutput = OR and not AND:the pink band between the linesORAND
Two hidden neurons, two lines: the smallest network that beats the 1969 objection.

The two "1" points sit on one diagonal and the two "0" points on the other. No single straight line can put both 1s on one side and both 0s on the other, so no single neuron can compute XOR. Minsky and Papert's 1969 book Perceptrons made this famous, and it helped trigger the first AI winter.

The fix was known in principle: use two neurons to draw two lines, and a third to combine them. A hidden layer. What was missing until 1986 was a way to train the hidden neurons. Level 7 builds that network and the algorithm that trains it.

Hands-on: build and train a neuron

A neuron in four lines

Python
import math

def neuron(inputs, weights, bias):
    z = sum(x * w for x, w in zip(inputs, weights)) + bias
    return 1 / (1 + math.exp(-z))             # sigmoid activation

film = [0.9, 0.2, 2.5]                        # action, comedy, hours
sam = [1.5, 0.8, -0.6]
print(f"Chance Sam enjoys it: {neuron(film, sam, -0.5):.2f}")
print(f"The same film cut to 1.5 hours: {neuron([0.9, 0.2, 1.5], sam, -0.5):.2f}")

Logic gates

Python
def step_neuron(inputs, weights, bias):
    z = sum(x * w for x, w in zip(inputs, weights)) + bias
    return 1 if z > 0 else 0

gates = {"AND": ([1, 1], -1.5), "OR": ([1, 1], -0.5), "NAND": ([-1, -1], 1.5)}
for name, (weights, bias) in gates.items():
    outputs = [step_neuron([a, b], weights, bias) for a in (0, 1) for b in (0, 1)]
    print(f"{name:4}: inputs 00 01 10 11 give {outputs}")

The perceptron learns OR, then fails at XOR

Python
def train_perceptron(data, epochs=12, lr=1.0):
    w1 = w2 = b = 0.0
    for epoch in range(1, epochs + 1):
        mistakes = 0
        for (x1, x2), target in data:
            output = 1 if w1 * x1 + w2 * x2 + b > 0 else 0
            error = target - output
            if error:
                mistakes += 1
                w1 += lr * error * x1
                w2 += lr * error * x2
                b += lr * error
        print(f"  epoch {epoch:2}: w = ({w1:+.0f}, {w2:+.0f}), b = {b:+.0f}, mistakes {mistakes}")
        if mistakes == 0:
            return "learned it"
    return "never settled"

OR = [((0, 0), 0), ((0, 1), 1), ((1, 0), 1), ((1, 1), 1)]
XOR = [((0, 0), 0), ((0, 1), 1), ((1, 0), 1), ((1, 1), 0)]
print("OR:")
print(" ", train_perceptron(OR))
print("XOR:")
print(" ", train_perceptron(XOR, epochs=6))

Try this

  1. Find weights and a bias that make a neuron output 1 only when both inputs are 0 (NOR).
  2. Train the perceptron on AND. How many epochs does it take?
  3. Change Sam's weights so that the long film is predicted to be enjoyable. Which weight did you change, and what does it say about the new Sam?

Level check

  • Label the parts of an artificial neuron and match them to a biological neuron
  • Calculate a neuron's weighted sum and output for given inputs, weights and bias
  • Explain how the weights and the bias change a neuron's decision boundary
  • Give weights and a bias that make a neuron act as AND, OR or NOT
  • Apply the perceptron learning rule to one example by hand
  • Explain why a single neuron cannot compute XOR, and what fixes it

Key terms you must be able to define

Artificial neuron
A unit that computes a weighted sum of its inputs plus a bias and passes it through an activation function.
Weight
A number multiplying one input; its sign and size set how much and which way that input pushes the output.
Bias
A number added to the weighted sum, shifting how easily the neuron outputs a high value.
Weighted sum
z = w₁x₁ + w₂x₂ + … + b, the dot product of weights and inputs plus the bias.
Activation function
The function applied to the weighted sum to give the neuron's output.
Step function
An activation that outputs 1 if its input is above 0 and 0 otherwise.
Perceptron
Rosenblatt's 1958 single neuron with a step activation, trained by the perceptron learning rule.
Linearly separable
Able to be split into classes by a straight line, plane or hyperplane.
XOR problem
The fact that no single neuron can compute exclusive or, because its classes are not linearly separable.
Hidden layer
A layer of neurons between the inputs and the output, whose values are not seen directly.

Flashcards

DefineArtificial neuron
AnswerA unit that computes a weighted sum of its inputs plus a bias and passes it through an activation function.
DefineWeight
AnswerA number multiplying one input; its sign and size set how much and which way that input pushes the output.
DefineBias
AnswerA number added to the weighted sum, shifting how easily the neuron outputs a high value.
DefineWeighted sum
Answerz = w₁x₁ + w₂x₂ + … + b, the dot product of weights and inputs plus the bias.
DefineActivation function
AnswerThe function applied to the weighted sum to give the neuron's output.
DefineStep function
AnswerAn activation that outputs 1 if its input is above 0 and 0 otherwise.
DefinePerceptron
AnswerRosenblatt's 1958 single neuron with a step activation, trained by the perceptron learning rule.
DefineLinearly separable
AnswerAble to be split into classes by a straight line, plane or hyperplane.

Go further: notes for future AI engineers

The geometry behind the weights

The decision boundary of a neuron is the set of points where w · x + b = 0. Because a dot product with w measures how far a point lies along the direction of w, the boundary is perpendicular to w, at a distance of |b| ÷ |w| from the origin. Doubling every weight and the bias leaves the line exactly where it is, but makes the sigmoid output change more sharply across it: the model becomes more confident without changing its mind. That is why weight decay (Level 4) also acts as a brake on overconfidence.

Hebb's rule and modern learning

Donald Hebb's 1949 idea, often summarised as "cells that fire together wire together", strengthens a connection when both neurons are active. The perceptron rule adds the crucial ingredient Hebb lacked: an error signal. Change the weights only when the output is wrong, in the direction that reduces the error. Gradient descent and backpropagation are the grown-up version of that same idea.

Misconceptions to avoid

Avoid these
  • Forgetting the bias. Without it, every boundary must pass through the origin.
  • Thinking a bigger input always increases the output. If its weight is negative, it decreases it.
  • Believing artificial neurons work like real ones. They are a simple mathematical idea inspired by biology.
  • Thinking the perceptron rule can learn anything. It only succeeds when the classes are linearly separable.
  • Mixing up the weighted sum z and the output a. The activation function turns one into the other.
Fun fact

The human brain has about 86 billion neurons joined by roughly 100 trillion synapses. The largest artificial networks now have trillions of parameters, but each of their neurons is vastly simpler than a real one.

Knowledge check

0 of 10 answered
1What does an artificial neuron compute first, before its activation function?
2Inputs are [2, 3], weights are [0.5, −1] and the bias is 1. What is the weighted sum?
3What does a negative weight mean?
4What does changing the bias do to a neuron's decision boundary?
5Which weights and bias make a step neuron compute AND?
6In the perceptron rule, what happens when the prediction is correct?
7A perceptron outputs 0 for an example whose target is 1. What does the rule do?
8Why can no single neuron compute XOR?
9What does the perceptron convergence theorem guarantee?
10Which part of an artificial neuron plays the role of synapse strength?
Score 0 / 10

Check you could explain this

11 marks
1A neuron has inputs x = [1, 0, 2], weights w = [0.4, 1.2, −0.3] and bias b = 0.1. Calculate its weighted sum and its output with a step activation that outputs 1 when z > 0.3 marks
Calculate

Mark scheme0 / 3
  • L1 × 0.4 + 0 × 1.2 + 2 × (−0.3) + 0.1
  • Lz = −0.1
  • LOutput 0, because z is not above 0
Model answer

z = 1 × 0.4 + 0 × 1.2 + 2 × (−0.3) + 0.1 = 0.4 + 0 − 0.6 + 0.1 = −0.1. Because z is not greater than 0, the step activation outputs 0.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.

Think this answer or mark scheme is wrong? Report it

2Explain how a perceptron learns using the perceptron learning rule.4 marks
Explain

Mark scheme0 / 4
  • LIt makes a prediction from the weighted sum / step function
  • LThe error is the target minus the prediction
  • LEach weight changes by learning rate × error × input
  • LNo change when correct; repeated over the examples until no mistakes
Model answer

For each training example the perceptron calculates its weighted sum and uses a step function to predict 0 or 1. It works out the error as the target minus the prediction. Each weight is then changed by the learning rate multiplied by the error and by that weight's input, and the bias by the learning rate times the error, so a wrong answer pushes the weighted sum towards the right side. When the prediction is correct the error is zero and nothing changes. This is repeated over all the examples, epoch after epoch, until it makes no mistakes.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.

Think this answer or mark scheme is wrong? Report it

3Explain why a single neuron cannot learn the XOR function, and how a neural network overcomes this.4 marks
Explain

Mark scheme0 / 4
  • LA single neuron's boundary is a straight line
  • LXOR's outputs are not linearly separable / 1s on one diagonal
  • LA hidden layer of neurons draws more than one line / transforms the inputs
  • LAn output neuron combines the hidden neurons' outputs
Model answer

A single neuron outputs 1 on one side of a straight line, its decision boundary, and 0 on the other. XOR's two 1 outputs lie on one diagonal of the square and its two 0 outputs on the other, so no single straight line can separate them: XOR is not linearly separable. A network overcomes this with a hidden layer: two hidden neurons each draw a line, for example one acting like OR and one like AND, and an output neuron combines their outputs so that it fires when OR is true but AND is not.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.

Think this answer or mark scheme is wrong? Report it

Machine mark 0 / 11