Docsity

Gradient Descent Algorithm, Lecture notes of Artificial Intelligence

Toggle sidebar
bg1
Gradient Descent Algorithm: How does it Work in Machine
Learning?
Imagin e you ’re lost in a dens e forest with no map or compas s. What do you do? You follo w the path of
steepest des cent, tak ing st eps in the direct ion th at decreases the slope an d br ings you closer to yo ur
destinat ion. Similar ly, g rad ient des cent i s the go- to algori th m for navig ating th e complex landscape of
machin e learning. It helps mod els find the optimal set of paramet ers by iter ati vel y adju stin g the m in the
oppos ite direction of t he g rad ient. In this arti cle, we’ll take a deep dive into the worl d of g rad ient desc ent,
explor ing its different flavors, applicat ions, and challenges. Ge t ready to sharpen your optimization skills
and join the ran ks of the machin e learni ng elite!
This artic le was p ublis hed as a p art of the Data Sc ience Blog athon.
Table of contents
Wh at is a C ost F unct ion?
Wh at is Gradie nt Descent?
How Does Gr adi ent D escen t Work?
Typ es of Gradie nt Descent
Batch Gradient Desc ent
Stochast ic Gradient Descent
Min i-Batch Gradient Descent
Plotting the Gradient Descent A lgorithm
Alpha – Th e Lear ning Rate
Loc al Min ima
Code Imp lementation of Gradien t Des cen t in Python
Ch allenges of Gradient Descen t
Frequ ently Asked Question s
What is a Cost Function?
It is a func ti on that measures th e performan ce of a model for any given data. Cost Functio n quan tifies the
error betwe en pr edicted values and exp ected value s and presents it in th e form of a sin gle real nu mber.
After making a h ypothes is with ini tial paramete rs, we cal culate the C ost functi on. A nd with a goal to
red uc e the cost functi on, w e modify the parameters by us ing the Gradient des cent alg orith m over t he given
dat a. Here’s the mathemat ical r epresentation for it:
BE GI N N E R
MA CH IN E L EA RNI NG
MAT HS
PYT HO N
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Gradient Descent Algorithm and more Lecture notes Artificial Intelligence in PDF only on Docsity!

Gradient Descent Algorithm: How does it Work in Machine

Learning?

Imagine you’re lost in a dense forest with no map or compass. What do you do? You follow the path of steepest descent, taking steps in the direction that decreases the slope and brings you closer to your destination. Similarly, gradient descent is the go-to algorithm for navigating the complex landscape of machine learning. It helps models find the optimal set of parameters by iteratively adjusting them in the opposite direction of the gradient. In this article, we’ll take a deep dive into the world of gradient descent, exploring its different flavors, applications, and challenges. Get ready to sharpen your optimization skills and join the ranks of the machine learning elite!

This article was published as a part of the Data Science Blogathon.

Table of contents

What is a Cost Function? What is Gradient Descent? How Does Gradient Descent Work? Types of Gradient Descent Batch Gradient Descent Stochastic Gradient Descent Mini-Batch Gradient Descent

Plotting the Gradient Descent Algorithm Alpha – The Learning Rate Local Minima Code Implementation of Gradient Descent in Python Challenges of Gradient Descent Frequently Asked Questions

What is a Cost Function?

It is a function that measures the performance of a model for any given data. Cost Function quantifies the error between predicted values and expected values and presents it in the form of a single real number.

After making a hypothesis with initial parameters, we calculate the Cost function. And with a goal to reduce the cost function, we modify the parameters by using the Gradient descent algorithm over the given data. Here’s the mathematical representation for it:

BE G I NNE R M A C HI NE LE A RNI NG M AT HS PYT HO N

Source: Coursera

What is Gradient Descent?

Gradient descent is an optimization algorithm used in machine learning to minimize the cost function by iteratively adjusting parameters in the direction of the negative gradient, aiming to find the optimal set of parameters.

The cost function represents the discrepancy between the predicted output of the model and the actual output. The goal of gradient descent is to find the set of parameters that minimizes this discrepancy and improves the model’s performance.

The algorithm operates by calculating the gradient of the cost function, which indicates the direction and magnitude of steepest ascent. However, since the objective is to minimize the cost function, gradient descent moves in the opposite direction of the gradient, known as the negative gradient direction.

By iteratively updating the model’s parameters in the negative gradient direction, gradient descent gradually converges towards the optimal set of parameters that yields the lowest cost. The learning rate, a hyperparameter, determines the step size taken in each iteration, influencing the speed and stability of convergence.

Gradient descent can be applied to various machine learning algorithms, including linear regression, logistic regression, neural networks, and support vector machines. It provides a general framework for optimizing models by iteratively refining their parameters based on the cost function.

  1. Compute the gradient (slope), the first order derivative of the function at that point
  2. Make a step (move) in the direction opposite to the gradient , opposite direction of slope increase from the current point by alpha times the gradient at that point

Source: Coursera

Alpha is called Learning rate – a tuning parameter in the optimization process. It decides the length of the steps.

How Does Gradient Descent Work?

  1. It is is an optimization algorithm used to minimize the cost function of a model.
  2. The cost function measures how well the model fits the training data and is defined based on the difference between the predicted and actual values.
  3. The gradient of the cost function is the derivative with respect to the model’s parameters and points in the direction of the steepest ascent.
  4. The algorithm starts with an initial set of parameters and updates them in small steps to minimize the cost function.
  5. In each iteration of the algorithm, the gradient of the cost function with respect to each parameter is computed.

. The gradient tells us the direction of the steepest ascent, and by moving in the opposite direction, we can find the direction of the steepest descent.

  1. The size of the step is controlled by the learning rate, which determines how quickly the algorithm moves towards the minimum. . The process is repeated until the cost function converges to a minimum, indicating that the model has reached the optimal set of parameters.
  2. There are different variations of gradient descent, including batch gradient descent, stochastic gradient descent, and mini-batch gradient descent, each with its own advantages and limitations.
  3. Efficient implementation of gradient descent is essential for achieving good performance in machine learning tasks. The choice of the learning rate and the number of iterations can significantly impact the performance of the algorithm.

Types of Gradient Descent

The choice of gradient descent algorithm depends on the problem at hand and the size of the dataset. Batch gradient descent is suitable for small datasets, while stochastic gradient descent algorithm is more suitable for large datasets. Mini-batch is a good compromise between the two and is often used in practice.

Batch Gradient Descent

Batch gradient descent updates the model’s parameters using the gradient of the entire training set. It calculates the average gradient of the cost function for all the training examples and updates the parameters in the opposite direction. Batch gradient descent guarantees convergence to the global minimum, but can be computationally expensive and slow for large datasets.

Stochastic Gradient Descent

Stochastic gradient descent updates the model’s parameters using the gradient of one training example at a time. It randomly selects a training example, computes the gradient of the cost function for that example, and updates the parameters in the opposite direction. Stochastic gradient descent is computationally efficient and can converge faster than batch gradient descent. However, it can be noisy and may not converge to the global minimum.

Mini-Batch Gradient Descent

Mini-batch gradient descent updates the model’s parameters using the gradient of a small subset of the training set, known as a mini-batch. It calculates the average gradient of the cost function for the mini- batch and updates the parameters in the opposite direction. Mini-batch gradient descent algorithm combines the advantages of both batch and stochastic gradient descent, and is the most commonly used method in practice. It is computationally efficient and less noisy than stochastic gradient descent, while still being able to converge to a good solution.

Gradient Descent and its Types

Plotting the Gradient Descent Algorithm

If the learning rate is too high, we might OVERSHOOT the minima and keep bouncing, without reaching the minima If the learning rate is too small, the training might turn out to be too long

Source: Coursera

Learning rate is optimal, model converges to the minimum Learning rate is too small, it takes more time but converges to the minimum Learning rate is higher than the optimal value, it overshoots but converges ( 1/C < η <2/C) Learning rate is very large, it overshoots and diverges, moves away from the minima, performance decreases on learning

Source: researchgate

Note: As the gradient decreases while moving towards the local minima, the size of the step decreases. So, the learning rate (alpha) can be constant over the optimization and need not be varied iteratively.

Local Minima

The cost function may consist of many minimum points. The gradient may settle on any one of the minima, which depends on the initial point (i.e initial parameters(theta)) and the learning rate. Therefore, the optimization may converge to different points with different starting points and learning rate.

Convergence of cost function w ith different starting points (Source: Gfycat )

Code Implementation of Gradient Descent in Python

Gradient Descent Algorithm

Challenges of Gradient Descent

While gradient descent is a powerful optimization algorithm, it can also present some challenges that can affect its performance. Some of these challenges include:

  1. Local Optima: Gradient descent can converge to local optima instead of the global optimum, especially if the cost function has multiple peaks and valleys.
  2. Learning Rate Selection: The choice of learning rate can significantly impact the performance of gradient descent. If the learning rate is too high, the algorithm may overshoot the minimum, and if it is too low, the algorithm may take too long to converge.
  3. Overfitting: Gradient descent can overfit the training data if the model is too complex or the learning rate is too high. This can lead to poor generalization performance on new data.

A. Gradient descent is an optimization algorithm used to find the minimum of a function by iteratively adjusting the parameters in the opposite direction of the gradient. It is commonly used in machine learning to optimize the parameters of models and improve their performance on a given task.

Q6. What are the two types of gradient descent?

A. The two types of gradient descent are batch gradient descent and stochastic gradient descent. Batch gradient descent updates the model’s parameters using the entire training set in each iteration, while stochastic gradient descent updates the parameters using only one training sample at a time.

Article Url - https://www.analyticsvidhya.com/blog/2020/10/how-does-the-gradient-descent-algorithm- work-in-machine-learning/

Crypto