Differentiation and Gradients
Calculus is the mathematical study of continuous change.
Thereโs two aspects: derivatives and integrals.
This Lesson Will Cover
- ๐ Derivatives and integrals
- ๐ข Partial derivatives
- ๐ Gradients and gradient descent
- ๐ The chain rule
- ๐ค Applications in machine learning
What are Derivatives and Integrals
If models the position of something at time , then we have:
Where
- is the velocity at time
- is the acceleration at time
And we could step backwards using integration:
Example: Taking Derivatives and Integrals
Letโs work through a concrete example with units.
Consider an object with position:
To find velocity, take the first derivative:
Notice how the units changed. When we take the derivative with respect to time, itโs like โdividing by timeโ as far as the units are concerned.
For acceleration, take the second derivative:
Notice how weโve lost some information here. The 3 just disappeared. Thatโs because it was constant - it did not change with . So when we calculate the rate of change (the derivative) we ignore it.
We can verify our work by integrating back:
Starting with acceleration:
Integrate to get velocity:
See how the units changed here. Integrating over time is like โmultiplying by timeโ as far as the units are concerned.
Integrate again to get position:
Comparing this to our initial equation, we can see that and .
But why did we add these? Well, remember when we lost the 3 earlier, when taking the derivative?
When we integrate, we need to account for these โconstantsโ that are discarded when taking the derivative.
So we say โhereโs the time-dependent part, and hereโs a constant that is not time dependent, and whose value we do not knowโ.
Calculus for Machine Learning
Approaching calculus from a physics perspective helps give us some intuition for what itโs all about.
In ML, we are not as interested in integrals as we are in derivatives.
Differentiation drives optimization algorithms like gradient descent for linear regression and backpropagation (which is also gradient descent) for deep learning, allowing our models to learn from training data
This lesson will cover the basics of differentiation and the role of gradients.
Partial Derivatives
A partial derivative measures how fast a function changes in some specific direction: the rate of change with respect to a single variable while keeping all others constant.
In the example above, we took the partial derivative with respect to the โtimeโ variable.
For ML, partial derivatives allow us to calculate how each parameter in a model affects the output. This allows us to make independent adjustments to each parameter which help minimize the modelโs error during training.
For example, in a linear regression model:
The partial derivatives with respect to , and tell us how fast changes in response to these variables changing:
We can see how this is helpful for tuning parameters to some task.
For example, if we have and our current weights are , we can calculate:
The partial derivatives tell us that:
- Increasing by will increase by (since )
- Increasing by will increase by (since )
- Increasing by will increase by
So if our target value was , we need to increase by 2.5. We could achieve this by one of the following:
- Increasing by 2.5
- Increasing by 0.833
- Increasing by 2.5
Or, we could make some combination of changes to all three parameters.
This is the fundamental idea behind gradient-based optimizationโwe can use these partial derivatives to guide how we adjust our parameters to move closer to our target.
Gradients
So, thinking about the example above, we want to know the rate of change of some function with respect to all of the dependent variables.
This is what the gradient does:
This gives us all the information we need to point us in the direction of the steepest ascent or descent.
Interpreting the Gradient
- Direction: The gradient points in the direction of the steepest ascent.
- Magnitude: The length of the gradient vector indicates how steep the slope is.
In the gradient descent algorithm, we use the negative gradient to move in the direction of steepest descent, minimizing the error function (loss function).
Example: Gradient Descent
For linear regression, our loss function (mean squared error) for a single training example is:
where
The gradient is the vector of partial derivatives with respect to our parameters:
Using our previous example where:
We can calculate the gradient:
This gradient tells us we should:
- Increase by an amount proportional to 2.5
- Increase by an amount proportional to 7.5
- Increase by an amount proportional to 2.5
to reduce our loss and move closer to our target value.
The Chain Rule
Thereโs one more aspect of calculus that weโll see again later, when we get to talking about backpropagation for neural networks. And thatโs the chain rule.
The chain rule tells us how to find the derivative of a composite function.
If we have a function that is composed of two functions , then:
For example, consider . We can break this into:
Then:
So:
As we will see later in this course, the chain rule is important for doing backpropagation calculations because it allows us to break down the rate of change of the loss function with respect to the weights and biases of our neural network layers.
Conclusion
I hope that this lesson helped you build an intuitive understanding of whatโs going on with calculus and the mathematics of change.
Key Takeaways
๐งฎ Calculus is essential for understanding continuous change, and it consists of differentiation and integration.
๐ข Partial derivatives help measure the rate of change of a function with respect to a single variable.
๐ Gradients guide optimization by pointing in the direction of steepest ascent or descent.
โ๏ธ Gradient descent is the foundation of many ML algorithms, allowing us to minimize loss functions efficiently.
๐ The chain rule is crucial for backpropagation, helping calculate how changes in parameters affect the model output.
Next: Optimization Techniques
In the next lesson, weโll continue talking about gradient descent and how it fits into the bigger picture of optimization for ML.
Comments (0)
No comments yet.