Lesson 4.4 · 5 min read · about 1 h to master at a very slow pace

Choosing Activation Functions

Which activation to use where: ReLU for deep hidden layers, sigmoid for shallow networks and binary outputs, linear for regression outputs, softmax for multi-class outputs.

Not startedJump to quiz

Hidden layers

  • Deep networks: ReLU (or Leaky ReLU / ELU if units die).
  • Shallow networks (one or two hidden layers): sigmoid or tanh work fine; vanishing gradients only bite with depth.
  • Never purely linear: the layers would collapse into one (Section 3).

Output layer: decided by the task

TaskOutput activationWhy
Regression (a number)LinearPredictions can be any real value; a sigmoid would trap them in (0, 1)
Binary classificationOne sigmoid unitOutputs
Multi-class classificationSoftmax over the classesPositive outputs that sum to 1, one probability per class

In PyTorch, the sigmoid or softmax is usually folded into the loss function instead of the model (Section 6.4).

Rule of thumb

Hidden layers: ReLU. Output: whatever makes the outputs mean what the task needs: a real number, a probability, or a probability per class.

Stuck? Common doubts

Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.

First passCan I use a sigmoid output for regression if my targets are between 0 and 1?

You can, if the targets genuinely live in (0, 1), such as proportions. But for general regression targets (prices, temperatures) a sigmoid caps every prediction between 0 and 1, which is wrong, and it also saturates. Use a linear output and standardise the targets instead.

First passWhy might the output neuron have no activation function?

For regression the target can be any real number, like a price or a temperature. ReLU would forbid negative outputs, and sigmoid or tanh would squash the output into a fixed range. The identity lets the output take any value, and the hidden layers still supply the non-linearity.

First passDo all neurons in a layer use the same activation function?

In practice, yes: a layer uses one activation for all its neurons, because that is simpler and works well. Nothing in the maths forbids mixing. Different layers often differ, though: ReLU in the hidden layers and identity, sigmoid or softmax at the output.

Cheatsheet

Output activation by task

Regression: linear. Binary: one sigmoid. Multi-class: softmax. Hidden layers: ReLU (deep) or sigmoid/tanh (shallow).

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1applyA network predicts tomorrow's temperature in °C. Which output activation fits?
Q2applyA network decides whether an email is spam. Which output layer fits?
Q3applyA network recognises which of 10 digits is in an image. Which output layer fits?
Q4conceptFor the hidden layers of a deep network, which activation is the recommended default?
Q5interpretWhy is a sigmoid acceptable in a network with one hidden layer but discouraged in a 20-layer one?
Q6applyIn the 2–3–1 network from lesson 3.1, why does the output neuron use the identity activation?
Q7conceptIs a neuron with no activation function still a neuron?
Q8conceptA neuron applies ReLU to its weighted sum, then a sigmoid to the result. How many activation functions does it really have?
Q9conceptCan a neuron contain another neuron inside it?