# Activation and Loss Functions

> Picking activations for hidden and output layers and matching the loss function to the prediction task. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/activations-and-losses
Last updated: 2026-11-28

Picking activations for hidden and output layers and matching the loss function to the prediction task.

Intermediate · about 3 hours · difficulty 2/5

Learn first: [Backpropagation and Autograd](https://www.internhack.in/roadmaps/tracks/machine-learning/backpropagation)

Activations give networks nonlinearity; the output activation and loss must match the task. Mismatches (for example softmax plus MSE for classification) train poorly or not at all.

### Hidden activations

- **ReLU**: simple, fast, the historical default. Can produce dead units.
- **Leaky ReLU / ELU**: small slope for negatives to avoid dead units.
- **GELU / SiLU (Swish)**: smooth variants used in transformers and modern CNNs.
- **Sigmoid and tanh**: saturate and cause vanishing gradients in deep stacks; still used for gates in LSTMs.

### Output layer plus loss

- **Regression**: linear output + MSE, MAE or Huber loss.
- **Binary classification**: one logit + binary cross-entropy (use the with-logits version for numerical stability).
- **Multiclass**: logits + softmax cross-entropy. In PyTorch, `nn.CrossEntropyLoss` expects raw logits, not probabilities.
- **Multi-label**: independent sigmoid per label + BCE.
- Specialized losses: focal loss for imbalance, contrastive and triplet losses for embeddings.

**Good enough to move on** when, given any prediction target, you can choose the output layer and loss and explain why applying softmax before `CrossEntropyLoss` is a bug.

## Key concepts

- **ReLU and variants**
- **GELU and SiLU**
- **Sigmoid, tanh and saturation**
- **Cross-entropy with logits**
- **Regression losses**
- **Focal and contrastive losses**

## Mini project

Train the same small MLP on Fashion-MNIST with sigmoid, tanh, ReLU and GELU activations and plot training loss curves side by side.

## Check yourself

Which output activation and loss would you use for tagging an image with any of 20 non-exclusive labels?

## Free resources

- [Dive into Deep Learning: Multilayer perceptrons](https://d2l.ai/chapter_multilayer-perceptrons/index.html) (d2l.ai)
- [PyTorch torch.nn reference](https://pytorch.org/docs/stable/nn.html) (PyTorch)

Previous: [Backpropagation and Autograd](https://www.internhack.in/roadmaps/tracks/machine-learning/backpropagation) · Next: [Optimizers and Learning Rate Schedules](https://www.internhack.in/roadmaps/tracks/machine-learning/deep-learning-optimizers)

## More in Neural Network Fundamentals

- [Perceptrons and Multilayer Networks](https://www.internhack.in/roadmaps/tracks/machine-learning/neural-networks-basics)
- [Backpropagation and Autograd](https://www.internhack.in/roadmaps/tracks/machine-learning/backpropagation)
- [Activation and Loss Functions](https://www.internhack.in/roadmaps/tracks/machine-learning/activations-and-losses)
- [Optimizers and Learning Rate Schedules](https://www.internhack.in/roadmaps/tracks/machine-learning/deep-learning-optimizers)
- [Regularization, Initialization and Normalization](https://www.internhack.in/roadmaps/tracks/machine-learning/regularization-and-normalization)
- [Debugging and Running Training Experiments](https://www.internhack.in/roadmaps/tracks/machine-learning/training-debugging)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
