AI Compass
Compass

How a neural network computes

From one neuron to a layer to a network: what actually happens as data flows through, and why depth does something different from width.

·2 min read·By Fachredaktion Technik
DETAIL
3 sections

The idea

A neuron takes several numbers, multiplies each by its own weight, adds them up, adds an offset, and passes the result through a simple function that sets everything negative to zero. That is it.

A layer is a row of such neurons side by side. A network is a sequence of layers. The first sees the raw data; every later one sees what the previous made of it.

What depth achieves

In an image, the first layer detects brightness jumps, the second corners from those, the third simple shapes, the fourth object parts. Each level composes the building blocks of the one before. Hence "deep learning".

A network by hand

import numpy as np
rng = np.random.default_rng(0)

def relu(x): return np.maximum(0, x)

# He initialisation: standard deviation sqrt(2/fan_in). With the wrong
# initialisation the signal dies out across the layers.
def layer(fan_in, fan_out):
    return (rng.normal(0, np.sqrt(2 / fan_in), (fan_in, fan_out)),
            np.zeros(fan_out))

W1, b1 = layer(784, 256)
W2, b2 = layer(256, 128)
W3, b3 = layer(128, 10)

x = rng.normal(size=(32, 784))               # 32 images of 28x28
a1 = relu(x  @ W1 + b1)
a2 = relu(a1 @ W2 + b2)
logits = a2 @ W3 + b3                        # 32 x 10, not yet probabilities

for name, a in [("a1", a1), ("a2", a2)]:
    print(name, "mean", round(float(a.mean()), 3),
          "zero fraction", round(float((a == 0).mean()), 3))

The zero fraction is the most important diagnostic when building a network. Above 90 percent means many neurons are dead and contribute nothing.

The parameter count

LayerInOutParameters
1784256200,960
225612832,896
3128101,290
Total235,146

The formula

One layer

a⁽ˡ⁾ = σ( W⁽ˡ⁾ · a⁽ˡ⁻¹⁾ + b⁽ˡ⁾ )

A layer's output is the activation function applied to a matrix multiplication plus a bias.

a⁽ˡ⁾
activation after layer l
W⁽ˡ⁾
weight matrix of layer l
b⁽ˡ⁾
bias vector
σ
the activation function

Without σ, W⁽²⁾(W⁽¹⁾x + b⁽¹⁾) + b⁽²⁾ = (W⁽²⁾W⁽¹⁾)x + (W⁽²⁾b⁽¹⁾ + b⁽²⁾), again a single linear map. Twenty layers would have exactly the expressiveness of one.

Initialisation

He and Xavier initialisation

He (for ReLU): σ = √(2 / n_in) Xavier (for tanh): σ = √(2 / (n_in + n_out))

The spread of the initial weights is chosen so the signal variance stays roughly constant across layers.

n_in
number of inputs to a layer
n_out
number of outputs

The reason is a variance calculation: with ReLU about half the outputs are zero, halving the variance. The factor 2 compensates. Without that correction, activation variance falls by half per layer and has dropped to 2⁻²⁰ ≈ 1e-6 after twenty.

Universal approximation, read correctly

The theorem says a network with one hidden layer can approximate any continuous function on a compact domain arbitrarily well. What it does not say:

  • How many neurons that takes. The count can grow exponentially in the input dimension.
  • Whether training finds those weights. The theorem is an existence statement.
  • Whether the approximation holds outside the domain considered. It does not.

The theorem is therefore not an argument for shallow networks. Deep networks reach the same accuracy with exponentially fewer neurons because they reuse features rather than placing them side by side.

Related courses and sources

PaperFreeEN

Batch Normalization

Why normalising intermediate values is what makes deep training stable in the first place.

For anyone asking why normalisation is what makes deep training possible.

BookFreeEN

Deep Learning

The standard work by Goodfellow, Bengio and Courville, free to read. Mathematically dense, complete, and in its foundational parts timeless.

For the systematic route. Strong as a reference, too dense as a first read.

CoursePartly free7200 minEN

Deep Learning Specialization

Five courses from the basics of neural networks to sequence models. Thorough, with programming exercises, and in places older than current practice.

For anyone who can program and wants to work through the field completely.

DeepLearning.AIGo to offer
BookFreeEN

Dive into Deep Learning

A textbook with runnable code beside every derivation. Each chapter opens as a notebook you can recompute yourself.

For anyone who wants to compute along while reading; every chapter opens as a notebook.

ToolFreeEN

Netron

Opens a model file and draws its structure. The fastest way to see what a delivered model actually contains.

For a quick look inside a delivered model before putting it into operation.

VideoFree150 minEN

Neural networks, explained visually

From a single weight through gradient descent to the attention mechanism. The best available intuition for what the formulas describe.

Watch it before your first textbook, not after. It saves weeks of confusion.

3Blue1BrownGo to offer
CourseFree4200 minEN

Practical Deep Learning for Coders

Starts with a working model in the first hour and supplies the theory afterwards. The shortest route from basic Python to a model you trained yourself.

For impatient readers who know Python: your first model runs within the first hour.

CourseFreeEN

PyTorch tutorials

The official guides, from a first tensor operation to distributed training. Short, runnable and continuously updated.

For getting into the library most research is written in.

ToolFreeEN

TensorFlow Playground

A neural network in the browser with sliders for layers, activation and learning rate. You see in seconds what each knob does.

For anyone who wants to see what learning rate, layers and activation actually do.

BookFreeEN

Understanding Deep Learning

A modern textbook with unusually clear figures that already covers transformers and diffusion models in full. Free as a PDF.

For a modern entry point; it already covers transformers and diffusion in full.

Was this page helpful?
How a neural network computes