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 x(t)x(t) models the position of something at time tt, then we have:

v(t)=โˆ‚x(t)โˆ‚ta(t)=โˆ‚2x(t)โˆ‚t2 v(t) = \frac{\partial x(t)}{\partial t} \\[10pt] a(t) = \frac{\partial^2 x(t)}{\partial t^2}

Where

  • v(t)v(t) is the velocity at time tt
  • a(t)a(t) is the acceleration at time tt

And we could step backwards using integration:

v(t)=โˆซa(t)dtx(t)=โˆซv(t)dt v(t) = \int a(t) dt \\[10pt] x(t) = \int v(t) dt
Derivative And Integral Circular Flowchart
The calculus loop for motion (time dependent position).

Example: Taking Derivatives and Integrals

Letโ€™s work through a concrete example with units.

Consider an object with position:

x(t)=2t2+3t[ meters] x(t) = 2t^2 + 3t \quad [\text{ meters}]

To find velocity, take the first derivative:

v(t)=ddt(2t2+3t)=4t+3[ meters/second] v(t) = \frac{d}{dt}(2t^2 + 3t) = 4t + 3 \quad [\text{ meters/second}]

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:

a(t)=ddt(4t+3)=4[ meters/second2] a(t) = \frac{d}{dt}(4t + 3) = 4 \quad [\text{ meters/second}^2]

Notice how weโ€™ve lost some information here. The 3 just disappeared. Thatโ€™s because it was constant - it did not change with tt. So when we calculate the rate of change (the derivative) we ignore it.

We can verify our work by integrating back:

Starting with acceleration:

a(t)=4[ meters/second2] a(t) = 4 \quad [\text{ meters/second}^2]

Integrate to get velocity:

v(t)=โˆซ4โ€‰dt=4t+C1[ meters/second] v(t) = \int 4 \, dt = 4t + C_1 \quad [\text{ meters/second}]

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:

x(t)=โˆซ(4t+C1)โ€‰dt=2t2+C1t+C2[ meters] x(t) = \int (4t + C_1) \, dt = 2t^2 + C_1t + C_2 \quad [\text{ meters}]

Comparing this to our initial equation, we can see that C1=3C_1 = 3 and C2=0C_2 = 0.

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:

y=w1x1+w2x2+b y = w_1x_1 + w_2x_2 + b

The partial derivatives with respect to w1w_1, w2w_2 and bb tell us how fast yy changes in response to these variables changing:

โˆ‚yโˆ‚w1=x1 \frac{\partial y}{\partial w_1} = x_1
โˆ‚yโˆ‚w2=x2 \frac{\partial y}{\partial w_2} = x_2
โˆ‚yโˆ‚b=1 \frac{\partial y}{\partial b} = 1

We can see how this is helpful for tuning parameters to some task.

For example, if we have (x1,x2)=(1,3)(x_1, x_2) = (1, 3) and our current weights are (w1,w2,b)=(0.5,โˆ’1,2)(w_1, w_2, b) = (0.5, -1, 2), we can calculate:

y=0.5(1)+(โˆ’1)(3)+2=โˆ’0.5 y = 0.5(1) + (-1)(3) + 2 = -0.5

The partial derivatives tell us that:

  • Increasing w1w_1 by ฮ”\Delta will increase yy by 1ฮ”1\Delta (since x1=1x_1=1)
  • Increasing w2w_2 by ฮ”\Delta will increase yy by 3ฮ”3\Delta (since x2=3x_2=3)
  • Increasing bb by ฮ”\Delta will increase yy by ฮ”\Delta

So if our target value was y=2y=2, we need to increase yy by 2.5. We could achieve this by one of the following:

  • Increasing w1w_1 by 2.5
  • Increasing w2w_2 by 0.833
  • Increasing bb 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:

โˆ‡f(x,y)=[โˆ‚fโˆ‚xโˆ‚fโˆ‚y] \nabla f(x, y) = \begin{bmatrix} \frac{\partial f}{\partial x} \\[5pt] \frac{\partial f}{\partial y} \end{bmatrix}

This gives us all the information we need to point us in the direction of the steepest ascent or descent.

3D Gradient Visualization - Direction and Magnitude on a Paraboloid Surface Plot
A 3D plot where the z-axis represents the value of the function f(x,y). The cyan arrows represent the gradient at various points in space. Notice how they are 2-dimensional (constrained to the x-y plane). They point outwards, indicating the exact direction that you should increase x and y if you want to get the largest increase in f(x,y). Also notice how their magnitude increases as the steepness of f(x,y) increases.

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:

L=12(ypredโˆ’ytarget)2 L = \frac{1}{2}(y_{pred} - y_{target})^2

where ypred=w1x1+w2x2+by_{pred} = w_1x_1 + w_2x_2 + b

The gradient is the vector of partial derivatives with respect to our parameters:

โˆ‡L=[โˆ‚Lโˆ‚w1โˆ‚Lโˆ‚w2โˆ‚Lโˆ‚b]=[(ypredโˆ’ytarget)x1(ypredโˆ’ytarget)x2(ypredโˆ’ytarget)] \nabla L = \begin{bmatrix} \frac{\partial L}{\partial w_1} \\[5pt] \frac{\partial L}{\partial w_2} \\[5pt] \frac{\partial L}{\partial b} \end{bmatrix} = \begin{bmatrix} (y_{pred} - y_{target})x_1 \\[5pt] (y_{pred} - y_{target})x_2 \\[5pt] (y_{pred} - y_{target}) \end{bmatrix}

Using our previous example where:

  • (x1,x2)=(1,3)(x_1, x_2) = (1, 3)
  • (w1,w2,b)=(0.5,โˆ’1,2)(w_1, w_2, b) = (0.5, -1, 2)
  • ytarget=2y_{target} = 2
  • ypred=0.5(1)+(โˆ’1)(3)+2=โˆ’0.5y_{pred} = 0.5(1) + (-1)(3) + 2 = -0.5

We can calculate the gradient:

โˆ‡L=[(โˆ’0.5โˆ’2)(1)(โˆ’0.5โˆ’2)(3)(โˆ’0.5โˆ’2)]=[โˆ’2.5โˆ’7.5โˆ’2.5] \nabla L = \begin{bmatrix} (-0.5 - 2)(1) \\[5pt] (-0.5 - 2)(3) \\[5pt] (-0.5 - 2) \end{bmatrix} = \begin{bmatrix} -2.5 \\[5pt] -7.5 \\[5pt] -2.5 \end{bmatrix}

This gradient tells us we should:

  • Increase w1w_1 by an amount proportional to 2.5
  • Increase w2w_2 by an amount proportional to 7.5
  • Increase bb 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 h(x)h(x) that is composed of two functions f(g(x))f(g(x)), then:

โˆ‚hโˆ‚x=โˆ‚fโˆ‚gโ‹…โˆ‚gโˆ‚x \frac{\partial h}{\partial x} = \frac{\partial f}{\partial g} \cdot \frac{\partial g}{\partial x}

For example, consider h(x)=(x2+1)3h(x) = (x^2 + 1)^3. We can break this into:

  • g(x)=x2+1g(x) = x^2 + 1
  • f(g)=g3f(g) = g^3

Then:

โˆ‚fโˆ‚g=3g2 \frac{\partial f}{\partial g} = 3g^2
โˆ‚gโˆ‚x=2x \frac{\partial g}{\partial x} = 2x

So:

โˆ‚hโˆ‚x=3(x2+1)2โ‹…2x=6x(x2+1)2 \frac{\partial h}{\partial x} = 3(x^2 + 1)^2 \cdot 2x = 6x(x^2 + 1)^2

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.