Concepts / Neural Network Loss Functions

Neural Network Loss Functions

When there is only one output, the Jacobian is the gradient represented as a row vector. In a multilayer network, Jacobians describe how one layer's outputs respond to another layer's inputs, making them the local derivative objects used by the chain rule.

  • Programming
Interactive lab

Try it: Backpropagation, Step by Step

A forward pass through a 2-2-1 network, then the backward pass that applies the chain rule node by node to get for every weight and bias, and one gradient-descent update that changes the loss.

How it works

  1. Forward: compute each hidden pre-activation and activation, then the sigmoid output y and the loss.
  2. Backward from the loss: for squared error, or for binary cross-entropy.
  3. Output weights: ; the error sent back to is .
  4. Hidden units: multiply by the activation's local derivative, then .
  5. Update every parameter and run the forward pass again to see the new loss.

Default run (12 steps): Inputs x = (1, 0.5), target t = 1. Hidden activation sigmoid, output sigmoid, loss: squared error L = (y - t)2. First the forward pass. … New forward pass with the updated weights: y = 0.6086, loss 0.203 -> 0.1532 (lower: the step moved downhill).

Simplified: One training example, two inputs, two hidden units, one output unit. Binary cross-entropy is evaluated from the output logit (same value, no log(0)). Toy dimensions, not a full real model.

Educational simulation

Loading the simulation…

From Prediction to Weight Updates

A neural network has a loss that depends on its weights. The network first transforms an input into a prediction, and the loss measures the result. To change the weights in a useful direction, we need to know how the loss changes when each individual weight changes. Backpropagation calculates this information: it calculates the gradient of the loss with respect to the weights, but it does not itself apply the weight update.

Network weightsPredictionLossGradientChanged weightsnetwork computationloss functionbackpropagationused to change weights
How do network weights produce a prediction and loss, and how is the loss connected to the gradients used to update those weights?

Forward Values Through Layers

Consider a network divided into layers V0 through VT. The weights connecting layer Vt-1 to layer Vt are represented by a matrix Wt-1. The incoming values at a layer are collected in at, and the neuron outputs are collected in ot. The layer first computes at = Wt-1 ot-1 and then applies the activation function element by element to obtain ot = σ(at). These forward quantities are calculated from the input side toward the output side.

o0input valuesa1W0 o0o1σ(a1)aTWT-1 oT-1oTfinal outputW0activationlater layersactivation
How do values move through a sequence of neural-network layers to produce the final output?
Worked example

Tracking One Layer's Forward Quantities

Describe the forward computation at layer Vt using the notation from the network model.

  1. 1
    Collect incoming values

    The outputs from the preceding layer are represented by ot-1.

  2. 2
    Apply the weight matrix

    The layer computes the pre-activation values at = Wt-1 ot-1.

  3. 3
    Apply the activation

    The activation function is applied element by element, producing ot = σ(at).

  4. 4
    Store the forward quantities

    The values at and ot are retained because the backward calculation uses the forward quantities while moving in the opposite direction.

Result

The forward pass records the values computed from the input side toward the output side, preparing information for backpropagation.

Partial Derivatives and Weight Sensitivity

Definition

A partial derivative describes how a function changes when one variable changes while the other variables are held constant. In a neural network, the loss has one partial derivative for each weight associated with an edge in the network. Each derivative therefore answers a specific question: how does the loss change if this particular weight changes while the other weights remain fixed?

w1original valuew1varied valuePartial derivativeloss change for w1Other weightsheld constantOther weightsheld constantvary only w1no changemeasure loss change
What changes when one network weight varies while all other weights are held constant?

The gradient collects all of these partial derivatives. If the network has many weights, the gradient has one corresponding component for each weight. The component associated with a particular weight reports the loss sensitivity for that weight; it is not a general score detached from the network's individual connections.

w1network weight∂loss/∂w1gradient componentw2network weight∂loss/∂w2gradient componentwnnetwork weight∂loss/∂wngradient componentcorresponds tocorresponds tocorresponds to
How does each component of the gradient correspond to one specific network weight and indicate how the loss changes with that weight?

Jacobians for Vector Outputs

A function can produce several outputs at once. When a function maps Rⁿ to Rᵐ, its derivatives are organized in a Jacobian matrix with m rows and n columns. The entry in row i and column j is the partial derivative of output i with respect to input variable j. Rows therefore correspond to output components, while columns correspond to input components.

∂f1/∂x1output 1, input 1∂f2/∂x1output 2, input 1∂f1/∂x2output 1, input 2∂f2/∂x2output 2, input 2same output rowsame output row
How are the derivatives of every output component with respect to every input component arranged in a matrix?
Key point

When there is only one output, the Jacobian is the gradient represented as a row vector. In a multilayer network, Jacobians describe how one layer's outputs respond to another layer's inputs, making them the local derivative objects used by the chain rule.

The Chain Rule Across Layers

A neural network composes many functions. A weight matrix produces pre-activation values, an activation function produces neuron outputs, later layers transform those outputs, and the final output determines the loss. The chain rule connects the derivative of the final loss to the derivatives of each intermediate transformation.

WeightPre-activationaLayer outputoLater outputsLosslocal derivativeactivation derivativelater transformationsloss dependence
How does a change in a weight propagate through several neural-network layers to change the final loss?

The order of the derivative operations matters because each derivative maps changes from one representation to the next. Applying the chain rule means combining local derivative information in the correct order. Backpropagation organizes this repeated multiplication recursively so that sensitivity arriving at one layer can be reused when calculating the sensitivity for the preceding layer.

Worked example

Following One Weight's Influence

Trace conceptually how a change in a weight near the input side can influence the final loss.

  1. 1
    Weight to pre-activation

    The weight contributes to a layer's pre-activation values through the weight-matrix transformation.

  2. 2
    Pre-activation to output

    The activation function transforms the pre-activation values into the layer outputs.

  3. 3
    Output through later layers

    Later layers transform those outputs and pass the effect toward the final output.

  4. 4
    Final output to loss

    The final output determines the loss, so the original weight can affect the loss through every intermediate transformation.

  5. 5
    Combine local derivatives

    The chain rule combines the local derivative at each link into the derivative of the complete network with respect to that weight.

Result

The derivative for the weight is obtained by composing the local sensitivities along the entire path from that weight to the loss.

Forward Pass and Backward Pass

Backpropagation has two directions of computation. During the forward pass, the network calculates the values at and ot from the input side toward the output side. During the backward pass, it uses those stored forward quantities while moving from the output side toward the input side. The forward values describe what the network computed; the backward sensitivities describe how strongly the later loss depends on those computed outputs.

InputForward valuesat and otFinal outputLossBackwardsensitivitiesδtWeight gradientsforwardforwardevaluatebackwardbackward
What happens during the forward pass and then during the backward pass, and in what direction does information move?
Definition

Let ℓt be the loss of the subnetwork beginning at layer Vt, viewed as a function of the outputs ot. The backward sensitivity is defined as δt = Jot(ℓt). This vector records how the subnetwork loss changes with respect to the outputs at that layer.

Key point

The backward quantities are not another set of forward activations. They are sensitivities. A sensitivity tells us how strongly a later loss depends on an output currently being considered.

Backward Recursion and Weight Gradients

At the final layer, the subnetwork loss is the loss function itself. For the squared loss Δ(u, y) = 1/2 ||u − y||², the derivative with respect to u is u − y. Therefore, at the final layer, δT = oT − y. This supplies the starting backward signal.

For an earlier layer, the chain rule expresses δt in terms of the sensitivity at the next layer, the derivative of the activation, and the transformation connecting the layers. The exact matrix arrangement follows from the Jacobians of those transformations. Repeating this step moves the sensitivity from the top layer toward the bottom layer.

Worked example

Tracing the Backward Signal

Explain how the backward recursion moves from the final layer to an earlier layer and contributes to the weight gradient.

  1. 1
    Start at the final layer

    For the squared loss, the final sensitivity is δT = oT − y.

  2. 2
    Move to the preceding layer

    Use the next layer's sensitivity together with the activation derivative and the connecting transformation.

  3. 3
    Repeat toward the input

    Apply the same chain-rule reasoning repeatedly so each earlier layer receives a sensitivity.

  4. 4
    Combine with forward information

    The stored forward quantities and the backward sensitivities are combined to obtain derivatives for the weights.

Result

The backward recursion produces the gradient of the loss with respect to the network weights by repeatedly applying local derivative relationships from the output side toward the input side.

Common Reasoning Mistakes

  • Common mistake

    Treating the gradient as one undifferentiated number.

    What it looks like

    Describing the gradient without identifying which component belongs to which weight.

    Why it fails

    The gradient collects one partial derivative for each network weight.

    Do this instead

    Track every gradient component as the derivative of the loss with respect to its corresponding weight.

  • Common mistake

    Confusing forward values with backward sensitivities.

    What it looks like

    Calling δt another layer output.

    Why it fails

    The forward quantities at and ot describe computed values, while δt describes how the loss depends on those outputs.

    Do this instead

    Label forward quantities as values and δ quantities as sensitivities.

  • Common mistake

    Ignoring the order of derivative composition.

    What it looks like

    Combining local derivatives without respecting the sequence of transformations between a weight and the loss.

    Why it fails

    Each derivative maps changes from one representation to the next, so the order matters.

    Do this instead

    Follow the path from the weight through pre-activations, activations, later layers, and finally the loss.

  • Common mistake

    Assuming backpropagation directly changes the weights.

    What it looks like

    Saying that backpropagation applies the new weight values.

    Why it fails

    Backpropagation calculates the gradient; it does not directly apply it.

    Do this instead

    Describe gradient calculation and weight updating as separate operations.

  • Common mistake

    Treating a Jacobian as a scalar derivative.

    What it looks like

    Using one derivative when a function has several outputs and several inputs.

    Why it fails

    For a function from Rⁿ to Rᵐ, the Jacobian contains the partial derivative for every output-input pair.

    Do this instead

    Use rows for output components and columns for input components.

Practice Check

MEDIUM

A network contains several layers. Explain, in order, what information is computed during the forward pass, where the initial backward sensitivity comes from for the squared loss, and how the chain rule carries that sensitivity toward earlier layers. End by explaining why the resulting gradient has one component for each network weight.

Hints
  • Begin with the quantities at and ot.
  • Use the final-layer relation δT = oT − y for the squared loss.
  • Distinguish the local derivatives at each layer from the complete derivative of the loss with respect to a weight.
  • Connect each gradient component to one particular weight.

Key Takeaways

  1. 1
    A partial derivative measures how the loss changes when one weight varies while the other weights are held constant.
  2. 2
    The gradient collects one such partial derivative for every network weight.
  3. 3
    A Jacobian organizes derivatives of every output component with respect to every input component.
  4. 4
    The chain rule combines local derivatives across the transformations in a multilayer network.
  5. 5
    Backpropagation moves sensitivities backward, combines them with stored forward quantities, and calculates the gradient; a separate process can then use that gradient to change the weights.

At a glance

  • A partial derivative links one network weight to the way the loss changes.
  • A gradient is a collection of these weight-specific partial derivatives.
  • Jacobians arrange derivatives between vector-valued inputs and outputs.
  • Backpropagation applies the chain rule recursively from the loss toward the input.
  • Forward values and backward sensitivities work together to calculate the gradient of the loss with respect to network weights.

Related concepts

More concepts starting with N