使用scipy.optimize最小化多元可微函数

8 投票
2 回答
3227 浏览
提问于 2025-04-18 03:55

我正在尝试用 scipy.optimize 来最小化一个函数:

在这里输入图片描述

这个函数的梯度是这样的:

在这里输入图片描述

(对那些感兴趣的人来说,这个是布拉德利-特里-卢斯模型的似然函数,用于成对比较。它和逻辑回归关系非常密切。)

很明显,给所有参数加一个常数不会改变函数的值。因此,我让 \theta_1 = 0。下面是我在 Python 中实现目标函数和梯度的代码(这里的 theta 变成了 x):

def objective(x):
    x = np.insert(x, 0, 0.0)
    tiles = np.tile(x, (len(x), 1))
    combs = tiles.T - tiles
    exps = np.dstack((zeros, combs))
    return np.sum(cijs * scipy.misc.logsumexp(exps, axis=2))

def gradient(x):
    zeros = np.zeros(cijs.shape)
    x = np.insert(x, 0, 0.0)
    tiles = np.tile(x, (len(x), 1))
    combs = tiles - tiles.T
    one = 1.0 / (np.exp(combs) + 1)
    two = 1.0 / (np.exp(combs.T) + 1)
    mat = (cijs * one) + (cijs.T * two)
    grad = np.sum(mat, axis=0)
    return grad[1:]  # Don't return the first element

这是 cijs 可能的样子:

[[ 0  5  1  4  6]
 [ 4  0  2  2  0]
 [ 6  4  0  9  3]
 [ 6  8  3  0  5]
 [10  7 11  4  0]]

这是我用来进行最小化的代码:

x0 = numpy.random.random(nb_items - 1)
# Let's try one algorithm...
xopt1 = scipy.optimize.fmin_bfgs(objective, x0, fprime=gradient, disp=True)
# And another one...
xopt2 = scipy.optimize.fmin_cg(objective, x0, fprime=gradient, disp=True)

但是,它总是在第一次迭代时失败:

Warning: Desired error not necessarily achieved due to precision loss.
         Current function value: 73.290610
         Iterations: 0
         Function evaluations: 38
         Gradient evaluations: 27

我搞不清楚为什么会失败。错误信息是因为这一行代码出现的:

https://github.com/scipy/scipy/blob/master/scipy/optimize/optimize.py#L853

所以这个“沃尔夫线搜索”似乎没有成功,但我不知道接下来该怎么做……任何帮助都很感激!

2 个回答

1

看起来你可以把这个问题转化为一个(非线性的)最小二乘问题。这样的话,你需要为每一个n个变量定义一个区间,并且为每个变量设置样本点的数量,以便构建系数矩阵。

在这个例子中,我对所有变量使用了相同数量的点和相同的区间:

from scipy.optimize import leastsq
from numpy import exp, linspace, zeros, ones

n = 4
npts = 1000
xs = [linspace(0, 1, npts) for _ in range(n)]

c = ones(n**2)

a = zeros((n*npts, n**2))
def residual(c):
    a.fill(0)
    for i in range(n):
        for j in range(n):
            for k in range(npts):
                a[i+k*n, i*n+j] = 1/(exp(xs[i][k] - xs[j][k]) + 1)
                a[i+k*n, j*n+i] = 1/(exp(xs[j][k] - xs[i][k]) + 1)

    return a.dot(c)

popt, pconv = leastsq(residual, x0=c)
print(popt.reshape(n, n))
#[[ -1.24886411   1.07854552  -2.67212118   1.86334625]
# [ -7.43330057   2.0935734   37.85989442   1.37005925]
# [ -3.51761322 -37.49627917  24.90538136  -4.23103535]
# [ 11.93000731   2.52750715 -14.84822686   1.38834225]]

编辑:关于上面构建的系数矩阵的更多细节:

在这里输入图片描述

4

正如@pv.在评论中指出的,我在计算梯度时犯了个错误。首先,我的目标函数的梯度的正确(数学)表达式是:

enter image description here

(注意那个负号。)此外,我在Python中的实现完全错误,除了符号错误之外。以下是我更新后的梯度:

def gradient(x):
    nb_comparisons = cijs + cijs.T
    x = np.insert(x, 0, 0.0)
    tiles = np.tile(x, (len(x), 1))
    combs = tiles - tiles.T
    probs = 1.0 / (np.exp(combs) + 1)
    mat = (nb_comparisons * probs) - cijs
    grad = np.sum(mat, axis=1)
    return grad[1:]  # Don't return the first element.

为了调试这个问题,我使用了:

  • scipy.optimize.check_grad:这个工具显示我的梯度函数的结果和一个近似的(有限差分)梯度相差很远。
  • scipy.optimize.approx_fprime来了解这些值应该是什么样子的。
  • 一些我自己挑选的简单例子,如果需要可以手动分析,还有一些Wolfram Alpha的查询来进行合理性检查。

撰写回答