Docsity

gradient descent algorithm, Slides of Artificial Intelligence

Partial preview of the text

Download gradient descent algorithm and more Slides Artificial Intelligence in PDF only on Docsity!

GRADIENT DESCENT

ALGORITHM

DEFINITIONS

- It is an algorithm to minimize a cost function by

iteratively updating the model ‘s parameters based

on the gradient (derivative) of the cost function with

respect to those parameters.

  • It is an algorithm to minimize a function by optimizing

parameters(general definition)

Explanation :

example linear regression model

y = mx + c

Cost function

J = y – ( y predicted)

Mean squared error

Using gradient descent to compute the line that best fit five set of points in the table below

Therefore the equation above , best fits the set of

points given.

ALGORITHM

1. Initialize the model's parameters with random values or predefined values.
2. Calculate the predicted output using the current parameter values.
3. Calculate the loss by comparing the predicted output with the actual output.
4. Calculate the gradients of the parameters by computing the derivatives of the loss function with
respect to each parameter.
5. Update the parameters by subtracting a fraction (learning rate) of the gradients from their current
values.
6. Repeat steps 2-5 for a certain number of iterations or until the loss reaches an acceptable level.

plt.scatter(x, y, color='red') k = 100 # Plot every kth line for i in range(0, iterations+1, k): m_curr_k = m_curr - i/k * md c_curr_k = c_curr - i/k * cd y_predicted_k = m_curr_k * x + c_curr_k plt.plot(x, y_predicted_k, color='blue') plt.title('Line of Best Fit') plt.xlabel('x') plt.ylabel('y')

Plotting line of best fit passing through the mean of x against the mean of y

x_mean = np.mean(x) y_mean = np.mean(y) slope, intercept = np.polyfit(x, y, 1) plt.plot(x_mean, y_mean, marker='o', markersize=5, color="green") plt.plot(x, slope*x + intercept, color='green') plt.show() x = np.array([1, 2, 3, 4, 5]) y = np.array([5, 7, 9, 11, 13]) gradient_descent(x, y)

ADVANTAGES

  • It is strong against noise in the data , the gradient valuation and the domain , if tuned in correctly.
  • Finds either a saddle point or extremal( relating to maximal or minimal values) point (maybe only a local one).
  • It is easy to compute, don’t need exact gradient, can update in pieces on high-dimension spaces(this form is sometimes called ‘coordinate descent’).
  • Handles the very largest problems ,low memory requirement.

APPLICATIONS

  • Training neural networks: Gradient descent is extensively used to train neural networks, which are a
fundamental component of many AI applications.
  • Supervised learning: Gradient descent plays a crucial role in supervised learning tasks, where the goal is
to learn a model that maps input data to desired output labels. In tasks such as image classification,
natural language processing
  • Deep learning: Deep learning models, which are neural networks with multiple hidden layers, have
gained significant popularity in recent years.
  • Used in Machine Learning and Deep Learning to minimize a cost/loss function.
  • Natural language processing: Gradient descent is widely used in natural language processing (NLP)
tasks such as machine translation, sentiment analysis, and text generation.