-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7b676f2
Showing
3 changed files
with
834 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import numpy as np | ||
|
||
def backtracking(f, grad_f, x): | ||
""" | ||
This function is a simple implementation of the backtracking algorithm for | ||
the GD (Gradient Descent) method. | ||
f: function. The function that we want to optimize. | ||
grad_f: function. The gradient of f(x). | ||
x: ndarray. The actual iterate x_k. | ||
""" | ||
alpha = 1 | ||
c = 0.8 | ||
tau = 0.25 | ||
|
||
while f(x - alpha * grad_f(x)) > f(x) - c * alpha * np.linalg.norm(grad_f(x), 2) ** 2: | ||
alpha = tau * alpha | ||
|
||
if alpha < 1e-5: | ||
break | ||
|
||
return alpha |