How Neural Networks Learn

How do artificial neural networks learn?

Neural networks learn by predicting, measuring the error, using backpropagation to assign blame to each weight, and taking small gradient descent steps.

On this page
  1. What gets learned: weights and biases
  2. Step 1: the forward pass
  3. Step 2: measure the error
  4. Step 3: backpropagation assigns credit
  5. Step 4: gradient descent updates the weights
  6. A worked example with two weights
  7. One pass is not enough
  8. Why activation functions matter
  9. Common problems in training
  10. Inference: using what was learned
  11. The short answer

Artificial neural networks learn by adjusting their weights to reduce error. For each training example, the network makes a prediction, a loss function measures how wrong that prediction was, backpropagation works out how each weight contributed to the error, and gradient descent nudges each weight a small step in the direction that reduces it. Repeating this over many examples gradually improves the network.

What gets learned: weights and biases

Each connection between units carries a weight, a number that scales the signal passing along it. Each unit also has a bias, a number added to its weighted sum. Together these are the network’s parameters, and training changes them. The architecture, meaning the number of layers, the number of units in each layer, and the activation functions, is usually chosen before training and stays fixed.

Step 1: the forward pass

The input goes into the first layer, which computes weighted sums and applies activation functions. Those outputs feed the next layer, and so on until the final layer produces the prediction. A small network with three layers follows the same steps as a larger one, with fewer parts. For a fuller picture of that size of network, see “What Is a 3 Layer Neural Network?”

For classification, the final layer often converts its scores into probabilities, commonly with a softmax function.

Step 2: measure the error

The loss function compares the prediction with the target and returns a single number.

  • For numeric predictions, mean squared error is common. It averages the squared differences between predictions and targets.
  • For classification, cross-entropy loss is common. It penalizes confident wrong answers heavily.

Step 3: backpropagation assigns credit

Backpropagation computes the gradient of the loss with respect to every weight. It applies the chain rule from calculus: the effect of a weight on the loss is found by multiplying the derivatives along the path from that weight to the loss. The computation starts at the output layer and works backward, reusing intermediate results at each layer. That reuse is what makes it practical for large networks. The method was popularized in the 1980s, and modern frameworks compute it automatically.

Step 4: gradient descent updates the weights

The gradient points in the direction in which the loss rises fastest, so each weight moves the opposite way:

new weight = old weight − learning rate × gradient

The learning rate sets the step size. Too large, and the updates overshoot the best values. Too small, and training is slow. Stochastic gradient descent, which works on small groups of examples, and Adam are widely used variants of this basic update.

A worked example with two weights

Take a single neuron with one input, x = 1.5, and a target output of 3. It has a weight, w, and a bias, b, starting at w = 1 and b = 0. Its prediction is w × x + b, and the loss is the squared error. Use a learning rate of 0.1.

Step w b Prediction Loss
0 1 0 1.5 2.25
1 1.45 0.3 2.475 0.276
2 1.6075 0.405 2.816 0.034

At step 0 the error is −1.5. The gradient with respect to w is 2 × error × x = −4.5, and the gradient with respect to b is 2 × error = −3. Subtracting 0.1 times each gradient gives w = 1.45 and b = 0.3. The prediction moves to 2.475 and the loss falls. One more update brings the prediction to about 2.82, with the loss near 0.03. A network with many weights needs many updates and many examples, but each weight is adjusted in the same way.

One pass is not enough

One full pass through the training data is called an epoch. Training usually runs for many epochs, with examples grouped into mini-batches so that each update uses a small group. Training is often stopped early, once performance on validation data stops improving. This is called early stopping. Holding back validation data is how training can detect overfitting, the situation where the network memorizes its training set and does worse on new inputs. The broader question of how to check a model is covered in “Which Are Methods Used for Validating Models?”

Why activation functions matter

Each unit applies an activation function after its weighted sum. Two common choices are ReLU, which outputs zero for negative inputs and the input itself otherwise, and sigmoid, which squashes values into the range between 0 and 1.

The nonlinearity is essential. Without it, stacked layers would combine into a single linear function, no matter how many layers there were. Nonlinear activations let deeper networks learn curved, complicated relationships. Adding more layers is what makes a network deep. The contrast between artificial neural networks and deep neural networks is covered in “The Difference Between Artificial Neural Networks and Deep Neural Networks.”

Common problems in training

  • Vanishing or exploding gradients. In deep networks, gradients can shrink or grow as they pass through many layers, which slows or destabilizes learning. Careful weight initialization, normalization, and residual connections are common remedies.
  • Settling on imperfect solutions. Training can end in a region of the loss landscape that is not the best possible, so results can depend on starting values and settings.

Inference: using what was learned

When training ends, the weights stop changing. Running the network on new inputs uses only the forward pass, and this is called inference. For a fuller definition, see “What Is Inference in the Context of AI Models?”

The short answer

Artificial neural networks learn by predicting, measuring the error with a loss function, using backpropagation to see how each weight contributed, and then making small gradient descent updates. Repeating this across many examples, with validation data to check progress, shapes the weights so the network makes better predictions.

The complete guide to how neural networks learn

How Neural Networks and Machine Learning Actually Learn
Read the guide

Keep exploring

Guides from other topics

All topics →