Skip to main content

๐Ÿ“ Neural Networks

Descriptionโ€‹

< What is it? >โ€‹

A neural network is a function built by composing many small units โ€” neurons โ€” arranged in layers. Each neuron does the same two things: combine its inputs linearly, then pass the result through a non-linear activation. Stacking those layers lets the network approximate mappings that no single linear model can express.

wโ‚
xโ‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
wโ‚‚ โ”‚
xโ‚‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ–ถ z = ฮฃ wแตขxแตข + b โ”€โ”€โ–ถ a = f(z) โ”€โ”€โ–ถ to the next layer
wโ‚ƒ โ”‚ (linear) (activation)
xโ‚ƒ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
b (bias)
react

Training adjusts the weights and biases; the structure and the choice of activation stay fixed.

Key pointsโ€‹

< Neurons and connections >โ€‹

A neuron holds no data of its own โ€” it is a rule for turning a vector of inputs into one number. A connection carries the output of one neuron to the input of another, and every connection has a weight attached to it. In a fully connected layer, every neuron in layer lโˆ’1l-1 connects to every neuron in layer ll, so a layer with nn inputs and mm outputs has nร—mn \times m connections.

< Weights and biases >โ€‹

The weight wiw_i on a connection says how strongly that input pushes the neuron's output up or down; a weight near zero makes the input irrelevant. The bias bb is an input-independent offset that shifts the neuron's threshold, letting it fire even when every input is zero.

Together they are the network's trainable parameters. A layer collects them into a matrix WW and a vector bb:

WโˆˆRmร—n,bโˆˆRmW \in \mathbb{R}^{m \times n}, \quad b \in \mathbb{R}^{m}

Their starting values matter โ€” see Weight Initialization.

< Propagation function: the linear part >โ€‹

The propagation function is what aggregates a neuron's incoming signals into a single pre-activation value zz. In practice it is the weighted sum plus bias, which for a whole layer is one matrix multiply:

z(l)=W(l)a(lโˆ’1)+b(l)z^{(l)} = W^{(l)} a^{(l-1)} + b^{(l)}

where a(lโˆ’1)a^{(l-1)} is the previous layer's output and a(0)=xa^{(0)} = x, the input. This is a linear function of the inputs (affine, once the bias is included): scaling an input scales its contribution proportionally, and contributions add.

< Activation function: the non-linear part >โ€‹

The activation function ff maps the pre-activation to the neuron's output:

a(l)=fโ€‰โฃ(z(l))a^{(l)} = f\!\left(z^{(l)}\right)

It is applied element-wise, so it changes each value's shape but not the layer's dimensions.

ActivationFormulaTypically used for
ReLUmaxโก(0,z)\max(0, z)Default for hidden layers โ€” cheap, and its gradient does not saturate for z>0z > 0
GELUzโ‹…ฮฆ(z)z \cdot \Phi(z)Hidden layers in transformers
Sigmoid1/(1+eโˆ’z)1 / (1 + e^{-z})A single output squashed to (0,1)(0, 1) for binary classification
Tanh(ezโˆ’eโˆ’z)/(ez+eโˆ’z)(e^{z} - e^{-z}) / (e^{z} + e^{-z})Outputs centred on zero, common in older recurrent networks
Softmaxezi/โˆ‘jezje^{z_i} / \sum_j e^{z_j}An output layer producing a distribution over classes

< Why the non-linearity is required >โ€‹

Without ff, depth buys nothing. Two stacked linear layers collapse into one:

W2(W1x+b1)+b2=(W2W1)x+(W2b1+b2)W_2(W_1 x + b_1) + b_2 = (W_2 W_1)x + (W_2 b_1 + b_2)

which is just another affine map. The activation is what makes a deep network more expressive than a single layer. Its derivative also decides how well gradients survive the trip back through many layers โ€” see Vanishing & Exploding Gradients.

< In PyTorch >โ€‹

import torch.nn as nn

model = nn.Sequential(
nn.Linear(784, 128), # W and b: the propagation function
nn.ReLU(), # f: the activation function
nn.Linear(128, 10), # output layer โ€” logits, softmax folded into the loss
)

nn.Linear(n, m) allocates the weight matrix and bias vector; nn.ReLU() has no parameters at all.

Referenceโ€‹