gradient descent algorithm - index out of bounds

1 回表示 (過去 30 日間)
HIRAKJYOTI BASUMATARY
HIRAKJYOTI BASUMATARY 2017 年 10 月 3 日
回答済み: Walter Roberson 2017 年 10 月 3 日
I have written a simple code for the gradient descent algorithm but i am getting an error while accessing grd(2) . It says " Attempted to access grd(2); index out of bounds because numel(grd)=1." . I am not able to find out the bug. If anyone expert in it ,. can you please tell me my where i am doing wrong.
function [xopt,fopt,niter,gnorm,dx] = mystochastic(varargin)
if nargin==0
% define starting point
x0 = [5 5]';
elseif nargin==1
% if a single input argument is provided, it is a user-defined starting
% point.
x0 = varargin{1};
else
error('Incorrect number of input arguments.')
end
% termination tolerance
tol = 1e-6;
% maximum number of allowed iterations
maxiter = 1000;
% minimum allowed perturbation
dxmin = 1e-6;
% step size ( 0.33 causes instability, 0.2 quite accurate)
alpha = 0.1;
% initialize gradient norm, optimization vector, iteration counter, perturbation
gnorm = inf; x = x0; niter = 0; dx = inf;
% define the objective function:
f = @(x1,x2) 1.7*exp(-(((x1-3).^2)/10 + ((x2-3).^2)/10)) + exp(-(((x1+5).^2)/8 + ((x2+5).^2)/8))+2*exp(-((x1.^2)/4 + ((x2.^2)/5)))+ 1.5*exp(-(((x1-4).^2)/18 + ((x2+4).^2)/16)) + 1.2 *exp(-(((x1+4).^2)/18 + ((x2-4).^2)/16));
% plot objective function contours for visualization:
figure(1); clf; ezcontour(f,[-10 10 -10 10]); axis equal; hold on
% gradient descent algorithm:
while and(gnorm>=tol, and(niter <= maxiter, dx >= dxmin))
% calculate gradient:
r = randi([1 2]);
g = grad(x,r);
gnorm = norm(g);
% take step:
xnew = x - alpha*g;
% check step
if ~isfinite(xnew)
display(['Number of iterations: ' num2str(niter)])
error('x is inf or NaN')
end
% 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;
fopt = f2(xopt);
niter = niter - 1;
function g = grad(x,r)
grd = (- x(1).*exp(- x(1).^2/4 - x(2).^2/5) - (4*x(2).*exp(- x(1).^2/4 - x(2).^2/5))/5 - (17*exp(- (x(1) - 3).^2/10 - (x(2) - 3).^2/10)*(x(1)/5 - 3/5))/10 - exp(- (x(1) + 5).^2/8 - (x(2) + 5).^2/8)*(x(1)/4 + 5/4) - (3*exp(- (x(1) - 4).^2/18 - (x(2) + 4).^2/16)*(x(1)/9 - 4/9))/2 - (6*exp(- (x(1) + 4).^2/18 - (x(2) - 4).^2/16)*(x(1)/9 + 4/9))/5 - (17*exp(- (x(1) - 3).^2/10 - (x(2) - 3).^2/10)*(x(2)/5 - 3/5))/10 - exp(- (x(1) + 5).^2/8 - (x(2) + 5).^2/8)*(x(2)/4 + 5/4) - (3*exp(- (x(1) - 4).^2/18 - (x(2) + 4).^2/16)*(x(2)/8 + 1/2))/2 - (6*exp(- (x(1) + 4).^2/18 - (x(2) - 4).^2/16)*(x(2)/8 - 1/2))/5);
g = zeros(size(x,1),1);
g(r) = grd(r);

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 10 月 3 日
grd = (- x(1).*exp(- x(1).^2/4 - x(2).^2/5) - (4*x(2).*exp(- x(1).^2/4 - x(2).^2/5))/5 - (17*exp(- (x(1) - 3).^2/10 - (x(2) - 3).^2/10)*(x(1)/5 - 3/5))/10 - exp(- (x(1) + 5).^2/8 - (x(2) + 5).^2/8)*(x(1)/4 + 5/4) - (3*exp(- (x(1) - 4).^2/18 - (x(2) + 4).^2/16)*(x(1)/9 - 4/9))/2 - (6*exp(- (x(1) + 4).^2/18 - (x(2) - 4).^2/16)*(x(1)/9 + 4/9))/5 - (17*exp(- (x(1) - 3).^2/10 - (x(2) - 3).^2/10)*(x(2)/5 - 3/5))/10 - exp(- (x(1) + 5).^2/8 - (x(2) + 5).^2/8)*(x(2)/4 + 5/4) - (3*exp(- (x(1) - 4).^2/18 - (x(2) + 4).^2/16)*(x(2)/8 + 1/2))/2 - (6*exp(- (x(1) + 4).^2/18 - (x(2) - 4).^2/16)*(x(2)/8 - 1/2))/5);
never uses [] or cat(), and only refers to x with a scalar subscript, and never uses any other variable. Therefore result is always going to be a scalar, and so the result cannot be indexed at location 2.

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by