Step 1 of 6

Neurons, layers & activation functions

A neural network is a stack of linear maps plus nonlinearities. Understand what a single neuron computes, why depth creates expressive power, and how ReLU replaced sigmoid.

16 min read

A neural network is a stack of simple functions. Each layer takes numbers in, multiplies by weights, adds a bias, and passes the result through a nonlinearity. That's it - there's no magic, just linear algebra and calculus, composed many times.

The single neuron#

A single neuron computes a weighted sum of its inputs, adds a bias, and applies an activation function:

output=σ(w1x1+w2x2+⋯+wnxn+b)=σ(w⊤x+b)\text{output} = \sigma(w_1 x_1 + w_2 x_2 + \cdots + w_n x_n + b) = \sigma(\mathbf{w}^\top \mathbf{x} + b)

One neuron: multiply each input by a weight, sum, add bias, and apply a nonlinear activation function σ.

The weights w determine how much each input matters. The bias b shifts the decision boundary. The activation function σ introduces nonlinearity - without it, stacking layers would collapse to a single linear transformation.

3Blue1Brown's visual introduction to neural networks - from pixels to predictions.

Activation functions#

The activation function is what makes neural networks more than linear models. Each has trade-offs:

ActivationFormulaProsCons
Sigmoid1/(1+e⁻ˣ)Smooth, outputs in (0,1)Vanishing gradients for large ∣x∣
Tanh(eˣ−e⁻ˣ)/(eˣ+e⁻ˣ)Zero-centered, outputs in (−1,1)Still vanishes at extremes
ReLUmax(0, x)Fast, no vanishing gradient for x > 0Dead neurons (gradient = 0 for x < 0)
Leaky ReLUmax(0.01x, x)No dead neuronsSlight extra complexity
GELUx · Φ(x)Smooth, used in transformersMore expensive to compute
SiLU/Swishx · σ(x)Smooth, self-gatedMore expensive to compute

Layers and depth#

Stack neurons in parallel to form a layer, and stack layers to form a network:

  • Input layer: your raw features (pixels, token embeddings, sensor readings)
  • Hidden layers: learned transformations - each extracts increasingly abstract features
  • Output layer: the final prediction (a probability, a regression value, a distribution over classes)
h=σ(W1x+b1)y^=W2h+b2\mathbf{h} = \sigma(\mathbf{W}_1 \mathbf{x} + \mathbf{b}_1) \qquad \hat{\mathbf{y}} = \mathbf{W}_2 \mathbf{h} + \mathbf{b}_2

A two-layer network: first layer transforms inputs, activation adds nonlinearity, second layer produces the output.

Depth matters. The universal approximation theorem says that a single hidden layer with enough neurons can approximate any continuous function - but "enough" can mean exponentially many neurons. Deep networks (many layers, fewer neurons per layer) can represent the same functions with exponentially fewer parameters. Each layer composes features from the previous one: edges → textures → parts → objects.

The multi-layer perceptron (MLP)#

An MLP (also called a feedforward network or dense network) is the simplest deep architecture: fully connected layers stacked with activations. Every neuron in layer l connects to every neuron in layer l+1.

  1. 01Input: feature vector x ∈ ℝⁿ
  2. 02Hidden layer 1: h₁ = σ(W₁x + b₁)
  3. 03Hidden layer 2: h₂ = σ(W₂h₁ + b₂)
  4. 04...more layers...
  5. 05Output: ŷ = Wₒhₗ + bₒ (regression) or softmax(Wₒhₗ + bₒ) (classification)

The number of parameters in a fully connected layer: (input_dim × output_dim) + output_dim. For a layer mapping 768 → 3072, that's 768 × 3072 + 3072 = 2,362,368 parameters. This is why transformer MLPs are the largest component by parameter count.

Softmax - from logits to probabilities#

For multi-class classification, the output layer produces logits (raw scores, one per class), and softmax converts them to a probability distribution:

softmax(zi)=ezi∑jezj\text{softmax}(z_i) = \frac{e^{z_i}}{\sum_j e^{z_j}}

Softmax: exponentiate each logit and normalize by the sum. The outputs sum to 1 and are all positive - a valid probability distribution.

The class with the highest probability is the prediction. Training minimizes cross-entropy loss - the negative log of the probability assigned to the correct class. This pushes the model to be confident about the right answer.

Logits
Raw, unnormalized scores output by the last layer of a network - one per class. Softmax converts these into probabilities.
Cross-entropy loss
Measures how far the model's predicted probability distribution is from the true label - the standard loss for classification tasks.

Loss functions#

TaskLoss functionWhat it measures
RegressionMSE: (1/n)Σ(yᵢ − ŷᵢ)²Average squared prediction error
Binary classificationBCE: −[y log(p) + (1−y) log(1−p)]How surprised the model is by the true label
Multi-classCross-entropy: −Σyᵢ log(pᵢ)Same, across all classes
Contrastive learningInfoNCE, triplet lossDistance between similar and dissimilar pairs

Where it shows up in ML#

Every transformer has MLPs

Each transformer block contains a multi-head attention layer followed by a two-layer MLP (expand 4×, then contract). The MLP is where knowledge is stored.

Activation choice matters

GPT-2 uses GELU, LLaMA uses SiLU/Swish, most CNNs use ReLU. The choice affects training dynamics, not just accuracy.

Softmax powers classification

The final layer of every classification network (and the attention mechanism) uses softmax to produce a probability distribution.

Depth enables abstraction

Early layers learn simple features, deep layers compose them into abstract concepts. This hierarchical representation is why deep networks work.

Further reading

Search Data Science After Dark

Search across prompts, models, papers, guides, and library topics.