Activation Functions

1 min

Objective: choose an activation and understand saturation, gradients and output.

Hidden activations

ActivationExpression/ideaUse and caution
ReLUmax(0, z)simple, fast; dead neurons possible
LeakyReLUsmall slope if z<0reduces permanently zero units
tanhoutput [-1, 1]centered, but saturates at extremes
sigmoidoutput [0, 1]mainly binary output; saturation
GELUsmooth modulationcommon in Transformers

The derivative of ReLU is 1 for z>0 and 0 for z<0. A unit that always receives negative pre-activations no longer transmits any gradient: it is "dead".

Outputs

  • Unbounded regression: linear output.
  • Binary probability: sigmoid for interpretation, but logits for BCEWithLogitsLoss.
  • Exclusive classes: softmax to read the probabilities, but logits for CrossEntropyLoss.
  • Multilabel: one independent sigmoid per label.

Softmax

softmax(z_i) = exp(z_i) / Σ_j exp(z_j) turns K logits into positive numbers that sum to 1. Adding the same constant to all logits does not change the probabilities. Stable implementations subtract the maximum before the exponential.

Practical rule

Use ReLU as a baseline for an MLP/CNN, GELU for a Transformer, and only place an output activation if the loss or interface explicitly requires it.

Quick check

Why must activations be non-linear? Which activation produces exclusive probabilities?

Answers

Otherwise the entire network reduces to an affine transformation. Softmax produces a distribution over exclusive classes.

Mastery activity — Choice and computation

Compute ReLU and sigmoid for z = {-2, 0, 2}, then the stable softmax of [1000, 1001, 999]. Then match an output to four tasks: binary, multiclass, multilabel and unbounded regression. Success: no double activation before a logit-based loss.

Choosing the activation in the right place

Reading: the loss function often dictates the expected numerical format. With stable losses that take logits, sigmoid or softmax are for interpretation, not for the loss input. The main trap is therefore the double activation, which degrades gradients and can give misleading probabilities.