annotate fmincg.m @ 9:8dd249e99b5b default tip

Optimisations for backprop
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Fri, 11 Nov 2011 20:36:02 -0500
parents 395fc40248c3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
1 function [X, fX, i] = fmincg(f, X, options, P1, P2, P3, P4, P5)
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
2 % Minimize a continuous differentialble multivariate function. Starting point
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
3 % is given by "X" (D by 1), and the function named in the string "f", must
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
4 % return a function value and a vector of partial derivatives. The Polack-
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
5 % Ribiere flavour of conjugate gradients is used to compute search directions,
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
6 % and a line search using quadratic and cubic polynomial approximations and the
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
7 % Wolfe-Powell stopping criteria is used together with the slope ratio method
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
8 % for guessing initial step sizes. Additionally a bunch of checks are made to
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
9 % make sure that exploration is taking place and that extrapolation will not
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
10 % be unboundedly large. The "length" gives the length of the run: if it is
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
11 % positive, it gives the maximum number of line searches, if negative its
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
12 % absolute gives the maximum allowed number of function evaluations. You can
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
13 % (optionally) give "length" a second component, which will indicate the
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
14 % reduction in function value to be expected in the first line-search (defaults
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
15 % to 1.0). The function returns when either its length is up, or if no further
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
16 % progress can be made (ie, we are at a minimum, or so close that due to
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
17 % numerical problems, we cannot get any closer). If the function terminates
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
18 % within a few iterations, it could be an indication that the function value
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
19 % and derivatives are not consistent (ie, there may be a bug in the
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
20 % implementation of your "f" function). The function returns the found
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
21 % solution "X", a vector of function values "fX" indicating the progress made
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
22 % and "i" the number of iterations (line searches or function evaluations,
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
23 % depending on the sign of "length") used.
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
24 %
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
25 % Usage: [X, fX, i] = fmincg(f, X, options, P1, P2, P3, P4, P5)
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
26 %
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
27 % See also: checkgrad
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
28 %
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
29 % Copyright (C) 2001 and 2002 by Carl Edward Rasmussen. Date 2002-02-13
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
30 %
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
31 %
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
32 % (C) Copyright 1999, 2000 & 2001, Carl Edward Rasmussen
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
33 %
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
34 % Permission is granted for anyone to copy, use, or modify these
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
35 % programs and accompanying documents for purposes of research or
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
36 % education, provided this copyright notice is retained, and note is
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
37 % made of any changes that have been made.
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
38 %
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
39 % These programs and documents are distributed without any warranty,
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
40 % express or implied. As the programs were written for research
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
41 % purposes only, they have not been tested to the degree that would be
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
42 % advisable in any important application. All use of these programs is
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
43 % entirely at the user's own risk.
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
44 %
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
45 % [ml-class] Changes Made:
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
46 % 1) Function name and argument specifications
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
47 % 2) Output display
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
48 %
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
49
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
50 % Read options
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
51 if exist('options', 'var') && ~isempty(options) && isfield(options, 'MaxIter')
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
52 length = options.MaxIter;
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
53 else
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
54 length = 100;
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
55 end
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
56
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
57
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
58 RHO = 0.01; % a bunch of constants for line searches
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
59 SIG = 0.5; % RHO and SIG are the constants in the Wolfe-Powell conditions
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
60 INT = 0.1; % don't reevaluate within 0.1 of the limit of the current bracket
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
61 EXT = 3.0; % extrapolate maximum 3 times the current bracket
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
62 MAX = 20; % max 20 function evaluations per line search
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
63 RATIO = 100; % maximum allowed slope ratio
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
64
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
65 argstr = ['feval(f, X']; % compose string used to call function
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
66 for i = 1:(nargin - 3)
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
67 argstr = [argstr, ',P', int2str(i)];
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
68 end
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
69 argstr = [argstr, ')'];
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
70
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
71 if max(size(length)) == 2, red=length(2); length=length(1); else red=1; end
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
72 S=['Iteration '];
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
73
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
74 i = 0; % zero the run length counter
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
75 ls_failed = 0; % no previous line search has failed
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
76 fX = [];
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
77 [f1 df1] = eval(argstr); % get function value and gradient
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
78 i = i + (length<0); % count epochs?!
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
79 s = -df1; % search direction is steepest
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
80 d1 = -s'*s; % this is the slope
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
81 z1 = red/(1-d1); % initial step is red/(|s|+1)
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
82
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
83 while i < abs(length) % while not finished
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
84 i = i + (length>0); % count iterations?!
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
85
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
86 X0 = X; f0 = f1; df0 = df1; % make a copy of current values
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
87 X = X + z1*s; % begin line search
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
88 [f2 df2] = eval(argstr);
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
89 i = i + (length<0); % count epochs?!
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
90 d2 = df2'*s;
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
91 f3 = f1; d3 = d1; z3 = -z1; % initialize point 3 equal to point 1
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
92 if length>0, M = MAX; else M = min(MAX, -length-i); end
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
93 success = 0; limit = -1; % initialize quanteties
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
94 while 1
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
95 while ((f2 > f1+z1*RHO*d1) | (d2 > -SIG*d1)) & (M > 0)
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
96 limit = z1; % tighten the bracket
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
97 if f2 > f1
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
98 z2 = z3 - (0.5*d3*z3*z3)/(d3*z3+f2-f3); % quadratic fit
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
99 else
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
100 A = 6*(f2-f3)/z3+3*(d2+d3); % cubic fit
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
101 B = 3*(f3-f2)-z3*(d3+2*d2);
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
102 z2 = (sqrt(B*B-A*d2*z3*z3)-B)/A; % numerical error possible - ok!
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
103 end
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
104 if isnan(z2) | isinf(z2)
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
105 z2 = z3/2; % if we had a numerical problem then bisect
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
106 end
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
107 z2 = max(min(z2, INT*z3),(1-INT)*z3); % don't accept too close to limits
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
108 z1 = z1 + z2; % update the step
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
109 X = X + z2*s;
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
110 [f2 df2] = eval(argstr);
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
111 M = M - 1; i = i + (length<0); % count epochs?!
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
112 d2 = df2'*s;
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
113 z3 = z3-z2; % z3 is now relative to the location of z2
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
114 end
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
115 if f2 > f1+z1*RHO*d1 | d2 > -SIG*d1
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
116 break; % this is a failure
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
117 elseif d2 > SIG*d1
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
118 success = 1; break; % success
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
119 elseif M == 0
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
120 break; % failure
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
121 end
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
122 A = 6*(f2-f3)/z3+3*(d2+d3); % make cubic extrapolation
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
123 B = 3*(f3-f2)-z3*(d3+2*d2);
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
124 z2 = -d2*z3*z3/(B+sqrt(B*B-A*d2*z3*z3)); % num. error possible - ok!
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
125 if ~isreal(z2) | isnan(z2) | isinf(z2) | z2 < 0 % num prob or wrong sign?
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
126 if limit < -0.5 % if we have no upper limit
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
127 z2 = z1 * (EXT-1); % the extrapolate the maximum amount
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
128 else
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
129 z2 = (limit-z1)/2; % otherwise bisect
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
130 end
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
131 elseif (limit > -0.5) & (z2+z1 > limit) % extraplation beyond max?
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
132 z2 = (limit-z1)/2; % bisect
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
133 elseif (limit < -0.5) & (z2+z1 > z1*EXT) % extrapolation beyond limit
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
134 z2 = z1*(EXT-1.0); % set to extrapolation limit
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
135 elseif z2 < -z3*INT
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
136 z2 = -z3*INT;
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
137 elseif (limit > -0.5) & (z2 < (limit-z1)*(1.0-INT)) % too close to limit?
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
138 z2 = (limit-z1)*(1.0-INT);
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
139 end
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
140 f3 = f2; d3 = d2; z3 = -z2; % set point 3 equal to point 2
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
141 z1 = z1 + z2; X = X + z2*s; % update current estimates
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
142 [f2 df2] = eval(argstr);
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
143 M = M - 1; i = i + (length<0); % count epochs?!
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
144 d2 = df2'*s;
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
145 end % end of line search
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
146
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
147 if success % if line search succeeded
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
148 f1 = f2; fX = [fX' f1]';
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
149 fprintf('%s %4i | Cost: %4.6e\r', S, i, f1);
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
150 s = (df2'*df2-df1'*df2)/(df1'*df1)*s - df2; % Polack-Ribiere direction
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
151 tmp = df1; df1 = df2; df2 = tmp; % swap derivatives
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
152 d2 = df1'*s;
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
153 if d2 > 0 % new slope must be negative
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
154 s = -df1; % otherwise use steepest direction
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
155 d2 = -s'*s;
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
156 end
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
157 z1 = z1 * min(RATIO, d1/(d2-realmin)); % slope ratio but max RATIO
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
158 d1 = d2;
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
159 ls_failed = 0; % this line search did not fail
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
160 else
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
161 X = X0; f1 = f0; df1 = df0; % restore point from before failed line search
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
162 if ls_failed | i > abs(length) % line search failed twice in a row
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
163 break; % or we ran out of time, so we give up
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
164 end
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
165 tmp = df1; df1 = df2; df2 = tmp; % swap derivatives
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
166 s = -df1; % try steepest
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
167 d1 = -s'*s;
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
168 z1 = 1/(1-d1);
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
169 ls_failed = 1; % this line search failed
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
170 end
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
171 if exist('OCTAVE_VERSION')
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
172 fflush(stdout);
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
173 end
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
174 end
395fc40248c3 Initial commit
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
diff changeset
175 fprintf('\n');