Cost Function – Intuition

The concept of the cost function is at the base of machine learning algorithms. What is the intuition behind it?

Given a training set of input variables x(i) and output variables y(i), where y(i) is what we are trying to predict, a supervised learning problem is formally defined as h : X→Y, so that h(x) is a good predictor for the corresponding values of y. When the output variable y is continuous we call the learning problem a regression problem; if y can take only a small number of discrete values we call it a classification problem.

The cost function is what lets us figure out the most accurate line through our data points. The best possible line is the one for which the average squared vertical distance of the scattered points from the line is the least.

Animated scatter plot: a line moves from a poor fit to the least-squares fit while the squared residuals, drawn as squares between each point and the line, shrink and the mean squared error readout falls from 4.45 to 0.11.
The line moving from a poor fit to the least-squares solution. Each square is one residual, with side equal to the vertical gap between a point and the line, so the shaded area is precisely what the cost function adds up.

That quantity is the mean squared error, and it is what gradient descent is actually minimising. It is worth sitting with the picture of vertical distances before reaching for the formula, because every later loss function is a variation on the same question: how wrong is this prediction, and how should that wrongness be counted.

Leave a Reply