In the previous lesson, we talked about calculus and looked at the gradient.
We saw how this mathematical tool allows us to calculate the rate of change of a function and the direction of steepest descent.
Weβll continue talking about gradients here, but first Iβll explain what weβre actually trying to optimize: the loss function.
This Lesson Will Cover
π Definition and purpose of loss functions
π Common loss functions used in machine learning
π Gradient descent optimization techniques
βοΈ Examples of optimization in linear and logistic regression
What is a Loss Function?
A loss function measures how well a machine learning model predicts outcomes.
It quantifies the error between the predicted values (y^β) and the actual values (y).
i.e.
Loss=Error(y^β,y)
By minimizing the loss function: searching over the parameter space for the values that yield the lowest loss, we tune parameters to make better predictions.
This is called optimization.
Common Loss Functions
Mean Squared Error (MSE)
This is commonly used for regression problems.
MSE=n1ββi=1nβ(y^βiββyiβ)2
MSE punishes large errors more than small ones by squaring the difference between predicted and true values. Itβs great for regression problems where you want to strongly discourage big mistakes.
It requires calculating the residuals: the differences between predicted and actual values.
The squared term ensures that:
Negative residuals (when predictions are too low) and positive residuals (when predictions are too high) are treated equally
Larger errors are penalized more heavily due to the quadratic nature
For example, if a model predicts 3 when the actual value is 5, the residual of -2 becomes 4 after squaring, just like a residual of +2 would.
The resulting error will have different units than the target variable y that is being modeled. We can fix this by taking the square root and returning the RMSE (Root Mean Squared Error) instead:
RMSE=n1ββi=1nβ(y^βiββyiβ)2β
RMSE is the square root of MSE. It gives error values in the same units as the target variable, making it easier to interpret. Itβs commonly used when you care about the scale of the prediction errors.
Cross-Entropy Loss (Log loss)
This is commonly used for classification problems. It requires calculating the product of the actual labels with the logarithm of the predicted labels, e.g.
yΓβlog(y^β)
where y is an actual label (0 or 1) and y^β is the predicted probability of that label.
Cross-entropy measures how confident and correct a model's predicted probability is. It gives small loss for confident right answers and high loss for confident wrong ones. It's the go-to for classification problems.
Itβs easy to see how this behaves by looking at a f(x)=βlogx graph for x<=1 (as seen above).
When x is near zero we get a large positive value and when x is near 1 we get a small positive value.
For example, if a model predicts 0.9 probability for the correct class, then we calculate the log of 0.9 and multiply it by negative one to get a small loss of 0.105, while a prediction of 0.1 for the correct class will give a much larger loss of 2.303.
This asymmetrical treatment helps the model optimize towards confident, correct predictions.
Hereβs the full cross-entropy loss for binary classification:
Selecting a loss function from the examples above, depending on the type of ML problem. Also including the multi-class generalization of the binary cross-entropy loss described above.
Gradient Descent Optimization
Now that we understand loss functions, letβs see how we can use them with gradient descent to optimize model parameters.
For simple functions, we can find the minimum analytically by taking the derivative, setting it equal to zero, and solving for the parameters.
For example, with a quadratic function f(x)=x2+2x+1, we can find the minimum by solving dxdfβ=2x+2=0, giving us x=β1.
However, in machine learning, this approach is typically not feasible because of:
High Dimensionality: Machine learning models often have thousands or even millions of parameters. Computing analytical solutions for the derivatives of loss functions with respect to all these parameters is computationally expensive and, in many cases, impossible.
Complex Loss Surfaces: The loss functions in machine learning are often non-convex, meaning they have many local minima, saddle points, and flat regions. This complexity makes it difficult to find a closed-form solution.
Dynamic and Nonlinear Relationships: The relationship between the model parameters and the loss function is often nonlinear and dynamic, making it hard to compute exact minima.
Instead, gradient descent provides an iterative approach to find the minimum of the loss function.
The Gradient Descent Algorithm
Gradient descent works by iteratively updating the model parameters in the direction of the steepest descent of the loss function.
This direction is given by the negative gradient of the loss function with respect to the parameters.
The update rule for each parameter ΞΈiβ is:
ΞΈiββΞΈiββΞ·β βΞΈiββLβ
Where:
ΞΈiβ is the parameter being updated.
Ξ· is the learning rate, which controls the step size of each update.
βΞΈiββLβ is the gradient of the loss functionL with respect to ΞΈiβ.
This can also be expressed in gradient notation:
ΞΈβΞΈβΞ·βΞΈβL(ΞΈ)
Where:
ΞΈ is the vector of all parameters
Ξ· is the learning rate
βΞΈβL(ΞΈ) is the gradient vector containing partial derivatives of the loss with respect to each parameter
The gradient descent algorithm itself is simple:
Initialize Parameters: Start with initial guesses for the model parameters. These can be random values or zeros, depending on the problem.
Compute Gradients: Calculate the gradient of the loss function with respect to each parameter. This step often involves backpropagation in neural networks.
Update Parameters: Adjust each parameter using the update rule.
Repeat: Iterate through steps 2 and 3 until the parameters converge, i.e., the changes in the parameters become negligible or the loss function stops decreasing significantly.
Flow chart for gradient descent examples below, where the loss functions are MSE for linear regression and cross-entropy for logistic regression.
Linear Regression with MSE
Letβs optimize a simple linear regression modely^β=wx+b with MSE loss
Just like our example of gradient descent for linear regression, these new parameter values will be used in the next iteration and the process will continue until the loss converges or we reach a maximum number of iterations.
Conclusion
I love plugging toy data into a math equation and watching it come to life.
We learned a lot about loss functions and optimization here, but we should be mindful that these examples are so simple that they hide much of the complexity involved in real-world applications.
Key Takeaways
π― Loss functions measure how well a model predicts outcomes.
π Mean Squared Error (MSE) is commonly used for regression, penalizing large errors more.
π’ Cross-Entropy Loss is commonly used for classification, encouraging confident predictions.
π Gradient Descent is an iterative optimization technique for finding minima in high-dimensional spaces.
βοΈ Learning rate is a critical parameter that controls how quickly the model updates.
Next: Optimization in Practice
In the next section, weβll talk about some of the practical considerations of optimization for machine learning and deep learning.
Comments (0)
No comments yet.