D Dev Duniya
Back to Journal
3 min read

Gradient Descent in Machine Learning with Example - DevDuniya

Ashish Yadav

Ashish Yadav

Published on Dev Duniya
Keep exploring
Compare the details before you buy.

Machine learning models aim to find the best parameters to minimize the error between predicted and actual outputs. Gradient Descent (GD) is a fundamental optimization algorithm that helps achieve this goal. In this article, we will explore Gradient Descent in detail, understand its types, working mechanism, applications, and implement a simple example.

What Is Gradient Descent?

Gradient Descent is an optimization algorithm used to minimize a cost function by iteratively adjusting the model's parameters. It works by moving in the direction of the steepest descent (negative gradient) to reduce errors.

Why Do We Need Gradient Descent?

  1. Large datasets: Gradient Descent is efficient for optimizing models with millions of parameters.
  2. Convex functions: It effectively finds the global minimum for convex functions.
  3. Non-convex functions: For non-convex functions, it helps find a local minimum.

The Math Behind Gradient Descent

To understand Gradient Descent, we start with a cost function J(θ), which measures the error of the model. For example, in linear regression, the cost function is:

Types of Gradient Descent

1. Batch Gradient Descent

  1. Updates parameters after computing the gradient for the entire dataset.
  2. Pros: Stable convergence.
  3. Cons: Slow for large datasets.

2. Stochastic Gradient Descent (SGD)

  1. Updates parameters after computing the gradient for each training example.
  2. Pros: Faster and can escape local minima.
  3. Cons: High variance, leading to oscillations.

3. Mini-Batch Gradient Descent

  1. Combines Batch and SGD by updating parameters after computing the gradient for a mini-batch of training examples.
  2. Pros: Balances efficiency and stability.
  3. Cons: May still converge to local minima in non-convex problems.

Factors Affecting Gradient Descent

1. Learning Rate (α)

The step size determines how fast or slow the algorithm converges. A small learning rate may take too long, while a large one might overshoot the minimum.

2. Initialization

Proper initialization of parameters can speed up convergence and avoid poor local minima.

3. Cost Function Shape

Convex cost functions ensure convergence to a global minimum, while non-convex functions might lead to local minima.

Example: Gradient Descent in Linear Regression

Let’s implement Gradient Descent in Python for a simple linear regression problem.

We have the following dataset:

X (Input)Y (Output)
12
22.8
33.6
44.5

We aim to fit a line Y= θ0+θ1X to this data.

import numpy as np

# Dataset
X = np.array([1, 2, 3, 4])
Y = np.array([2, 2.8, 3.6, 4.5])

# Parameters
theta_0 = 0  # Intercept
theta_1 = 0  # Slope
alpha = 0.01  # Learning rate
epochs = 1000  # Number of iterations
m = len(X)  # Number of data points

# Gradient Descent
for _ in range(epochs):
    Y_pred = theta_0 + theta_1 * X
    d_theta_0 = -(2/m) * np.sum(Y - Y_pred)
    d_theta_1 = -(2/m) * np.sum((Y - Y_pred) * X)
    theta_0 -= alpha * d_theta_0
    theta_1 -= alpha * d_theta_1

print(f"Optimized parameters: theta_0 = {theta_0}, theta_1 = {theta_1}")

After running the code, you will find optimized values for θ0​ and θ1​, giving the best-fit line.

Applications of Gradient Descent

Linear Regression and Logistic Regression

  • Optimize parameters to minimize cost functions.

Neural Networks

  • Train weights and biases for complex architectures.

Clustering

  • Refine centroids in algorithms like K-Means.

Natural Language Processing

  • Improve embeddings and sequence models.

Common Challenges and Solutions

Choosing the Right Learning Rate

  • Use techniques like learning rate schedules or adaptive optimizers (e.g., Adam, RMSprop).

Convergence Issues

  • Normalize data or apply techniques like momentum.

Overfitting

  • Use regularization techniques like L1/L2 regularization.

Gradient Descent is the backbone of optimization in machine learning. It enables models to learn from data by iteratively reducing errors. By understanding its variants and factors influencing its performance, you can effectively train machine learning models. Practice implementing Gradient Descent on various problems to strengthen your grasp.

Keep reading

12 latest articles

The newest stories from Dev Duniya, excluding the article you are reading.

All articles →

From the product catalog

Explore device specifications

Specification cards update automatically from published catalog products, grouped by brand.

Apple specifications

Explore Apple devices →
Apple Apple iPhone Duo
Apple Apple iPhone Duo specifications

Apple iPhone Duo is a new-generation iPhone model focused on a sleek design, powerful performance, and everyday usability. Explore iPhone Duo specifications, features, display, cameras, battery, and expected price in the USA. Get the latest information about Apple iPhone Duo and its key features. Compare iPhone Duo with other popular Apple iPhone models.

Size (inches)
Open: 4.64 x 6.48 x 0.21 Closed: 4.64 x 3.31 x 0.44
Weight (ounces)
8.96
Display size (inches)
Inner: 7.6-inch (diagonal) all-screen OLED folding display Outer: 5.4-inch (diagonal) all-screen OLED display
Resolution (pixels)
Inner: 1878-by-2670-pixel resolution at 430 ppi Outer: 1398-by-2034-pixel resolution at 460 ppi
See all specifications →
Apple Apple iPhone 18 Pro Max
Apple Apple iPhone 18 Pro Max specifications

Apple iPhone 18 Pro Max is a high-end flagship smartphone designed for powerful performance and advanced photography. Explore iPhone 18 Pro Max specifications, features, display, cameras, battery, and expected price in the USA. Discover the latest upgrades and key features of Apple’s largest Pro iPhone. Compare iPhone 18 Pro Max with other premium flagship smartphones.

Size (inches)
6.43 x 3.07 x 0.34
Weight (ounces)
8.78
Display size (inches)
6.9-inch (diagonal) all-screen OLED display
Resolution (pixels)
2868-by-1320-pixel resolution at 460 ppi
See all specifications →
Apple Apple iPhone 18 Pro
Apple Apple iPhone 18 Pro specifications

Apple iPhone 18 Pro is a premium flagship smartphone with advanced performance and an upgraded camera system. Explore iPhone 18 Pro specifications, features, display, cameras, battery, and expected price in the USA. Get the latest details about Apple’s new Pro model and its key upgrades. Compare iPhone 18 Pro features with other flagship smartphones.

Size (inches)
5.91 x 2.83 x 0.34
Weight (ounces)
7.44
Display size (inches)
6.3-inch (diagonal) all-screen OLED display
Resolution (pixels)
2622-by-1206-pixel resolution at 460 ppi
See all specifications →

Google specifications

Explore Google devices →

Samsung specifications

Explore Samsung devices →