No history yet

Neural Network Mechanics

The Flow of Information

A neural network processes information in a two-part cycle. First, data flows forward through the network. This is called forward propagation. An input, like the pixels of an image, is fed into the first layer. Each connection between neurons has a weight, which is just a number. The network multiplies the input values by these weights, adds a bias term, and passes the result through an activation function to the next layer. This happens layer by layer until an output is produced, like a prediction of whether the image is a cat or a dog.

The initial prediction is almost always wrong. The network then measures its error using a loss function, which calculates the difference between the network's prediction and the actual correct answer. To learn, the network must adjust its internal weights to reduce this error.

This is where backpropagation comes in. It's the backward pass. The error is propagated backward through the network, from the output layer to the input layer. At each connection, the network calculates how much the weight contributed to the total error. It then adjusts the weight in the direction that will minimize the error. This forward-pass, backward-pass cycle is repeated thousands or even millions of times, with the network getting slightly better with each iteration.

Learning Through Correction

The core mechanism for updating weights during backpropagation is an optimization algorithm called gradient descent. Imagine the loss function as a hilly landscape, where the lowest point represents the smallest possible error. The network's goal is to find this lowest point. Gradient descent does this by calculating the slope (the gradient) of the loss function at the network's current position and taking a small step downhill.

wnew=woldηLw_{\text{new}} = w_{\text{old}} - \eta \cdot \nabla L

Calculating the gradient across an entire dataset can be slow. To speed things up, a few variants of gradient descent are commonly used. Each offers a different trade-off between accuracy and speed.

MethodDescriptionProCon
BatchCalculates the gradient using the entire training dataset.Stable convergence to the minimum.Very slow and memory intensive for large datasets.
Stochastic (SGD)Updates weights after every single training example.Much faster and can escape local minima.Noisy, erratic updates; may never fully converge.
Mini-batchCalculates the gradient on a small, random batch of examples.A good balance of speed and stability.Requires an additional hyperparameter (batch size).

Making Decisions

If a neural network only used weights and biases to combine inputs linearly, it would be no more powerful than a simple linear regression model. It couldn't learn complex patterns like the curves in a handwritten digit or the nuances of human language.

This is where come in. They are applied to the output of each neuron and introduce non-linearity, allowing the network to model incredibly complex relationships. A good activation function is also computationally efficient, as it's calculated millions of times during training.

Two of the most common activation functions are Sigmoid and ReLU.

Sigmoid squashes its input into a range between 0 and 1. This is useful for the output layer in a binary classification problem, where the output can be interpreted as a probability. However, it suffers from the vanishing gradient problem, where the gradient becomes extremely small for large positive or negative inputs, effectively halting learning in deep networks.

ReLU (Rectified Linear Unit) is much simpler. It outputs the input directly if it's positive, and 0 otherwise. It is computationally very efficient and is the most popular activation function for hidden layers. Its main drawback is the "dying ReLU" problem, where neurons can get stuck outputting 0 and stop learning entirely.

Measuring Mistakes

A is crucial because it gives the network a concrete score for how wrong its predictions are. The choice of loss function depends on the problem you're trying to solve.

For regression problems (predicting a continuous value, like a house price), Mean Squared Error (MSE) is a common choice. For classification problems (predicting a category), Cross-Entropy Loss is typically used.

Before training begins, the network's weights must be set to some initial values. This isn't a trivial step. If all weights are initialized to zero, every neuron in a layer will behave identically, and the network won't learn. If they are initialized to be too large, the activation functions can saturate, leading to vanishing gradients.

Weight initialization techniques like Xavier or He initialization are designed to set the initial weights to small random numbers, carefully chosen based on the size of the previous layer. This helps ensure that the signal flows properly in both the forward and backward passes, leading to faster and more reliable training.

Quiz Questions 1/6

What is the primary purpose of the forward propagation phase in a neural network?

Quiz Questions 2/6

What is the main role of an activation function in a neural network?