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.
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
| Task | Output activation | Why |
|---|---|---|
| Regression (a number) | Linear | Predictions can be any real value; a sigmoid would trap them in (0, 1) |
| Binary classification | One sigmoid unit | Outputs |
| Multi-class classification | Softmax over the classes | Positive 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
Check your understanding
Score 75% or more to master this lesson and earn XP. Answer first, then check.