Parlons-en

Understand LLMs from Zero#27

Activation Functions (ReLU, GELU): Nonlinearity in Neural Networks

One neuron could only draw a straight border. Connecting neurons does not help either: with nothing in between, the border is still one straight line. Put a small function that is “not straight”, an activation function, at the exit of each neuron, and the output folds and the border bends. Switch the function and compare the shapes.

  • POINT 1Without an activation function, any chain of neurons can be rewritten as one neuron. The border stays one straight line.
  • POINT 2ReLU: negative becomes 0, positive passes unchanged. With this bend (nonlinearity) in between, the neurons can no longer be rewritten as one.
  • POINT 3Same network, same start, same 4,000 training steps: without an activation 12 mistakes, with ReLU or GELU 0 mistakes. Gemma uses GELU.

TRY 1

Switch the function and compare the shape of the output

12 red marbles are surrounded by 20 blue ones (the layout is an example). A small network, 2 inputs → 3 neurons → 1 neuron, was really trained on this layout. The only thing that differs is the activation function at the exit of the 3 neurons. Switch it, and the shape of the output (the sheet in 3D) and the border (the golden line on the map) change.

Choose the activation function

Reddish area = output positive (judged red), bluish area = negative (judged blue). Golden line = the border, white dotted lines = the fold of each neuron, white ring = a marble on the wrong side.

mistakes 0 / 32ReLU

What the 3D view shows

With ReLU, there are 0 mistakes from correction 498 to the end. The border goes around the red marbles.

Output of the network: (−3.95) × f(neuron 1) + (−3.74) × f(neuron 2) + 4.19 × f(neuron 3) + (−3.38)

Every function was trained from the same start (the same first weights), in the same way, for 4,000 corrections. The slider walks through the record of that training. The height scale of the 3D sheet adjusts itself so that the shape is easy to see (parts far below the table are cut off). There are 12 red and 20 blue marbles. GELU is computed with the tanh formula, as in Gemma.

TRY 2

The functions themselves

An activation function takes one number and returns one number. Move the input and read the output. “None” is a straight line, ReLU a line bent at 0, GELU the same with its corner rounded off.

−4−3−2−11234−11234

GELU(−1.00) = −0.159

functionoutput for input −1.00what it does
none−1.000returns the number as it is (straight)
ReLU0.000negative becomes 0, positive stays
GELU−0.159ReLU with a rounded corner; lets a little of the negative through
tanh−0.762an S shape between −1 and 1
sigmoid0.269an S shape between 0 and 1

GELU is lowest around an input of −0.75, where it gives −0.17. For input −1 it gives −0.16 (ReLU: 0), for input 3 it gives 2.996 (ReLU: 3).

Sigmoid and tanh are the smooth S-shaped functions that were common in the past. Today's LLMs mostly use relatives of ReLU, such as GELU. All values are computed on this page.

TRY 3

Two neurons in a row

The example of the video: the first neuron doubles and adds 1, the second triples and subtracts 4. Whatever numbers you choose, with nothing in between they can always be rewritten as one neuron. What happens with ReLU or GELU in between?

What sits between the two

−4−3−2−11234

1.0 × 2 + 1 = 3.0 → × 3 + (−4) = 5.0

These two can be rewritten as one neuron: x × 6 + (−1) (for the current input: 5.0). The graph is straight.

“Multiply and add”, repeated any number of times, is still one “multiply and add”. That is why, without an activation function, any chain of neurons keeps a straight border.

TRY 4

Different starts; inside Gemma

The training of TRY 1 used one fixed start (the first weights, chosen at random). Here the same training was repeated from 20 different starts. Green = it reached 0 mistakes, red = it did not.

activation3 neurons8 neurons
none0 / 200 / 20
ReLU9 / 2020 / 20
GELU15 / 2020 / 20
tanh20 / 2020 / 20
sigmoid20 / 2020 / 20

Without an activation it fails all 20 times, however many neurons there are (the border is one straight line). With 3 neurons ReLU succeeds 9 times and GELU 15 times. With 8 neurons both succeed all 20 times. More neurons are next time's topic.

The activation function inside Gemma

"hidden_activation": "gelu_pytorch_tanh"

10,240 × 34 = 348,160

The configuration file of Gemma 3 4B names GELU (in its tanh form) as the activation function. Gemma has 34 floors; every floor has a matrix with 10,240 rows, and a GELU sits at each of its exits. That makes 348,160. Every time Gemma reads one token, it passes through that many GELUs.

To be exact: the “multiply and add” of every floor has two matrices with 10,240 rows, and the GELU sits at the exits of one of them. The numbers that went through GELU are multiplied by the outputs of the other matrix before they move on (more about this in a much later episode).

CHECK

Check yourself

Three questions about what this episode showed.

Q1You connect 100 neurons without any activation function. What do you get?

Q2You put −2 into ReLU. What comes out?

Q3How does GELU differ from ReLU?

The calculations of TRY 1 to 3 run in your browser; nothing is sent to a server. The weights of the networks are the record of a real training done beforehand (the layout of the marbles is an example). Gemma's activation function was checked in the configuration file of Gemma 3 4B and in the code of llama.cpp.

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…