Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 4: Neural Network Foundations

From neurons to the perceptron

P4.135 min●○○ BeginnerReviewed Dec 2, 2026

Why this matters for machine learning

The artificial neuron is the building block of every neural network: a weighted sum of the inputs plus a bias, followed by a simple decision. Every later lesson in this part stacks, trains or improves this one calculation.

Before this: Dot product and cosine similarity, Linear regression

After this lesson you can

  • Describe the biological neuron in a few plain words and say where the analogy with an artificial neuron stops
  • Compute the output of a neuron by hand, z=w⋅x+bz=\mathbf{w}\cdot\mathbf{x}+b followed by a step, and explain the bias as a movable threshold
  • Choose weights and a bias that make AND, OR and NOT gates, and check them on every input
  • Explain the decision boundary w⋅x+b=0\mathbf{w}\cdot\mathbf{x}+b=0 as a line, say what w\mathbf{w} and bb do to it, and compute the distance of a point from it
  • Say what a neuron with a sigmoid is, see a layer as Wx+b\mathbf{W}\mathbf{x}+\mathbf{b}, and say why one neuron cannot compute XOR

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):

  • x1x_1: the sky is cloudy.
  • x2x_2: the forecast says rain.
  • x3x_3: you will be outside a long time.

Not all three matter equally. Say they get weights w1=2w_1=2, w2=3w_2=3 and w3=1w_3=1: the forecast counts most. The neuron multiplies each input by its weight and adds the results. Then it adds a number bb, the bias, which here is −2.5-2.5. The total is the score zz:

z=w1x1+w2x2+w3x3+b=w⋅x+b.z=w_1x_1+w_2x_2+w_3x_3+b=\mathbf{w}\cdot\mathbf{x}+b .

That is the dot product of P1.B3 plus a constant. Finally the neuron applies a step: it outputs 11 (“take the umbrella”) if z≥0z\ge0 and 00 otherwise.

Try x=(1,0,1)\mathbf{x}=(1,0,1): cloudy, no rain forecast, a long time outside.

z=2⋅1+3⋅0+1⋅1−2.5=0.5.z=2\cdot1+3\cdot0+1\cdot1-2.5=0.5 .

Since 0.5≥00.5\ge0, the output is 11. Now try (0,0,1)(0,0,1): no cloud, no forecast, a long time outside. The score is 0+0+1−2.5=−1.50+0+1-2.5=-1.5, so the output is 00. The forecast alone is enough (z=3−2.5=0.5z=3-2.5=0.5 for (0,1,0)(0,1,0)), but a long time outside alone is not.

1constant input1x₁0x₂1x₃b = −2.5w = 2w = 3w = 1Σz = 0.5fires if z ≥ 01fires

z = 2×1 + 3×0 + 1×1 + (−2.5) = 0.5

The step makes the output 1, because z is at least 0.

Made-up numbers. Blue solid edge: positive weight. Red dashed edge: negative weight. The top box is the constant input 1, so its weight is the bias.

One neuron with three inputs. The top box is a constant input 1 whose weight is the bias. Edges are drawn in proportion to the weight: solid blue for positive, dashed red for negative. Change the inputs, weights and bias and watch z and the output. “Send the signals” plays a short animation of the signals flowing in (it is skipped in Lite Mode and with reduced motion, where the diagram already shows the result).

Things to try: the widget starts at x=(1,0,1)\mathbf{x}=(1,0,1), where z=0.5z=0.5 and the neuron fires. (1) Set x3x_3 to 0: z=−0.5z=-0.5 and the neuron goes silent. Then put x3x_3 back to 1 and set x2x_2 to 1 instead: z=3.5z=3.5. (2) Make the forecast weight w2w_2 negative and see the edge turn dashed red: an input can also push the neuron away from firing. (3) With these weights, raise bb to 00 and the neuron fires for every input.

The general rule

With dd inputs, the neuron has dd weights and one bias:

z=∑i=1dwixi+b=w⋅x+b,y^={1if z≥00if z<0.\begin{aligned}z&=\sum_{i=1}^{d}w_ix_i+b=\mathbf{w}\cdot\mathbf{x}+b,\\\hat{y}&=\begin{cases}1&\text{if }z\ge0\\0&\text{if }z<0.\end{cases}\end{aligned}

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 w\mathbf{w}, the bias is bb, and the input is x\mathbf{x}. 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 11 at exactly z=0z=0. Some books use >0>0 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 θ\theta. The perceptron moves that number to the other side of the inequality:

w⋅x≥θ⟺w⋅x+b≥0,b=−θ.\begin{gathered}\mathbf{w}\cdot\mathbf{x}\ge\theta\\\Longleftrightarrow\\\mathbf{w}\cdot\mathbf{x}+b\ge0,\quad b=-\theta .\end{gathered}

So the bias is the threshold with its sign flipped. In the umbrella neuron, b=−2.5b=-2.5 means the weighted sum has to reach 2.52.5. A more negative bias makes the neuron harder to fire, and a higher one makes it easier. Why bother? Because bb 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 11, with weight bb. Then b⋅1b\cdot1 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 w=(1,1)\mathbf{w}=(1,1) and b=−1.5b=-1.5. Then z=x1+x2−1.5z=x_1+x_2-1.5:

x1,x2x_1,x_2zzoutput
0,00,0−1.5-1.500
0,10,1−0.5-0.500
1,01,0−0.5-0.500
1,11,10.50.511

OR (output 1 when at least one input is 1): keep w=(1,1)\mathbf{w}=(1,1) and change only the bias to b=−0.5b=-0.5. Then z=−0.5, 0.5, 0.5, 1.5z=-0.5,\ 0.5,\ 0.5,\ 1.5 for the four inputs in the same order, and the outputs are 0,1,1,10,1,1,1. The bias alone changed AND into OR: the threshold dropped from 1.51.5 to 0.50.5.

NOT x1x_1 (output 1 when x1=0x_1=0): take w=(−1,0)\mathbf{w}=(-1,0) and b=0.5b=0.5. A negative weight turns “on” into “push away”. The scores for the four inputs are 0.5, 0.5, −0.5, −0.50.5,\ 0.5,\ -0.5,\ -0.5, so the outputs are 1,1,0,01,1,0,0, which is the opposite of x1x_1 and ignores x2x_2 (its weight is 0).

These settings are not unique. For AND, any bb from −2-2 up to (not including) −1-1 works with w=(1,1)\mathbf{w}=(1,1). For OR, any bb from −1-1 up to (not including) 00 works. The values −1.5-1.5 and −0.5-0.5 were picked because they sit in the middle, so no input lands exactly on z=0z=0.

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 x1x_1 on the horizontal axis and x2x_2 on the vertical axis. Every possible input is a point. The score z=w1x1+w2x2+bz=w_1x_1+w_2x_2+b is a number at each point, and the neuron fires where z≥0z\ge0.

The points where z=0z=0 exactly form the decision boundary:

w⋅x+b=0.\mathbf{w}\cdot\mathbf{x}+b=0 .

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 z>0z>0 and the neuron fires, on the other z<0z<0 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, w=(1,1)\mathbf{w}=(1,1) and b=−1.5b=-1.5, the boundary is x1+x2=1.5x_1+x_2=1.5. It runs between the point (1,1)(1,1) and the other three corners of the square.

What w\mathbf{w} 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 (1.5,0)(1.5,0) and (0,1.5)(0,1.5), so it runs in the direction (1.5,−1.5)(1.5,-1.5), and w⋅(1.5,−1.5)=1.5−1.5=0\mathbf{w}\cdot(1.5,-1.5)=1.5-1.5=0. A zero dot product means perpendicular (P1.B3). The ratio w1:w2w_1:w_2 sets the tilt of the line.

What bb does. The bias slides the line without turning it. For w=(1,1)\mathbf{w}=(1,1) the AND line x1+x2=1.5x_1+x_2=1.5 becomes the OR line x1+x2=0.5x_1+x_2=0.5 when bb goes from −1.5-1.5 to −0.5-0.5. The line is parallel and has moved towards the origin by a distance of 1/∥w∥=0.7071/\lVert\mathbf{w}\rVert=0.707. Lowering bb moves the line in the direction that w\mathbf{w} points, so the firing side shrinks.

What scaling does. Multiply w\mathbf{w} and bb by the same positive number, say 22, to get w=(2,2)\mathbf{w}=(2,2) and b=−3b=-3. Every score zz doubles, but z≥0z\ge0 holds for exactly the same points, so the boundary and the outputs do not change. Only the size of zz changes. (For a step the size of zz 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 x\mathbf{x} to the boundary is

distance=∣z∣∥w∥=∣w⋅x+b∣∥w∥.\text{distance}=\frac{|z|}{\lVert\mathbf{w}\rVert}=\frac{|\mathbf{w}\cdot\mathbf{x}+b|}{\lVert\mathbf{w}\rVert}.

For the AND neuron, ∥w∥=2≈1.414\lVert\mathbf{w}\rVert=\sqrt2\approx1.414. The point (1,1)(1,1) has z=0.5z=0.5, so it is 0.5/1.414=0.3540.5/1.414=0.354 from the line, and (0,0)(0,0) has z=−1.5z=-1.5, so it is 1.5/1.414=1.0611.5/1.414=1.061 away. With the doubled weights (2,2)(2,2) and b=−3b=-3, the point (1,1)(1,1) has z=1z=1 and ∥w∥=2.828\lVert\mathbf{w}\rVert=2.828, so the distance is again 0.3540.354: a distance does not depend on the scaling. The score zz is therefore the distance times ∥w∥\lVert\mathbf{w}\rVert, signed by the side.

Go deeper Why w is perpendicular, and where the distance formula comes from

Take two points p\mathbf{p} and q\mathbf{q} on the boundary. Then w⋅p+b=0\mathbf{w}\cdot\mathbf{p}+b=0 and w⋅q+b=0\mathbf{w}\cdot\mathbf{q}+b=0. Subtracting gives w⋅(p−q)=0\mathbf{w}\cdot(\mathbf{p}-\mathbf{q})=0. The vector p−q\mathbf{p}-\mathbf{q} runs along the line, and w\mathbf{w} is perpendicular to every such vector.

For the distance, let u=w/∥w∥\mathbf{u}=\mathbf{w}/\lVert\mathbf{w}\rVert be the unit vector in the direction of w\mathbf{w}. Moving a point by tut\mathbf{u} changes zz by w⋅tu=t∥w∥\mathbf{w}\cdot t\mathbf{u}=t\lVert\mathbf{w}\rVert. To get from a point with score zz to a point with score 00 along u\mathbf{u}, move by t=−z/∥w∥t=-z/\lVert\mathbf{w}\rVert. Moving straight along the perpendicular is the shortest way, so the distance is ∣z∣/∥w∥|z|/\lVert\mathbf{w}\rVert. Why no other move is shorter: any move d\mathbf{d} changes zz by w⋅d≤∥w∥∥d∥\mathbf{w}\cdot\mathbf{d}\le\lVert\mathbf{w}\rVert\lVert\mathbf{d}\rVert (P1.B3), so reaching z=0z=0 needs ∥d∥≥∣z∣/∥w∥\lVert\mathbf{d}\rVert\ge|z|/\lVert\mathbf{w}\rVert. In the AND example, the foot of the perpendicular from (1,1)(1,1) is (1,1)−0.5/2⋅(1,1)=(0.75,0.75)(1,1)-0.5/2\cdot(1,1)=(0.75,0.75), which lies on x1+x2=1.5x_1+x_2=1.5.

AND: Fire only when both inputs are 1.

−1−0.500.511.52−1−0.500.511.52x₁x₂
Correct: 4 of 4
‖w‖ = 1.41

All four corners get the target output: a straight line does this task. The line sits between the corners that should fire and the ones that should not.

Distance of (1, 1) from the line: |z| / ‖w‖ = 0.5 / 1.41 = 0.35

The pink arrow shows the direction of w, which is perpendicular to the line and points to the output-1 side. Its length is fixed and does not show ‖w‖. The dashed segment is the shortest way from the chosen input to the line.

The boundary of one step neuron on the four corners of the unit square (inputs 0 or 1). Choose a task, then move w₁, w₂ and b. The thick line is w·x + b = 0, the pink arrow is the direction of w, the hatched blue side is where the neuron outputs 1, and the grey side is output 0. Circles are inputs whose target is 1, squares are inputs whose target is 0, and a red ring marks a wrong output. Choose one of the four inputs in the list to see its distance from the line.

Things to try (all numbers repeat exactly):

  1. AND is loaded with w=(1,1)\mathbf{w}=(1,1), b=−1.5b=-1.5: 4 of 4 correct. Choose the input (1,1)(1,1) in the list: z=0.5z=0.5, distance 0.350.35. Choose (0,0)(0,0): z=−1.5z=-1.5, distance 1.061.06.
  2. Change only bb to −0.5-0.5. 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.
  3. On AND, set w1=w2=2w_1=w_2=2 and b=−3b=-3. Still 4 of 4, and the distance of (1,1)(1,1) is still 0.350.35 while zz is 11.
  4. On NOT x1x_1, move w2w_2 away from 0. The line tilts, yet the neuron stays correct for w2w_2 from −0.5-0.5 up to (not including) 0.50.5.
  5. 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 W\mathbf{W} and the biases into a vector b\mathbf{b}. Then all the scores come out in one go:

z=Wx+b.\mathbf{z}=\mathbf{W}\mathbf{x}+\mathbf{b}.

This is the dense layer of P1.B5 and the linear algebra map. Take the numbers used there: W=(1−121)\mathbf{W}=\begin{pmatrix}1&-1\\2&1\end{pmatrix}, x=(1,2)\mathbf{x}=(1,2) and b=(1,−2)\mathbf{b}=(1,-2). Then Wx=(1−2, 2+2)=(−1,4)\mathbf{W}\mathbf{x}=(1-2,\ 2+2)=(-1,4) and z=(0,2)\mathbf{z}=(0,2). Each row of W\mathbf{W} is one neuron’s weights, so there are two neurons here. With a step, the layer outputs (1,1)(1,1), because 0≥00\ge0. With a sigmoid, which gives σ(0)=0.5\sigma(0)=0.5 and σ(2)≈0.881\sigma(2)\approx0.881, it outputs (0.5, 0.881)(0.5,\ 0.881).

The gate neurons fit together the same way. Put AND, OR and NOT x1x_1 in one layer with W=(1111−10)\mathbf{W}=\begin{pmatrix}1&1\\1&1\\-1&0\end{pmatrix} and b=(−1.5,−0.5,0.5)\mathbf{b}=(-1.5,-0.5,0.5). For x=(1,0)\mathbf{x}=(1,0) the scores are (−0.5, 0.5, −0.5)(-0.5,\ 0.5,\ -0.5) and the step gives (0,1,0)(0,1,0): AND is off, OR is on and NOT x1x_1 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 z=w⋅x+bz=\mathbf{w}\cdot\mathbf{x}+b and change only the activation:

  • No activation (output =z=z): this is linear regression (P3.A2).
  • Sigmoid (output =σ(z)=1/(1+e−z)=\sigma(z)=1/(1+e^{-z}), a number between 0 and 1): this is logistic regression (P3.A7). With w=(1,2)\mathbf{w}=(1,2), b=−3b=-3 and x=(2,2)\mathbf{x}=(2,2), the score is z=3z=3 and the output is σ(3)=0.9526\sigma(3)=0.9526, the number P3.A7 uses. The boundary of the neuron is where σ(z)=0.5\sigma(z)=0.5, which is z=0z=0, the same line as for the step.
  • Step (output =1=1 if z≥0z\ge0): 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 zz with respect to a weight wiw_i is the input xix_i that the weight multiplies (P1.C4). At x=(1,0,1)\mathbf{x}=(1,0,1) in the umbrella neuron, raising w1w_1 by 0.10.1 raises zz from 0.50.5 to 0.60.6, while raising w2w_2 by 0.10.1 changes nothing, because x2=0x_2=0.

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 zz: scaling w\mathbf{w} and bb by 22 keeps the boundary but makes the probabilities more extreme.

What one neuron cannot do

Take the XOR gate (“exclusive or”): output 11 when exactly one input is 11. The targets for (0,0),(0,1),(1,0),(1,1)(0,0),(0,1),(1,0),(1,1) are 0,1,1,00,1,1,0. Can one step neuron do it?

Look at the plane. The two points to fire for, (0,1)(0,1) and (1,0)(1,0), sit at opposite corners of the unit square. The two points to stay silent for, (0,0)(0,0) and (1,1)(1,1), 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 (0,1)(0,1) and (1,0)(1,0) both fire, the midpoint (0.5,0.5)(0.5,0.5) of the segment between them fires too. But (0.5,0.5)(0.5,0.5) is also the midpoint of the segment between (0,0)(0,0) and (1,1)(1,1), so one of those two silent points would have to fire as well.

The same fact in algebra: z(0,1)≥0z(0,1)\ge0 and z(1,0)≥0z(1,0)\ge0 add up to w1+w2+2b≥0w_1+w_2+2b\ge0, which is z(0,0)+z(1,1)≥0z(0,0)+z(1,1)\ge0, so at least one of the two silent points scores 00 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.

Try it: gates, distance and scaling

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:

z=w⋅x+b,y^=step(z).z=\mathbf{w}\cdot\mathbf{x}+b,\qquad\hat{y}=\mathrm{step}(z).

With the constant input x0=1x_0=1 and w0=bw_0=b, the score is w~⋅x~\tilde{\mathbf{w}}\cdot\tilde{\mathbf{x}}. The boundary {x:w⋅x+b=0}\{\mathbf{x}:\mathbf{w}\cdot\mathbf{x}+b=0\} is a hyperplane with normal w\mathbf{w}, and the signed distance of a point from it is z/∥w∥z/\lVert\mathbf{w}\rVert. A layer of mm neurons is z=Wx+b\mathbf{z}=\mathbf{W}\mathbf{x}+\mathbf{b} with W\mathbf{W} of shape (m,d)(m,d). Replacing the step by the identity gives linear regression, and by σ\sigma 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

  1. A step neuron has weights w=(1,1)\mathbf{w}=(1,1) and bias b=−1.5b=-1.5 (the AND neuron). You raise the bias to b=−0.5b=-0.5 and keep the weights. What happens?

  2. You multiply both the weights and the bias of a step neuron by 10. What changes?

  3. A neuron has w=(3,4)\mathbf{w}=(3,4) and b=−10b=-10. How far is the input (4,3)(4,3) from its decision boundary?

  4. Why can one step neuron not compute XOR on two binary inputs?

  5. A neuron computes σ(w⋅x+b)\sigma(\mathbf{w}\cdot\mathbf{x}+b) and is trained to predict a yes-or-no label. Which model from Part 3 is this?

Exercises

  1. 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 answerTake w=(1,1,1)\mathbf{w}=(1,1,1) and b=−2.5b=-2.5. The score is x1+x2+x3−2.5x_1+x_2+x_3-2.5. For (1,1,1)(1,1,1) it is 0.50.5 (fires). For any input with two 1s it is −0.5-0.5, with one 1 it is −1.5-1.5, and with none −2.5-2.5 (all silent). Any bb from −3-3 up to (not including) −2-2 works.
  2. For the umbrella neuron (w=(2,3,1)\mathbf{w}=(2,3,1), b=−2.5b=-2.5), find the output for (0,1,0)(0,1,0) and for (1,0,0)(1,0,0).
    Show answerFor (0,1,0)(0,1,0): z=3−2.5=0.5z=3-2.5=0.5, so the output is 11. For (1,0,0)(1,0,0): z=2−2.5=−0.5z=2-2.5=-0.5, so the output is 00. Cloud alone does not trigger the umbrella.
  3. The neuron with w=(2,2)\mathbf{w}=(2,2) and b=−3b=-3 is the AND neuron with everything doubled. Compute the distance of (0,1)(0,1) from its boundary, and say how it compares with the AND neuron w=(1,1)\mathbf{w}=(1,1), b=−1.5b=-1.5.
    Show answerz=2⋅0+2⋅1−3=−1z=2\cdot0+2\cdot1-3=-1 and ∥w∥=8=2.828\lVert\mathbf{w}\rVert=\sqrt8=2.828, so the distance is 1/2.828=0.3541/2.828=0.354. For the AND neuron, z=−0.5z=-0.5 and ∥w∥=1.414\lVert\mathbf{w}\rVert=1.414 give 0.3540.354 too. The two neurons have the same boundary, so they agree.
  4. Compute the output of the three-neuron layer of the gate section (AND, OR, NOT x1x_1) on the input (0,0)(0,0).
    Show answerz=(0+0−1.5, 0+0−0.5, 0+0+0.5)=(−1.5,−0.5,0.5)\mathbf{z}=(0+0-1.5,\ 0+0-0.5,\ 0+0+0.5)=(-1.5,-0.5,0.5), so the step gives (0,0,1)(0,0,1): only NOT x1x_1 fires, because x1=0x_1=0.
  5. A neuron has w=(2,−1)\mathbf{w}=(2,-1) and b=1b=1. Find where its boundary crosses the two axes, and check that w\mathbf{w} is perpendicular to it.
    Show answerOn the x2x_2 axis (x1=0x_1=0): −x2+1=0-x_2+1=0, so x2=1x_2=1, the point (0,1)(0,1). On the x1x_1 axis (x2=0x_2=0): 2x1+1=02x_1+1=0, so x1=−0.5x_1=-0.5, the point (−0.5,0)(-0.5,0). The line runs along (0,1)−(−0.5,0)=(0.5,1)(0,1)-(-0.5,0)=(0.5,1), and w⋅(0.5,1)=1−1=0\mathbf{w}\cdot(0.5,1)=1-1=0, so w\mathbf{w} 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.

Key takeaways

  • An artificial neuron multiplies each input by a weight, adds the results and a bias, z=w⋅x+bz=\mathbf{w}\cdot\mathbf{x}+b, and then applies an activation. The perceptron uses a step: output 1 if z≥0z\ge0, otherwise 0.
  • The bias moves the threshold: the neuron fires when w⋅x≥−b\mathbf{w}\cdot\mathbf{x}\ge-b. A constant input of 1 with weight bb turns the bias into one more weight.
  • With the right weights and bias, one neuron computes AND, OR and NOT of binary inputs. For two inputs, AND uses w=(1,1)\mathbf{w}=(1,1) and b=−1.5b=-1.5, and OR uses w=(1,1)\mathbf{w}=(1,1) and b=−0.5b=-0.5.
  • The set of inputs with z=0z=0 is a line (a plane in 3-D, a hyperplane in general). w\mathbf{w} is perpendicular to it and points to the output-1 side, bb slides the line, and the distance of a point from it is ∣z∣/∥w∥|z|/\lVert\mathbf{w}\rVert. Scaling w\mathbf{w} and bb together by a positive number changes zz but not the line or the outputs.
  • A layer of neurons is Wx+b\mathbf{W}\mathbf{x}+\mathbf{b} followed by the activation. A neuron with no activation has the same form as linear regression, and with a sigmoid the same form as logistic regression (the loss and training are chosen separately).
  • One step neuron has one straight boundary, so it cannot compute XOR. Real neurons are far more complicated than this calculation, so the artificial neuron is an idea borrowed from biology and not a model of a brain cell.
Found a mistake? Tell us