Understanding the Cost Function in Machine Learning
The Engine of Optimization
In machine learning, training a model means finding the best parameters (weights and biases) that allow the model to make accurate predictions. To measure “how accurate” our predictions are, we use a Cost Function.
The cost function is the mathematical compass that guides our optimization algorithms, such as Gradient Descent, toward the optimal parameters.
The Mean Squared Error (MSE)
For linear regression, the most common cost function is the Mean Squared Error (MSE). It measures the average of the squares of the errors—that is, the average squared difference between the estimated values and the actual true values.
Let be our model’s prediction for the -th example, and be the actual true label. The MSE cost function is defined as:
Where:
- is the total number of training examples.
- is the weight parameter.
- is the bias parameter.
- is the linear model.
Geometric Interpretation
When we plot the cost function against its parameters and , we get a 3D surface. For linear regression using MSE, this surface is always a convex “bowl” shape.
This geometry is critical. Because it is convex, it guarantees that there is only one global minimum. No matter where we initialize our parameters on this surface, Gradient Descent will always roll down into the same global minimum—the absolute bottom of the bowl—representing the optimal fit.
Matrix Formulation (Vectorized)
For a technically accurate and computationally efficient implementation, the cost function is often expressed in its matrix form. This allows us to leverage optimized linear algebra libraries like NumPy.
Let be the design matrix of shape , where each row is a training example (with a bias term ), be the target vector of shape , and be the parameter vector containing weights and bias of shape .
The predicted values are given by . The MSE cost function in vectorized form is:
This elegant matrix equation computes the exact same squared errors as the summation loop but executes exponentially faster on modern hardware by utilizing SIMD instructions and GPU acceleration.
Gradient Descent
The algorithm used to find this minimum is Gradient Descent, which iteratively updates the parameters:
Where is the learning rate, controlling the size of the steps we take down the hill. By computing the partial derivatives, the algorithm knows exactly which direction points “downhill”, systematically minimizing our error until the model is perfectly optimized.