Nhận tư vấn

Understand LLMs from Zero#26

What Is a Neuron? Weights and Biases in a Neural Network

The smallest part of a neural network is the neuron. Inside, it multiplies its inputs by weights, adds everything up, and adds a bias at the end. With two inputs, its border is one straight line. Move the weights and the bias yourself and try to separate real words.

  • POINT 1A neuron multiplies its inputs by weights, adds everything up, and adds a bias. Nothing more.
  • POINT 2With two inputs, the border between positive and negative output is one straight line. The weights turn it; the bias shifts it sideways.
  • POINT 3One neuron that takes a whole row (2,560 numbers) as its input sorted 84 of 85 words it had never seen into animal or food. But all it can draw is a straight border.

TRY 1

Move the weights until the line fits

The dots are real words from Gemma 3's embedding table (orange = animals, blue = foods). Their 2,560 numbers were projected to two numbers and placed on a map. Move the sliders and the neuron's border moves. The orange side of the line is judged “animal”, the blue side “food”. How few mistakes can you get?

Orange = animal words, blue = food words, red ring = a mistake. Press a dot to see that word's calculation below.

mistakes 85 / 172corrections: 0

On this map one straight line can separate everything. Aim for 0 mistakes.

Words that are wrong right now

…

Pick a word to see the neuron's calculation

Pick a word.

“Correct once” goes through the words in a fixed order and, at the first wrong one, sets weights ← weights ± the position of that word, bias ← bias ± 1 (plus for an animal, minus for a food). In the video this started from all zeros and took 4 corrections. The training words: 82 animals and 90 foods. The map coordinates are the 2,560 numbers of each row reduced to two by principal component analysis, times 10.

TRY 2

A neuron by hand

The “shall we see a movie?” example from the video. Switch the answers, change the weights and the bias, and watch the output move. If the output is above 0, you go. The numbers are an example, not measurements.

It's sunny+2
A movie I want to see+3
I'm tired−4
bias (always added)+1

1 × 2 + 0 × 3 + 1 × (−4) + 1 = −1

−9−8−7−6−5−4−3−2−10123456789

The output is −1. Not above 0, so you stay home.

Even when every input is “no”, the bias still ends up in the output. An input with a negative weight lowers the output when it is “yes”.

TRY 3

A neuron with 2,560 inputs

Not the two numbers of the map: this neuron takes all 2,560 numbers of a row as its inputs, and has 2,560 weights. After correcting it the same way, we gave it words it had never seen.

The 172 training words were all correct after only 12 corrections (2,560 inputs; the bias ended at 0).

84 / 85

Of 85 words it had never seen, 84 are correct. The one it missed is “cricket” (output −0.08).

wordtruthoutput
elephantanimal +0.43✓
wolfanimal +0.42✓
vultureanimal +0.39✓
coyoteanimal +0.38✓
sharkanimal +0.37✓
butterflyanimal +0.37✓
spideranimal +0.37✓
donkeyanimal +0.36✓
snakeanimal +0.36✓
lionanimal +0.35✓
owlanimal +0.34✓
camelanimal +0.34✓
buffaloanimal +0.34✓
rabbitanimal +0.34✓
scorpionanimal +0.34✓
dolphinanimal +0.34✓
squirrelanimal +0.33✓
gorillaanimal +0.33✓
froganimal +0.31✓
bearanimal +0.31✓
turtleanimal +0.29✓
catanimal +0.28✓
apeanimal +0.27✓
tickanimal +0.27✓
falconanimal +0.22✓
hedgehoganimal +0.21✓
doganimal +0.21✓
slothanimal +0.21✓
cobraanimal +0.20✓
mouseanimal +0.19✓
mareanimal +0.18✓
pandaanimal +0.17✓
mooseanimal +0.17✓
ravenanimal +0.16✓
hamsteranimal +0.12✓
antanimal +0.11✓
crowanimal +0.10✓
jellyfishanimal +0.06✓
starfishanimal +0.06✓
octopusanimal +0.06✓
rumfood −0.04✓
cricketanimal −0.08✗
mushroomfood −0.11✓
eggfood −0.19✓
raspberryfood −0.23✓
apricotfood −0.27✓
fruitfood −0.27✓
limefood −0.27✓
carrotfood −0.29✓
plumfood −0.30✓
teafood −0.31✓
lemonfood −0.33✓
beerfood −0.33✓
vanillafood −0.34✓
brandyfood −0.35✓
lasagnafood −0.36✓
noodlefood −0.36✓
ciderfood −0.37✓
piefood −0.37✓
champagnefood −0.39✓
hamburgerfood −0.40✓
meatfood −0.41✓
cucumberfood −0.41✓
burritofood −0.42✓
beanfood −0.42✓
vinegarfood −0.43✓
mustardfood −0.43✓
cookiefood −0.43✓
wafflefood −0.44✓
onionfood −0.44✓
toastfood −0.46✓
cheesefood −0.46✓
donutfood −0.46✓
margarinefood −0.48✓
espressofood −0.48✓
lettucefood −0.50✓
juicefood −0.52✓
candyfood −0.53✓
cinnamonfood −0.55✓
porridgefood −0.59✓
biscuitfood −0.59✓
mayonnaisefood −0.60✓
cornfood −0.61✓
wheatfood −0.65✓
pastafood −0.65✓

All 262,144 tokens of Gemma through the neuron

The 40 largest outputs (green frame = never taught)

seal+1.02stork+1.01caterpillar+0.98yak+0.97Seal+0.83seal+0.82Seal+0.80sheep+0.80seals+0.77flea+0.76Yak+0.73sealing+0.73Seals+0.69Yak+0.68Sheep+0.67SEAL+0.64sealed+0.64Sheep+0.62sheep+0.61sealed+0.60caterpillars+0.56Caterpillar+0.54Sealed+0.52yak+0.49wildlife+0.48beetle+0.46tiger+0.44羊+0.44insect+0.44elephant+0.43Tiger+0.42wolf+0.42eagle+0.42worm+0.41Wildlife+0.41யான+0.40sealer+0.40deer+0.40livestock+0.40Lion+0.39

The 40 smallest outputs (green frame = never taught)

fries−1.51gravy−1.47peas−1.34flour−1.31cream−1.24pretzel−1.19pretzels−1.12Cream−1.08Cream−1.01cream−0.92creamy−0.92potatoes−0.90creams−0.90CREAM−0.89クリーム−0.82creme−0.82Flour−0.80biscuits−0.79Fries−0.78peas−0.78sauces−0.77crème−0.77beans−0.77fries−0.77mashed−0.77Peas−0.77harina−0.76Potatoes−0.74Creme−0.74loaves−0.74pea−0.73muffins−0.73frosting−0.73lotion−0.73tortillas−0.73sauce−0.73crema−0.72noodles−0.72bagels−0.72waffles−0.71

Careful: all this neuron was taught is “animal or food”. It sorts unrelated words to one side or the other as well. Of all 262,144 tokens, 119,120 (45%) got a positive output (the animal side); the other 55% landed on the food side.

desk−0.01 → foodTokyo+0.00 → animalrun−0.06 → foodblue−0.05 → foodmusic−0.04 → foodmath−0.09 → foodcar+0.10 → animalmountain+0.19 → animalbook+0.10 → animalthe−0.02 → food

It was taught with English words only, yet the top and the bottom hold words of similar meaning in other languages. Words with similar meanings have similar rows, whatever the language (episode 21). Tokens that share the spelling of a training word, such as “sealed” next to “seal”, come along too.

TRY 4

The neurons inside Gemma

The matrix of episode 23 was a bundle of neurons.

10,240 × 68 = 696,320

One row of a matrix with 10,240 rows and 2,560 columns is the weights of one neuron (2,560 of them). Multiplying by the matrix means computing 10,240 neurons at once. Gemma 3 4B has 68 matrices of this size; together they have 696,320 rows.

The first 8 numbers of a real row (row 1 of blk.0.ffn_gate.weight); there are 2,560 in all:

0.0073 0.0014 0.0131 −0.0161 0.0014 −0.0044 −0.0103 0.0014 …

Of the 444 tensors in the file, 0 are a bias. Gemma works without biases, with weights only. Our own neuron in TRY 3 also ended with a bias of 0.

Many recent LLMs leave the bias out. How the border gets bent (activation functions) is next time.

CHECK

Check yourself

Three questions about what this episode showed.

Q1What does a neuron compute?

Q2In a neuron with two inputs you change only the bias. What happens to the border line?

Q3Which borders can a single neuron make?

The calculations of TRY 1 and 2 run in your browser; nothing is sent to a server. The word positions and the numbers of TRY 3 and 4 were computed beforehand from the embedding table and the file of Gemma 3 4B (gemma-3-4b-it-Q4_K_M.gguf).

FINISH

Finished the episode?

COMMENTS

Comments & questions

Something unclear, something you want to know more about, or just your thoughts: write it here. We answer quickly, and other learners can reply too.

Loading…