我正在研究数值优化。最近我遇到了一种名为fmincg.
该函数是用 MATLAB 编写的,用于 Coursera 上著名的 Andrew Ng 的机器学习课程。根据版权声明,fmincg由Carl Edward Rasmussen撰写。
fmincg您可以在 MATLAB文件交换中找到全文。
我正在尝试找出算法。该结构非常简单,因为它紧跟我当前使用的参考文本(数值优化、Nocedal 和 Wright)。
但是,我很难弄清楚线路搜索过程的细节。具体来说,这是行搜索循环的代码:
while 1
while ((f2 > f1+z1*RHO*d1) | (d2 > -SIG*d1)) & (M > 0)
limit = z1; % tighten the bracket
if f2 > f1
z2 = z3 - (0.5*d3*z3*z3)/(d3*z3+f2-f3); % quadratic fit
else
A = 6*(f2-f3)/z3+3*(d2+d3); % cubic fit
B = 3*(f3-f2)-z3*(d3+2*d2);
z2 = (sqrt(B*B-A*d2*z3*z3)-B)/A; % numerical error possible - ok!
end
if isnan(z2) | isinf(z2)
z2 = z3/2; % if we had a numerical problem then bisect
end
z2 = max(min(z2, INT*z3),(1-INT)*z3); % don't accept too close to limits
z1 = z1 + z2; % update the step
X = X + z2*s;
[f2 df2] = eval(argstr);
M = M - 1; i = i + (length<0); % count epochs?!
d2 = df2'*s;
z3 = z3-z2; % z3 is now relative to the location of z2
end
if f2 > f1+z1*RHO*d1 | d2 > -SIG*d1
break; % this is a failure
elseif d2 > SIG*d1
success = 1; break; % success
elseif M == 0
break; % failure
end
A = 6*(f2-f3)/z3+3*(d2+d3); % make cubic extrapolation
B = 3*(f3-f2)-z3*(d3+2*d2);
z2 = -d2*z3*z3/(B+sqrt(B*B-A*d2*z3*z3)); % num. error possible - ok!
if ~isreal(z2) | isnan(z2) | isinf(z2) | z2 < 0 % num prob or wrong sign?
if limit < -0.5 % if we have no upper limit
z2 = z1 * (EXT-1); % the extrapolate the maximum amount
else
z2 = (limit-z1)/2; % otherwise bisect
end
elseif (limit > -0.5) & (z2+z1 > limit) % extraplation beyond max?
z2 = (limit-z1)/2; % bisect
elseif (limit < -0.5) & (z2+z1 > z1*EXT) % extrapolation beyond limit
z2 = z1*(EXT-1.0); % set to extrapolation limit
elseif z2 < -z3*INT
z2 = -z3*INT;
elseif (limit > -0.5) & (z2 < (limit-z1)*(1.0-INT)) % too close to limit?
z2 = (limit-z1)*(1.0-INT);
end
f3 = f2; d3 = d2; z3 = -z2; % set point 3 equal to point 2
z1 = z1 + z2; X = X + z2*s; % update current estimates
[f2 df2] = eval(argstr);
M = M - 1; i = i + (length<0); % count epochs?!
d2 = df2'*s;
end
我想了解以下内容:
- 在循环之前,点 3 被初始化为:
f3 = f1; d3 = d1; z3 = -z1;并且注释说Save point 0. 但是他为什么要设置z3 = -z1?
(如果有人想知道,不,它 - 显然 - 不是一个错误,因为 1. 算法有效,2. 我试过运行它z3 = z1但它坏了) - 作者如何使用沃尔夫条件及其常数?
插值/外推。他使用的方程式来自哪里?例如:
A = 6*(f2-f3)/z3+3*(d2+d3); B = 3*(f3-f2)-z3*(d3+2*d2); z2 = -d2*z3*z3/(B+sqrt(B*B-A*d2*z3*z3));limit涉及变量的 if-else 检查中发生了什么?请注意,在进入之前while 1,他设置limit = -1
编辑:
在项目符号列表中添加了一个问题