What Is a Cost Function and How Is It Used in Training a Neural Network?

What is a cost function and how is it used in training a neural network

Every time I train a neural network, there’s one number I watch more closely than any other: the cost. It tells me, in a single value, exactly how wrong my model currently is. In this article, I want to unpack what a cost function really is, how it’s mathematically defined, how it differs from a loss function, and why it’s the single most important signal guiding the entire training process.

What Is a Cost Function?

A cost function (sometimes called an objective function) is a mathematical function that measures how well a neural network’s predictions match the true target values across an entire training dataset or a batch of examples. It quantifies the “cost” of being wrong — the higher the cost, the worse the model’s predictions.

Formally, if $\hat{y}^{(i)}$ is the network’s prediction for training example $i$, and $y^{(i)}$ is the corresponding true label, the cost function $J$ over $m$ examples is typically defined as the average of a per-example loss function $L$:

$$ J(\theta) = \frac{1}{m} \sum_{i=1}^{m} L\left(\hat{y}^{(i)}, y^{(i)}\right) $$

Here, $\theta$ represents all the trainable parameters of the network — its weights and biases.

Loss Function vs. Cost Function: Clarifying the Terminology

I want to address a common point of confusion early, since I ran into it myself when I was learning. The terms “loss function” and “cost function” are often used interchangeably in casual conversation, but there is a subtle technical distinction:

In practice, many people (and even some textbooks) use these terms interchangeably, so I don’t worry too much about being overly pedantic — but understanding the distinction helps clarify discussions around batch processing and optimization.

Why the Cost Function Is So Central to Training

The cost function serves as the single guiding signal for the entire training process. Everything that happens during training — forward passes, backpropagation, gradient descent updates — exists purely to minimize this one function. Without a well-defined cost function, there would be no way to quantify what “better” even means for a given model.

Training a neural network is, at its core, the following optimization problem:

$$ \theta^* = \arg\min_{\theta} J(\theta) $$

I’m searching for the parameter values $\theta^*$ that minimize the cost function across my training data.

Common Cost Functions and When to Use Them

1. Mean Squared Error (MSE) — for Regression

$$ J(\theta) = \frac{1}{m} \sum_{i=1}^{m} \left(\hat{y}^{(i)} – y^{(i)}\right)^2 $$

MSE penalizes larger errors more heavily than smaller ones (due to the squaring), making it sensitive to outliers. It’s the standard choice for continuous-value prediction tasks.

2. Mean Absolute Error (MAE) — for Regression

$$ J(\theta) = \frac{1}{m} \sum_{i=1}^{m} \left| \hat{y}^{(i)} – y^{(i)} \right| $$

MAE is more robust to outliers than MSE since it doesn’t square the errors, but its gradient is constant in magnitude, which can make convergence near the minimum slightly less smooth.

3. Binary Cross-Entropy — for Binary Classification

$$ J(\theta) = -\frac{1}{m} \sum_{i=1}^{m} \left[ y^{(i)} \log(\hat{y}^{(i)}) + (1 – y^{(i)}) \log(1 – \hat{y}^{(i)}) \right] $$

This cost function heavily penalizes confident, incorrect predictions, which is exactly the behavior I want when training a classifier to output well-calibrated probabilities.

4. Categorical Cross-Entropy — for Multi-Class Classification

$$ J(\theta) = -\frac{1}{m} \sum_{i=1}^{m} \sum_{k=1}^{K} y_k^{(i)} \log(\hat{y}_k^{(i)}) $$

Where $K$ is the number of classes. This generalizes binary cross-entropy to problems with more than two mutually exclusive classes.

5. Hinge Loss — for Support Vector Machines and Some Classification Tasks

$$ J(\theta) = \frac{1}{m} \sum_{i=1}^{m} \max(0, 1 – y^{(i)} \cdot \hat{y}^{(i)}) $$

Less common in modern deep learning, but still relevant in certain classification contexts, particularly margin-based classifiers.

Comparison Table of Common Cost Functions

Cost FunctionTask TypeSensitive to OutliersTypical Output Activation
Mean Squared Error (MSE)RegressionYesLinear
Mean Absolute Error (MAE)RegressionNoLinear
Binary Cross-EntropyBinary ClassificationN/ASigmoid
Categorical Cross-EntropyMulti-Class ClassificationN/ASoftmax
Hinge LossMargin-based ClassificationSomewhatLinear (raw score)

How the Cost Function Drives Training: The Full Loop

Here’s how the cost function fits into the broader training process I described in my article on gradient descent:

  1. Forward pass: Input data flows through the network, producing predictions $\hat{y}$.
  2. Compute cost: The cost function $J(\theta)$ compares predictions to true labels $y$, producing a single scalar value representing overall error.
  3. Backward pass (backpropagation): The gradient of the cost function with respect to every parameter, $\nabla J(\theta)$, is computed using the chain rule.
  4. Parameter update: Gradient descent (or a variant like Adam) uses this gradient to adjust the parameters in the direction that reduces the cost.
  5. Repeat: This cycle continues, iteration after iteration, until the cost converges to a satisfactorily low value or stops improving.

The Shape of the Cost Function: Convex vs. Non-Convex

For simple models like linear regression, the cost function (MSE) is convex — it has a single global minimum, and gradient descent is guaranteed to find it (given an appropriate learning rate).

For deep neural networks, however, the cost function is highly non-convex, with a complex landscape full of local minima, saddle points, and flat regions. This is one of the fascinating aspects of deep learning research — despite this complexity, gradient-based methods still tend to find good solutions in practice, in large part because of the high dimensionality of the parameter space, which makes true local minima relatively rare compared to saddle points.

Regularization Terms Within the Cost Function

I often add a regularization term to the cost function to discourage overly complex models and reduce overfitting:

$$ J_{\text{reg}}(\theta) = J(\theta) + \lambda \sum_{j} \theta_j^2 $$

This is known as L2 regularization (or weight decay), where $\lambda$ is a hyperparameter controlling the strength of the penalty on large weight values. There’s also L1 regularization, which uses the absolute value of weights instead of their square, and tends to encourage sparsity (many weights becoming exactly zero):

$$ J_{\text{reg}}(\theta) = J(\theta) + \lambda \sum_{j} |\theta_j| $$

Implementing Cost Functions in Python

Here’s how I might implement several common cost functions manually, followed by their framework equivalents.

import numpy as np

def mse_cost(y_true, y_pred):
    return np.mean((y_true - y_pred) ** 2)

def binary_cross_entropy_cost(y_true, y_pred, epsilon=1e-12):
    y_pred = np.clip(y_pred, epsilon, 1 - epsilon)  # avoid log(0)
    return -np.mean(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred))

def categorical_cross_entropy_cost(y_true, y_pred, epsilon=1e-12):
    y_pred = np.clip(y_pred, epsilon, 1 - epsilon)
    return -np.mean(np.sum(y_true * np.log(y_pred), axis=1))

And using PyTorch’s built-in cost (loss) functions:

import torch.nn as nn

mse_loss = nn.MSELoss()
bce_loss = nn.BCELoss()
cross_entropy_loss = nn.CrossEntropyLoss()

# Example usage during training
cost = mse_loss(predictions, targets)
cost.backward()  # Computes gradients via backpropagation
optimizer.step()  # Updates parameters using gradient descent

Advantages of a Well-Chosen Cost Function

Disadvantages and Limitations

Real-World Use Cases

Comparing Cost Function Choice Across Problems

Problem TypeRecommended Cost FunctionReasoning
Predicting continuous valuesMSE or MAEMeasures magnitude of numerical error
Binary classificationBinary Cross-EntropyPenalizes confident wrong predictions heavily
Multi-class classificationCategorical Cross-EntropyNaturally handles probability distributions over classes
Imbalanced classificationWeighted Cross-Entropy or Focal LossAddresses class imbalance by adjusting penalty per class
Object detectionCombined losses (classification + localization)Task requires multiple simultaneous objectives

Best Practices When Working With Cost Functions

  1. Match the cost function to the task type — regression tasks need MSE/MAE, classification tasks need cross-entropy variants.
  2. Normalize input and target data to help the cost function’s landscape remain well-behaved for optimization.
  3. Add regularization terms (L1/L2) to the cost function when overfitting is a concern.
  4. Monitor both training and validation cost throughout training to detect overfitting early.
  5. Consider class imbalance when choosing or weighting cost functions for classification problems.
  6. Use numerically stable implementations (e.g., framework-provided loss functions) rather than naive manual implementations, which can suffer from issues like log(0).

Summary

A cost function is the mathematical measure of how wrong a neural network’s predictions are across a training dataset, and it serves as the central guiding signal for the entire training process. By combining a per-example loss function (like squared error or cross-entropy) and averaging it across the training set, the cost function gives gradient descent a clear direction to follow: adjust the parameters to make this number as small as possible. Choosing the right cost function for a given task — and understanding how it interacts with regularization, optimization, and the output layer’s activation function — is one of the most fundamental skills in building effective, well-trained neural networks.

References

Exit mobile version