Created
October 6, 2019 04:15
-
-
Save helve2017/72081c8cbdf0a3a072ec932e01536fad to your computer and use it in GitHub Desktop.
直線探索(Armijo条件)を使った直線探索のクラス
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
class GradientDescent: | |
def __init__(self, fun, der, xi=0.3, tau=0.9, tol=1e-6, ite_max=2000): | |
self.fun = fun # 目的関数 | |
self.der = der # 関数の勾配 | |
self.xi = xi # Armijo条件の定数 | |
self.tau = tau # 方向微係数の学習率 | |
self.tol = tol # 勾配ベクトルのL2ノルムがこの値より小さくなると計算を停止 | |
self.path = None # 解の点列 | |
self.ite_max = ite_max # 最大反復回数 | |
def minimize(self, x): | |
path = [x] | |
for i in range(self.ite_max): | |
grad = self.der(x) | |
if np.linalg.norm(grad, ord=2)<self.tol: | |
break | |
else: | |
beta = 1 | |
while self.fun(x - beta*grad) > (self.fun(x) - self.xi*beta*np.dot(grad, grad)): | |
# Armijo条件を満たすまでループする | |
beta = self.tau*beta | |
x = x - beta * grad | |
path.append(x) | |
self.opt_x = x # 最適解 | |
self.opt_result = self.fun(x) # 関数の最小値 | |
self.path = np.array(path) # 探索解の推移 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment