What is a cost function in machine learning?

Updated May 15, 2026

Short answer

A cost function measures how far model predictions are from actual values.

Deep explanation

A cost function (loss function) quantifies the error between predicted outputs and true labels. It guides optimization by providing a scalar value that the training process minimizes. Common examples include Mean Squared Error (MSE) for regression and Cross-Entropy for classification.

Real-world example

Used in housing price prediction to measure prediction error in dollars.

Common mistakes

  • Confusing cost function with accuracy
  • they represent opposite goals.

Follow-up questions

  • Is cost function always differentiable?
  • How does cost function affect training speed?

More Cost Function interview questions

View all →