forked from souravjain540/Basic-Python-Programs
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradient_descent.py
More file actions
Latest commit
31 lines (26 loc) · 829 Bytes
/
Copy pathgradient_descent.py
File metadata and controls
31 lines (26 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fromnumpyimportasarray
fromnumpy.randomimportrand
# objective function
defobjective(x):
returnx**2.0
defderivative(x):
returnx*2.0
defgradient_descent(objective, derivative, bounds, n_iter, step_size):
solution=bounds[:, 0] +rand(len(bounds)) * (bounds[:, 1] -bounds[:, 0])
foriinrange(n_iter):
# calculate gradient
gradient=derivative(solution)
# take a step
solution=solution-step_size*gradient
# evaluate candidate point
solution_eval=objective(solution)
# report progress
print('>%d f(%s) = %.5f'% (i, solution, solution_eval))
return [solution, solution_eval]
# Driver Code:
bounds=asarray([[-1.0, 1.0]])
n_iter=30
step_size=0.1
best, score=gradient_descent(objective, derivative, bounds, n_iter, step_size)
print('Done!')
print('f(%s) = %f'% (best, score))