我正在嘗試對具有 2 個變數的函式進行最陡下降。它在已知步長 = 0.3 的情況下作業正常。但我想找到一種方法來優化步長并創建一個函式來找到一個好的步長。我發現了一個叫做 Armijo-Goldstein 條件的東西,但我不明白,而且這個公式讓我有點困惑。所以我請求你的幫助,如果你知道如何平衡這一點,因為我認為與我的代碼步長相關的所有內容都是錯誤的。我猜它必須計算在 x 和 y 上加深的步長。
x, y = f.optimal_range() ##getting random start
step = 0.3 ## <--- This number have to be random between 0 to 1. But my step size calculations are wrong so I can't do it.
while f.fprime_x(x) != 0: ##loop to find 0 point for derivative of a function on x
fx = -f.fprime_x(x)
x = x (step * fx)
print(x)
if not f.delta_check(step, x, y): <--- Here's the problem. By the defenition the step have to be smaller if it doesn't pass the check, but if I make it smaller - it enters the eternal loop around the mi
step = step * 1.001
while f.fprime_y(y) != 0: ##loop to find 0 point for derivative of a function on x
fy = -f.fprime_y(y)
y = y (step * fy)
print(x, y)
if not f.delta_check(step, x, y):
step = step * 1.001
print(f"\n\nMIN ({x}, {y})")
這是增量/步長檢查的功能:
def delta_check(delta, x, y):
ux = -fprime_x(x)
uy = -fprime_y(y)
f_del_xy = func(x (delta * ux), y (delta * uy))
return f_del_xy <= func(delta * ux, delta * uy) delta
uj5u.com熱心網友回復:
這是一個名義上的 Armijo–Goldstein 實作。但是,如果沒有資料 函式示例,則無法對其進行測驗。
# both should be less than, but usually close to 1
c = 0.8 # how much imperfection in function improvement we'll settle up with
tau = 0.8 # how much the step will be decreased at each iteration
x = np.array(f.optimal_range()) # assume everything is a vector; x is an n-dimensional coordinate
# NOTE: the part below is repeated for every X update
step = 0.3 # alpha in Armijo–Goldstein terms
gradient = np.array(f.fprime_x(x[0]), f.fprime_y(x[1]), ...)
# in the simplest case (SGD) p can point in the direction of gradient,
# but in general they don't have to be the same, e.g. because of added momentum
p = -gradient / ((gradient**2).sum() **0.5)
m = gradient.dot(p) # "happy case" improvement per unit step
t = - c * m # improvement we'll consider good enough
# func(*x) might be worth precomputing
while func(*x) - func(*(x step*p)) < step * t: # good enough step size found
step *= tau
# update X and repeat
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/409369.html
標籤:
下一篇:簡單平衡問題的簡單演算法
