How machine learning works
I know internet is full of videos and articles that explains how machine learning (ML) works. I thought I could write a short essay about it from a mathematical point of view, only for reinforcing my knowledge about it :). Obviously, most of this post will be write by AI, but I will try to give it my personal touch with some personal remarks and some code and interactive views.
Maths behind the ML
I assume you are familiar with the basics of calculus, linear algebra and probability. If not, I recommend start with the basic concepts of them.
ML is not AI. ML is a corelation between two random variables
The most basic ML model is the linear regression. We try to find a function
Here,
Measuring the Mess: What Error Should We Use?
To find the “best” line, we need a mathematical way to define how wrong our current line is. We do this using a Loss Function (or Cost Function).
For linear regression, the undisputed champion of loss functions is the Mean Squared Error (MSE).
If we have
Personal Remark: Why do we square the error instead of just taking the absolute value? Two reasons! First, squaring ensures that negative and positive errors don’t cancel each other out. Second, and more importantly for the math, a squared function creates a beautiful, smooth, convex curve (think of a bowl shape). This guarantees that when we use calculus to find the minimum point (the bottom of the bowl), there is only one global minimum to find. It makes the optimization math much easier.
When Linear Regression is a Terrible Idea
Linear regression is fantastic because it is interpretable and fast, but it is entirely useless in a few key scenarios:
- Non-Linear Relationships: If your data follows a complex curve, a wave, or a circle, trying to fit a straight line through it is like trying to fit a square peg in a round hole. The model will “underfit” and your predictions will be garbage.
- Classification Problems: If you are trying to predict a category rather than a continuous number (e.g., “Is this email spam or not?” or “Is this a picture of a cat or a dog?”), linear regression fails. A straight line can stretch to infinity, but probabilities must live strictly between 0 and 1. In these cases, we swap to Logistic Regression (which uses a Sigmoid function to squash the output) or jump into neural networks.
- Highly Complex Data: For things like image recognition, audio processing, or natural language processing, the relationships between pixels or words are far too complex for a single linear equation to capture.
Backpropagation: The Calculus Workhorse
When linear models aren’t enough, we stack multiple functions together to create a Neural Network. Instead of a simple
This brings up a massive problem: if the network makes a bad prediction, how do we know which specific weight (
Enter Backpropagation (Backward Propagation of Errors).
Backpropagation is essentially the Chain Rule from calculus, applied systematically from the output of the network all the way back to the input.
Let’s look at the math. Suppose our Loss
Here is how the backpropagation algorithm actually works in practice:
- Forward Pass: We push our data
through the network to get a prediction. We compare this prediction to the actual and calculate the total Loss . - Backward Pass: We calculate the gradient (the partial derivatives) of the loss function with respect to every single weight and bias in the network using the chain rule formula above.
- Gradient Descent: We update every weight to move slightly in the opposite direction of the gradient. If the derivative is positive (meaning increasing the weight increases the error), we decrease the weight.
The update rule looks like this:
Here,