ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Calculus Fundamentals for Machine Learning
Chapter 1: Why Calculus for Machine Learning?
The Role of Math in Machine Learning
Introducing Functions: Inputs and Outputs
Visualizing Functions: Graphs
What is a Limit?
Intuition Behind Limits
Connecting Functions and Limits to ML
Quiz for Chapter 1
Chapter 2: Derivatives: Measuring Change
Rate of Change: Average vs Instantaneous
The Derivative: Slope of a Tangent Line
Derivative Notation (Leibniz and Lagrange)
Calculating Derivatives: The Power Rule
Calculating Derivatives: Constants and Sums
Introduction to Higher-Order Derivatives
Practice: Calculating Simple Derivatives
Quiz for Chapter 2
Chapter 3: Optimization with Derivatives
Finding Maximum and Minimum Points
Optimization: Why Minimize or Maximize?
Cost Functions in Machine Learning
Goal: Minimizing the Cost Function
Introduction to Gradient Descent
How Derivatives Guide Gradient Descent
Visualizing Gradient Descent
Quiz for Chapter 3
Chapter 4: Handling Multiple Inputs: Partial Derivatives
Functions of Multiple Variables
Partial Derivatives: The Concept
Calculating Partial Derivatives
Partial Derivative Notation
The Gradient Vector
Geometric Meaning of the Gradient
Practice: Calculating Partial Derivatives and Gradients
Quiz for Chapter 4
Chapter 5: Calculus in Action: Simple Optimization
Recap: Optimization Goal and Gradient Descent
Example: Simple Linear Regression Model
Defining a Cost Function for Linear Regression
Calculating Gradients for the Cost Function
Performing a Gradient Descent Step
The Learning Rate Parameter
Putting It All Together: The Optimization Process
Hands-on Practical: Manual Gradient Calculation
Quiz for Chapter 5

Hands-on Practical: Manual Gradient Calculation

Cost functions, gradients, and the basic idea of gradient descent are essential concepts in machine learning optimization. A manual calculation of the gradient for a simple linear regression model, using a tiny dataset, provides practical insight. This process solidifies understanding of how derivatives drive optimization.

Setting the Scene: A Simple Problem

Imagine we have a very small dataset with just three points (x,y)(x, y)(x,y): (1,2)(1, 2)(1,2), (2,3)(2, 3)(2,3), and (3,5)(3, 5)(3,5). Our goal is to find the best-fitting line of the form y=mx+by = mx + by=mx+b for these points.

Our tiny dataset containing three points, along with an initial guess for our line: y=0x+0y = 0x + 0y=0x+0.

We need a way to measure how "good" our line is. We'll use the Mean Squared Error (MSE) as our cost function, which we discussed earlier. For our three data points (x1,y1),(x2,y2),(x3,y3)(x_1, y_1), (x_2, y_2), (x_3, y_3)(x1​,y1​),(x2​,y2​),(x3​,y3​), the MSE is:

J(m,b)=13∑i=13(yi−y^i)2=13∑i=13(yi−(mxi+b))2J(m, b) = \frac{1}{3} \sum_{i=1}^{3} (y_i - \hat{y}_i)^2 = \frac{1}{3} \sum_{i=1}^{3} (y_i - (mx_i + b))^2J(m,b)=31​i=1∑3​(yi​−y^​i​)2=31​i=1∑3​(yi​−(mxi​+b))2

Let's start with an initial guess for our parameters: m=0m = 0m=0 and b=0b = 0b=0. Our line is initially y=0y = 0y=0.

Calculating the Initial Cost

First, let's calculate the cost for our initial guess (m=0,b=0m=0, b=0m=0,b=0). The predicted values (y^i=mxi+b\hat{y}_i = mx_i + by^​i​=mxi​+b) are:

  • y^1=0(1)+0=0\hat{y}_1 = 0(1) + 0 = 0y^​1​=0(1)+0=0
  • y^2=0(2)+0=0\hat{y}_2 = 0(2) + 0 = 0y^​2​=0(2)+0=0
  • y^3=0(3)+0=0\hat{y}_3 = 0(3) + 0 = 0y^​3​=0(3)+0=0

The squared errors are:

  • (y1−y^1)2=(2−0)2=4(y_1 - \hat{y}_1)^2 = (2 - 0)^2 = 4(y1​−y^​1​)2=(2−0)2=4
  • (y2−y^2)2=(3−0)2=9(y_2 - \hat{y}_2)^2 = (3 - 0)^2 = 9(y2​−y^​2​)2=(3−0)2=9
  • (y3−y^3)2=(5−0)2=25(y_3 - \hat{y}_3)^2 = (5 - 0)^2 = 25(y3​−y^​3​)2=(5−0)2=25

The Mean Squared Error is:

J(0,0)=13(4+9+25)=383≈12.67J(0, 0) = \frac{1}{3} (4 + 9 + 25) = \frac{38}{3} \approx 12.67J(0,0)=31​(4+9+25)=338​≈12.67

This is our starting cost. Our goal is to reduce this value by adjusting mmm and bbb.

Calculating the Gradients

Now for the core calculus part: finding the gradient of the cost function J(m,b)J(m, b)J(m,b). The gradient is a vector containing the partial derivatives with respect to each parameter: ∇J=[∂J∂m,∂J∂b]\nabla J = \left[ \frac{\partial J}{\partial m}, \frac{\partial J}{\partial b} \right]∇J=[∂m∂J​,∂b∂J​]. These derivatives tell us how the cost changes as we slightly change mmm or bbb.

Let's find ∂J∂m\frac{\partial J}{\partial m}∂m∂J​ and ∂J∂b\frac{\partial J}{\partial b}∂b∂J​. Remember, when taking a partial derivative with respect to one variable (like mmm), we treat other variables (like bbb) as constants.

Partial Derivative with respect to mmm (∂J∂m\frac{\partial J}{\partial m}∂m∂J​)

We start with the cost function:

J(m,b)=13[(y1−(mx1+b))2+(y2−(mx2+b))2+(y3−(mx3+b))2]J(m, b) = \frac{1}{3} \left[ (y_1 - (mx_1 + b))^2 + (y_2 - (mx_2 + b))^2 + (y_3 - (mx_3 + b))^2 \right]J(m,b)=31​[(y1​−(mx1​+b))2+(y2​−(mx2​+b))2+(y3​−(mx3​+b))2]

We differentiate term by term with respect to mmm. Let's focus on one term: (yi−(mxi+b))2(y_i - (mx_i + b))^2(yi​−(mxi​+b))2. We use the chain rule. Let u=yi−mxi−bu = y_i - mx_i - bu=yi​−mxi​−b. Then the term is u2u^2u2. The derivative of u2u^2u2 with respect to mmm is 2u⋅∂u∂m2u \cdot \frac{\partial u}{\partial m}2u⋅∂m∂u​. Now, we find ∂u∂m=∂∂m(yi−mxi−b)\frac{\partial u}{\partial m} = \frac{\partial}{\partial m} (y_i - mx_i - b)∂m∂u​=∂m∂​(yi​−mxi​−b). Since yiy_iyi​, xix_ixi​, and bbb are treated as constants when differentiating with respect to mmm, this simplifies to ∂∂m(−mxi)=−xi\frac{\partial}{\partial m} (-mx_i) = -x_i∂m∂​(−mxi​)=−xi​.

So, the derivative of (yi−(mxi+b))2(y_i - (mx_i + b))^2(yi​−(mxi​+b))2 with respect to mmm is 2(yi−mxi−b)(−xi)2(y_i - mx_i - b)(-x_i)2(yi​−mxi​−b)(−xi​).

Applying this to our cost function J(m,b)J(m, b)J(m,b):

∂J∂m=13∑i=132(yi−(mxi+b))(−xi)\frac{\partial J}{\partial m} = \frac{1}{3} \sum_{i=1}^{3} 2(y_i - (mx_i + b))(-x_i)∂m∂J​=31​i=1∑3​2(yi​−(mxi​+b))(−xi​) ∂J∂m=−23∑i=13xi(yi−(mxi+b))\frac{\partial J}{\partial m} = -\frac{2}{3} \sum_{i=1}^{3} x_i(y_i - (mx_i + b))∂m∂J​=−32​i=1∑3​xi​(yi​−(mxi​+b))

Partial Derivative with respect to bbb (∂J∂b\frac{\partial J}{\partial b}∂b∂J​)

Similarly, we differentiate J(m,b)J(m, b)J(m,b) with respect to bbb. For instance, one term is (yi−(mxi+b))2(y_i - (mx_i + b))^2(yi​−(mxi​+b))2. Let u=yi−mxi−bu = y_i - mx_i - bu=yi​−mxi​−b. The derivative with respect to bbb is 2u⋅∂u∂b2u \cdot \frac{\partial u}{\partial b}2u⋅∂b∂u​. Now, ∂u∂b=∂∂b(yi−mxi−b)\frac{\partial u}{\partial b} = \frac{\partial}{\partial b} (y_i - mx_i - b)∂b∂u​=∂b∂​(yi​−mxi​−b). Treating yiy_iyi​, mmm, and xix_ixi​ as constants, this is ∂∂b(−b)=−1\frac{\partial}{\partial b} (-b) = -1∂b∂​(−b)=−1.

So, the derivative of (yi−(mxi+b))2(y_i - (mx_i + b))^2(yi​−(mxi​+b))2 with respect to bbb is 2(yi−mxi−b)(−1)2(y_i - mx_i - b)(-1)2(yi​−mxi​−b)(−1).

Applying this to the cost function:

∂J∂b=13∑i=132(yi−(mxi+b))(−1)\frac{\partial J}{\partial b} = \frac{1}{3} \sum_{i=1}^{3} 2(y_i - (mx_i + b))(-1)∂b∂J​=31​i=1∑3​2(yi​−(mxi​+b))(−1) ∂J∂b=−23∑i=13(yi−(mxi+b))\frac{\partial J}{\partial b} = -\frac{2}{3} \sum_{i=1}^{3} (y_i - (mx_i + b))∂b∂J​=−32​i=1∑3​(yi​−(mxi​+b))

Numerical Gradient Calculation

Now we plug our data points (1,2),(2,3),(3,5)(1, 2), (2, 3), (3, 5)(1,2),(2,3),(3,5) and our current parameters m=0,b=0m=0, b=0m=0,b=0 into these formulas. Remember our predictions were y^1=0,y^2=0,y^3=0\hat{y}_1=0, \hat{y}_2=0, \hat{y}_3=0y^​1​=0,y^​2​=0,y^​3​=0.

Calculating ∂J∂m\frac{\partial J}{\partial m}∂m∂J​:

∂J∂m=−23[x1(y1−y^1)+x2(y2−y^2)+x3(y3−y^3)]\frac{\partial J}{\partial m} = -\frac{2}{3} \left[ x_1(y_1 - \hat{y}_1) + x_2(y_2 - \hat{y}_2) + x_3(y_3 - \hat{y}_3) \right]∂m∂J​=−32​[x1​(y1​−y^​1​)+x2​(y2​−y^​2​)+x3​(y3​−y^​3​)] ∂J∂m=−23[1(2−0)+2(3−0)+3(5−0)]\frac{\partial J}{\partial m} = -\frac{2}{3} \left[ 1(2 - 0) + 2(3 - 0) + 3(5 - 0) \right]∂m∂J​=−32​[1(2−0)+2(3−0)+3(5−0)] ∂J∂m=−23[1(2)+2(3)+3(5)]\frac{\partial J}{\partial m} = -\frac{2}{3} \left[ 1(2) + 2(3) + 3(5) \right]∂m∂J​=−32​[1(2)+2(3)+3(5)] ∂J∂m=−23[2+6+15]=−23[23]=−463≈−15.33\frac{\partial J}{\partial m} = -\frac{2}{3} \left[ 2 + 6 + 15 \right] = -\frac{2}{3} [23] = -\frac{46}{3} \approx -15.33∂m∂J​=−32​[2+6+15]=−32​[23]=−346​≈−15.33

Calculating ∂J∂b\frac{\partial J}{\partial b}∂b∂J​:

∂J∂b=−23[(y1−y^1)+(y2−y^2)+(y3−y^3)]\frac{\partial J}{\partial b} = -\frac{2}{3} \left[ (y_1 - \hat{y}_1) + (y_2 - \hat{y}_2) + (y_3 - \hat{y}_3) \right]∂b∂J​=−32​[(y1​−y^​1​)+(y2​−y^​2​)+(y3​−y^​3​)] ∂J∂b=−23[(2−0)+(3−0)+(5−0)]\frac{\partial J}{\partial b} = -\frac{2}{3} \left[ (2 - 0) + (3 - 0) + (5 - 0) \right]∂b∂J​=−32​[(2−0)+(3−0)+(5−0)] ∂J∂b=−23[2+3+5]=−23[10]=−203≈−6.67\frac{\partial J}{\partial b} = -\frac{2}{3} \left[ 2 + 3 + 5 \right] = -\frac{2}{3} [10] = -\frac{20}{3} \approx -6.67∂b∂J​=−32​[2+3+5]=−32​[10]=−320​≈−6.67

So, at m=0,b=0m=0, b=0m=0,b=0, the gradient is ∇J≈[−15.33,−6.67]\nabla J \approx [-15.33, -6.67]∇J≈[−15.33,−6.67].

Interpreting the Gradient

What do these numbers tell us?

  • ∂J∂m≈−15.33\frac{\partial J}{\partial m} \approx -15.33∂m∂J​≈−15.33: This is negative, meaning if we increase mmm slightly, the cost JJJ will decrease. The magnitude (15.33) indicates the cost is quite sensitive to changes in mmm.
  • ∂J∂b≈−6.67\frac{\partial J}{\partial b} \approx -6.67∂b∂J​≈−6.67: This is also negative. Increasing bbb slightly will also decrease the cost JJJ. The cost is less sensitive to changes in bbb compared to mmm at this point.

The gradient [−15.33,−6.67][-15.33, -6.67][−15.33,−6.67] points in the direction of the steepest increase in cost. To decrease the cost (which is our goal in optimization), we need to move in the opposite direction of the gradient. This means we should increase both mmm and bbb.

Taking a Small Step (Gradient Descent)

Let's perform one step of gradient descent. We need a learning rate, α\alphaα. Let's choose a small value, say α=0.01\alpha = 0.01α=0.01.

The update rules are:

  • mnew=mold−α∂J∂mm_{new} = m_{old} - \alpha \frac{\partial J}{\partial m}mnew​=mold​−α∂m∂J​
  • bnew=bold−α∂J∂bb_{new} = b_{old} - \alpha \frac{\partial J}{\partial b}bnew​=bold​−α∂b∂J​

Plugging in our values (mold=0,bold=0m_{old}=0, b_{old}=0mold​=0,bold​=0, α=0.01\alpha=0.01α=0.01, ∂J∂m=−46/3\frac{\partial J}{\partial m} = -46/3∂m∂J​=−46/3, ∂J∂b=−20/3\frac{\partial J}{\partial b} = -20/3∂b∂J​=−20/3):

  • mnew=0−(0.01)(−463)=0.01×463=0.463≈0.153m_{new} = 0 - (0.01) \left(-\frac{46}{3}\right) = 0.01 \times \frac{46}{3} = \frac{0.46}{3} \approx 0.153mnew​=0−(0.01)(−346​)=0.01×346​=30.46​≈0.153
  • bnew=0−(0.01)(−203)=0.01×203=0.203≈0.067b_{new} = 0 - (0.01) \left(-\frac{20}{3}\right) = 0.01 \times \frac{20}{3} = \frac{0.20}{3} \approx 0.067bnew​=0−(0.01)(−320​)=0.01×320​=30.20​≈0.067

Our new parameters are approximately m≈0.153m \approx 0.153m≈0.153 and b≈0.067b \approx 0.067b≈0.067.

Let's visualize the new line y≈0.153x+0.067y \approx 0.153x + 0.067y≈0.153x+0.067:

Our data points along with the initial line (dashed red) and the line after one gradient descent step (solid green). The new line is slightly closer to the data points.

If we were to calculate the cost J(0.153,0.067)J(0.153, 0.067)J(0.153,0.067), we would find it's lower than our initial cost of 12.6712.6712.67 (as calculated in the thought process, it's around 10.03). By repeating this process - calculating gradients and updating parameters - gradient descent iteratively finds better values for mmm and bbb, minimizing the cost function.

This manual calculation shows the mechanism: derivatives (gradients) tell us the direction to adjust parameters (mmm and bbb) to improve our model by reducing the cost function (MSEMSEMSE). While libraries automate this, understanding the underlying calculation is fundamental to grasping how machine learning models learn.

© 2026 ApX Machine Learning