annotate toolboxes/FullBNT-1.0.7/netlab3.3/graddesc.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [x, options, flog, pointlog] = graddesc(f, x, options, gradf, ...
Daniel@0 2 varargin)
Daniel@0 3 %GRADDESC Gradient descent optimization.
Daniel@0 4 %
Daniel@0 5 % Description
Daniel@0 6 % [X, OPTIONS, FLOG, POINTLOG] = GRADDESC(F, X, OPTIONS, GRADF) uses
Daniel@0 7 % batch gradient descent to find a local minimum of the function F(X)
Daniel@0 8 % whose gradient is given by GRADF(X). A log of the function values
Daniel@0 9 % after each cycle is (optionally) returned in ERRLOG, and a log of the
Daniel@0 10 % points visited is (optionally) returned in POINTLOG.
Daniel@0 11 %
Daniel@0 12 % Note that X is a row vector and F returns a scalar value. The point
Daniel@0 13 % at which F has a local minimum is returned as X. The function value
Daniel@0 14 % at that point is returned in OPTIONS(8).
Daniel@0 15 %
Daniel@0 16 % GRADDESC(F, X, OPTIONS, GRADF, P1, P2, ...) allows additional
Daniel@0 17 % arguments to be passed to F() and GRADF().
Daniel@0 18 %
Daniel@0 19 % The optional parameters have the following interpretations.
Daniel@0 20 %
Daniel@0 21 % OPTIONS(1) is set to 1 to display error values; also logs error
Daniel@0 22 % values in the return argument ERRLOG, and the points visited in the
Daniel@0 23 % return argument POINTSLOG. If OPTIONS(1) is set to 0, then only
Daniel@0 24 % warning messages are displayed. If OPTIONS(1) is -1, then nothing is
Daniel@0 25 % displayed.
Daniel@0 26 %
Daniel@0 27 % OPTIONS(2) is the absolute precision required for the value of X at
Daniel@0 28 % the solution. If the absolute difference between the values of X
Daniel@0 29 % between two successive steps is less than OPTIONS(2), then this
Daniel@0 30 % condition is satisfied.
Daniel@0 31 %
Daniel@0 32 % OPTIONS(3) is a measure of the precision required of the objective
Daniel@0 33 % function at the solution. If the absolute difference between the
Daniel@0 34 % objective function values between two successive steps is less than
Daniel@0 35 % OPTIONS(3), then this condition is satisfied. Both this and the
Daniel@0 36 % previous condition must be satisfied for termination.
Daniel@0 37 %
Daniel@0 38 % OPTIONS(7) determines the line minimisation method used. If it is
Daniel@0 39 % set to 1 then a line minimiser is used (in the direction of the
Daniel@0 40 % negative gradient). If it is 0 (the default), then each parameter
Daniel@0 41 % update is a fixed multiple (the learning rate) of the negative
Daniel@0 42 % gradient added to a fixed multiple (the momentum) of the previous
Daniel@0 43 % parameter update.
Daniel@0 44 %
Daniel@0 45 % OPTIONS(9) should be set to 1 to check the user defined gradient
Daniel@0 46 % function GRADF with GRADCHEK. This is carried out at the initial
Daniel@0 47 % parameter vector X.
Daniel@0 48 %
Daniel@0 49 % OPTIONS(10) returns the total number of function evaluations
Daniel@0 50 % (including those in any line searches).
Daniel@0 51 %
Daniel@0 52 % OPTIONS(11) returns the total number of gradient evaluations.
Daniel@0 53 %
Daniel@0 54 % OPTIONS(14) is the maximum number of iterations; default 100.
Daniel@0 55 %
Daniel@0 56 % OPTIONS(15) is the precision in parameter space of the line search;
Daniel@0 57 % default FOPTIONS(2).
Daniel@0 58 %
Daniel@0 59 % OPTIONS(17) is the momentum; default 0.5. It should be scaled by the
Daniel@0 60 % inverse of the number of data points.
Daniel@0 61 %
Daniel@0 62 % OPTIONS(18) is the learning rate; default 0.01. It should be scaled
Daniel@0 63 % by the inverse of the number of data points.
Daniel@0 64 %
Daniel@0 65 % See also
Daniel@0 66 % CONJGRAD, LINEMIN, OLGD, MINBRACK, QUASINEW, SCG
Daniel@0 67 %
Daniel@0 68
Daniel@0 69 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 70
Daniel@0 71 % Set up the options.
Daniel@0 72 if length(options) < 18
Daniel@0 73 error('Options vector too short')
Daniel@0 74 end
Daniel@0 75
Daniel@0 76 if (options(14))
Daniel@0 77 niters = options(14);
Daniel@0 78 else
Daniel@0 79 niters = 100;
Daniel@0 80 end
Daniel@0 81
Daniel@0 82 line_min_flag = 0; % Flag for line minimisation option
Daniel@0 83 if (round(options(7)) == 1)
Daniel@0 84 % Use line minimisation
Daniel@0 85 line_min_flag = 1;
Daniel@0 86 % Set options for line minimiser
Daniel@0 87 line_options = foptions;
Daniel@0 88 if options(15) > 0
Daniel@0 89 line_options(2) = options(15);
Daniel@0 90 end
Daniel@0 91 else
Daniel@0 92 % Learning rate: must be positive
Daniel@0 93 if (options(18) > 0)
Daniel@0 94 eta = options(18);
Daniel@0 95 else
Daniel@0 96 eta = 0.01;
Daniel@0 97 end
Daniel@0 98 % Momentum term: allow zero momentum
Daniel@0 99 if (options(17) >= 0)
Daniel@0 100 mu = options(17);
Daniel@0 101 else
Daniel@0 102 mu = 0.5;
Daniel@0 103 end
Daniel@0 104 end
Daniel@0 105
Daniel@0 106 % Check function string
Daniel@0 107 f = fcnchk(f, length(varargin));
Daniel@0 108 gradf = fcnchk(gradf, length(varargin));
Daniel@0 109
Daniel@0 110 % Display information if options(1) > 0
Daniel@0 111 display = options(1) > 0;
Daniel@0 112
Daniel@0 113 % Work out if we need to compute f at each iteration.
Daniel@0 114 % Needed if using line search or if display results or if termination
Daniel@0 115 % criterion requires it.
Daniel@0 116 fcneval = (options(7) | display | options(3));
Daniel@0 117
Daniel@0 118 % Check gradients
Daniel@0 119 if (options(9) > 0)
Daniel@0 120 feval('gradchek', x, f, gradf, varargin{:});
Daniel@0 121 end
Daniel@0 122
Daniel@0 123 dxold = zeros(1, size(x, 2));
Daniel@0 124 xold = x;
Daniel@0 125 fold = 0; % Must be initialised so that termination test can be performed
Daniel@0 126 if fcneval
Daniel@0 127 fnew = feval(f, x, varargin{:});
Daniel@0 128 options(10) = options(10) + 1;
Daniel@0 129 fold = fnew;
Daniel@0 130 end
Daniel@0 131
Daniel@0 132 % Main optimization loop.
Daniel@0 133 for j = 1:niters
Daniel@0 134 xold = x;
Daniel@0 135 grad = feval(gradf, x, varargin{:});
Daniel@0 136 options(11) = options(11) + 1; % Increment gradient evaluation counter
Daniel@0 137 if (line_min_flag ~= 1)
Daniel@0 138 dx = mu*dxold - eta*grad;
Daniel@0 139 x = x + dx;
Daniel@0 140 dxold = dx;
Daniel@0 141 if fcneval
Daniel@0 142 fold = fnew;
Daniel@0 143 fnew = feval(f, x, varargin{:});
Daniel@0 144 options(10) = options(10) + 1;
Daniel@0 145 end
Daniel@0 146 else
Daniel@0 147 sd = - grad./norm(grad); % New search direction.
Daniel@0 148 fold = fnew;
Daniel@0 149 % Do a line search: normalise search direction to have length 1
Daniel@0 150 [lmin, line_options] = feval('linemin', f, x, sd, fold, ...
Daniel@0 151 line_options, varargin{:});
Daniel@0 152 options(10) = options(10) + line_options(10);
Daniel@0 153 x = xold + lmin*sd;
Daniel@0 154 fnew = line_options(8);
Daniel@0 155 end
Daniel@0 156 if nargout >= 3
Daniel@0 157 flog(j) = fnew;
Daniel@0 158 if nargout >= 4
Daniel@0 159 pointlog(j, :) = x;
Daniel@0 160 end
Daniel@0 161 end
Daniel@0 162 if display
Daniel@0 163 fprintf(1, 'Cycle %5d Function %11.8f\n', j, fnew);
Daniel@0 164 end
Daniel@0 165 if (max(abs(x - xold)) < options(2) & abs(fnew - fold) < options(3))
Daniel@0 166 % Termination criteria are met
Daniel@0 167 options(8) = fnew;
Daniel@0 168 return;
Daniel@0 169 end
Daniel@0 170 end
Daniel@0 171
Daniel@0 172 if fcneval
Daniel@0 173 options(8) = fnew;
Daniel@0 174 else
Daniel@0 175 options(8) = feval(f, x, varargin{:});
Daniel@0 176 options(10) = options(10) + 1;
Daniel@0 177 end
Daniel@0 178 if (options(1) >= 0)
Daniel@0 179 disp(maxitmess);
Daniel@0 180 end