Cost function

What Is a Cost Function?

A cost function is a mathematical function that quantifies the discrepancy between a model's predicted output and the true, observed outcome. It produces a scalar value representing the total error over a dataset, and that scalar becomes the objective a learning algorithm seeks to minimize during training. The concept is foundational across machine learning, control theory, and numerical optimization, wherever a system must tune its parameters to improve performance against a measurable standard.

Cost functions are closely related to, and often used interchangeably with, loss functions, though a technical distinction exists: a loss function typically measures error on a single training example, while a cost function aggregates that error across the full dataset, usually as a mean or sum. Both sit at the center of the training loop that adjusts model parameters.

Mathematical Formulation

Formally, a cost function maps a vector of model parameters to a real number. For a model with parameters expressed as a weight vector and a dataset of input-output pairs, the cost is computed by comparing predicted outputs against ground-truth labels using some distance or divergence measure. Mean squared error, for example, computes the average squared difference between predictions and targets, penalizing large deviations more heavily than small ones. Cross-entropy loss computes the negative log-likelihood of the correct class under the predicted probability distribution, making it natural for classification problems where the output is a probability vector.

The choice of cost function encodes an assumption about the error distribution. Squared error implicitly assumes Gaussian-distributed noise, while absolute error (mean absolute error) assumes Laplacian noise and is more robust to outliers. Selecting the right cost function for a given task is therefore as important as choosing the model architecture.

Optimization and Minimization

Once defined, the cost function becomes the target of an optimization algorithm. Gradient descent, the dominant method in modern machine learning, iteratively updates model parameters by moving them in the direction opposite to the gradient of the cost. The gradient indicates the slope of the cost surface, and stepping against it produces lower cost values at each iteration. Variants such as stochastic gradient descent, momentum, RMSprop, and Adam refine this basic procedure to handle noisy gradients and accelerate convergence, as surveyed in the comprehensive overview of gradient descent optimization algorithms by Sebastian Ruder.

The cost surface for deep neural networks is a high-dimensional, non-convex landscape with many local minima, saddle points, and flat regions. Research collected in a 2025 review of loss functions in deep learning examines how the geometry of this surface affects optimization behavior and how task-specific loss designs can steer training toward more useful solutions.

Regularization and Modified Cost Functions

Unmodified cost functions can lead a model to fit the training data too closely, a condition known as overfitting. Regularized cost functions address this by adding a penalty term proportional to the magnitude of the model weights. L2 regularization, which adds the squared norm of the weight vector to the cost, discourages large individual parameter values and tends to produce smoother models. L1 regularization, which adds the absolute norm, encourages sparsity by driving many weights to zero. These modifications change the shape of the cost surface in ways that favor solutions that generalize better to unseen data. The IEEE Xplore publication record on cost function design for machine learning reflects the continuing interest in formulations that balance fitting accuracy against model complexity.

Applications

Cost functions have applications across a wide range of fields, including:

  • Supervised learning, where minimizing prediction error trains classifiers and regression models
  • Reinforcement learning, where reward signals are converted into cost functions for policy optimization
  • Control systems engineering, where quadratic cost functions define optimal control objectives
  • Signal processing, where cost functions drive adaptive filtering and system identification
  • Computer vision, where specialized losses guide image segmentation, object detection, and generative model training
Loading…