π 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)
- Bionics
- 1 hidden layer neural network


Training adjusts the weights and biases; the structure and the choice of activation stay fixed.
Key pointsβ
< Input, hidden, and output layers >β
A neural network usually has three roles of layers:
input features or embeddings β hidden layers β task-specific output layer
- Input layer: receives numeric features, pixels, or embeddings.
- Hidden layers: repeatedly transform the representation so the model can learn useful intermediate features.
- Output layer: produces the value needed by the task, such as one real number, one binary logit, or one logit per class.
< 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 connects to every neuron in layer , so a layer with inputs and outputs has connections.
< Weights and biases >β
The weight 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 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 and a vector :
For a dense layer with inputs and outputs, this is trainable parameters. CNNs and Transformers use more structured connections or weight sharing, but the same ideaβlearn parameters that transform representationsβstill applies.
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 . In practice it is the weighted sum plus bias, which for a whole layer is one matrix multiply:
where is the previous layer's output and , 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.
< Batches and tensor shapes >β
Models process many examples together as a batch. In PyTorch, nn.Linear(n, m) maps an input of shape (B, n) to an output of shape (B, m), where B is the batch size. More generally, it transforms the final dimension, so (B, T, n) becomes (B, T, m) for a sequence of T token representations.
< Activation function: the non-linear part >β
The activation function maps the pre-activation to the neuron's output:
It is applied element-wise, so it changes each value's shape but not the layer's dimensions.
| Activation | Formula | Typically used for |
|---|---|---|
| ReLU | Default for hidden layers β cheap, and its gradient does not saturate for | |
| GELU | Hidden layers in transformers | |
| Sigmoid | A single output squashed to for binary classification | |
| Tanh | Outputs centred on zero, common in older recurrent networks | |
| Softmax | An output layer producing a distribution over classes |
< Why the non-linearity is required >β
Without , depth buys nothing. Two stacked linear layers collapse into one:
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.
< Output layer and loss >β
The output layer and loss function depend on the task:
| Task | Network output | Common training loss | Interpretation at inference |
|---|---|---|---|
| Regression | One or more real values | MSE or MAE | Predicted quantity, such as price |
| Binary classification | One logit | Binary cross-entropy with logits | Apply sigmoid to obtain a positive-class probability |
| Multiclass classification | One logit per mutually exclusive class | Cross-entropy | Apply softmax to obtain class probabilities |
Stable framework losses usually accept logits directly, so do not manually apply sigmoid or softmax before those losses.
< How a network learns >β
Training repeatedly follows this loop:
batch β forward pass β predictions/logits β loss
β backpropagation β gradients β optimizer update
The forward pass produces predictions. The loss measures their error, backpropagation computes how each trainable parameter affected that error, and an optimizer uses gradient descent to update the parameters. At inference time, the learned parameters are fixed and the model performs only the forward pass.
< 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
)
This model maps a batch of 784-dimensional inputs to 10 logits. nn.Linear(n, m) allocates the weight matrix and bias vector; nn.ReLU() has no parameters at all.
Comparisonβ
< Linear model vs neural network >β
| Aspect | Linear model | Neural network |
|---|---|---|
| Function | One affine mapping | Many affine mappings separated by non-linear activations |
| Intermediate features | Usually engineered outside the model | Learned in hidden layers |
| Strengths | Simple, fast, interpretable, often strong for small or well-structured data | Can model complex patterns in images, audio, text, and other high-dimensional inputs |
| Tradeoffs | Cannot represent complex non-linear relationships by itself | Usually needs more data, compute, tuning, and regularization |
Related ideasβ
- Multilayer Perceptron (MLP) combines fully connected layers for prediction and representation learning.
- Embeddings turn discrete inputs such as tokens into vectors for neural networks.
- Feed Forward Network is the simplest arrangement of these layers.
- Activation Functions introduce the non-linearity that makes depth useful.
- Backpropagation computes how the loss changes with each weight and bias.
- Gradient Descent uses those gradients to update them.