# Neural network fundamentals
Problem: learning which features are useful. Applications: detection and recognition.
# Forward propagation
The forward pass computes the loss.

Here is the input, the weights and the loss.
# Score function
A linear scoring function maps an input to one score per class: , where represents the image, the weights and the bias. The weights determine how input values contribute; the bias offsets the resulting scores.
For a image and ten classes, flattening gives a input. The weights have shape and the bias . The result is a vector of class scores. Training seeks weights that produce the desired results.
# Loss function
The loss measures the discrepancy between the predictions and the true labels.
Loss = data loss + regularisation penalty.
One example of data loss for sample is , where is the correct class.

For the three examples:
The regularisation penalty depends on the weights rather than on any individual observation. For an L2 penalty, . Increasing penalises large weights more strongly to discourage overfitting.
A softmax classifier converts scores to probabilities, then computes a loss for the correct class.
- Apply to the scores so higher scores carry more weight when the probabilities are normalised.
- Normalise: , where .
- Compute .
# Backpropagation
Backpropagation gives the gradients needed to optimise the weights. Gradient descent updates in the opposite direction to the gradient, using a step size , to reduce the loss.
The chain rule carries gradients backwards through the calculations. For , work backwards from the multiplication by . Think of the computation as a set of gates. In the forward pass the output of one gate becomes the input of the next. In the backward pass each gate multiplies the gradient it receives by its own local derivative.
In the diagram, the red box is one gate. Values in green above the arrows travel forwards. Values in red below the arrows are gradients travelling back through the graph.

- Addition: for , both partial derivatives are 1. Each input receives the incoming gradient unchanged.
- Multiplication: for , the local derivative with respect to is , and vice versa. The gradient passed to is the incoming gradient multiplied by .
- Maximum: when there’s a unique maximum, that input receives the gradient. The smaller inputs receive zero.
# Network architecture

- Each layer transforms the preceding layer’s values.
- The input width is the number of input features: three input units correspond to an matrix for examples. In the diagram each hidden layer has four units.
- In a fully connected layer, each unit connects to every unit in the preceding layer. The diagram’s three weight matrices have shapes , and .
- A nonlinear activation function transforms a layer’s result before it passes to the next layer.
Adding units increases the network’s capacity to fit the training data, but also costs computation and increases the risk of overfitting.
# Activation functions
Apply the activation function before passing values to the next layer.

Sigmoid

At the far left and right of the curve the derivative is close to zero. Multiplying small derivatives through the backward pass can make gradients vanish. A zero local derivative stops the gradient on that path. That is one reason sigmoid is less commonly used in hidden layers.
ReLU

ReLU is a common choice. Tanh is another activation function.
# Data preprocessing
- Centre each feature by subtracting its mean.
- Rescale the centred values by dividing by the feature’s standard deviation.

# Initialising the parameters
One random initialisation for a weight matrix is . The factor of 0.01 keeps the initial spread of the weights small.
# Handling overfitting
Two approaches are to add a regularisation term to the loss, and to use dropout (the “Seven Injuries Fist” of the training toolkit).
With dropout, training temporarily switches off randomly chosen units in the layers where dropout is applied. The selection changes between training steps.


