What is the role of the activation function in a neural network?

Instruction: Explain the purpose of activation functions and give examples.

Context: This question examines the candidate's understanding of the mechanisms that introduce non-linearity into neural networks, making them capable of learning complex patterns.

Updated

Example answer

A nonlinear activation lets a neural network represent nonlinear relationships. Without nonlinear operations, composing ordinary affine layers produces another affine mapping. Adding an activation increases possible expressiveness; it does not guarantee that optimization will learn an arbitrary target from finite data.

ReLU computes max(0,x), sigmoid maps real inputs into (0,1), and tanh into (-1,1). Their derivative behavior affects training: sigmoid or tanh can saturate, while a negative ReLU input has zero local gradient. Initialization, depth and optimization also matter; no activation solves every gradient problem.

Choose hidden activations and output transformations for their different purposes. A multiclass model often outputs logits. PyTorch CrossEntropyLoss expects unnormalized logits and handles the relevant log-softmax computation internally, so do not apply softmax first solely for that loss. Softmax may be used to interpret the logits as a distribution, though those probabilities are not automatically calibrated.

For a binary task, a sigmoid output can represent a probability, while a logits-based loss may expect the value before sigmoid. Check the actual loss interface and target format. An activation choice is part of a complete model/loss contract, not an isolated recipe for improved accuracy.

Sources: PyTorch ReLU, PyTorch CrossEntropyLoss.

Related Questions