Lesson 1 of 188 min
The artificial neuron
A neuron multiplies each input by its own weight, adds them up with a bias, and bends the total: a sum you can work out by hand.
After this lesson you can compute a neuron's output from its inputs, weights, bias and activation function.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
The total as one line
Books write a neuron's total as a sum: each input times its weight, all added, plus the bias. In code that sum is a dot product. Here it is for the two-input penguin neuron, on one Chinstrap's bill, then the S-curve on it.
import torch
x = torch.tensor([46.5, 17.9])
w = torch.tensor([1.30, -1.79])
b = -24.47
total = torch.dot(x, w) + b
print(round(total.item(), 2), round(torch.sigmoid(total).item(), 2))3.94 0.98
Other bends you will meet
ReLU and the S-curve are not the only bends. tanh is an S-curve from −1 to 1. GELU and SiLU are smooth ReLUs. Leaky ReLU keeps a small slope below zero, so a neuron can't die. Here each one is on −2 and 0.5.
from torch import nn
t = torch.tensor([-2.0, 0.5])
for bend in (nn.Tanh(), nn.GELU(), nn.SiLU(), nn.LeakyReLU(0.01)):
print(type(bend).__name__, [round(v, 3) for v in bend(t).tolist()])Tanh [-0.964, 0.462] GELU [-0.046, 0.346] SiLU [-0.238, 0.311] LeakyReLU [-0.02, 0.5]
Count the dead neurons in a layer
A neuron is dead when ReLU gives it zero for every input you have. So put all your inputs through the layer, take each neuron's largest answer, and count the zeros. A fresh layer on the digits has none. Push every bias down by 5 and all 32 die.
from sklearn.datasets import load_digits
torch.manual_seed(0)
X = torch.tensor(load_digits().data / 16, dtype=torch.float32)
layer = nn.Linear(64, 32)def dead(layer):
return int((torch.relu(layer(X)).max(0).values == 0).sum())
print(dead(layer))
with torch.no_grad():
layer.bias -= 5
print(dead(layer))0 32
Why the weights are 32 by 64
A Linear layer keeps one row of weights for each neuron, so its weights are (outputs, inputs). To use them on a row of inputs, it turns them on their side: x @ W.T + b. That is the matrix times its input, plus a number for each output.
layer = nn.Linear(64, 32)
x = X[:1]
print(layer.weight.shape, layer.bias.shape)
print(torch.allclose(layer(x), x @ layer.weight.T + layer.bias))torch.Size([32, 64]) torch.Size([32]) True
A layer with no bias
bias=False makes a layer with weights only. You will see it in big models, often right before a normalization step, which adds its own shift; the Normalization lesson says why. The layer then keeps 2,048 numbers instead of 2,080.
layer = nn.Linear(64, 32, bias=False)
print(layer.bias, sum(p.numel() for p in layer.parameters()))None 2048
Run on Python 3.13.3, PyTorch 2.15.0 and scikit-learn 1.9.1. Checked on .
Quiz
Two questions on this lesson, and one from earlier. Answer as often as you like.
1 of 3
From earlier: The dot product
You rate a comedy and a horror movie [2, 1]. Sam rates them exactly like you: dot product 5. Max rates them [2, 2]: dot product 6. Does Max's taste fit yours better than Sam's?
2 of 3
A neuron that finds zeros has its 64 weights drawn as a picture: plus in a ring, minus in a hole in the middle. A 0 and a 1 come in. Which one gets the bigger total?
3 of 3
A neuron reads two inputs, 3 and 2. Their weights are 2 and −1, and its bias is −1. It bends its total with ReLU. What does it say?
The ideas in this lesson
- Artificial neuron
- It multiplies each input by its weight, adds them up with a bias, then bends the total.
- What a neuron looks for
- Its total is biggest when the input looks like its weights, so the weights draw the pattern it finds.
- Activation function (ReLU)
- The bend after a neuron's total; ReLU keeps a total above zero and turns one below zero into 0.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
Deep Learning
How does a neural network actually work? Each part is simple enough to work out by hand. There are just a lot of them. Hi, I'm Mohi Fayden. Deep learning means training these networks on lots of examples. Deep means they have many layers. It's what runs chatbots and image makers. This course teaches you to build and train them in PyTorch. By the end, you'll train one and fix it when it breaks. To start, you need a little Python and school math. Every lesson has its own page on faydenai.com, with a quiz. There's a lab every few lessons, and a project at the end.
The artificial neuron
Here's a game. A neuron learned to find pictures of a zero. It gives each square of the picture a weight. Now draw those weights as a picture. What will you see?
Keep your guess
Hold on to your guess. We'll draw those weights soon. First, one neuron with just one input.
One input, a weight and a bias
A neuron is the smallest part of a neural network. This one reads a penguin's bill, forty-five millimeters long. It will give the chance that the penguin is a Chinstrap, one of two kinds. First, it multiplies the bill by its weight, a number it learned. Then it adds one more number, its bias. The bias stays the same, no matter the input. Here, it's a negative number. It's like the price of a taxi ride: so much for each mile, plus a fixed amount to start. What comes out is the neuron's total. Here, zero point seven five.
A bend
Then the neuron bends its total. This one uses the S-curve. It turns any total into a number between zero and one. A total of zero gives exactly one half. So what does a total of zero point seven five give? A little more than half, or nearly all?
Sixty-eight percent: the chance that this penguin is a Chinstrap. This neuron is exactly the logistic regression from the Machine Learning course. So logistic regression is one neuron. The bend... is called the activation function.
Each input its own weight
And with more than one input? Add how deep the bill is. It gets its own weight. With two inputs, the neuron learns new weights, and a new bias. It multiplies each input by its own weight, adds them up, and adds the bias. Take one Chinstrap. The length times its weight, plus the depth times its weight, plus the bias. Together, almost four. That's ninety-eight percent. Multiply the matching numbers and add them up: that's the dot product. Here, of the inputs and the weights. The depth's weight is a negative number, so a deeper bill lowers the total. A taxi ride's price never does that. That's all a neuron is. Multiply, add, and bend.
The weights are a picture
A neuron gives each input a weight, adds them up with a bias, and bends the total.
Now, back to the guess from the start. A picture of a digit here is sixty-four pixels. A pixel is one square of the picture. It's a number for how dark it is: zero for paper, up to one for full ink.
The neuron that finds zeros reads all sixty-four pixels, so it has sixty-four weights.
Draw them as squares, plus weights in blue and minus weights striped. A ring of plus... and a hole of minus in the middle. That's where a zero has ink, and where it has none.
Many people guess random noise. But you can read one neuron's weights.
A big total means a match
Now a zero and a one come in. Which one gets the bigger total?
Most of the zero's ink lands on the ring, on plus weights. With the bias, its total is about one point seven. Eighty-five percent: most likely a zero. The one's ink falls down the middle, onto the minus weights. Its total is about minus six: under one percent. The neuron isn't perfect, though. A few zeros get a total below zero too.
So the total is big when the picture looks like the weights. It's the dot product again: big when two lists point the same way. The weights are the pattern the neuron looks for.
ReLU
A neuron adds up its inputs times its weights, then bends the total. That bend is its activation function. So far, it's been the S-curve. But inside a network, most neurons bend with ReLU. A total above zero stays as it is. A total below zero becomes zero. At forty-five millimeters, the total is zero point seven five, so ReLU says zero point seven five. At forty millimeters, the total is below zero. What does ReLU say?
Zero. And ReLU's number is not a chance. In a network, it goes on to other neurons.
Why ReLU
So why not the S-curve? Far from its middle, it goes flat, on both sides. Slide the bill from forty-nine to fifty millimeters. The S-curve's answer is already above ninety-nine percent, and it hardly changes. Its slope there, how much it moves for a small nudge, is almost zero. Training moves a weight by how much the neuron's answer changes when the weight is nudged. Here, that's almost nothing, so the weights hardly move at all. ReLU is only flat on one side. Above zero, its slope is always one: its answer moves exactly as much as its total. So the same millimeter moves ReLU's answer by about one. Today's big language models bend with a smooth ReLU.
A dead neuron
And the flat side? Push the bias down to minus seventy. Spot the problem.
Every penguin's total is now below zero. So ReLU says zero for all one hundred and sixty-four of them. And its slope there is zero, so training can never move this neuron again. It's a dead neuron. It happens in real networks. Take a network that reads digits, with thirty-two neurons in its middle. Train it with a learning rate that's too big, and twenty-six of the thirty-two end up dead. With the usual learning rate, only four of them do.
In code
In PyTorch, a neuron's multiply and add is a Linear layer. Make one with one input and one output, and give it the penguin neuron's weight and bias. Put a bill of forty-five millimeters through it, then through the S-curve, which PyTorch calls sigmoid. What will it print?
About zero point six eight: the same sixty-eight percent. ReLU on minus two and zero point five gives zero and zero point five.
And a Linear layer with sixty-four inputs and thirty-two outputs? That's thirty-two neurons. They all read the same sixty-four pixels at once.
Its weights are thirty-two rows of sixty-four, one row for each neuron. That's the layer from the Linear Algebra course: a matrix times its input, plus a number for each output.
Three ideas
So, a neuron multiplies each input by its weight, adds them up with a bias, and bends the total. Its total is big when the input looks like its weights. And inside a network, the bend is mostly ReLU.
Multiply, add, bend
A neuron adds up its inputs times their weights, then bends. Now try it on this lesson's page, and take the quiz. Find the zero that it misses.
You use this lesson in Lab 1: A digit reader, part by part, after Softmax and cross-entropy.
Facts last checked on .
