问题描述
修改梯度下降法的实现,使得图还包括下面的线搜索方法检查的更多点,并且检查的线应该在每次迭代时可视化。 我的代码:
function [ x ] = grad_descent(f,df,~,x0,iter,a,b,c,d,res) % f: vector->scalar objective function
% df: gradient function
% x0: starting point
% iter: number of iterations
X = linspace(a,res); Y = linspace(c,res); Z = zeros(res,res); for i=1:res for j=1:res Z(i,j)=f([X(i);Y(j)]); end end
rho=0.5; %For the Armijo LS usually 0.5 or 0.9
c=0.2; %For the Armijo LS usually 0.01 or 0.2
alpha=1; %For the Armijo LS
x=x0;
v = zeros(iter,2);
for k=1:iter
p=-df(x); %Direction of (steepest) descent
p=p/norm(p); %normalized
gamma =alpha;
gamma=Armijoh_LS(f,p,x,gamma,rho,c); %Compute step length
x=x+gamma*p;
v(k,1) = x(1);
v(k,2) = x(2);
contourf(X,Y,Z',200); hold on
scatter(v(:,1),v(:,2),'r','filled');
hold off;
pause;
end
end
function [ alpha ] = Armijoh_LS(f,alpha,c)
% f: vector->scalar function -- the objective
% df: gradient function
% p: search direction; must satisfy that A) it is a descent direction at starting point x and B) norm(p)=1
% x: starting point
% alpha: initial (maximal) step length
% rho: step lenght multiplier
% c: descent condition multiplier
% This method performs a single linesearch on f in the descent direction p and returns the chosen step length;
% terminates when the Armijo condition is first satisfied w.r.t. input parameters alpha,rho and c.
f0 = f(x);
g0 = df(x);
x0 = x;
x = x0 + alpha * p;
fk = f(x);
% repeat until the Armijo condition is satisfied
while fk > f0 + c * alpha * (g0'*p)
alpha = rho * alpha;
x = x0 + alpha * p;
fk = f(x);
end
end
f=@(x) x(1)^4+x(2)^4-4x(1)^2; df=@(x) [4x(1)^3-8x(1);4x(2)^3];
ddf=@(x) [12x(1)^2-8 0;0 12x(2)^2];
grad_descent(f,[1.134556432;3.456789654],50,-4,+4,300)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)