Neural Networks

How a Neural Network Learns: Weights, Loss and Gradients

By NeuralBed Editorial Team · · 5 min read

A neural network starts out knowing nothing. Its internal numbers are set at random, so its first predictions are close to guesses. Learning is the process of nudging those numbers, again and again, until the predictions get useful.

That process rests on three ideas: weights, a loss, and gradients. Once you see how they fit together, training stops looking like magic and starts looking like a careful loop of trial and correction.

Weights: the numbers the network adjusts

Every connection between two neurons carries a number called a weight. A neuron multiplies each incoming value by its weight, adds the results together, and usually adds a small extra number called a bias. The total is then passed through an activation function before moving on to the next layer.

Weights decide how much each input matters. A large positive weight means "pay a lot of attention to this signal." A weight near zero means "mostly ignore it." A negative weight means "treat this signal as evidence against."

A network with millions of weights is just a very large formula with many adjustable dials. The architecture fixes the shape of the formula. Training finds good settings for the dials.

The forward pass: making a prediction

Training begins with a forward pass. You feed in an example, such as an image of a handwritten digit, and the data flows through the layers. Each layer applies its weights and activation functions. At the end, the network produces an output, for example a score for each possible digit.

At first this output is poor. The network may say the image is a 7 when it is clearly a 3. That is expected. The point of the forward pass is simply to produce something we can measure.

The loss: measuring how wrong the network is

To improve, the network needs a single number that says how bad its prediction was. That number is the loss, produced by a loss function.

Common choices include:

  • Mean squared error, used when predicting a continuous value such as a temperature. It squares the gap between prediction and truth, so large mistakes count much more than small ones.
  • Cross-entropy, used for classification. It punishes the network heavily when it is confident and wrong, and lightly when it is unsure or right.

A low loss means the predictions match the labels. A high loss means they do not. The whole goal of training can be stated in one line: find weights that make the loss as small as possible across the training data.

Gradients: which way to turn each dial

Knowing the loss is not enough. The network has millions of weights, and it must know which ones to change and in which direction.

This is where the gradient comes in. For each weight, the gradient tells you how the loss would change if you increased that weight by a tiny amount. If the gradient is positive, raising the weight makes the loss worse, so you should lower it. If the gradient is negative, raising the weight helps, so you should raise it. The size of the gradient shows how sensitive the loss is to that weight.

Picture standing on a hilly landscape in fog. You cannot see the lowest valley, but you can feel the slope under your feet. The gradient is that slope, and the loss is your height above sea level.

Backpropagation: computing all the gradients efficiently

Working out the gradient for every weight sounds expensive. Backpropagation is the method that makes it practical. It uses the chain rule from calculus to move backward through the network, starting at the loss and ending at the first layer.

At each layer, it reuses work already done for the layer after it. Instead of recomputing everything from scratch for each weight, it passes a signal backward and collects every gradient in a single sweep. This is why one forward pass followed by one backward pass is enough to update the whole network.

Gradient descent: taking the step

With gradients in hand, the update is simple. For each weight:

weight = weight - learning_rate * gradient

This is gradient descent. You step downhill, opposite to the slope. The learning rate controls the step size. If it is too large, the network overshoots the valley and the loss bounces around or grows. If it is too small, training crawls and may stall.

In practice, networks rarely use the entire dataset for each step. They use small groups of examples called mini-batches. The gradient from a mini-batch is a noisy but useful estimate of the true slope, and it makes each step much cheaper. Variants such as momentum and Adam adjust the step using past gradients to move faster and more smoothly.

Putting the loop together

One full training cycle looks like this:

  1. Take a mini-batch of examples.
  2. Run the forward pass to get predictions.
  3. Compute the loss against the correct labels.
  4. Run backpropagation to get the gradient for every weight.
  5. Update the weights with gradient descent.
  6. Repeat with the next mini-batch.

One pass through the whole dataset is called an epoch. Training usually runs for many epochs, and the loss is watched over time. A falling loss on both training and held-out data is a healthy sign.

Common pitfalls

Learning does not always go smoothly. Gradients can become extremely small in deep networks, which slows early layers to a halt. They can also grow too large and make training unstable. Good initialization, sensible activation functions and careful learning rates all help.

A network can also learn the training data too well and fail on new examples. That is why practitioners keep a separate validation set and stop training when performance on it stops improving.

Frequently Asked Questions

Does a neural network understand what it is learning?

Not in the human sense. It adjusts numbers so that its outputs reduce a loss. Useful patterns often emerge in the weights, but the network has no awareness of meaning. It is optimizing a measurable objective.

Why is the learning rate so important?

It sets how big each weight update is. Too high and training becomes unstable, too low and it takes far too long or gets stuck. Many practitioners start with a common default and then adjust it based on how the loss curve behaves.

What is the difference between a gradient and backpropagation?

The gradient is the quantity: how much the loss changes with respect to a weight. Backpropagation is the algorithm that computes all those gradients efficiently by working backward through the network layer by layer.

Related reading