gradient "ascent" update python code code example
Example 1: gradient ascent algorithm python
import matplotlib.pyplot as mp, numpy as np
origin = lambda x: 2 * x - x ** 2
x = np.linspace(0, 2, 9999)
mp.plot(x, origin(x), c='black')
derivative = lambda x: 2 - 2 * x
extreme_point = 0
alpha = 0.1
presision = 0.001
while True:
mp.scatter(extreme_point, origin(extreme_point))
error = alpha * derivative(extreme_point)
extreme_point += error Climbing
if abs(error) < presision:
break
mp.show()123456789101112131415161718
Example 2: gradient ascent algorithm python
import matplotlib.pyplot as mp, numpy as np
origin = lambda x: 2 * x - x ** 2
x = np.linspace(0, 2, 9999)
mp.plot(x, origin(x), c='black')
derivative = lambda x: 2 - 2 * x
extreme_point = 0
alpha = 0.1
precision = 0.001
while True:
mp.scatter(extreme_point, origin(extreme_point))
error = alpha * derivative(extreme_point)
extreme_point += error Climbing
if abs(error) < precision:
break
mp.show()123456789101112131415161718