comparison toolboxes/FullBNT-1.0.7/netlab3.3/graddesc.m @ 0:e9a9cd732c1e tip

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