Activation functions in neural networks

Choose a hidden-layer activation and match your output to binary, multilabel, or multiclass targets. See how logits change the loss in PyTorch and Keras.

Three oscilloscope-style plots compare the ReLU, sigmoid, and tanh activation functions.

Can a customer email go to both Billing and Shipping, or must it go to exactly one queue? The answer changes the activation you need, even if the network produces the same two scores. Choose a hidden-layer activation for the features you want the network to learn. Choose an output activation for the meaning of the target, then check whether your training loss expects scores or probabilities.

Hidden layers: what happens to a signal before the prediction?

A neuron first computes a weighted sum plus a bias, z. The activation turns that number into the signal passed to the next layer. If every layer uses only weighted sums and biases, the stack collapses to a single affine transformation. A nonlinear activation lets the network respond differently in different parts of the input space. For the upstream calculation, see how a neural network layer combines inputs and weights.

For a standard feedforward hidden layer, try ReLU first: it passes positive inputs through and clips negative ones to zero. A unit that produces zero for every training example gets no gradient through that branch. If that persists across batches, check the learning rate and input scale before switching activations. Leaky ReLU keeps a small negative-side slope; ELU allows negative outputs but flattens for very negative inputs. Neither guarantees that training will improve.

Use tanh when a bounded, signed signal is useful, or sigmoid for a gate that controls how much signal passes. Both flatten near their extremes, so large inputs can yield very small derivatives. See why gradients vanish across layers for the training consequence. To compare the shapes directly, use the separate activation function chart with graphs and formulas.

Output layers: what answers are allowed?

Match the target for one example to the output head.
TargetNetwork outputHow to read it
Binary: refund requested, yes or noOne logit; use sigmoid for the probability.If the positive class has probability p, the other has probability 1 − p.
Multilabel: any combination of topic tagsOne logit per tag; apply sigmoid to each separately.Billing and Shipping can both be high. Their probabilities need not add up to one.
Multiclass: exactly one destination queueOne logit per queue; apply softmax across the class dimension.Queues compete for one unit of probability. Raising one score reduces the others’ shares.
Regression: predict a signed account adjustmentOne linear output (no final activation) for an unrestricted dollar amount.A negative value is valid; sigmoid would incorrectly restrict the amount to the interval (0, 1).

Same two scores, two different decisions

Suppose an email mentions a late parcel and a duplicate charge. A model gives Billing a raw score (logit) of ln(3) ≈ 1.099 and Shipping a score of ln(2) ≈ 0.693. Holding these invented scores fixed isolates the effect of the output rule:

Tag the email: sigmoid converts each logit z to exp(z) / (1 + exp(z)). Billing gets 3/4 = 0.75; Shipping gets 2/3 ≈ 0.667. With an illustrative threshold of 0.60 per tag, assign both. Separate sigmoid outputs allow tags to co-occur; they do not prove the topics are statistically independent.

Route to one queue: softmax divides each exponentiated score by their shared sum, 3 + 2. Billing gets 3/5 = 0.60 and Shipping gets 2/5 = 0.40. Route to Billing if the rule is to pick the highest score. If Billing is the correct queue, categorical cross-entropy on this one example is −ln(0.60) ≈ 0.511. Applying softmax to a single score instead would always return 1, not a useful yes/no prediction.

During training, does the loss already include the activation?

In PyTorch, pass raw logits to BCEWithLogitsLoss for binary or multilabel targets. It combines sigmoid and binary cross-entropy in a numerically stable calculation. For one correct queue per example, pass raw class logits to CrossEntropyLoss with a class index target. Do not apply sigmoid or softmax first: the loss would treat those probabilities as fresh scores.

In Keras, pair a linear output with BinaryCrossentropy(from_logits=True) for yes/no labels or SparseCategoricalCrossentropy(from_logits=True) for integer class IDs. If the model already returns the corresponding probabilities, use from_logits=False. Check the Keras loss definitions and target formats when switching heads. At inference, convert logits once if you need probabilities; if you only need the top queue, the largest logit already wins.

These scores illustrate the arithmetic, not model quality. Train on targets that match the actual task. If missed tags cost more than extra reviews, select thresholds on held-out labeled emails rather than assuming 0.50 is right for every tag. If you interpret 0.75 as “right three times in four,” check calibration on representative data: applying sigmoid or softmax does not establish that claim.

Further reading

For a fuller treatment of hidden units and the choice of output units, see chapter 6 of Deep Learning by Ian Goodfellow, Yoshua Bengio, and Aaron Courville. As an Amazon Associate I earn from qualifying purchases. View the hardcover edition of Deep Learning on Amazon US.