Use gradient descent with a learning rate of 0.01. to find the local minima of the function
Question:
Use gradient descent with a “learning rate” of 0.01. to find the local minima of the function y=(2x+5)² starting from the point x=3.
At each step show the point (x, y), the value of the gradient.
Use the following code snippet to get started.
cur_x = 3
rate = 0.01 # Learning rate
precision = 0.0000001 #This tells us when to stop the algorithm
max_iters = 10000 # maximum number of iterations
iters = 0 #iteration counter
cur_x = 3
while iters < max_iters:
prev_x= cur_x
cur_x = cur_x - rate * grad(prev_x) #grad returns the gradient
items = iters+1 #iteration count
Change the code to introduce the required precision. That is to stop when the difference between two consecutive iterations is less than the given precision parameter.
Now use a different function that has local and global minima and experiment with different starting points.