首页 > 解决方案 > 如何在梯度下降法中获得正确的步长?

问题描述

我必须在 Matlab 中实现梯度下降法以获得最小点,但我无法猜测最佳步长并且我的代码不起作用。你能解释一下这个函数的步长吗?函数为:f(x,y) = - ((x* y* ((1-2 x y)/ (2 y+2 x)))^2); 起点:[1;1]

function [xopt,fopt,niter,gnorm,dx] = grad_descent
if nargin==0
    % define starting point
    x0 = [1 1]';
 else
     error('Incorrect number of input arguments.')
end

%tolerance
tol = 1e-6;

% maximum number of allowed iterations
maxiter = 100 ;

dxmin = 1e-6;

% step size
alpha = 0.33;

gnorm = inf; x = x0; niter = 0; dx = inf;

% define the objective function:
f = @(x1, x2) - ((x1* x2* ((1-2*x1*x2)/ (2*x2+2*x1)))^2);
% plot objective function for visualization:
figure(1); clf; ezcontour(f,[-5 5 -5 5]); axis equal; hold on

% redefine objective function syntax for use with optimization:
f2 = @(x) f(x(1),x(2));
% gradient descent algorithm:
while and(gnorm>=tol, and(niter <= maxiter, dx >= dxmin))
    % calculate gradient:
    g = grad(x);
    gnorm = norm(g);
    % take step:
    xnew = x - alpha*g;   
    % plot current point
    plot([x(1) xnew(1)],[x(2) xnew(2)],'ko-')
    refresh
    % update termination metrics
    niter = niter + 1;
    dx = norm(xnew-x);
    x = xnew;   
end
xopt = x;
sprintf("The point of minimum (x; y) : %f %f \n  Function value in this point: %f", xopt, f2(xopt))
niter = niter - 1;

% define the gradient of the objective (derrivative function)
function g = grad(x)

g = [-( (x(1)*x(2)^3*(4*x(1)^3*x(2) + 8*x(1)^2*x(2)^2 -2*x(1)^2-6*x(1)*x(2) +1)   )/ (2*(x(1)+x(2))^3)   )
    -( (x(1)^3*x(2)*(4*x(1)*x(2)^3 + 8*x(1)^2*x(2)^2 -2*x(2)^2-6*x(1)*x(2) +1)   )/ (2*(x(1)+x(2))^3)   )];

标签: matlaboptimizationgradient-descent

解决方案


推荐阅读