annotate toolboxes/FullBNT-1.0.7/netlab3.3/olgd.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 [net, options, errlog, pointlog] = olgd(net, options, x, t)
Daniel@0 2 %OLGD On-line gradient descent optimization.
Daniel@0 3 %
Daniel@0 4 % Description
Daniel@0 5 % [NET, OPTIONS, ERRLOG, POINTLOG] = OLGD(NET, OPTIONS, X, T) uses on-
Daniel@0 6 % line gradient descent to find a local minimum of the error function
Daniel@0 7 % for the network NET computed on the input data X and target values T.
Daniel@0 8 % A log of the error values after each cycle is (optionally) returned
Daniel@0 9 % in ERRLOG, and a log of the points visited is (optionally) returned
Daniel@0 10 % in POINTLOG. Because the gradient is computed on-line (i.e. after
Daniel@0 11 % each pattern) this can be quite inefficient in Matlab.
Daniel@0 12 %
Daniel@0 13 % The error function value at final weight vector is returned in
Daniel@0 14 % OPTIONS(8).
Daniel@0 15 %
Daniel@0 16 % The optional parameters have the following interpretations.
Daniel@0 17 %
Daniel@0 18 % OPTIONS(1) is set to 1 to display error values; also logs error
Daniel@0 19 % values in the return argument ERRLOG, and the points visited in the
Daniel@0 20 % return argument POINTSLOG. If OPTIONS(1) is set to 0, then only
Daniel@0 21 % warning messages are displayed. If OPTIONS(1) is -1, then nothing is
Daniel@0 22 % displayed.
Daniel@0 23 %
Daniel@0 24 % OPTIONS(2) is the precision required for the value of X at the
Daniel@0 25 % solution. If the absolute difference between the values of X between
Daniel@0 26 % two successive steps is less than OPTIONS(2), then this condition is
Daniel@0 27 % satisfied.
Daniel@0 28 %
Daniel@0 29 % OPTIONS(3) is the precision required of the objective function at the
Daniel@0 30 % solution. If the absolute difference between the error functions
Daniel@0 31 % between two successive steps is less than OPTIONS(3), then this
Daniel@0 32 % condition is satisfied. Both this and the previous condition must be
Daniel@0 33 % satisfied for termination. Note that testing the function value at
Daniel@0 34 % each iteration roughly halves the speed of the algorithm.
Daniel@0 35 %
Daniel@0 36 % OPTIONS(5) determines whether the patterns are sampled randomly with
Daniel@0 37 % replacement. If it is 0 (the default), then patterns are sampled in
Daniel@0 38 % order.
Daniel@0 39 %
Daniel@0 40 % OPTIONS(6) determines if the learning rate decays. If it is 1 then
Daniel@0 41 % the learning rate decays at a rate of 1/T. If it is 0 (the default)
Daniel@0 42 % then the learning rate is constant.
Daniel@0 43 %
Daniel@0 44 % OPTIONS(9) should be set to 1 to check the user defined gradient
Daniel@0 45 % function.
Daniel@0 46 %
Daniel@0 47 % OPTIONS(10) returns the total number of function evaluations
Daniel@0 48 % (including those in any line searches).
Daniel@0 49 %
Daniel@0 50 % OPTIONS(11) returns the total number of gradient evaluations.
Daniel@0 51 %
Daniel@0 52 % OPTIONS(14) is the maximum number of iterations (passes through the
Daniel@0 53 % complete pattern set); default 100.
Daniel@0 54 %
Daniel@0 55 % OPTIONS(17) is the momentum; default 0.5.
Daniel@0 56 %
Daniel@0 57 % OPTIONS(18) is the learning rate; default 0.01.
Daniel@0 58 %
Daniel@0 59 % See also
Daniel@0 60 % GRADDESC
Daniel@0 61 %
Daniel@0 62
Daniel@0 63 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 64
Daniel@0 65 % Set up the options.
Daniel@0 66 if length(options) < 18
Daniel@0 67 error('Options vector too short')
Daniel@0 68 end
Daniel@0 69
Daniel@0 70 if (options(14))
Daniel@0 71 niters = options(14);
Daniel@0 72 else
Daniel@0 73 niters = 100;
Daniel@0 74 end
Daniel@0 75
Daniel@0 76 % Learning rate: must be positive
Daniel@0 77 if (options(18) > 0)
Daniel@0 78 eta = options(18);
Daniel@0 79 else
Daniel@0 80 eta = 0.01;
Daniel@0 81 end
Daniel@0 82 % Save initial learning rate for annealing
Daniel@0 83 lr = eta;
Daniel@0 84 % Momentum term: allow zero momentum
Daniel@0 85 if (options(17) >= 0)
Daniel@0 86 mu = options(17);
Daniel@0 87 else
Daniel@0 88 mu = 0.5;
Daniel@0 89 end
Daniel@0 90
Daniel@0 91 pakstr = [net.type, 'pak'];
Daniel@0 92 unpakstr = [net.type, 'unpak'];
Daniel@0 93
Daniel@0 94 % Extract initial weights from the network
Daniel@0 95 w = feval(pakstr, net);
Daniel@0 96
Daniel@0 97 display = options(1);
Daniel@0 98
Daniel@0 99 % Work out if we need to compute f at each iteration.
Daniel@0 100 % Needed if display results or if termination
Daniel@0 101 % criterion requires it.
Daniel@0 102 fcneval = (display | options(3));
Daniel@0 103
Daniel@0 104 % Check gradients
Daniel@0 105 if (options(9))
Daniel@0 106 feval('gradchek', w, 'neterr', 'netgrad', net, x, t);
Daniel@0 107 end
Daniel@0 108
Daniel@0 109 dwold = zeros(1, length(w));
Daniel@0 110 fold = 0; % Must be initialised so that termination test can be performed
Daniel@0 111 ndata = size(x, 1);
Daniel@0 112
Daniel@0 113 if fcneval
Daniel@0 114 fnew = neterr(w, net, x, t);
Daniel@0 115 options(10) = options(10) + 1;
Daniel@0 116 fold = fnew;
Daniel@0 117 end
Daniel@0 118
Daniel@0 119 j = 1;
Daniel@0 120 if nargout >= 3
Daniel@0 121 errlog(j, :) = fnew;
Daniel@0 122 if nargout == 4
Daniel@0 123 pointlog(j, :) = w;
Daniel@0 124 end
Daniel@0 125 end
Daniel@0 126
Daniel@0 127 % Main optimization loop.
Daniel@0 128 while j <= niters
Daniel@0 129 wold = w;
Daniel@0 130 if options(5)
Daniel@0 131 % Randomise order of pattern presentation: with replacement
Daniel@0 132 pnum = ceil(rand(ndata, 1).*ndata);
Daniel@0 133 else
Daniel@0 134 pnum = 1:ndata;
Daniel@0 135 end
Daniel@0 136 for k = 1:ndata
Daniel@0 137 grad = netgrad(w, net, x(pnum(k),:), t(pnum(k),:));
Daniel@0 138 if options(6)
Daniel@0 139 % Let learning rate decrease as 1/t
Daniel@0 140 lr = eta/((j-1)*ndata + k);
Daniel@0 141 end
Daniel@0 142 dw = mu*dwold - lr*grad;
Daniel@0 143 w = w + dw;
Daniel@0 144 dwold = dw;
Daniel@0 145 end
Daniel@0 146 options(11) = options(11) + 1; % Increment gradient evaluation count
Daniel@0 147 if fcneval
Daniel@0 148 fold = fnew;
Daniel@0 149 fnew = neterr(w, net, x, t);
Daniel@0 150 options(10) = options(10) + 1;
Daniel@0 151 end
Daniel@0 152 if display
Daniel@0 153 fprintf(1, 'Iteration %5d Error %11.8f\n', j, fnew);
Daniel@0 154 end
Daniel@0 155 j = j + 1;
Daniel@0 156 if nargout >= 3
Daniel@0 157 errlog(j) = fnew;
Daniel@0 158 if nargout == 4
Daniel@0 159 pointlog(j, :) = w;
Daniel@0 160 end
Daniel@0 161 end
Daniel@0 162 if (max(abs(w - wold)) < options(2) & abs(fnew - fold) < options(3))
Daniel@0 163 % Termination criteria are met
Daniel@0 164 options(8) = fnew;
Daniel@0 165 net = feval(unpakstr, net, w);
Daniel@0 166 return;
Daniel@0 167 end
Daniel@0 168 end
Daniel@0 169
Daniel@0 170 if fcneval
Daniel@0 171 options(8) = fnew;
Daniel@0 172 else
Daniel@0 173 % Return error on entire dataset
Daniel@0 174 options(8) = neterr(w, net, x, t);
Daniel@0 175 options(10) = options(10) + 1;
Daniel@0 176 end
Daniel@0 177 if (options(1) >= 0)
Daniel@0 178 disp(maxitmess);
Daniel@0 179 end
Daniel@0 180
Daniel@0 181 net = feval(unpakstr, net, w);