liveBook · Manning

ReLU

As explained in LLMs in Production

A rectified linear unit is an activation function that outputs zero for negative inputs and otherwise passes through the input.

ReLU, or rectified linear unit, outputs zero for negative inputs and otherwise passes the input through. It commonly sits inside a neural network as an activation function. section 2.2.4 section 9.1.3

ReLU is a practical default, not a universal answer: it helps with vanishing gradients but leaves exploding gradients unresolved and removes negative comparisons from the model. section 2.2.4

Choose ReLU with these constraints in mind:

  • Use it when a simple, familiar activation is appropriate for a neural-network training loop. section 2.2.4
  • Do not treat it as a solution to exploding gradients; that failure mode remains. section 2.2.4
  • Consider ELU when retaining negative values matters, or GEGLU and SWIGLU for increasingly difficult language scenarios. section 2.2.4

Two small language models

A NumPy continuous CBoW model places ReLU alongside weight initialization, softmax, forward propagation, and backpropagation. A compact feed-forward language model places ReLU between two linear layers before producing vocabulary logits. section 2.2.4 section 9.1.3

Listing 9.3 Simple model and training loop
class SimpleFeedForwardNN(nn.Module):
    def __init__(self, config=MASTER_CONFIG):
        super().__init__()
        self.config = config

        self.embedding = nn.Embedding(
            config["vocab_size"], config["d_model"]
        )
        self.linear = nn.Sequential(
            nn.Linear(config["d_model"], config["d_model"]),
            nn.ReLU(),
            nn.Linear(config["d_model"], config["vocab_size"]),
        )

        print(
            f"model params: {sum([m.numel() for m in self.parameters()])}"
        )

    def forward(self, idx, targets=None):
        x = self.embedding(idx)
        logits = self.linear(x)

        if targets is not None:
            loss = F.cross_entropy(
                logits.view(-1, self.config["vocab_size"]),
                targets.view(-1),
                ignore_index=tokenizer.pad_token_id,
                # reduction="sum",
            )
            return logits, loss

        else:
            return logits


model = SimpleFeedForwardNN(MASTER_CONFIG).to(device)
opt_model = torch.compile(model)           #1

Inspect the simple model implementation to see ReLU positioned between the embedding projection and vocabulary-output layer, alongside cross-entropy loss. section 9.1.3

How LLMs in Production covers it

How deep it goes
Defines behavior and implementation placement, but assumes activation-function familiarity and defers a full explanation.
Examples
Uses a NumPy CBoW language model and a PyTorch two-layer feed-forward language model.
What it assumes
Assumes familiarity with machine-learning paradigms and activation functions rather than teaching ReLU from zero.
What you can do after
Build it from scratch in a training loop or place it between linear layers in a small language model.
How it connects
Connects activation functions with vanishing gradients, exploding gradients, ELU, GEGLU, and SWIGLU.

Also explained in

Compare how 12 books explain ReLU →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of LLMs in Production. The section links point to the parts of the book it draws on.