我正在Python中实现一种算法,以使用gradient descent方法查找二维函数的最接近最小值。它以精度间隔 eps 作为输入,并在给定迭代中初始点与新发现点之间的距离小于 eps 时停止。那个阶段的代码如下:
while(slow_steps <= 4):
lbd_current = lbd(x_current)
x_previous = x_current.copy()
grad = normalized_gradient(f_multi, x_previous)
x_current = [x_previous[i] + lbd_current * grad[i] for i in range(len(x_previous))]
x_list.append(x_current.copy())
iteration += 1
if(distance(x_previous, x_current) <= eps):
slow_steps += 1
但是,我在算法的初始版本中遇到了一个问题:取决于函数,它经常卡在this one之类的“谷”中。
到目前为止,我已经尝试过第二步来穿越山谷:如果算法检测到下降速度较慢,而不是立即结束,它将采用最新和第三至最新迭代中找到的点,并找到两者之间最接近的低点,该线有望与山谷的方向对齐。
if(distance(x_previous, x_current) <= eps * 10 and canyon_steps >= 3):
x_canyon = x_list[len(x_list) - 2]
vector_canyon = [x_current[i] - x_canyon[i] for i in range(len(x_canyon))]
lbd_current = lbd_canyon()
x_current = [x_canyon[i] + vector_canyon[i] * lbd_current for i in range(len(x_canyon))]
if(distance(x_previous, x_current) > eps):
canyon_steps = 0
slow_steps = 0
if(distance(x_previous, x_current) <= eps * 10):
canyon_steps += 1
该算法适用于我尝试过的大多数起始位置,但对于其他this one而言,如果精度较低,则该算法将失败,否则需要花费很长时间才能完成。如何确保算法以尽可能好的机会达到局部最小值?