ReLU, sigmoid, tanh and GELU
What it is
Activations transform a layer's pre-activation values before they enter the next layer. ReLU is max(0, x) and is a strong default for many hidden layers because it is simple and has a healthy positive-side gradient; sigmoid maps to (0, 1), tanh to (-1, 1), and GELU is common in Transformers because it smoothly gates values. The final activation is chosen to match the output distribution: sigmoid for one binary probability, softmax for mutually exclusive classes, and often no activation at all when a stable loss function expects logits.
Why it matters
Without an activation, any number of linear layers collapses algebraically to one linear transformation. Activations add the bends that let networks carve nonlinear decision regions and represent complex functions. Their gradient behaviour also matters: saturated sigmoid or tanh units receive little gradient, while a ReLU that stays negative can stop learning entirely for that input region.
In the real world: The small functions that let networks learn complex patterns, like telling a genuine review from a fake one.
Visual example
Activation shapesThe shape determines which signals pass and which are suppressed.
ReLU is zero for negative inputs; sigmoid and tanh saturate at their extremes.
import torch, torch.nn as nn
z = torch.tensor([-2.0, -0.5, 0.0, 0.5, 2.0])
print(nn.ReLU()(z)) # tensor([0.0000, 0.0000, 0.0000, 0.5000, 2.0000])
print(nn.Sigmoid()(z)) # tensor([0.1192, 0.3775, 0.5000, 0.6225, 0.8808])
print(nn.Tanh()(z)) # tensor([-0.9640, -0.4621, 0.0000, 0.4621, 0.9640])
print(nn.GELU()(z)) # tensor([-0.0455, -0.1543, 0.0000, 0.3457, 1.9545])Putting sigmoid before BCEWithLogitsLoss. That loss already applies the numerically stable sigmoid internally; applying it twice weakens gradients and gives the wrong objective.
Practise it: ReLU, Forward and Backward