/usr/share/octave/packages/nnet-0.1.13/__trainlm.m is in octave-nnet 0.1.13-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | ## Copyright (C) 2006 Michel D. Schmid <michaelschmid@users.sourceforge.net>
##
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; see the file COPYING. If not, see
## <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} {}[@var{netOut}] = __trainlm (@var{net},@var{mInputN},@var{mOutput},@var{[]},@var{[]},@var{VV})
## A neural feed-forward network will be trained with @code{__trainlm}
##
## @example
## [netOut,tr,out,E] = __trainlm(net,mInputN,mOutput,[],[],VV);
## @end example
## @noindent
##
## left side arguments:
## @example
## netOut: the trained network of the net structure @code{MLPnet}
## tr :
## out:
## E : Error
## @end example
## @noindent
##
## right side arguments:
## @example
## net : the untrained network, created with @code{newff}
## mInputN: normalized input matrix
## mOutput: output matrix
## [] : unused parameter
## [] : unused parameter
## VV : validize structure
## out:
## E : Error
## @end example
## @noindent
##
##
## @noindent
## are equivalent.
## @end deftypefn
## @seealso{newff,prestd,trastd}
## Author: Michel D. Schmid
## Comments: see in "A neural network toolbox for Octave User's Guide" [4]
## for variable naming... there have inputs or targets only one letter,
## e.g. for inputs is P written. To write a program, this is stupid, you can't
## search for 1 letter variable... that's why it is written here like Pp, or Tt
## instead only P or T.
function [net] = __trainlm(net,Im,Pp,Tt,VV)
## check range of input arguments
error(nargchk(5,5,nargin))
## Initialize
##------------
## get parameters for training
epochs = net.trainParam.epochs;
goal = net.trainParam.goal;
maxFail = net.trainParam.max_fail;
minGrad = net.trainParam.min_grad;
mu = net.trainParam.mu;
muInc = net.trainParam.mu_inc;
muDec = net.trainParam.mu_dec;
muMax = net.trainParam.mu_max;
show = net.trainParam.show;
time = net.trainParam.time;
## parameter checking
checkParameter(epochs,goal,maxFail,minGrad,mu,\
muInc,muDec,muMax,show,time);
## Constants
shortStr = "TRAINLM"; # TODO: shortStr is longer as TRAINLM !!!!!!!!!!!
doValidation = !isempty(VV);
stop = "";
#startTime = clock(); # TODO: maybe this row can be placed
# some rows later
## the weights are used in column vector format
xx = __getx(net); # x is the variable with respect to, but no
# variables with only one letter!!
## define identity matrix
muI = eye(length(xx));
startTime = clock(); # if the next some tests are OK, I can delete
# startTime = clock(); 9 rows above..
## calc performance of the actual net
[perf,vE,Aa,Nn] = __calcperf(net,xx,Im,Tt);
if (doValidation)
## calc performance if validation is used
VV.net = net; # save the actual net in the validate
# structure... if no train loop will show better validate
# performance, this will be the returned net
vperf = __calcperf(net,xx,VV.Im,VV.Tt);
VV.perf = vperf;
VV.numFail = 0; # one of the stop criterias
endif
nLayers = net.numLayers;
for iEpochs = 0:epochs # longest loop & one of the stop criterias
ve = vE{nLayers,1};
## calc jacobian
## Jj is jacobian matrix
[Jj] = __calcjacobian(net,Im,Nn,Aa,vE);
## rerange error vector for jacobi matrix
ve = ve(:);
Jjve = (Jj' * ve); # will be used to calculate the gradient
normGradX = sqrt(Jjve'*Jjve);
## record training progress for later plotting
## if requested
trainRec.perf(iEpochs+1) = perf;
trainRec.mu(iEpochs+1) = mu;
if (doValidation)
trainRec.vperf(iEpochs+1) = VV.perf;
endif
## stoping criteria
[stop,currentTime] = stopifnecessary(stop,startTime,perf,goal,\
iEpochs,epochs,time,normGradX,minGrad,mu,muMax,\
doValidation,VV,maxFail);
## show train progress
showtrainprogress(show,stop,iEpochs,epochs,time,currentTime, \
goal,perf,minGrad,normGradX,shortStr,net);
## show performance plot, if needed
if !isnan(show) # if no performance plot is needed
## now make it possible to define after how much loops the
## performance plot should be updated
if (mod(iEpochs,show)==0)
plot(1:length(trainRec.perf),trainRec.perf);
if (doValidation)
hold on;
plot(1:length(trainRec.vperf),trainRec.vperf,"--g");
endif
endif
endif # if !(strcmp(show,"NaN"))
# legend("Training","Validation");
## stop if one of the criterias is reached.
if length(stop)
if (doValidation)
net = VV.net;
endif
break
endif
## calculate DeltaX
while (mu <= muMax)
## calculate change in x
## see [4], page 12-21
dx = -((Jj' * Jj) + (muI*mu)) \ Jjve;
## add changes in x to actual x values (xx)
x1 = xx + dx;
## now add x1 to a new network to see if performance will be better
net1 = __setx(net,x1);
## calc now new performance with the new net
[perf1,vE1,Aa1,N1] = __calcperf(net1,x1,Im,Tt);
if (perf1 < perf)
## this means, net performance with new weight values is better...
## so save the new values
xx = x1;
net = net1;
Nn = N1;
Aa = Aa1;
vE = vE1;
perf = perf1;
mu = mu * muDec;
if (mu < 1e-20) # 1e-20 is properly the hard coded parameter in MATLAB(TM)
mu = 1e-20;
endif
break
endif
mu = mu * muInc;
endwhile
## validate with DeltaX
if (doValidation)
vperf = __calcperf(net,xx,VV.Im,VV.Tt);
if (vperf < VV.perf)
VV.perf = vperf;
VV.net = net;
## if actual validation performance is better,
## set numFail to zero again
VV.numFail = 0;
elseif (vperf > VV.perf)
VV.numFail = VV.numFail + 1;
endif
endif
endfor #for iEpochs = 0:epochs
#=======================================================
#
# additional functions
#
#=======================================================
function checkParameter(epochs,goal,maxFail,minGrad,mu,\
muInc, muDec, muMax, show, time)
## Parameter Checking
## epochs must be a positive integer
if ( !isposint(epochs) )
error("Epochs is not a positive integer.")
endif
## goal can be zero or a positive double
if ( (goal<0) || !(isa(goal,"double")) )
error("Goal is not zero or a positive real value.")
endif
## maxFail must be also a positive integer
if ( !isposint(maxFail) ) # this will be used, to see if validation can
# break the training
error("maxFail is not a positive integer.")
endif
if (!isa(minGrad,"double")) || (!isreal(minGrad)) || (!isscalar(minGrad)) || \
(minGrad < 0)
error("minGrad is not zero or a positive real value.")
end
## mu must be a positive real value. this parameter is responsible
## for moving from stepest descent to quasi newton
if ((!isa(mu,"double")) || (!isreal(mu)) || (any(size(mu)) != 1) || (mu <= 0))
error("mu is not a positive real value.")
endif
## muDec defines the decrement factor
if ((!isa(muDec,"double")) || (!isreal(muDec)) || (any(size(muDec)) != 1) || \
(muDec < 0) || (muDec > 1))
error("muDec is not a real value between 0 and 1.")
endif
## muInc defines the increment factor
if (~isa(muInc,"double")) || (!isreal(muInc)) || (any(size(muInc)) != 1) || \
(muInc < 1)
error("muInc is not a real value greater than 1.")
endif
## muMax is the upper boundary for the mu value
if (!isa(muMax,"double")) || (!isreal(muMax)) || (any(size(muMax)) != 1) || \
(muMax <= 0)
error("muMax is not a positive real value.")
endif
## check for actual mu value
if (mu > muMax)
error("mu is greater than muMax.")
end
## check if show is activated
if (!isnan(show))
if (!isposint(show))
error(["Show is not " "NaN" " or a positive integer."])
endif
endif
## check at last the time argument, must be zero or a positive real value
if (!isa(time,"double")) || (!isreal(time)) || (any(size(time)) != 1) || \
(time < 0)
error("Time is not zero or a positive real value.")
end
endfunction # parameter checking
#
# -----------------------------------------------------------------------------
#
function showtrainprogress(show,stop,iEpochs,epochs,time,currentTime, \
goal,perf,minGrad,normGradX,shortStr,net)
## check number of inputs
error(nargchk(12,12,nargin));
## show progress
if isfinite(show) && (!rem(iEpochs,show) || length(stop))
fprintf(shortStr); # outputs the training algorithm
if isfinite(epochs)
fprintf(", Epoch %g/%g",iEpochs, epochs);
endif
if isfinite(time)
fprintf(", Time %4.1f%%",currentTime/time*100); # \todo: Time wird nicht ausgegeben
endif
if isfinite(goal)
fprintf(", %s %g/%g",upper(net.performFcn),perf,goal); # outputs the performance function
endif
if isfinite(minGrad)
fprintf(", Gradient %g/%g",normGradX,minGrad);
endif
fprintf("\n")
if length(stop)
fprintf("%s, %s\n\n",shortStr,stop);
endif
fflush(stdout); # writes output to stdout as soon as output messages are available
endif
endfunction
#
# -----------------------------------------------------------------------------
#
function [stop,currentTime] = stopifnecessary(stop,startTime,perf,goal,\
iEpochs,epochs,time,normGradX,minGrad,mu,muMax,\
doValidation,VV,maxFail)
## check number of inputs
error(nargchk(14,14,nargin));
currentTime = etime(clock(),startTime);
if (perf <= goal)
stop = "Performance goal met.";
elseif (iEpochs == epochs)
stop = "Maximum epoch reached, performance goal was not met.";
elseif (currentTime > time)
stop = "Maximum time elapsed, performance goal was not met.";
elseif (normGradX < minGrad)
stop = "Minimum gradient reached, performance goal was not met.";
elseif (mu > muMax)
stop = "Maximum MU reached, performance goal was not met.";
elseif (doValidation)
if (VV.numFail > maxFail)
stop = "Validation stop.";
endif
endif
endfunction
# =====================================================================
#
# END additional functions
#
# =====================================================================
endfunction
|