Question 04: Implement Gradient Descent Algorithm to find the local minima of a function. For example, find the local minima of the function y=(x+3)² starting from the point x=2. Download hole Program / Project code, by clicking following link: Question ? Answer Programming Code: Following code write in: ML_P04.py # ML Project Program 04
# Gradient Descent Algorithm
# initialize parameters
cur_x = 2 # x = 2, given
rate = 0.01
precision = 0.000001
previous_step_size = 1
max_iters = 1000
iters = 0
# Gradient function y=(x+3)²
df = lambda x : 2 * (x + 3)
# Create a loop to perform Gradient Descent
while previous_step_size > precision and iters < max_iters:
prev_x = cur_x
cur_x -= rate * df(prev_x)
previous_step_size = abs(prev_x - cur_x)
iters += 1
print("Local Minima Occurs at : ", cur_x)
# Thanks For Reading.
Output:
# ML Project Program 04 # Gradient Descent Algorithm # initialize parameters cur_x = 2 # x = 2, given rate = 0.01 precision = 0.000001 previous_step_size = 1 max_iters = 1000 iters = 0 # Gradient function y=(x+3)² df = lambda x : 2 * (x + 3) # Create a loop to perform Gradient Descent while previous_step_size > precision and iters < max_iters: prev_x = cur_x cur_x -= rate * df(prev_x) previous_step_size = abs(prev_x - cur_x) iters += 1 print("Local Minima Occurs at : ", cur_x) # Thanks For Reading.
Output:
0 Comments