Skip to content
IT glossary
Data and MLUpdated December 3, 2026

Activation function

Функция активации

An activation function turns a neuron's computed score into the value it passes on. Nonlinear activations let neural networks learn patterns that a single linear layer cannot represent.

Also known as: ReLU, sigmoid

Why neural networks need activations

A neuron typically multiplies its inputs by weights, adds the products, and adds a bias. An activation function takes that result and determines what the neuron sends to the next layer. For example, ReLU turns a score of −3 into 0 and leaves a score of 2 unchanged.

A layer that only multiplies by a weight matrix and adds a bias is an affine transformation. Stack any number of these layers without nonlinear activations and you can still rewrite the whole stack as one matrix multiplication plus one bias. For a binary classifier that thresholds its final score, that means the decision boundary is still a straight line in two dimensions. Nonlinear hidden layers let the model learn boundaries that bend or change direction.

ReLU and sigmoid behave differently

  • ReLU, short for rectified linear unit, computes max(0, z). For positive inputs its derivative is 1, so it doesn't shrink the gradient at that step. For negative inputs its derivative is 0. If a unit stays on the negative side for every training example, no gradient reaches its weights through that unit. This is the dying ReLU problem. Leaky ReLU is one alternative: it keeps a small positive slope on the negative side.
  • Sigmoid computes 1 / (1 + exp(-z)). It maps a real-valued score to a number strictly between 0 and 1. Far from zero, the curve flattens and its derivative gets close to zero. This is called saturation. In a deep stack of sigmoid layers, multiplying small derivatives during backpropagation can leave early layers with very little gradient.

Choose the output activation to match the task

The hidden activation and the output activation have different jobs. ReLU is a common choice for hidden layers, but it doesn't turn a score into a probability. For binary classification, sigmoid converts one raw score, or logit, into an estimated probability for the positive class. For a single choice among several classes, softmax acts on all class logits together and gives probabilities that sum to 1. For regression with an unrestricted numeric target, the final layer often returns its raw value.

Example: keep the logits for training

import torch
from torch import nn

logits = torch.tensor([-2.0, 1.5])
targets = torch.tensor([0.0, 1.0])
loss = nn.BCEWithLogitsLoss()(logits, targets)
probabilities = logits.sigmoid()

PyTorch's BCEWithLogitsLoss combines sigmoid with binary cross-entropy in a numerically stable calculation. Pass it the raw logits, not probabilities. Use sigmoid separately when you need probability estimates, for example to apply a classification threshold.

What interviewers ask

A common follow-up is whether adding sigmoid only to the output makes a binary classifier's decision boundary nonlinear. It doesn't: sigmoid is monotonic, so thresholding its output is equivalent to thresholding the raw score. If everything before it is affine, the boundary is still a hyperplane. You may also be asked how backpropagation handles ReLU at zero. Its derivative is undefined there, so the implementation uses a convention; PyTorch uses zero.