liveBook · Manning

ReLU

As explained in Learn Generative AI with PyTorch

A piecewise linear neural-network activation function that outputs zero for negative inputs and the input value for nonnegative inputs.

ReLU, or rectified linear unit, is a piecewise linear activation function: it outputs zero for negative inputs and the input value for nonnegative inputs. It helps neural networks learn nonlinear relationships by deciding whether a neuron turns on from its weighted sum. section 2.3.2 section 2.5

A common pattern uses ReLU in hidden layers and sigmoid at the final layer. ReLU supplies hidden-layer nonlinearity, while sigmoid converts the final value into a probability for binary classification. section 2.3.2

A practical PyTorch network applies ReLU between dense layers:

  • Flatten each 28 × 28 image into 784 values before the first dense layer. section 2.3.2
  • Pass activations through dense layers with 256, 128, and 32 neurons, applying ReLU after each hidden layer. section 2.3.2
  • Finish with one output, dropout, and sigmoid for ankle-boot versus t-shirt prediction. section 2.3.2

ReLU in a discriminator

A PyTorch discriminator can use ReLU after each of three dense layers, with dropout between layers, then produce one sigmoid output representing the probability that a sample is real. section 3.3.1

Listing 2.5 Creating a binary classification model
import torch.nn as nn
  
device="cuda" if torch.cuda.is_available() else "cpu"  #1
  
binary_model=nn.Sequential(                            #2
    nn.Linear(28*28,256),                              #3
    nn.ReLU(),                                         #4
    nn.Linear(256,128),
    nn.ReLU(),
    nn.Linear(128,32),
    nn.ReLU(),
    nn.Linear(32,1),
    nn.Dropout(p=0.25),
    nn.Sigmoid()).to(device)                           #5

Inspect the complete sequential binary-classification model, including three nn.ReLU layers and the final sigmoid. section 2.3.2

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains ReLU through weighted-sum decisions, nonlinear outputs, layer placement, and code, without deriving optimization behavior.
Examples
Demonstrates ReLU in an ankle-boot classifier and a two-dimensional discriminator network.
What it assumes
Assumes basic neural-network layers and weighted sums while introducing ReLU's activation role alongside tensors and model training.
What you can do after
Apply ReLU via PyTorch's nn.ReLU in hidden layers of binary classifiers and discriminator networks.
How it connects
Ties ReLU to neural networks, dense layers, sigmoid, dropout, binary classification, and generative-adversarial discriminators.

Also explained in

Compare how 12 books explain ReLU →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.