Index Ai Academy
Phase 4 · Deep learning · Neural networks

The artificial neuron

Weighted sum plus a nonlinearity.

About 25 minutes · Free lesson

Start this lesson

Why it matters

Every neural network is built from one small unit, copied over and over. That's true of a spam filter. It's also true of a chatbot with hundreds of billions of parameters (adjustable numbers). The unit is called a neuron. You've already trained one without calling it that: logistic regression, from Phase 3. This lesson shows exactly what one neuron can do, and the kind of pattern it can never learn. That gap is why networks need layers, which is the story of this phase.

What you'll be able to do

  • Compute a neuron's output: a weighted sum plus a bias, passed through an activation
  • Recognize logistic regression as a single neuron
  • See a neuron's decision boundary as a straight line
  • Explain why one neuron can't learn XOR, and why that matters

What it covers

  1. A neuron is a tiny scorecard
  2. Worked example: one neuron by hand
  3. The same neuron in NumPy
  4. A neuron draws a straight line
  5. Why XOR mattered

A neuron is a tiny scorecard

Picture deciding whether to go for a run after work. You weigh a few things. Sunshine pushes you toward yes. Rain pushes you toward no, and pushes hard. You also have a starting mood. Some days you're keen before you even look outside, and some days you're not.

A neuron makes a decision the same way. It takes in a few numbers, called inputs. It gives back one number, its output. It gets there in two steps.

Step 1: the weighted sum. Each input has a weight. A weight says how much its input matters, and in which direction. A positive weight pushes the output up. A negative weight pushes it down. The neuron multiplies each input by its weight and adds up the results. Then it adds one more number, the bias. The bias is the starting mood: where the score sits before any input has a say.

Call the result zz. Write the first input as x1x_1. Its weight is w1w_1. The second input is x2x_2. Its weight is w2w_2, and so on. The letter bb is the bias. Say there are nn inputs. Then the weighted sum is z=w1x1+w2x2+⋯+wnxn+b=w⋅x+bz = w_1 x_1 + w_2 x_2 + \dots + w_n x_n + b = \mathbf{w} \cdot \mathbf{x} + b. The three dots mean "and so on, for every input".

In plain words: multiply each input by its weight, add them all up, then add the bias.

The short form on the right uses bold letters for whole lists. The bold w\mathbf{w} is the list of weights. The bold x\mathbf{x} is the list of inputs. The dot between them is the dot product from Phase 2: multiply in pairs, then add. In NumPy that's np.dot(w, x).

Try it with small numbers. Two inputs are x1=1x_1 = 1 and x2=3x_2 = 3. Their weights are w1=2w_1 = 2 and w2=−0.5w_2 = -0.5. The bias is b=−1b = -1. Then z=2×1+(−0.5)×3−1=2−1.5−1=−0.5z = 2 \times 1 + (-0.5) \times 3 - 1 = 2 - 1.5 - 1 = -0.5.

Quick check
Inputs 1 and 2, weights 3 and −1, bias 0.5. What is the weighted sum?
print(1 * 3 + 2 * -1 + 0.5)
  1. 2.5
  2. 6.5
  3. 1.5
  4. -1.5
Show the answer

1.5

3 − 2 + 0.5 = 1.5. The negative weight means that input pulls the score down.

Step 2: the activation. The neuron passes zz through one more function, called the activation function. Call that function ff. The output is a=f(z)a = f(z), and aa is called the activation. Put both steps together:

a=f(w⋅x+b)a = f(\mathbf{w} \cdot \mathbf{x} + b)

In plain words: take the weighted sum, add the bias, then pass the result through the activation function. What comes out is the neuron's answer.

In this lesson ff is the sigmoid you met in logistic regression. It squashes any number into the range 0 to 1. A big positive zz gives an answer close to 1. A big negative zz gives an answer close to 0. A zz of exactly 0 gives 0.5. For the numbers above, z=−0.5z = -0.5 gives a≈0.378a \approx 0.378, about a 38% chance.

With the sigmoid as ff, a neuron is exactly logistic regression from Phase 3. So a neuron with a sigmoid outputs a probability. The weights decide how much each input matters. The bias shifts the point where the neuron "switches on".

Go deeper The sigmoid formula
σ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}}

Read it as: "sigma of z is 1 divided by 1 plus e to the minus z." The Greek letter σ\sigma (sigma) is the sigmoid's usual name. The letter ee is a fixed number, about 2.718.

When zz is large, e−ze^{-z} is tiny, so the answer is close to 1. When zz is very negative, e−ze^{-z} is huge, so the answer is close to 0. At z=0z = 0, e0=1e^{0} = 1, so the answer is 1/21/2. In the code below it's the line 1 / (1 + np.exp(-z)).

Try it now

Multiply each input by its weight, add the bias, then pass the sum through an activation. Move anything and follow the numbers.

Worked example

Worked example: one neuron by hand

A shop wants to guess whether a customer will click on its next email. The neuron has two inputs. The first is 1 if they opened the last email, and 0 if not. The second is how many days since they last visited. This customer opened it (1) and last visited 3 days ago (3). The weights are 2 and −0.5, and the bias is −1.

import math

x = [1, 3]        # opened the last email (1 = yes), days since last visit
w = [2.0, -0.5]   # weights: how much each input matters
b = -1.0          # bias: the starting point

z = sum(wi * xi for wi, xi in zip(w, x)) + b   # weighted sum plus bias
a = 1 / (1 + math.exp(-z))                      # sigmoid activation
print(z, round(a, 3))
It runs right here in your browser, with nothing to install.

It prints -0.5 0.378. The sum(...) line is the weighted sum: 2 × 1 = 2, and −0.5 × 3 = −1.5. Those add to 0.5. The bias of −1 brings zz down to −0.5. The sigmoid turns −0.5 into 0.378, so the neuron gives this customer about a 38% chance of clicking.

Read the signs. Opening the last email has a positive weight, so it pushes the chance up. Each day away has a negative weight, so it pushes the chance down. Had they visited today (0 days), zz would be 2 − 1 = 1. The chance would rise to about 73%.

Quick check
A neuron has weights [1, 2], inputs [4, −1] and bias −1. What does this print?
w = [1, 2]
x = [4, -1]
b = -1
z = w[0] * x[0] + w[1] * x[1] + b
print(z)
  1. 3
  2. 1
  3. -3
  4. 2
Show the answer

1

1 × 4 = 4 and 2 × (−1) = −2. That's 4 − 2 = 2, and the bias of −1 makes it 1. Forgetting the bias gives 2.

The same neuron in NumPy

Here's a neuron that guesses whether a customer will churn, meaning stop buying from you. It has two inputs. The first is days since their last order, in hundreds (so 1.5 means 150 days). The second is how many orders they place per year.

The weight on days is 1.8. It's positive, so a long gap pushes the churn chance up. The weight on orders is −0.6. It's negative, so frequent buying pushes the chance down. The bias is −1.

import numpy as np

def neuron(x, w, b):
    z = np.dot(w, x) + b
    return 1 / (1 + np.exp(-z))          # sigmoid activation

# A "will this customer churn?" neuron: inputs are days since last order (in 100s) and orders per year
w = np.array([1.8, -0.6])
b = -1.0
for x in [[0.2, 9], [1.5, 2], [3.0, 1]]:
    print(f"inputs {x}  →  output {neuron(np.array(x), w, b):.3f}")

Follow the first customer by hand. They ordered 20 days ago and buy 9 times a year. The weighted sum is 1.8 × 0.2 = 0.36, plus −0.6 × 9 = −5.4. Add the bias of −1, and zz is −6.04. That's a big negative number, so the sigmoid gives 0.002: almost no chance of churning.

The second customer has z=0.5z = 0.5 and an output of 0.622. The third hasn't ordered for 300 days and buys once a year. Their output is 0.978, so the neuron is nearly sure they'll leave.

The name "neuron" comes from a loose comparison with brain cells. A brain cell adds up incoming signals and fires once they pass a threshold. The comparison stops there. An artificial neuron is nothing more than the formula above.

Quick check
In the churn neuron, the weight on orders per year is −0.6. What does the minus sign tell you?
  1. More orders per year push the churn chance down
  2. Customers with few orders are left out
  3. Orders per year have no effect on the output
  4. The neuron's output can be negative
Show the answer

More orders per year push the churn chance down

A negative weight pushes the output down as the input grows. So a customer who orders often gets a lower churn chance.

A neuron draws a straight line

The sigmoid gives more than 0.5 exactly when zz is above 0. So every possible input lands on one of two sides. Where z>0z > 0, the neuron says "yes". Where z<0z < 0, it says "no". The dividing line, where zz is exactly 0, is called the decision boundary: w⋅x+b=0\mathbf{w} \cdot \mathbf{x} + b = 0.

In plain words: the boundary is every input where the weighted sum plus the bias comes to exactly zero. On one side the neuron says yes, and on the other it says no.

With two inputs, that boundary is always a straight line. With three inputs, it's a flat sheet, a plane. With more inputs, it's the same kind of flat cut in more directions, called a hyperplane. So a single neuron can split two groups only if a straight line (or flat cut) can split them. Groups that can be split this way are called linearly separable.

Here's a small example. Take weights w1=1w_1 = 1 and w2=1w_2 = 1, and bias b=−1.5b = -1.5. Then z=x1+x2−1.5z = x_1 + x_2 - 1.5. Feed in each corner of a square, where both inputs are 0 or 1. Only (1,1)(1, 1) makes zz positive: 1+1−1.5=0.51 + 1 - 1.5 = 0.5. The line x1+x2=1.5x_1 + x_2 = 1.5 cuts that one corner off from the other three.

w1, w2, b = 1.0, 1.0, -1.5   # a hand-picked neuron
for x1, x2 in [(0, 0), (0, 1), (1, 0), (1, 1)]:
    z = w1 * x1 + w2 * x2 + b
    print((x1, x2), z, "yes" if z > 0 else "no")

It prints no for the first three corners and yes for (1,1)(1, 1).

Three tiny logic problems

Now let a neuron learn its own line. Each problem below has the same four inputs: (0,0), (0,1), (1,0) and (1,1). Each one wants a different answer, 0 or 1, for each input:

  • AND gives 1 only when both inputs are 1.
  • OR gives 1 when at least one input is 1.
  • XOR, short for "exclusive or", gives 1 when exactly one input is 1. A hallway light with a switch at each end can be wired this way.

The code trains one neuron per problem with gradient descent. The update lines are the same kind you used for logistic regression. In each plot, red dots want 1 and blue dots want 0. The gray line is the boundary the neuron learned.

import numpy as np
import matplotlib.pyplot as plt

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=float)
problems = {"AND": [0, 0, 0, 1], "OR": [0, 1, 1, 1], "XOR": [0, 1, 1, 0]}

def train(X, y, lr=1.0, steps=5000):
    w, b = np.zeros(2), 0.0
    for _ in range(steps):
        p = 1 / (1 + np.exp(-(X @ w + b)))
        w -= lr * X.T @ (p - y) / len(y)
        b -= lr * np.mean(p - y)
    return w, b

fig, axes = plt.subplots(1, 3, figsize=(10, 3.3))
for ax, (name, y) in zip(axes, problems.items()):
    y = np.array(y, dtype=float)
    w, b = train(X, y)
    pred = (1 / (1 + np.exp(-(X @ w + b))) > 0.5).astype(int)
    print(f"{name:<4} weights {np.round(w, 1)}  bias {b:5.1f}  predictions {pred}  correct {int((pred == y).sum())}/4")
    ax.scatter(X[:, 0], X[:, 1], c=y, cmap="coolwarm", s=120, edgecolors="gray")
    if abs(w[1]) > 1e-6:
        xs = np.linspace(-0.5, 1.5, 2)
        ax.plot(xs, -(w[0] * xs + b) / w[1], color="gray")
    ax.set_xlim(-0.5, 1.5)
    ax.set_ylim(-0.5, 1.5)
    ax.set_title(name)

AND and OR come out perfect, 4 out of 4. One straight line puts the 1s on one side and the 0s on the other. The AND neuron learns a weight of about 10.7 on each input and a bias of about −16.2. That's close to the hand-picked line x1+x2=1.5x_1 + x_2 = 1.5, with bigger numbers. Bigger numbers push the outputs closer to 0 and 1.

XOR fails. Its 1s sit on opposite corners, (0,1) and (1,0). Its 0s sit on the other two corners. No straight line can put both 1s on one side and both 0s on the other. The neuron's weights and bias stay at 0, so it outputs 0.5 for every input. That counts as a 0 every time, so it gets only 2 of the 4 right. Its plot has no line at all.

Go deeper Why no line can solve XOR

Suppose some weights w1w_1, w2w_2 and bias bb did work. The four corners would need:

  • (0,0)(0, 0) says no, so b≤0b \le 0;
  • (0,1)(0, 1) says yes, so w2+b>0w_2 + b > 0;
  • (1,0)(1, 0) says yes, so w1+b>0w_1 + b > 0;
  • (1,1)(1, 1) says no, so w1+w2+b≤0w_1 + w_2 + b \le 0.

Here "says no" means zz is zero or below, and ≤\le means "at most". Add the two "yes" lines: w1+w2+2b>0w_1 + w_2 + 2b > 0. But w1+w2+2bw_1 + w_2 + 2b is (w1+w2+b)+b(w_1 + w_2 + b) + b. Both parts are zero or below, so their total is zero or below too. It can't also be above zero. So no weights and bias can do it.

Quick check
Why can't a single neuron learn XOR?
  1. Its decision boundary is a straight line, and no straight line separates XOR's classes
  2. The sigmoid can't output exactly 0 or 1
  3. XOR needs more than 4 training examples
  4. The learning rate was too small
Show the answer

Its decision boundary is a straight line, and no straight line separates XOR's classes

A neuron separates the two groups with a straight line (a hyperplane, with more inputs). XOR's 1s sit on opposite corners, so no line can split them from the 0s.

Why XOR mattered

In 1969, Marvin Minsky and Seymour Papert published a book called Perceptrons. It showed that single-layer networks can't represent patterns like XOR. The book helped cause years of reduced interest in neural networks.

The fix was known in principle: stack neurons in layers. Then later neurons can combine the lines drawn by earlier ones. But training stacked layers needed a fast way to work out every weight's gradient: how much the error changes when that weight changes. Backpropagation, made popular in 1986, provided it. The next few lessons build exactly that.

From one neuron to an LLM

Roughly speaking, a large language model is a huge number of these units arranged in layers. Each one takes a weighted sum of its inputs and applies an activation. The model isn't capable because each unit is smarter. It's capable because of how many units there are, how they're wired, and the training that sets billions of weights.

For marketers: a lead score that adds up weighted signals and squashes the result to 0–1 is a single neuron. It works when the signals add up independently. Sometimes the answer depends on a combination. Say a big company is a good lead when its buying team is small, but a poor lead when the team is large. For a small company, it's the other way around. That flip is the XOR pattern, and no weighted sum can capture it. Then you need more than one neuron.

Practice what you just read

  • 4 code examples you can edit and run
  • a practice exercise that checks your code
  • a 5-question quiz
  • 4 review cards for spaced repetition

Start this lesson

Try a quiz question

Which Phase 3 model is a single neuron with a sigmoid activation?

  1. Logistic regression
  2. k-means
  3. A decision tree
  4. k-nearest neighbors

Answer it at the end of the lesson to see if you're right, and why.

Before this lesson