最小二乘回溯线搜索算法的实现

时间:2019-06-12 09:35:07

标签: python mathematical-optimization

我正在尝试使用Python实现回溯线搜索算法,以优化最小二乘函数。我面临的问题是,我在每次迭代中收到的错误指标正在增加而不是减少。我要实现的特定算法是这样的: img

我编写的代码如下:

import numpy as np


# Least-square function
def f(x, A, b):
    return np.linalg.norm(A @ x - b) ** 2

# Gradient for least-square.
def grad(x, A, b):
    return 2 * (((A.T @ A) @ x) - A.T @ b)

# Parameters.
alpha = 0.3
beta = 0.8
t = 1
count = 1

A = np.array([[1, 2], [3, 4]])
b = np.array([[5], [6]])
x = np.random.normal(0, 1, (2))
grad = grad(x, A, b)

# Algorithm implementation.
while (f((x - (t * grad)), A, b) > (f(x, A, b) - (alpha * t * np.linalg.norm(grad)**2))):
       t *= beta
       print('Iteration: {}'.format(count))
       error = f(x, A, b) - (f((x - (t * grad)), A, b) + (alpha * t) * np.linalg.norm(grad)**2)
       print('Error: {}'.format(error))
       count += 1

它运行正常,但是当我检查输出时,它看起来像这样:

Iteration: 1
Error: -518474.8580281086
Iteration: 2
Error: -329567.4778574929
Iteration: 3
Error: -209120.46903309406
Iteration: 4
Error: -132397.3549733152
Iteration: 5
Error: -83584.93924532575
Iteration: 6
Error: -52577.294995627766
Iteration: 7
Error: -32918.244128793245
Iteration: 8
Error: -20485.124736396905
Iteration: 9
Error: -12646.866855165446
Iteration: 10
Error: -7725.532635099086
Iteration: 11
Error: -4651.999393394028
Iteration: 12
Error: -2745.8346460127177
Iteration: 13
Error: -1574.6064295366227
Iteration: 14
Error: -863.9941484702779
Iteration: 15
Error: -440.3813105705005
Iteration: 16
Error: -194.2123119007902
Iteration: 17
Error: -56.618726821093276
Iteration: 18
Error: 15.477508374778608

据我所知,错误率应该在增加。我不明白为什么会这样,或者我对算法的理解是错误的,这实际上是正常的吗?

任何反馈表示赞赏。

0 个答案:

没有答案