Think of a very small decision-maker. It gets a few numbers, decides how much each one matters, adds them up, and says “yes” if the total is big enough. That is all. Could such a simple unit be the building block of systems that read handwriting, translate languages and write code?
It can. Joined together by the million, units like this are the neural networks of deep learning. This lesson builds one unit with small numbers, shows what it can and cannot do, and uses it to meet the perceptron, the first trainable neuron. It also explains where the old idea of a “brain cell” came from, and how little of a real brain cell it keeps.
The brain cell, in plain words
The idea started with biology, so start there. A neuron is a nerve cell. A simplified picture has four parts:
- Dendrites are thin branches that receive signals from other neurons.
- The cell body collects those signals.
- The axon is a long fibre that carries the neuron’s own signal away.
- At the end of the axon, the neuron meets other neurons, often at their dendrites. A meeting place is a synapse.
Some incoming signals push the neuron towards firing and others push it away. If the total push is strong enough, the neuron sends a short electrical pulse, a spike, along its axon. A spike is all-or-nothing: it is about the same size however strong the push that caused it. A stronger push tends to produce more spikes per second, not bigger ones.
That gives a recipe in three steps: receive many signals, weigh them, and fire or stay silent depending on the total. Artificial neurons keep that recipe and almost nothing else. Here is where the analogy breaks:
- Timing. A real neuron communicates with spikes whose timing carries information. An artificial neuron here takes numbers in and gives one number out, with no time at all.
- Chemistry. Synapses pass signals with chemical messengers, and the strength of a synapse can depend on many chemical and electrical details. An artificial weight is one number.
- Learning. Brains change their connections by mechanisms that scientists are still studying. The way artificial networks learn, backpropagation (P4.7), is a different procedure, and whether brains do anything like it is an open research question.
- Complexity. A real neuron is a living cell with an elaborate internal structure, and research suggests that a single one can do more than a weighted sum.
So an artificial neuron is a loose inspiration, not a copy. Treat the brain as where the idea came from (see also P0.1), and the artificial neuron as a simple calculation that you can fully understand.
Two early ideas
McCulloch and Pitts, 1943. The neurophysiologist Warren McCulloch and the logician Walter Pitts published a paper (“A logical calculus of the ideas immanent in nervous activity”) that treated a nerve cell as a logic unit. Because a spike is all-or-nothing, they modelled a neuron as a unit with on/off inputs that works in discrete time steps. It fires when the number of active excitatory inputs reaches a threshold, unless an inhibitory input is active, which blocks it completely. They showed that networks of such units can compute logical statements such as AND, OR and NOT. The units had fixed settings. There was no learning.
Rosenblatt, 1958. The psychologist Frank Rosenblatt, at the Cornell Aeronautical Laboratory, described the perceptron (“The perceptron: a probabilistic model for information storage and organization in the brain”). The new part was that the weights were not fixed: the perceptron could adjust them from examples. His group also built a machine for it, the Mark I Perceptron, which looked at pictures through a 20 by 20 grid of 400 light sensors. In its original design, units in front of the learning unit had fixed connections; the single trainable unit this lesson calls “the perceptron” is the simplified version that textbooks keep.
Next lesson (P4.2) teaches how the perceptron learns. This lesson teaches what it computes.
One artificial neuron
Here is a made-up decision. Should you take an umbrella? Three inputs, each 1 (yes) or 0 (no):
- : the sky is cloudy.
- : the forecast says rain.
- : you will be outside a long time.
Not all three matter equally. Say they get weights , and : the forecast counts most. The neuron multiplies each input by its weight and adds the results. Then it adds a number , the bias, which here is . The total is the score :
That is the dot product of P1.B3 plus a constant. Finally the neuron applies a step: it outputs (“take the umbrella”) if and otherwise.
Try : cloudy, no rain forecast, a long time outside.
Since , the output is . Now try : no cloud, no forecast, a long time outside. The score is , so the output is . The forecast alone is enough ( for ), but a long time outside alone is not.
Things to try: the widget starts at , where and the neuron fires. (1) Set to 0: and the neuron goes silent. Then put back to 1 and set to 1 instead: . (2) Make the forecast weight negative and see the edge turn dashed red: an input can also push the neuron away from firing. (3) With these weights, raise to and the neuron fires for every input.
The general rule
With inputs, the neuron has weights and one bias:
The step is the activation function: the last stage that turns the score into the output. A neuron that uses the step is a perceptron. Many other activations exist; P4.4 teaches them. Following the notation of this course, the weights are , the bias is , and the input is . A weight is positive if the input pushes towards firing, negative if it pushes away, and near zero if the input is nearly ignored.
The step is a function with a jump (P1.A1). Its value at the jump itself is a convention: this lesson uses at exactly . Some books use instead. The next lesson (P4.2) counts a score of exactly 0 as undecided, so that its learning rule can start from all-zero weights.
The bias is a movable threshold
The McCulloch–Pitts neuron fires when the weighted sum reaches a threshold . The perceptron moves that number to the other side of the inequality:
So the bias is the threshold with its sign flipped. In the umbrella neuron, means the weighted sum has to reach . A more negative bias makes the neuron harder to fire, and a higher one makes it easier. Why bother? Because can be learned like any weight (P4.2), and it keeps the formula as one tidy expression.
The always-1 trick. Add one extra input that is always , with weight . Then is just one more term of the weighted sum, and the bias becomes an ordinary weight. This is the same constant feature that P3.A2 used for linear regression, and it is why the widget above draws the bias as a box with the constant 1.
Logic gates by hand
Let the inputs be 0 or 1 (false or true) and look for weights that make a neuron behave like a logic gate. Take two inputs.
AND (output 1 only when both inputs are 1): try and . Then :
| output | ||
|---|---|---|
OR (output 1 when at least one input is 1): keep and change only the bias to . Then for the four inputs in the same order, and the outputs are . The bias alone changed AND into OR: the threshold dropped from to .
NOT (output 1 when ): take and . A negative weight turns “on” into “push away”. The scores for the four inputs are , so the outputs are , which is the opposite of and ignores (its weight is 0).
These settings are not unique. For AND, any from up to (not including) works with . For OR, any from up to (not including) works. The values and were picked because they sit in the middle, so no input lands exactly on .
One gate is missing from this list on purpose: XOR. A single neuron cannot make it, as a later section shows.
The geometry: a neuron draws a line
A neuron with two inputs is a picture in a plane. Put on the horizontal axis and on the vertical axis. Every possible input is a point. The score is a number at each point, and the neuron fires where .
The points where exactly form the decision boundary:
With two inputs this is a straight line. With three inputs it is a flat plane, and with more it is a hyperplane, the flat object of P1.B3. The line cuts the plane into two half-planes: on one side and the neuron fires, on the other and it stays silent. The boundary is straight whatever the weights are, which is why a single neuron is called a linear classifier.
For the AND neuron, and , the boundary is . It runs between the point and the other three corners of the square.
What does. The weight vector is perpendicular to the boundary and points towards the side where the neuron fires. Check with the AND line: it contains the points and , so it runs in the direction , and . A zero dot product means perpendicular (P1.B3). The ratio sets the tilt of the line.
What does. The bias slides the line without turning it. For the AND line becomes the OR line when goes from to . The line is parallel and has moved towards the origin by a distance of . Lowering moves the line in the direction that points, so the firing side shrinks.
What scaling does. Multiply and by the same positive number, say , to get and . Every score doubles, but holds for exactly the same points, so the boundary and the outputs do not change. Only the size of changes. (For a step the size of is thrown away. With a sigmoid it matters, as shown later. A negative number gives the same line but swaps the two sides, so the outputs flip.)
How far from the line? The distance from a point to the boundary is
For the AND neuron, . The point has , so it is from the line, and has , so it is away. With the doubled weights and , the point has and , so the distance is again : a distance does not depend on the scaling. The score is therefore the distance times , signed by the side.
Go deeper Why w is perpendicular, and where the distance formula comes from
Take two points and on the boundary. Then and . Subtracting gives . The vector runs along the line, and is perpendicular to every such vector.
For the distance, let be the unit vector in the direction of . Moving a point by changes by . To get from a point with score to a point with score along , move by . Moving straight along the perpendicular is the shortest way, so the distance is . Why no other move is shorter: any move changes by (P1.B3), so reaching needs . In the AND example, the foot of the perpendicular from is , which lies on .
Things to try (all numbers repeat exactly):
- AND is loaded with , : 4 of 4 correct. Choose the input in the list: , distance . Choose : , distance .
- Change only to . The line slides towards the origin and now two corners are wrong: 2 of 4 correct. Choosing OR gives the same weights and 4 of 4.
- On AND, set and . Still 4 of 4, and the distance of is still while is .
- On NOT , move away from 0. The line tilts, yet the neuron stays correct for from up to (not including) .
- Choose XOR and try for 4 of 4. You cannot: the best you will find is 3 of 4. The section on XOR below explains why.
A layer is many neurons
Put several neurons side by side, all looking at the same inputs. Each has its own weight vector and bias. Stack the weight vectors as the rows of a matrix and the biases into a vector . Then all the scores come out in one go:
This is the dense layer of P1.B5 and the linear algebra map. Take the numbers used there: , and . Then and . Each row of is one neuron’s weights, so there are two neurons here. With a step, the layer outputs , because . With a sigmoid, which gives and , it outputs .
The gate neurons fit together the same way. Put AND, OR and NOT in one layer with and . For the scores are and the step gives : AND is off, OR is on and NOT is off. Deep learning libraries compute exactly this, for many examples at once. In PyTorch it is nn.Linear, which stores the weight with one row per neuron, shape (outputs, inputs). P4.5 covers batches and shapes in detail.
A neuron is a model you already know
Take the score and change only the activation:
- No activation (output ): this is linear regression (P3.A2).
- Sigmoid (output , a number between 0 and 1): this is logistic regression (P3.A7). With , and , the score is and the output is , the number P3.A7 uses. The boundary of the neuron is where , which is , the same line as for the step.
- Step (output if ): the perceptron.
So a neuron is not a strange new object: it is a linear model with a choice of final squashing. What makes it linear or logistic regression is the loss it is trained with (P4.6). One more fact about the score will matter when networks are trained. The slope of with respect to a weight is the input that the weight multiplies (P1.C4). At in the umbrella neuron, raising by raises from to , while raising by changes nothing, because .
The step has a weakness. It is flat everywhere except at its jump, so a tiny change in a weight changes nothing, and gradient descent (P1.F3) has no slope to follow. Smooth activations, introduced in P4.4, fix this. Unlike the step, the sigmoid also keeps the size of : scaling and by keeps the boundary but makes the probabilities more extreme.
What one neuron cannot do
Take the XOR gate (“exclusive or”): output when exactly one input is . The targets for are . Can one step neuron do it?
Look at the plane. The two points to fire for, and , sit at opposite corners of the unit square. The two points to stay silent for, and , sit at the other two corners. The firing side of a line is a half-plane, and a half-plane contains the whole straight segment between any two of its points. So if and both fire, the midpoint of the segment between them fires too. But is also the midpoint of the segment between and , so one of those two silent points would have to fire as well.
The same fact in algebra: and add up to , which is , so at least one of the two silent points scores or more and fires, which is wrong. A search over a grid of weights and biases (the code cell in “Try it” below) also finds at most 3 of 4 correct, and the algebra above shows that no setting can do better.
Data that one straight line can split is called linearly separable. AND, OR and NOT are, and XOR is not. This limit, and what it meant for the early history of neural networks, are the topic of P4.2. The way out, stacking neurons into layers so that later neurons see transformed inputs, is P4.3.
Try it: gates and distances in code
The cell builds the three gates, prints the scores and outputs, and checks the distance and the scaling claims.
Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).
Depth: three ways to see it
Intuition
A neuron is a small committee. Each input is a member who votes with some strength: the weight says how loudly (and a negative weight votes against). The bias is the number of votes the committee needs before it says yes. The committee adds the votes and decides. The analogy breaks in two places: real committees can talk to each other and change their minds, while a neuron computes one fixed sum; and the weights are numbers learned from examples, not opinions.
Math
Score and output:
With the constant input and , the score is . The boundary is a hyperplane with normal , and the signed distance of a point from it is . A layer of neurons is with of shape . Replacing the step by the identity gives linear regression, and by gives logistic regression.
Code
import numpy as np
def neuron(x, w, b):
z = w @ x + b
return 1 if z >= 0 else 0
print(neuron(np.array([1, 0, 1]), np.array([2, 3, 1]), -2.5))The 2 by 3 layer of the linear algebra map (3 inputs, 2 neurons) in PyTorch. PyTorch is not run in the browser, so this block shows a real local run (PyTorch 2.9). Note that the weight has shape (outputs, inputs):
import torch
from torch import nn
layer = nn.Linear(3, 2)
with torch.no_grad():
layer.weight.copy_(torch.tensor([[1., 2., 3.],
[4., 5., 6.]]))
layer.bias.copy_(torch.tensor([10., 20.]))
x = torch.tensor([1., 0., -1.])
z = layer(x).detach()
print(layer.weight.shape)
print(z)
print((z >= 0).int())torch.Size([2, 3])
tensor([ 8., 18.])
tensor([1, 1], dtype=torch.int32)Common mistakes
Quiz
Check your understanding
A step neuron has weights and bias (the AND neuron). You raise the bias to and keep the weights. What happens?
The boundary is , so changing slides the line without turning it, and a higher bias means a lower threshold, so more inputs reach it. Rotation needs a change in the weights. The weights are unchanged, and the bias is part of the score, so it does change which inputs fire.
You multiply both the weights and the bias of a step neuron by 10. What changes?
The condition is the same for as for , so the same inputs fire, and the line is the same line. Both the weights and the bias are scaled, so the line neither moves nor tilts. The distance of a point from the line is the score divided by , which does not change either.
A neuron has and . How far is the input from its decision boundary?
The score is and , so the distance is . The number is the score, which is times the distance. The value divides by , the squared length, and multiplies by the length instead of dividing.
Why can one step neuron not compute XOR on two binary inputs?
The firing corners of XOR are diagonal opposites, as are the silent corners, so one line cannot split them. XOR has two inputs, like AND and OR. No finite weights work, so size is not the issue. A step neuron can output 1 for several inputs, as OR does.
A neuron computes and is trained to predict a yes-or-no label. Which model from Part 3 is this?
A score passed through the sigmoid to give a probability is logistic regression (P3.A7). Linear regression would output the score itself, with no sigmoid. k-nearest neighbors and naive Bayes use no weighted sum of this kind.
Exercises
- Choose weights and a bias for a neuron with three binary inputs that fires only when all three are 1 (a three-input AND).
Show answer
Take and . The score is . For it is (fires). For any input with two 1s it is , with one 1 it is , and with none (all silent). Any from up to (not including) works. - For the umbrella neuron (, ), find the output for and for .
Show answer
For : , so the output is . For : , so the output is . Cloud alone does not trigger the umbrella. - The neuron with and is the AND neuron with everything doubled. Compute the distance of from its boundary, and say how it compares with the AND neuron , .
Show answer
and , so the distance is . For the AND neuron, and give too. The two neurons have the same boundary, so they agree. - Compute the output of the three-neuron layer of the gate section (AND, OR, NOT ) on the input .
Show answer
, so the step gives : only NOT fires, because . - A neuron has and . Find where its boundary crosses the two axes, and check that is perpendicular to it.
Show answer
On the axis (): , so , the point . On the axis (): , so , the point . The line runs along , and , so is perpendicular to the line.
Next: P4.2 teaches how a perceptron finds its own weights from examples, and what the XOR limit did to the field. P4.3 stacks neurons into layers to get past it. P4.4 replaces the step by smooth activations, and P4.5 writes a whole layer as one matrix multiplication.