Gradient Descent: An Intuitive Approach
Why the algorithm behind almost all of deep learning works—from hilltop intuition and prerequisites to partial derivatives, Taylor approximation, and parameter update rules.
- Gradient Descent
- Classical ML
- Machine Learning
- Statistical Learning
- Mathematics

Welcome to the second article of the series! Previously, we learnt about Linear regression. Honestly, we have been doing that for a while now, either through one article or some other coursework. Now, some of you might have questioned what gradient descent was that was mentioned earlier, or some of you might have already studied that well. To begin with, let's just take the fact that Gradient Descent is one of the most fundamental algorithms to update weights.
What is Gradient Descent?
Gradient descent is an optimization algorithm that is often used as a black-box optimizer that uses iterative methods to minimize a cost function by adjusting model parameters in the direction of the steepest descent of the function's gradient.
Too Technical? Even I failed to understand the definition when I first read it. So why don't we understand it with an example?
Suppose you are standing on the top of Everest. Would it be an exaggeration, right? So let's just imagine that you are standing on any hilltop which is covered by dense fog, and your goal is to reach the lowest point in the valley. So what would you do?
Well, I would just take a random step in a descending direction and follow the steepest descent so that I would reach my goal faster. That is what the Gradient Descent Algorithm does:
- STEP 1: It takes a random guess and initialises that point.
- STEP 2: Calculate the gradient at that point.
- STEP 3: Moves in the opposite direction of the gradient to minimise the cost function.
- STEP 4: Iteratively repeats the process until the cost function converges.
What are the prerequisites?
Before we move towards an intuitive understanding of the gradient descent, it is necessary to understand the conditions that a function must satisfy.
The function must be:
- 01.Differentiable: A function is differentiable when there exists a derivative of that function for each point in the domain. For example: Polynomials like f(x) = x² + 2xy, Trigonometric functions like \sin(x), \cos(x), and constant functions like f(x) = 10.

- 01.Convex: A function is said to be a convex function when a line segment between any two points in a domain lies on or above the graph, forming a bowl-shaped curve. For example: f(x) = x², f(x) = e^x.

Mathematics behind Gradient Descent
Now, let's understand gradient descent by taking inspiration from the cost function of Linear regression, as we understood in the previous article:
J(θ₀, θ₁) = (1 / 2m) Σ_{i=1..m} (y_i - (θ₀ + θ₁ x_i))²While considering an algorithm, our goal is to minimise the cost function such that with each iteration we update the parameters, i.e., θ_0 and θ_1 in our scenario, till the convergence. Now, what do I mean by that? Simply, we are trying to find values that help us understand the function that best describes the relationship between variables.
To do that, let's take a partial derivative on both sides w.r.t θ_0:
∂J / ∂θ₀ = (∂ / ∂θ₀) [ (1 / 2m) Σ_{i=1..m} (y_i - (θ₀ + θ₁ x_i))² ]Moving the constants outside the derivative:
= (1 / 2m) Σ_{i=1..m} (∂ / ∂θ₀) (y_i - (θ₀ + θ₁ x_i))²Applying the chain rule:
= (1 / 2m) Σ_{i=1..m} 2 (y_i - (θ₀ + θ₁ x_i)) (-1)Simplifying and rearranging the equation:
∂J / ∂θ₀ = (1 / m) Σ_{i=1..m} ((θ₀ + θ₁ x_i) - y_i)Similarly, deriving a partial derivative with respect to θ_1:
∂J / ∂θ₁ = (1 / m) Σ_{i=1..m} ((θ₀ + θ₁ x_i) - y_i) x_iNow, before we move forward, we must know what we mean by gradients. In the context of Machine Learning and calculus, we assume the gradient as a vector that contains all the partial derivatives of a function that points in the direction of the steepest ascent or the maximum rate of increase.

As we can see in the above image, the gradient is normal to the surface, which is basically the consequence of the multi-variable chain rule. Now, you might be thinking, why does the gradient point towards the direction of the steepest ascent?
Imagine we are dealing with a single variable case, say \partial f / \partial x. If this function is positive, then moving in the direction of the slope (i.e. increasing the value of x) will also increase the value of the function. On the other hand, if the same function is negative, the function value will start increasing as we move along the slope (in -x direction).
Deriving the Update Rule via First-Order Taylor Approximation
Now that we have the basic understanding of gradients, let's move back to the original problem: since our goal is to minimise the cost function, the question arises — how do we do this?
Assuming a single parameter, for a small change Δθ we can utilise the First-Order Taylor Approximation as:
J(θ + Δθ) ≈ J(θ) + (dJ / dθ) ΔθNow that we know, the derivative is extremely important to understand the cost and sensitivity of the function at a point. We also want to move towards a direction where the cost moves towards the steepest descent. That means:
J(θ + Δθ) < J(θ)Using the approximation:
J(θ) + (dJ / dθ) Δθ < J(θ)Simplifying:
(dJ / dθ) Δθ < 0Which indicates that Δθ must move opposite to the sign of dJ/dθ, which implies:
Δθ = -α (dJ / dθ)Where \alpha > 0 is a small step size, and substituting these results, we get:
θ_new = θ + Δθ
θ_new = θ - α (dJ / dθ)This substitution gives us the update rule for the cost function. Now, going back to the linear regression problem, where we were interested in two parameters, θ_0 and θ_1, we will have a gradient vector instead of a single derivative:
∇J = [ ∂J / ∂θ₀ , ∂J / ∂θ₁ ]ᵀSince this gradient vector moves in the direction of the steepest ascent, we need to move in the opposite direction, which can be done by utilising the update rule we derived above, which expands to:
θ₀ := θ₀ - α (∂J / ∂θ₀)
θ₁ := θ₁ - α (∂J / ∂θ₁)Substituting these derivatives, we get:
θ₀ := θ₀ - α ( (1 / m) Σ_{i=1..m} ((θ₀ + θ₁ x_i) - y_i) )
θ₁ := θ₁ - α ( (1 / m) Σ_{i=1..m} ((θ₀ + θ₁ x_i) - y_i) x_i )We keep on repeating these update rules until we reach convergence. Now, what do we mean by convergence? It is just a point in the iterative process where the model reaches the most or near-optimal parameters that minimise the loss function.
After all these derivations, the gradient descent may feel like a heavy mathematical concept with lots of derivatives and summation, but it is just a systematic way of assuming the direction where we need to move in order to reduce the error in the fastest way possible.
And with this, you now don't just know the formula, but you understand the intuition and the working behind this.
In the next article, we will talk about different types of gradient descent with the basic differences and what makes them powerful.
References
- 01.McCormick, C. (2014, March 4). Gradient descent derivation. Chris McCormick Research Archive. https://mccormickml.com/2014/03/04/gradient-descent-derivation/
- 02.Stanford Online. (2020, April 17). Stanford CS229: Machine Learning — Linear Regression and Gradient Descent | Lecture 2 (Autumn 2018) [Video]. YouTube. https://www.youtube.com/watch?v=4b4MUYve_U8
- 03.Marty B. (2014, May 26). Direction of gradient from level surface? Mathematics Stack Exchange. https://math.stackexchange.com/questions/810171/direction-of-gradient-from-level-surface