[2806] | 1 | /*************************************************************************
|
---|
| 2 | Copyright (c) 2007-2008, Sergey Bochkanov (ALGLIB project).
|
---|
| 3 |
|
---|
| 4 | >>> SOURCE LICENSE >>>
|
---|
| 5 | This program is free software; you can redistribute it and/or modify
|
---|
| 6 | it under the terms of the GNU General Public License as published by
|
---|
| 7 | the Free Software Foundation (www.fsf.org); either version 2 of the
|
---|
| 8 | License, or (at your option) any later version.
|
---|
| 9 |
|
---|
| 10 | This program is distributed in the hope that it will be useful,
|
---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | GNU General Public License for more details.
|
---|
| 14 |
|
---|
| 15 | A copy of the GNU General Public License is available at
|
---|
| 16 | http://www.fsf.org/licensing/licenses
|
---|
| 17 |
|
---|
| 18 | >>> END OF LICENSE >>>
|
---|
| 19 | *************************************************************************/
|
---|
| 20 |
|
---|
| 21 | using System;
|
---|
| 22 |
|
---|
| 23 | namespace alglib
|
---|
| 24 | {
|
---|
| 25 | public class mlpe
|
---|
| 26 | {
|
---|
| 27 | /*************************************************************************
|
---|
| 28 | Neural networks ensemble
|
---|
| 29 | *************************************************************************/
|
---|
| 30 | public struct mlpensemble
|
---|
| 31 | {
|
---|
| 32 | public int[] structinfo;
|
---|
| 33 | public int ensemblesize;
|
---|
| 34 | public int nin;
|
---|
| 35 | public int nout;
|
---|
| 36 | public int wcount;
|
---|
| 37 | public bool issoftmax;
|
---|
| 38 | public bool postprocessing;
|
---|
| 39 | public double[] weights;
|
---|
| 40 | public double[] columnmeans;
|
---|
| 41 | public double[] columnsigmas;
|
---|
| 42 | public int serializedlen;
|
---|
| 43 | public double[] serializedmlp;
|
---|
| 44 | public double[] tmpweights;
|
---|
| 45 | public double[] tmpmeans;
|
---|
| 46 | public double[] tmpsigmas;
|
---|
| 47 | public double[] neurons;
|
---|
| 48 | public double[] dfdnet;
|
---|
| 49 | public double[] y;
|
---|
| 50 | };
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | public const int mlpntotaloffset = 3;
|
---|
| 56 | public const int mlpevnum = 9;
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | /*************************************************************************
|
---|
| 60 | Like MLPCreate0, but for ensembles.
|
---|
| 61 |
|
---|
| 62 | -- ALGLIB --
|
---|
| 63 | Copyright 18.02.2009 by Bochkanov Sergey
|
---|
| 64 | *************************************************************************/
|
---|
| 65 | public static void mlpecreate0(int nin,
|
---|
| 66 | int nout,
|
---|
| 67 | int ensemblesize,
|
---|
| 68 | ref mlpensemble ensemble)
|
---|
| 69 | {
|
---|
| 70 | mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
|
---|
| 71 |
|
---|
| 72 | mlpbase.mlpcreate0(nin, nout, ref net);
|
---|
| 73 | mlpecreatefromnetwork(ref net, ensemblesize, ref ensemble);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 | /*************************************************************************
|
---|
| 78 | Like MLPCreate1, but for ensembles.
|
---|
| 79 |
|
---|
| 80 | -- ALGLIB --
|
---|
| 81 | Copyright 18.02.2009 by Bochkanov Sergey
|
---|
| 82 | *************************************************************************/
|
---|
| 83 | public static void mlpecreate1(int nin,
|
---|
| 84 | int nhid,
|
---|
| 85 | int nout,
|
---|
| 86 | int ensemblesize,
|
---|
| 87 | ref mlpensemble ensemble)
|
---|
| 88 | {
|
---|
| 89 | mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
|
---|
| 90 |
|
---|
| 91 | mlpbase.mlpcreate1(nin, nhid, nout, ref net);
|
---|
| 92 | mlpecreatefromnetwork(ref net, ensemblesize, ref ensemble);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | /*************************************************************************
|
---|
| 97 | Like MLPCreate2, but for ensembles.
|
---|
| 98 |
|
---|
| 99 | -- ALGLIB --
|
---|
| 100 | Copyright 18.02.2009 by Bochkanov Sergey
|
---|
| 101 | *************************************************************************/
|
---|
| 102 | public static void mlpecreate2(int nin,
|
---|
| 103 | int nhid1,
|
---|
| 104 | int nhid2,
|
---|
| 105 | int nout,
|
---|
| 106 | int ensemblesize,
|
---|
| 107 | ref mlpensemble ensemble)
|
---|
| 108 | {
|
---|
| 109 | mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
|
---|
| 110 |
|
---|
| 111 | mlpbase.mlpcreate2(nin, nhid1, nhid2, nout, ref net);
|
---|
| 112 | mlpecreatefromnetwork(ref net, ensemblesize, ref ensemble);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 |
|
---|
| 116 | /*************************************************************************
|
---|
| 117 | Like MLPCreateB0, but for ensembles.
|
---|
| 118 |
|
---|
| 119 | -- ALGLIB --
|
---|
| 120 | Copyright 18.02.2009 by Bochkanov Sergey
|
---|
| 121 | *************************************************************************/
|
---|
| 122 | public static void mlpecreateb0(int nin,
|
---|
| 123 | int nout,
|
---|
| 124 | double b,
|
---|
| 125 | double d,
|
---|
| 126 | int ensemblesize,
|
---|
| 127 | ref mlpensemble ensemble)
|
---|
| 128 | {
|
---|
| 129 | mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
|
---|
| 130 |
|
---|
| 131 | mlpbase.mlpcreateb0(nin, nout, b, d, ref net);
|
---|
| 132 | mlpecreatefromnetwork(ref net, ensemblesize, ref ensemble);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 | /*************************************************************************
|
---|
| 137 | Like MLPCreateB1, but for ensembles.
|
---|
| 138 |
|
---|
| 139 | -- ALGLIB --
|
---|
| 140 | Copyright 18.02.2009 by Bochkanov Sergey
|
---|
| 141 | *************************************************************************/
|
---|
| 142 | public static void mlpecreateb1(int nin,
|
---|
| 143 | int nhid,
|
---|
| 144 | int nout,
|
---|
| 145 | double b,
|
---|
| 146 | double d,
|
---|
| 147 | int ensemblesize,
|
---|
| 148 | ref mlpensemble ensemble)
|
---|
| 149 | {
|
---|
| 150 | mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
|
---|
| 151 |
|
---|
| 152 | mlpbase.mlpcreateb1(nin, nhid, nout, b, d, ref net);
|
---|
| 153 | mlpecreatefromnetwork(ref net, ensemblesize, ref ensemble);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | /*************************************************************************
|
---|
| 158 | Like MLPCreateB2, but for ensembles.
|
---|
| 159 |
|
---|
| 160 | -- ALGLIB --
|
---|
| 161 | Copyright 18.02.2009 by Bochkanov Sergey
|
---|
| 162 | *************************************************************************/
|
---|
| 163 | public static void mlpecreateb2(int nin,
|
---|
| 164 | int nhid1,
|
---|
| 165 | int nhid2,
|
---|
| 166 | int nout,
|
---|
| 167 | double b,
|
---|
| 168 | double d,
|
---|
| 169 | int ensemblesize,
|
---|
| 170 | ref mlpensemble ensemble)
|
---|
| 171 | {
|
---|
| 172 | mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
|
---|
| 173 |
|
---|
| 174 | mlpbase.mlpcreateb2(nin, nhid1, nhid2, nout, b, d, ref net);
|
---|
| 175 | mlpecreatefromnetwork(ref net, ensemblesize, ref ensemble);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 |
|
---|
| 179 | /*************************************************************************
|
---|
| 180 | Like MLPCreateR0, but for ensembles.
|
---|
| 181 |
|
---|
| 182 | -- ALGLIB --
|
---|
| 183 | Copyright 18.02.2009 by Bochkanov Sergey
|
---|
| 184 | *************************************************************************/
|
---|
| 185 | public static void mlpecreater0(int nin,
|
---|
| 186 | int nout,
|
---|
| 187 | double a,
|
---|
| 188 | double b,
|
---|
| 189 | int ensemblesize,
|
---|
| 190 | ref mlpensemble ensemble)
|
---|
| 191 | {
|
---|
| 192 | mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
|
---|
| 193 |
|
---|
| 194 | mlpbase.mlpcreater0(nin, nout, a, b, ref net);
|
---|
| 195 | mlpecreatefromnetwork(ref net, ensemblesize, ref ensemble);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 | /*************************************************************************
|
---|
| 200 | Like MLPCreateR1, but for ensembles.
|
---|
| 201 |
|
---|
| 202 | -- ALGLIB --
|
---|
| 203 | Copyright 18.02.2009 by Bochkanov Sergey
|
---|
| 204 | *************************************************************************/
|
---|
| 205 | public static void mlpecreater1(int nin,
|
---|
| 206 | int nhid,
|
---|
| 207 | int nout,
|
---|
| 208 | double a,
|
---|
| 209 | double b,
|
---|
| 210 | int ensemblesize,
|
---|
| 211 | ref mlpensemble ensemble)
|
---|
| 212 | {
|
---|
| 213 | mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
|
---|
| 214 |
|
---|
| 215 | mlpbase.mlpcreater1(nin, nhid, nout, a, b, ref net);
|
---|
| 216 | mlpecreatefromnetwork(ref net, ensemblesize, ref ensemble);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 |
|
---|
| 220 | /*************************************************************************
|
---|
| 221 | Like MLPCreateR2, but for ensembles.
|
---|
| 222 |
|
---|
| 223 | -- ALGLIB --
|
---|
| 224 | Copyright 18.02.2009 by Bochkanov Sergey
|
---|
| 225 | *************************************************************************/
|
---|
| 226 | public static void mlpecreater2(int nin,
|
---|
| 227 | int nhid1,
|
---|
| 228 | int nhid2,
|
---|
| 229 | int nout,
|
---|
| 230 | double a,
|
---|
| 231 | double b,
|
---|
| 232 | int ensemblesize,
|
---|
| 233 | ref mlpensemble ensemble)
|
---|
| 234 | {
|
---|
| 235 | mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
|
---|
| 236 |
|
---|
| 237 | mlpbase.mlpcreater2(nin, nhid1, nhid2, nout, a, b, ref net);
|
---|
| 238 | mlpecreatefromnetwork(ref net, ensemblesize, ref ensemble);
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 |
|
---|
| 242 | /*************************************************************************
|
---|
| 243 | Like MLPCreateC0, but for ensembles.
|
---|
| 244 |
|
---|
| 245 | -- ALGLIB --
|
---|
| 246 | Copyright 18.02.2009 by Bochkanov Sergey
|
---|
| 247 | *************************************************************************/
|
---|
| 248 | public static void mlpecreatec0(int nin,
|
---|
| 249 | int nout,
|
---|
| 250 | int ensemblesize,
|
---|
| 251 | ref mlpensemble ensemble)
|
---|
| 252 | {
|
---|
| 253 | mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
|
---|
| 254 |
|
---|
| 255 | mlpbase.mlpcreatec0(nin, nout, ref net);
|
---|
| 256 | mlpecreatefromnetwork(ref net, ensemblesize, ref ensemble);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 |
|
---|
| 260 | /*************************************************************************
|
---|
| 261 | Like MLPCreateC1, but for ensembles.
|
---|
| 262 |
|
---|
| 263 | -- ALGLIB --
|
---|
| 264 | Copyright 18.02.2009 by Bochkanov Sergey
|
---|
| 265 | *************************************************************************/
|
---|
| 266 | public static void mlpecreatec1(int nin,
|
---|
| 267 | int nhid,
|
---|
| 268 | int nout,
|
---|
| 269 | int ensemblesize,
|
---|
| 270 | ref mlpensemble ensemble)
|
---|
| 271 | {
|
---|
| 272 | mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
|
---|
| 273 |
|
---|
| 274 | mlpbase.mlpcreatec1(nin, nhid, nout, ref net);
|
---|
| 275 | mlpecreatefromnetwork(ref net, ensemblesize, ref ensemble);
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 |
|
---|
| 279 | /*************************************************************************
|
---|
| 280 | Like MLPCreateC2, but for ensembles.
|
---|
| 281 |
|
---|
| 282 | -- ALGLIB --
|
---|
| 283 | Copyright 18.02.2009 by Bochkanov Sergey
|
---|
| 284 | *************************************************************************/
|
---|
| 285 | public static void mlpecreatec2(int nin,
|
---|
| 286 | int nhid1,
|
---|
| 287 | int nhid2,
|
---|
| 288 | int nout,
|
---|
| 289 | int ensemblesize,
|
---|
| 290 | ref mlpensemble ensemble)
|
---|
| 291 | {
|
---|
| 292 | mlpbase.multilayerperceptron net = new mlpbase.multilayerperceptron();
|
---|
| 293 |
|
---|
| 294 | mlpbase.mlpcreatec2(nin, nhid1, nhid2, nout, ref net);
|
---|
| 295 | mlpecreatefromnetwork(ref net, ensemblesize, ref ensemble);
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 |
|
---|
| 299 | /*************************************************************************
|
---|
| 300 | Creates ensemble from network. Only network geometry is copied.
|
---|
| 301 |
|
---|
| 302 | -- ALGLIB --
|
---|
| 303 | Copyright 17.02.2009 by Bochkanov Sergey
|
---|
| 304 | *************************************************************************/
|
---|
| 305 | public static void mlpecreatefromnetwork(ref mlpbase.multilayerperceptron network,
|
---|
| 306 | int ensemblesize,
|
---|
| 307 | ref mlpensemble ensemble)
|
---|
| 308 | {
|
---|
| 309 | int i = 0;
|
---|
| 310 | int ccount = 0;
|
---|
| 311 | int i_ = 0;
|
---|
| 312 | int i1_ = 0;
|
---|
| 313 |
|
---|
| 314 | System.Diagnostics.Debug.Assert(ensemblesize>0, "MLPECreate: incorrect ensemble size!");
|
---|
| 315 |
|
---|
| 316 | //
|
---|
| 317 | // network properties
|
---|
| 318 | //
|
---|
| 319 | mlpbase.mlpproperties(ref network, ref ensemble.nin, ref ensemble.nout, ref ensemble.wcount);
|
---|
| 320 | if( mlpbase.mlpissoftmax(ref network) )
|
---|
| 321 | {
|
---|
| 322 | ccount = ensemble.nin;
|
---|
| 323 | }
|
---|
| 324 | else
|
---|
| 325 | {
|
---|
| 326 | ccount = ensemble.nin+ensemble.nout;
|
---|
| 327 | }
|
---|
| 328 | ensemble.postprocessing = false;
|
---|
| 329 | ensemble.issoftmax = mlpbase.mlpissoftmax(ref network);
|
---|
| 330 | ensemble.ensemblesize = ensemblesize;
|
---|
| 331 |
|
---|
| 332 | //
|
---|
| 333 | // structure information
|
---|
| 334 | //
|
---|
| 335 | ensemble.structinfo = new int[network.structinfo[0]-1+1];
|
---|
| 336 | for(i=0; i<=network.structinfo[0]-1; i++)
|
---|
| 337 | {
|
---|
| 338 | ensemble.structinfo[i] = network.structinfo[i];
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | //
|
---|
| 342 | // weights, means, sigmas
|
---|
| 343 | //
|
---|
| 344 | ensemble.weights = new double[ensemblesize*ensemble.wcount-1+1];
|
---|
| 345 | ensemble.columnmeans = new double[ensemblesize*ccount-1+1];
|
---|
| 346 | ensemble.columnsigmas = new double[ensemblesize*ccount-1+1];
|
---|
| 347 | for(i=0; i<=ensemblesize*ensemble.wcount-1; i++)
|
---|
| 348 | {
|
---|
| 349 | ensemble.weights[i] = AP.Math.RandomReal()-0.5;
|
---|
| 350 | }
|
---|
| 351 | for(i=0; i<=ensemblesize-1; i++)
|
---|
| 352 | {
|
---|
| 353 | i1_ = (0) - (i*ccount);
|
---|
| 354 | for(i_=i*ccount; i_<=(i+1)*ccount-1;i_++)
|
---|
| 355 | {
|
---|
| 356 | ensemble.columnmeans[i_] = network.columnmeans[i_+i1_];
|
---|
| 357 | }
|
---|
| 358 | i1_ = (0) - (i*ccount);
|
---|
| 359 | for(i_=i*ccount; i_<=(i+1)*ccount-1;i_++)
|
---|
| 360 | {
|
---|
| 361 | ensemble.columnsigmas[i_] = network.columnsigmas[i_+i1_];
|
---|
| 362 | }
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | //
|
---|
| 366 | // serialized part
|
---|
| 367 | //
|
---|
| 368 | mlpbase.mlpserialize(ref network, ref ensemble.serializedmlp, ref ensemble.serializedlen);
|
---|
| 369 |
|
---|
| 370 | //
|
---|
| 371 | // temporaries, internal buffers
|
---|
| 372 | //
|
---|
| 373 | ensemble.tmpweights = new double[ensemble.wcount-1+1];
|
---|
| 374 | ensemble.tmpmeans = new double[ccount-1+1];
|
---|
| 375 | ensemble.tmpsigmas = new double[ccount-1+1];
|
---|
| 376 | ensemble.neurons = new double[ensemble.structinfo[mlpntotaloffset]-1+1];
|
---|
| 377 | ensemble.dfdnet = new double[ensemble.structinfo[mlpntotaloffset]-1+1];
|
---|
| 378 | ensemble.y = new double[ensemble.nout-1+1];
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 |
|
---|
| 382 | /*************************************************************************
|
---|
| 383 | Copying of MLPEnsemble strucure
|
---|
| 384 |
|
---|
| 385 | INPUT PARAMETERS:
|
---|
| 386 | Ensemble1 - original
|
---|
| 387 |
|
---|
| 388 | OUTPUT PARAMETERS:
|
---|
| 389 | Ensemble2 - copy
|
---|
| 390 |
|
---|
| 391 | -- ALGLIB --
|
---|
| 392 | Copyright 17.02.2009 by Bochkanov Sergey
|
---|
| 393 | *************************************************************************/
|
---|
| 394 | public static void mlpecopy(ref mlpensemble ensemble1,
|
---|
| 395 | ref mlpensemble ensemble2)
|
---|
| 396 | {
|
---|
| 397 | int i = 0;
|
---|
| 398 | int ssize = 0;
|
---|
| 399 | int ccount = 0;
|
---|
| 400 | int ntotal = 0;
|
---|
| 401 | int i_ = 0;
|
---|
| 402 |
|
---|
| 403 |
|
---|
| 404 | //
|
---|
| 405 | // Unload info
|
---|
| 406 | //
|
---|
| 407 | ssize = ensemble1.structinfo[0];
|
---|
| 408 | if( ensemble1.issoftmax )
|
---|
| 409 | {
|
---|
| 410 | ccount = ensemble1.nin;
|
---|
| 411 | }
|
---|
| 412 | else
|
---|
| 413 | {
|
---|
| 414 | ccount = ensemble1.nin+ensemble1.nout;
|
---|
| 415 | }
|
---|
| 416 | ntotal = ensemble1.structinfo[mlpntotaloffset];
|
---|
| 417 |
|
---|
| 418 | //
|
---|
| 419 | // Allocate space
|
---|
| 420 | //
|
---|
| 421 | ensemble2.structinfo = new int[ssize-1+1];
|
---|
| 422 | ensemble2.weights = new double[ensemble1.ensemblesize*ensemble1.wcount-1+1];
|
---|
| 423 | ensemble2.columnmeans = new double[ensemble1.ensemblesize*ccount-1+1];
|
---|
| 424 | ensemble2.columnsigmas = new double[ensemble1.ensemblesize*ccount-1+1];
|
---|
| 425 | ensemble2.tmpweights = new double[ensemble1.wcount-1+1];
|
---|
| 426 | ensemble2.tmpmeans = new double[ccount-1+1];
|
---|
| 427 | ensemble2.tmpsigmas = new double[ccount-1+1];
|
---|
| 428 | ensemble2.serializedmlp = new double[ensemble1.serializedlen-1+1];
|
---|
| 429 | ensemble2.neurons = new double[ntotal-1+1];
|
---|
| 430 | ensemble2.dfdnet = new double[ntotal-1+1];
|
---|
| 431 | ensemble2.y = new double[ensemble1.nout-1+1];
|
---|
| 432 |
|
---|
| 433 | //
|
---|
| 434 | // Copy
|
---|
| 435 | //
|
---|
| 436 | ensemble2.nin = ensemble1.nin;
|
---|
| 437 | ensemble2.nout = ensemble1.nout;
|
---|
| 438 | ensemble2.wcount = ensemble1.wcount;
|
---|
| 439 | ensemble2.ensemblesize = ensemble1.ensemblesize;
|
---|
| 440 | ensemble2.issoftmax = ensemble1.issoftmax;
|
---|
| 441 | ensemble2.postprocessing = ensemble1.postprocessing;
|
---|
| 442 | ensemble2.serializedlen = ensemble1.serializedlen;
|
---|
| 443 | for(i=0; i<=ssize-1; i++)
|
---|
| 444 | {
|
---|
| 445 | ensemble2.structinfo[i] = ensemble1.structinfo[i];
|
---|
| 446 | }
|
---|
| 447 | for(i_=0; i_<=ensemble1.ensemblesize*ensemble1.wcount-1;i_++)
|
---|
| 448 | {
|
---|
| 449 | ensemble2.weights[i_] = ensemble1.weights[i_];
|
---|
| 450 | }
|
---|
| 451 | for(i_=0; i_<=ensemble1.ensemblesize*ccount-1;i_++)
|
---|
| 452 | {
|
---|
| 453 | ensemble2.columnmeans[i_] = ensemble1.columnmeans[i_];
|
---|
| 454 | }
|
---|
| 455 | for(i_=0; i_<=ensemble1.ensemblesize*ccount-1;i_++)
|
---|
| 456 | {
|
---|
| 457 | ensemble2.columnsigmas[i_] = ensemble1.columnsigmas[i_];
|
---|
| 458 | }
|
---|
| 459 | for(i_=0; i_<=ensemble1.serializedlen-1;i_++)
|
---|
| 460 | {
|
---|
| 461 | ensemble2.serializedmlp[i_] = ensemble1.serializedmlp[i_];
|
---|
| 462 | }
|
---|
| 463 | }
|
---|
| 464 |
|
---|
| 465 |
|
---|
| 466 | /*************************************************************************
|
---|
| 467 | Serialization of MLPEnsemble strucure
|
---|
| 468 |
|
---|
| 469 | INPUT PARAMETERS:
|
---|
| 470 | Ensemble- original
|
---|
| 471 |
|
---|
| 472 | OUTPUT PARAMETERS:
|
---|
| 473 | RA - array of real numbers which stores ensemble,
|
---|
| 474 | array[0..RLen-1]
|
---|
| 475 | RLen - RA lenght
|
---|
| 476 |
|
---|
| 477 | -- ALGLIB --
|
---|
| 478 | Copyright 17.02.2009 by Bochkanov Sergey
|
---|
| 479 | *************************************************************************/
|
---|
| 480 | public static void mlpeserialize(ref mlpensemble ensemble,
|
---|
| 481 | ref double[] ra,
|
---|
| 482 | ref int rlen)
|
---|
| 483 | {
|
---|
| 484 | int i = 0;
|
---|
| 485 | int ssize = 0;
|
---|
| 486 | int ntotal = 0;
|
---|
| 487 | int ccount = 0;
|
---|
| 488 | int hsize = 0;
|
---|
| 489 | int offs = 0;
|
---|
| 490 | int i_ = 0;
|
---|
| 491 | int i1_ = 0;
|
---|
| 492 |
|
---|
| 493 | hsize = 13;
|
---|
| 494 | ssize = ensemble.structinfo[0];
|
---|
| 495 | if( ensemble.issoftmax )
|
---|
| 496 | {
|
---|
| 497 | ccount = ensemble.nin;
|
---|
| 498 | }
|
---|
| 499 | else
|
---|
| 500 | {
|
---|
| 501 | ccount = ensemble.nin+ensemble.nout;
|
---|
| 502 | }
|
---|
| 503 | ntotal = ensemble.structinfo[mlpntotaloffset];
|
---|
| 504 | rlen = hsize+ssize+ensemble.ensemblesize*ensemble.wcount+2*ccount*ensemble.ensemblesize+ensemble.serializedlen;
|
---|
| 505 |
|
---|
| 506 | //
|
---|
| 507 | // RA format:
|
---|
| 508 | // [0] RLen
|
---|
| 509 | // [1] Version (MLPEVNum)
|
---|
| 510 | // [2] EnsembleSize
|
---|
| 511 | // [3] NIn
|
---|
| 512 | // [4] NOut
|
---|
| 513 | // [5] WCount
|
---|
| 514 | // [6] IsSoftmax 0/1
|
---|
| 515 | // [7] PostProcessing 0/1
|
---|
| 516 | // [8] sizeof(StructInfo)
|
---|
| 517 | // [9] NTotal (sizeof(Neurons), sizeof(DFDNET))
|
---|
| 518 | // [10] CCount (sizeof(ColumnMeans), sizeof(ColumnSigmas))
|
---|
| 519 | // [11] data offset
|
---|
| 520 | // [12] SerializedLen
|
---|
| 521 | //
|
---|
| 522 | // [..] StructInfo
|
---|
| 523 | // [..] Weights
|
---|
| 524 | // [..] ColumnMeans
|
---|
| 525 | // [..] ColumnSigmas
|
---|
| 526 | //
|
---|
| 527 | ra = new double[rlen-1+1];
|
---|
| 528 | ra[0] = rlen;
|
---|
| 529 | ra[1] = mlpevnum;
|
---|
| 530 | ra[2] = ensemble.ensemblesize;
|
---|
| 531 | ra[3] = ensemble.nin;
|
---|
| 532 | ra[4] = ensemble.nout;
|
---|
| 533 | ra[5] = ensemble.wcount;
|
---|
| 534 | if( ensemble.issoftmax )
|
---|
| 535 | {
|
---|
| 536 | ra[6] = 1;
|
---|
| 537 | }
|
---|
| 538 | else
|
---|
| 539 | {
|
---|
| 540 | ra[6] = 0;
|
---|
| 541 | }
|
---|
| 542 | if( ensemble.postprocessing )
|
---|
| 543 | {
|
---|
| 544 | ra[7] = 1;
|
---|
| 545 | }
|
---|
| 546 | else
|
---|
| 547 | {
|
---|
| 548 | ra[7] = 9;
|
---|
| 549 | }
|
---|
| 550 | ra[8] = ssize;
|
---|
| 551 | ra[9] = ntotal;
|
---|
| 552 | ra[10] = ccount;
|
---|
| 553 | ra[11] = hsize;
|
---|
| 554 | ra[12] = ensemble.serializedlen;
|
---|
| 555 | offs = hsize;
|
---|
| 556 | for(i=offs; i<=offs+ssize-1; i++)
|
---|
| 557 | {
|
---|
| 558 | ra[i] = ensemble.structinfo[i-offs];
|
---|
| 559 | }
|
---|
| 560 | offs = offs+ssize;
|
---|
| 561 | i1_ = (0) - (offs);
|
---|
| 562 | for(i_=offs; i_<=offs+ensemble.ensemblesize*ensemble.wcount-1;i_++)
|
---|
| 563 | {
|
---|
| 564 | ra[i_] = ensemble.weights[i_+i1_];
|
---|
| 565 | }
|
---|
| 566 | offs = offs+ensemble.ensemblesize*ensemble.wcount;
|
---|
| 567 | i1_ = (0) - (offs);
|
---|
| 568 | for(i_=offs; i_<=offs+ensemble.ensemblesize*ccount-1;i_++)
|
---|
| 569 | {
|
---|
| 570 | ra[i_] = ensemble.columnmeans[i_+i1_];
|
---|
| 571 | }
|
---|
| 572 | offs = offs+ensemble.ensemblesize*ccount;
|
---|
| 573 | i1_ = (0) - (offs);
|
---|
| 574 | for(i_=offs; i_<=offs+ensemble.ensemblesize*ccount-1;i_++)
|
---|
| 575 | {
|
---|
| 576 | ra[i_] = ensemble.columnsigmas[i_+i1_];
|
---|
| 577 | }
|
---|
| 578 | offs = offs+ensemble.ensemblesize*ccount;
|
---|
| 579 | i1_ = (0) - (offs);
|
---|
| 580 | for(i_=offs; i_<=offs+ensemble.serializedlen-1;i_++)
|
---|
| 581 | {
|
---|
| 582 | ra[i_] = ensemble.serializedmlp[i_+i1_];
|
---|
| 583 | }
|
---|
| 584 | offs = offs+ensemble.serializedlen;
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 |
|
---|
| 588 | /*************************************************************************
|
---|
| 589 | Unserialization of MLPEnsemble strucure
|
---|
| 590 |
|
---|
| 591 | INPUT PARAMETERS:
|
---|
| 592 | RA - real array which stores ensemble
|
---|
| 593 |
|
---|
| 594 | OUTPUT PARAMETERS:
|
---|
| 595 | Ensemble- restored structure
|
---|
| 596 |
|
---|
| 597 | -- ALGLIB --
|
---|
| 598 | Copyright 17.02.2009 by Bochkanov Sergey
|
---|
| 599 | *************************************************************************/
|
---|
| 600 | public static void mlpeunserialize(ref double[] ra,
|
---|
| 601 | ref mlpensemble ensemble)
|
---|
| 602 | {
|
---|
| 603 | int i = 0;
|
---|
| 604 | int ssize = 0;
|
---|
| 605 | int ntotal = 0;
|
---|
| 606 | int ccount = 0;
|
---|
| 607 | int hsize = 0;
|
---|
| 608 | int offs = 0;
|
---|
| 609 | int i_ = 0;
|
---|
| 610 | int i1_ = 0;
|
---|
| 611 |
|
---|
| 612 | System.Diagnostics.Debug.Assert((int)Math.Round(ra[1])==mlpevnum, "MLPEUnserialize: incorrect array!");
|
---|
| 613 |
|
---|
| 614 | //
|
---|
| 615 | // load info
|
---|
| 616 | //
|
---|
| 617 | hsize = 13;
|
---|
| 618 | ensemble.ensemblesize = (int)Math.Round(ra[2]);
|
---|
| 619 | ensemble.nin = (int)Math.Round(ra[3]);
|
---|
| 620 | ensemble.nout = (int)Math.Round(ra[4]);
|
---|
| 621 | ensemble.wcount = (int)Math.Round(ra[5]);
|
---|
| 622 | ensemble.issoftmax = (int)Math.Round(ra[6])==1;
|
---|
| 623 | ensemble.postprocessing = (int)Math.Round(ra[7])==1;
|
---|
| 624 | ssize = (int)Math.Round(ra[8]);
|
---|
| 625 | ntotal = (int)Math.Round(ra[9]);
|
---|
| 626 | ccount = (int)Math.Round(ra[10]);
|
---|
| 627 | offs = (int)Math.Round(ra[11]);
|
---|
| 628 | ensemble.serializedlen = (int)Math.Round(ra[12]);
|
---|
| 629 |
|
---|
| 630 | //
|
---|
| 631 | // Allocate arrays
|
---|
| 632 | //
|
---|
| 633 | ensemble.structinfo = new int[ssize-1+1];
|
---|
| 634 | ensemble.weights = new double[ensemble.ensemblesize*ensemble.wcount-1+1];
|
---|
| 635 | ensemble.columnmeans = new double[ensemble.ensemblesize*ccount-1+1];
|
---|
| 636 | ensemble.columnsigmas = new double[ensemble.ensemblesize*ccount-1+1];
|
---|
| 637 | ensemble.tmpweights = new double[ensemble.wcount-1+1];
|
---|
| 638 | ensemble.tmpmeans = new double[ccount-1+1];
|
---|
| 639 | ensemble.tmpsigmas = new double[ccount-1+1];
|
---|
| 640 | ensemble.neurons = new double[ntotal-1+1];
|
---|
| 641 | ensemble.dfdnet = new double[ntotal-1+1];
|
---|
| 642 | ensemble.serializedmlp = new double[ensemble.serializedlen-1+1];
|
---|
| 643 | ensemble.y = new double[ensemble.nout-1+1];
|
---|
| 644 |
|
---|
| 645 | //
|
---|
| 646 | // load data
|
---|
| 647 | //
|
---|
| 648 | for(i=offs; i<=offs+ssize-1; i++)
|
---|
| 649 | {
|
---|
| 650 | ensemble.structinfo[i-offs] = (int)Math.Round(ra[i]);
|
---|
| 651 | }
|
---|
| 652 | offs = offs+ssize;
|
---|
| 653 | i1_ = (offs) - (0);
|
---|
| 654 | for(i_=0; i_<=ensemble.ensemblesize*ensemble.wcount-1;i_++)
|
---|
| 655 | {
|
---|
| 656 | ensemble.weights[i_] = ra[i_+i1_];
|
---|
| 657 | }
|
---|
| 658 | offs = offs+ensemble.ensemblesize*ensemble.wcount;
|
---|
| 659 | i1_ = (offs) - (0);
|
---|
| 660 | for(i_=0; i_<=ensemble.ensemblesize*ccount-1;i_++)
|
---|
| 661 | {
|
---|
| 662 | ensemble.columnmeans[i_] = ra[i_+i1_];
|
---|
| 663 | }
|
---|
| 664 | offs = offs+ensemble.ensemblesize*ccount;
|
---|
| 665 | i1_ = (offs) - (0);
|
---|
| 666 | for(i_=0; i_<=ensemble.ensemblesize*ccount-1;i_++)
|
---|
| 667 | {
|
---|
| 668 | ensemble.columnsigmas[i_] = ra[i_+i1_];
|
---|
| 669 | }
|
---|
| 670 | offs = offs+ensemble.ensemblesize*ccount;
|
---|
| 671 | i1_ = (offs) - (0);
|
---|
| 672 | for(i_=0; i_<=ensemble.serializedlen-1;i_++)
|
---|
| 673 | {
|
---|
| 674 | ensemble.serializedmlp[i_] = ra[i_+i1_];
|
---|
| 675 | }
|
---|
| 676 | offs = offs+ensemble.serializedlen;
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 |
|
---|
| 680 | /*************************************************************************
|
---|
| 681 | Randomization of MLP ensemble
|
---|
| 682 |
|
---|
| 683 | -- ALGLIB --
|
---|
| 684 | Copyright 17.02.2009 by Bochkanov Sergey
|
---|
| 685 | *************************************************************************/
|
---|
| 686 | public static void mlperandomize(ref mlpensemble ensemble)
|
---|
| 687 | {
|
---|
| 688 | int i = 0;
|
---|
| 689 |
|
---|
| 690 | for(i=0; i<=ensemble.ensemblesize*ensemble.wcount-1; i++)
|
---|
| 691 | {
|
---|
| 692 | ensemble.weights[i] = AP.Math.RandomReal()-0.5;
|
---|
| 693 | }
|
---|
| 694 | }
|
---|
| 695 |
|
---|
| 696 |
|
---|
| 697 | /*************************************************************************
|
---|
| 698 | Return ensemble properties (number of inputs and outputs).
|
---|
| 699 |
|
---|
| 700 | -- ALGLIB --
|
---|
| 701 | Copyright 17.02.2009 by Bochkanov Sergey
|
---|
| 702 | *************************************************************************/
|
---|
| 703 | public static void mlpeproperties(ref mlpensemble ensemble,
|
---|
| 704 | ref int nin,
|
---|
| 705 | ref int nout)
|
---|
| 706 | {
|
---|
| 707 | nin = ensemble.nin;
|
---|
| 708 | nout = ensemble.nout;
|
---|
| 709 | }
|
---|
| 710 |
|
---|
| 711 |
|
---|
| 712 | /*************************************************************************
|
---|
| 713 | Return normalization type (whether ensemble is SOFTMAX-normalized or not).
|
---|
| 714 |
|
---|
| 715 | -- ALGLIB --
|
---|
| 716 | Copyright 17.02.2009 by Bochkanov Sergey
|
---|
| 717 | *************************************************************************/
|
---|
| 718 | public static bool mlpeissoftmax(ref mlpensemble ensemble)
|
---|
| 719 | {
|
---|
| 720 | bool result = new bool();
|
---|
| 721 |
|
---|
| 722 | result = ensemble.issoftmax;
|
---|
| 723 | return result;
|
---|
| 724 | }
|
---|
| 725 |
|
---|
| 726 |
|
---|
| 727 | /*************************************************************************
|
---|
| 728 | Procesing
|
---|
| 729 |
|
---|
| 730 | INPUT PARAMETERS:
|
---|
| 731 | Ensemble- neural networks ensemble
|
---|
| 732 | X - input vector, array[0..NIn-1].
|
---|
| 733 |
|
---|
| 734 | OUTPUT PARAMETERS:
|
---|
| 735 | Y - result. Regression estimate when solving regression task,
|
---|
| 736 | vector of posterior probabilities for classification task.
|
---|
| 737 | Subroutine does not allocate memory for this vector, it is
|
---|
| 738 | responsibility of a caller to allocate it. Array must be
|
---|
| 739 | at least [0..NOut-1].
|
---|
| 740 |
|
---|
| 741 | -- ALGLIB --
|
---|
| 742 | Copyright 17.02.2009 by Bochkanov Sergey
|
---|
| 743 | *************************************************************************/
|
---|
| 744 | public static void mlpeprocess(ref mlpensemble ensemble,
|
---|
| 745 | ref double[] x,
|
---|
| 746 | ref double[] y)
|
---|
| 747 | {
|
---|
| 748 | int i = 0;
|
---|
| 749 | int es = 0;
|
---|
| 750 | int wc = 0;
|
---|
| 751 | int cc = 0;
|
---|
| 752 | double v = 0;
|
---|
| 753 | int i_ = 0;
|
---|
| 754 | int i1_ = 0;
|
---|
| 755 |
|
---|
| 756 | es = ensemble.ensemblesize;
|
---|
| 757 | wc = ensemble.wcount;
|
---|
| 758 | if( ensemble.issoftmax )
|
---|
| 759 | {
|
---|
| 760 | cc = ensemble.nin;
|
---|
| 761 | }
|
---|
| 762 | else
|
---|
| 763 | {
|
---|
| 764 | cc = ensemble.nin+ensemble.nout;
|
---|
| 765 | }
|
---|
| 766 | v = (double)(1)/(double)(es);
|
---|
| 767 | for(i=0; i<=ensemble.nout-1; i++)
|
---|
| 768 | {
|
---|
| 769 | y[i] = 0;
|
---|
| 770 | }
|
---|
| 771 | for(i=0; i<=es-1; i++)
|
---|
| 772 | {
|
---|
| 773 | i1_ = (i*wc) - (0);
|
---|
| 774 | for(i_=0; i_<=wc-1;i_++)
|
---|
| 775 | {
|
---|
| 776 | ensemble.tmpweights[i_] = ensemble.weights[i_+i1_];
|
---|
| 777 | }
|
---|
| 778 | i1_ = (i*cc) - (0);
|
---|
| 779 | for(i_=0; i_<=cc-1;i_++)
|
---|
| 780 | {
|
---|
| 781 | ensemble.tmpmeans[i_] = ensemble.columnmeans[i_+i1_];
|
---|
| 782 | }
|
---|
| 783 | i1_ = (i*cc) - (0);
|
---|
| 784 | for(i_=0; i_<=cc-1;i_++)
|
---|
| 785 | {
|
---|
| 786 | ensemble.tmpsigmas[i_] = ensemble.columnsigmas[i_+i1_];
|
---|
| 787 | }
|
---|
| 788 | mlpbase.mlpinternalprocessvector(ref ensemble.structinfo, ref ensemble.tmpweights, ref ensemble.tmpmeans, ref ensemble.tmpsigmas, ref ensemble.neurons, ref ensemble.dfdnet, ref x, ref ensemble.y);
|
---|
| 789 | for(i_=0; i_<=ensemble.nout-1;i_++)
|
---|
| 790 | {
|
---|
| 791 | y[i_] = y[i_] + v*ensemble.y[i_];
|
---|
| 792 | }
|
---|
| 793 | }
|
---|
| 794 | }
|
---|
| 795 |
|
---|
| 796 |
|
---|
| 797 | /*************************************************************************
|
---|
| 798 | Relative classification error on the test set
|
---|
| 799 |
|
---|
| 800 | INPUT PARAMETERS:
|
---|
| 801 | Ensemble- ensemble
|
---|
| 802 | XY - test set
|
---|
| 803 | NPoints - test set size
|
---|
| 804 |
|
---|
| 805 | RESULT:
|
---|
| 806 | percent of incorrectly classified cases.
|
---|
| 807 | Works both for classifier betwork and for regression networks which
|
---|
| 808 | are used as classifiers.
|
---|
| 809 |
|
---|
| 810 | -- ALGLIB --
|
---|
| 811 | Copyright 17.02.2009 by Bochkanov Sergey
|
---|
| 812 | *************************************************************************/
|
---|
| 813 | public static double mlperelclserror(ref mlpensemble ensemble,
|
---|
| 814 | ref double[,] xy,
|
---|
| 815 | int npoints)
|
---|
| 816 | {
|
---|
| 817 | double result = 0;
|
---|
| 818 | double relcls = 0;
|
---|
| 819 | double avgce = 0;
|
---|
| 820 | double rms = 0;
|
---|
| 821 | double avg = 0;
|
---|
| 822 | double avgrel = 0;
|
---|
| 823 |
|
---|
| 824 | mlpeallerrors(ref ensemble, ref xy, npoints, ref relcls, ref avgce, ref rms, ref avg, ref avgrel);
|
---|
| 825 | result = relcls;
|
---|
| 826 | return result;
|
---|
| 827 | }
|
---|
| 828 |
|
---|
| 829 |
|
---|
| 830 | /*************************************************************************
|
---|
| 831 | Average cross-entropy (in bits per element) on the test set
|
---|
| 832 |
|
---|
| 833 | INPUT PARAMETERS:
|
---|
| 834 | Ensemble- ensemble
|
---|
| 835 | XY - test set
|
---|
| 836 | NPoints - test set size
|
---|
| 837 |
|
---|
| 838 | RESULT:
|
---|
| 839 | CrossEntropy/(NPoints*LN(2)).
|
---|
| 840 | Zero if ensemble solves regression task.
|
---|
| 841 |
|
---|
| 842 | -- ALGLIB --
|
---|
| 843 | Copyright 17.02.2009 by Bochkanov Sergey
|
---|
| 844 | *************************************************************************/
|
---|
| 845 | public static double mlpeavgce(ref mlpensemble ensemble,
|
---|
| 846 | ref double[,] xy,
|
---|
| 847 | int npoints)
|
---|
| 848 | {
|
---|
| 849 | double result = 0;
|
---|
| 850 | double relcls = 0;
|
---|
| 851 | double avgce = 0;
|
---|
| 852 | double rms = 0;
|
---|
| 853 | double avg = 0;
|
---|
| 854 | double avgrel = 0;
|
---|
| 855 |
|
---|
| 856 | mlpeallerrors(ref ensemble, ref xy, npoints, ref relcls, ref avgce, ref rms, ref avg, ref avgrel);
|
---|
| 857 | result = avgce;
|
---|
| 858 | return result;
|
---|
| 859 | }
|
---|
| 860 |
|
---|
| 861 |
|
---|
| 862 | /*************************************************************************
|
---|
| 863 | RMS error on the test set
|
---|
| 864 |
|
---|
| 865 | INPUT PARAMETERS:
|
---|
| 866 | Ensemble- ensemble
|
---|
| 867 | XY - test set
|
---|
| 868 | NPoints - test set size
|
---|
| 869 |
|
---|
| 870 | RESULT:
|
---|
| 871 | root mean square error.
|
---|
| 872 | Its meaning for regression task is obvious. As for classification task
|
---|
| 873 | RMS error means error when estimating posterior probabilities.
|
---|
| 874 |
|
---|
| 875 | -- ALGLIB --
|
---|
| 876 | Copyright 17.02.2009 by Bochkanov Sergey
|
---|
| 877 | *************************************************************************/
|
---|
| 878 | public static double mlpermserror(ref mlpensemble ensemble,
|
---|
| 879 | ref double[,] xy,
|
---|
| 880 | int npoints)
|
---|
| 881 | {
|
---|
| 882 | double result = 0;
|
---|
| 883 | double relcls = 0;
|
---|
| 884 | double avgce = 0;
|
---|
| 885 | double rms = 0;
|
---|
| 886 | double avg = 0;
|
---|
| 887 | double avgrel = 0;
|
---|
| 888 |
|
---|
| 889 | mlpeallerrors(ref ensemble, ref xy, npoints, ref relcls, ref avgce, ref rms, ref avg, ref avgrel);
|
---|
| 890 | result = rms;
|
---|
| 891 | return result;
|
---|
| 892 | }
|
---|
| 893 |
|
---|
| 894 |
|
---|
| 895 | /*************************************************************************
|
---|
| 896 | Average error on the test set
|
---|
| 897 |
|
---|
| 898 | INPUT PARAMETERS:
|
---|
| 899 | Ensemble- ensemble
|
---|
| 900 | XY - test set
|
---|
| 901 | NPoints - test set size
|
---|
| 902 |
|
---|
| 903 | RESULT:
|
---|
| 904 | Its meaning for regression task is obvious. As for classification task
|
---|
| 905 | it means average error when estimating posterior probabilities.
|
---|
| 906 |
|
---|
| 907 | -- ALGLIB --
|
---|
| 908 | Copyright 17.02.2009 by Bochkanov Sergey
|
---|
| 909 | *************************************************************************/
|
---|
| 910 | public static double mlpeavgerror(ref mlpensemble ensemble,
|
---|
| 911 | ref double[,] xy,
|
---|
| 912 | int npoints)
|
---|
| 913 | {
|
---|
| 914 | double result = 0;
|
---|
| 915 | double relcls = 0;
|
---|
| 916 | double avgce = 0;
|
---|
| 917 | double rms = 0;
|
---|
| 918 | double avg = 0;
|
---|
| 919 | double avgrel = 0;
|
---|
| 920 |
|
---|
| 921 | mlpeallerrors(ref ensemble, ref xy, npoints, ref relcls, ref avgce, ref rms, ref avg, ref avgrel);
|
---|
| 922 | result = avg;
|
---|
| 923 | return result;
|
---|
| 924 | }
|
---|
| 925 |
|
---|
| 926 |
|
---|
| 927 | /*************************************************************************
|
---|
| 928 | Average relative error on the test set
|
---|
| 929 |
|
---|
| 930 | INPUT PARAMETERS:
|
---|
| 931 | Ensemble- ensemble
|
---|
| 932 | XY - test set
|
---|
| 933 | NPoints - test set size
|
---|
| 934 |
|
---|
| 935 | RESULT:
|
---|
| 936 | Its meaning for regression task is obvious. As for classification task
|
---|
| 937 | it means average relative error when estimating posterior probabilities.
|
---|
| 938 |
|
---|
| 939 | -- ALGLIB --
|
---|
| 940 | Copyright 17.02.2009 by Bochkanov Sergey
|
---|
| 941 | *************************************************************************/
|
---|
| 942 | public static double mlpeavgrelerror(ref mlpensemble ensemble,
|
---|
| 943 | ref double[,] xy,
|
---|
| 944 | int npoints)
|
---|
| 945 | {
|
---|
| 946 | double result = 0;
|
---|
| 947 | double relcls = 0;
|
---|
| 948 | double avgce = 0;
|
---|
| 949 | double rms = 0;
|
---|
| 950 | double avg = 0;
|
---|
| 951 | double avgrel = 0;
|
---|
| 952 |
|
---|
| 953 | mlpeallerrors(ref ensemble, ref xy, npoints, ref relcls, ref avgce, ref rms, ref avg, ref avgrel);
|
---|
| 954 | result = avgrel;
|
---|
| 955 | return result;
|
---|
| 956 | }
|
---|
| 957 |
|
---|
| 958 |
|
---|
| 959 | /*************************************************************************
|
---|
| 960 | Training neural networks ensemble using bootstrap aggregating (bagging).
|
---|
| 961 | Modified Levenberg-Marquardt algorithm is used as base training method.
|
---|
| 962 |
|
---|
| 963 | INPUT PARAMETERS:
|
---|
| 964 | Ensemble - model with initialized geometry
|
---|
| 965 | XY - training set
|
---|
| 966 | NPoints - training set size
|
---|
| 967 | Decay - weight decay coefficient, >=0.001
|
---|
| 968 | Restarts - restarts, >0.
|
---|
| 969 |
|
---|
| 970 | OUTPUT PARAMETERS:
|
---|
| 971 | Ensemble - trained model
|
---|
| 972 | Info - return code:
|
---|
| 973 | * -2, if there is a point with class number
|
---|
| 974 | outside of [0..NClasses-1].
|
---|
| 975 | * -1, if incorrect parameters was passed
|
---|
| 976 | (NPoints<0, Restarts<1).
|
---|
| 977 | * 2, if task has been solved.
|
---|
| 978 | Rep - training report.
|
---|
| 979 | OOBErrors - out-of-bag generalization error estimate
|
---|
| 980 |
|
---|
| 981 | -- ALGLIB --
|
---|
| 982 | Copyright 17.02.2009 by Bochkanov Sergey
|
---|
| 983 | *************************************************************************/
|
---|
| 984 | public static void mlpebagginglm(ref mlpensemble ensemble,
|
---|
| 985 | ref double[,] xy,
|
---|
| 986 | int npoints,
|
---|
| 987 | double decay,
|
---|
| 988 | int restarts,
|
---|
| 989 | ref int info,
|
---|
| 990 | ref mlptrain.mlpreport rep,
|
---|
| 991 | ref mlptrain.mlpcvreport ooberrors)
|
---|
| 992 | {
|
---|
| 993 | mlpebagginginternal(ref ensemble, ref xy, npoints, decay, restarts, 0.0, 0, true, ref info, ref rep, ref ooberrors);
|
---|
| 994 | }
|
---|
| 995 |
|
---|
| 996 |
|
---|
| 997 | /*************************************************************************
|
---|
| 998 | Training neural networks ensemble using bootstrap aggregating (bagging).
|
---|
| 999 | L-BFGS algorithm is used as base training method.
|
---|
| 1000 |
|
---|
| 1001 | INPUT PARAMETERS:
|
---|
| 1002 | Ensemble - model with initialized geometry
|
---|
| 1003 | XY - training set
|
---|
| 1004 | NPoints - training set size
|
---|
| 1005 | Decay - weight decay coefficient, >=0.001
|
---|
| 1006 | Restarts - restarts, >0.
|
---|
| 1007 | WStep - stopping criterion, same as in MLPTrainLBFGS
|
---|
| 1008 | MaxIts - stopping criterion, same as in MLPTrainLBFGS
|
---|
| 1009 |
|
---|
| 1010 | OUTPUT PARAMETERS:
|
---|
| 1011 | Ensemble - trained model
|
---|
| 1012 | Info - return code:
|
---|
| 1013 | * -8, if both WStep=0 and MaxIts=0
|
---|
| 1014 | * -2, if there is a point with class number
|
---|
| 1015 | outside of [0..NClasses-1].
|
---|
| 1016 | * -1, if incorrect parameters was passed
|
---|
| 1017 | (NPoints<0, Restarts<1).
|
---|
| 1018 | * 2, if task has been solved.
|
---|
| 1019 | Rep - training report.
|
---|
| 1020 | OOBErrors - out-of-bag generalization error estimate
|
---|
| 1021 |
|
---|
| 1022 | -- ALGLIB --
|
---|
| 1023 | Copyright 17.02.2009 by Bochkanov Sergey
|
---|
| 1024 | *************************************************************************/
|
---|
| 1025 | public static void mlpebagginglbfgs(ref mlpensemble ensemble,
|
---|
| 1026 | ref double[,] xy,
|
---|
| 1027 | int npoints,
|
---|
| 1028 | double decay,
|
---|
| 1029 | int restarts,
|
---|
| 1030 | double wstep,
|
---|
| 1031 | int maxits,
|
---|
| 1032 | ref int info,
|
---|
| 1033 | ref mlptrain.mlpreport rep,
|
---|
| 1034 | ref mlptrain.mlpcvreport ooberrors)
|
---|
| 1035 | {
|
---|
| 1036 | mlpebagginginternal(ref ensemble, ref xy, npoints, decay, restarts, wstep, maxits, false, ref info, ref rep, ref ooberrors);
|
---|
| 1037 | }
|
---|
| 1038 |
|
---|
| 1039 |
|
---|
| 1040 | /*************************************************************************
|
---|
| 1041 | Training neural networks ensemble using early stopping.
|
---|
| 1042 |
|
---|
| 1043 | INPUT PARAMETERS:
|
---|
| 1044 | Ensemble - model with initialized geometry
|
---|
| 1045 | XY - training set
|
---|
| 1046 | NPoints - training set size
|
---|
| 1047 | Decay - weight decay coefficient, >=0.001
|
---|
| 1048 | Restarts - restarts, >0.
|
---|
| 1049 |
|
---|
| 1050 | OUTPUT PARAMETERS:
|
---|
| 1051 | Ensemble - trained model
|
---|
| 1052 | Info - return code:
|
---|
| 1053 | * -2, if there is a point with class number
|
---|
| 1054 | outside of [0..NClasses-1].
|
---|
| 1055 | * -1, if incorrect parameters was passed
|
---|
| 1056 | (NPoints<0, Restarts<1).
|
---|
| 1057 | * 6, if task has been solved.
|
---|
| 1058 | Rep - training report.
|
---|
| 1059 | OOBErrors - out-of-bag generalization error estimate
|
---|
| 1060 |
|
---|
| 1061 | -- ALGLIB --
|
---|
| 1062 | Copyright 10.03.2009 by Bochkanov Sergey
|
---|
| 1063 | *************************************************************************/
|
---|
| 1064 | public static void mlpetraines(ref mlpensemble ensemble,
|
---|
| 1065 | ref double[,] xy,
|
---|
| 1066 | int npoints,
|
---|
| 1067 | double decay,
|
---|
| 1068 | int restarts,
|
---|
| 1069 | ref int info,
|
---|
| 1070 | ref mlptrain.mlpreport rep)
|
---|
| 1071 | {
|
---|
| 1072 | int i = 0;
|
---|
| 1073 | int k = 0;
|
---|
| 1074 | int ccount = 0;
|
---|
| 1075 | int pcount = 0;
|
---|
| 1076 | double[,] trnxy = new double[0,0];
|
---|
| 1077 | double[,] valxy = new double[0,0];
|
---|
| 1078 | int trnsize = 0;
|
---|
| 1079 | int valsize = 0;
|
---|
| 1080 | mlpbase.multilayerperceptron network = new mlpbase.multilayerperceptron();
|
---|
| 1081 | int tmpinfo = 0;
|
---|
| 1082 | mlptrain.mlpreport tmprep = new mlptrain.mlpreport();
|
---|
| 1083 | int i_ = 0;
|
---|
| 1084 | int i1_ = 0;
|
---|
| 1085 |
|
---|
| 1086 | if( npoints<2 | restarts<1 | (double)(decay)<(double)(0) )
|
---|
| 1087 | {
|
---|
| 1088 | info = -1;
|
---|
| 1089 | return;
|
---|
| 1090 | }
|
---|
| 1091 | if( ensemble.issoftmax )
|
---|
| 1092 | {
|
---|
| 1093 | for(i=0; i<=npoints-1; i++)
|
---|
| 1094 | {
|
---|
| 1095 | if( (int)Math.Round(xy[i,ensemble.nin])<0 | (int)Math.Round(xy[i,ensemble.nin])>=ensemble.nout )
|
---|
| 1096 | {
|
---|
| 1097 | info = -2;
|
---|
| 1098 | return;
|
---|
| 1099 | }
|
---|
| 1100 | }
|
---|
| 1101 | }
|
---|
| 1102 | info = 6;
|
---|
| 1103 |
|
---|
| 1104 | //
|
---|
| 1105 | // allocate
|
---|
| 1106 | //
|
---|
| 1107 | if( ensemble.issoftmax )
|
---|
| 1108 | {
|
---|
| 1109 | ccount = ensemble.nin+1;
|
---|
| 1110 | pcount = ensemble.nin;
|
---|
| 1111 | }
|
---|
| 1112 | else
|
---|
| 1113 | {
|
---|
| 1114 | ccount = ensemble.nin+ensemble.nout;
|
---|
| 1115 | pcount = ensemble.nin+ensemble.nout;
|
---|
| 1116 | }
|
---|
| 1117 | trnxy = new double[npoints-1+1, ccount-1+1];
|
---|
| 1118 | valxy = new double[npoints-1+1, ccount-1+1];
|
---|
| 1119 | mlpbase.mlpunserialize(ref ensemble.serializedmlp, ref network);
|
---|
| 1120 | rep.ngrad = 0;
|
---|
| 1121 | rep.nhess = 0;
|
---|
| 1122 | rep.ncholesky = 0;
|
---|
| 1123 |
|
---|
| 1124 | //
|
---|
| 1125 | // train networks
|
---|
| 1126 | //
|
---|
| 1127 | for(k=0; k<=ensemble.ensemblesize-1; k++)
|
---|
| 1128 | {
|
---|
| 1129 |
|
---|
| 1130 | //
|
---|
| 1131 | // Split set
|
---|
| 1132 | //
|
---|
| 1133 | do
|
---|
| 1134 | {
|
---|
| 1135 | trnsize = 0;
|
---|
| 1136 | valsize = 0;
|
---|
| 1137 | for(i=0; i<=npoints-1; i++)
|
---|
| 1138 | {
|
---|
| 1139 | if( (double)(AP.Math.RandomReal())<(double)(0.66) )
|
---|
| 1140 | {
|
---|
| 1141 |
|
---|
| 1142 | //
|
---|
| 1143 | // Assign sample to training set
|
---|
| 1144 | //
|
---|
| 1145 | for(i_=0; i_<=ccount-1;i_++)
|
---|
| 1146 | {
|
---|
| 1147 | trnxy[trnsize,i_] = xy[i,i_];
|
---|
| 1148 | }
|
---|
| 1149 | trnsize = trnsize+1;
|
---|
| 1150 | }
|
---|
| 1151 | else
|
---|
| 1152 | {
|
---|
| 1153 |
|
---|
| 1154 | //
|
---|
| 1155 | // Assign sample to validation set
|
---|
| 1156 | //
|
---|
| 1157 | for(i_=0; i_<=ccount-1;i_++)
|
---|
| 1158 | {
|
---|
| 1159 | valxy[valsize,i_] = xy[i,i_];
|
---|
| 1160 | }
|
---|
| 1161 | valsize = valsize+1;
|
---|
| 1162 | }
|
---|
| 1163 | }
|
---|
| 1164 | }
|
---|
| 1165 | while( ! (trnsize!=0 & valsize!=0) );
|
---|
| 1166 |
|
---|
| 1167 | //
|
---|
| 1168 | // Train
|
---|
| 1169 | //
|
---|
| 1170 | mlptrain.mlptraines(ref network, ref trnxy, trnsize, ref valxy, valsize, decay, restarts, ref tmpinfo, ref tmprep);
|
---|
| 1171 | if( tmpinfo<0 )
|
---|
| 1172 | {
|
---|
| 1173 | info = tmpinfo;
|
---|
| 1174 | return;
|
---|
| 1175 | }
|
---|
| 1176 |
|
---|
| 1177 | //
|
---|
| 1178 | // save results
|
---|
| 1179 | //
|
---|
| 1180 | i1_ = (0) - (k*ensemble.wcount);
|
---|
| 1181 | for(i_=k*ensemble.wcount; i_<=(k+1)*ensemble.wcount-1;i_++)
|
---|
| 1182 | {
|
---|
| 1183 | ensemble.weights[i_] = network.weights[i_+i1_];
|
---|
| 1184 | }
|
---|
| 1185 | i1_ = (0) - (k*pcount);
|
---|
| 1186 | for(i_=k*pcount; i_<=(k+1)*pcount-1;i_++)
|
---|
| 1187 | {
|
---|
| 1188 | ensemble.columnmeans[i_] = network.columnmeans[i_+i1_];
|
---|
| 1189 | }
|
---|
| 1190 | i1_ = (0) - (k*pcount);
|
---|
| 1191 | for(i_=k*pcount; i_<=(k+1)*pcount-1;i_++)
|
---|
| 1192 | {
|
---|
| 1193 | ensemble.columnsigmas[i_] = network.columnsigmas[i_+i1_];
|
---|
| 1194 | }
|
---|
| 1195 | rep.ngrad = rep.ngrad+tmprep.ngrad;
|
---|
| 1196 | rep.nhess = rep.nhess+tmprep.nhess;
|
---|
| 1197 | rep.ncholesky = rep.ncholesky+tmprep.ncholesky;
|
---|
| 1198 | }
|
---|
| 1199 | }
|
---|
| 1200 |
|
---|
| 1201 |
|
---|
| 1202 | /*************************************************************************
|
---|
| 1203 | Calculation of all types of errors
|
---|
| 1204 |
|
---|
| 1205 | -- ALGLIB --
|
---|
| 1206 | Copyright 17.02.2009 by Bochkanov Sergey
|
---|
| 1207 | *************************************************************************/
|
---|
| 1208 | private static void mlpeallerrors(ref mlpensemble ensemble,
|
---|
| 1209 | ref double[,] xy,
|
---|
| 1210 | int npoints,
|
---|
| 1211 | ref double relcls,
|
---|
| 1212 | ref double avgce,
|
---|
| 1213 | ref double rms,
|
---|
| 1214 | ref double avg,
|
---|
| 1215 | ref double avgrel)
|
---|
| 1216 | {
|
---|
| 1217 | int i = 0;
|
---|
| 1218 | double[] buf = new double[0];
|
---|
| 1219 | double[] workx = new double[0];
|
---|
| 1220 | double[] y = new double[0];
|
---|
| 1221 | double[] dy = new double[0];
|
---|
| 1222 | int i_ = 0;
|
---|
| 1223 | int i1_ = 0;
|
---|
| 1224 |
|
---|
| 1225 | workx = new double[ensemble.nin-1+1];
|
---|
| 1226 | y = new double[ensemble.nout-1+1];
|
---|
| 1227 | if( ensemble.issoftmax )
|
---|
| 1228 | {
|
---|
| 1229 | dy = new double[0+1];
|
---|
| 1230 | bdss.dserrallocate(ensemble.nout, ref buf);
|
---|
| 1231 | }
|
---|
| 1232 | else
|
---|
| 1233 | {
|
---|
| 1234 | dy = new double[ensemble.nout-1+1];
|
---|
| 1235 | bdss.dserrallocate(-ensemble.nout, ref buf);
|
---|
| 1236 | }
|
---|
| 1237 | for(i=0; i<=npoints-1; i++)
|
---|
| 1238 | {
|
---|
| 1239 | for(i_=0; i_<=ensemble.nin-1;i_++)
|
---|
| 1240 | {
|
---|
| 1241 | workx[i_] = xy[i,i_];
|
---|
| 1242 | }
|
---|
| 1243 | mlpeprocess(ref ensemble, ref workx, ref y);
|
---|
| 1244 | if( ensemble.issoftmax )
|
---|
| 1245 | {
|
---|
| 1246 | dy[0] = xy[i,ensemble.nin];
|
---|
| 1247 | }
|
---|
| 1248 | else
|
---|
| 1249 | {
|
---|
| 1250 | i1_ = (ensemble.nin) - (0);
|
---|
| 1251 | for(i_=0; i_<=ensemble.nout-1;i_++)
|
---|
| 1252 | {
|
---|
| 1253 | dy[i_] = xy[i,i_+i1_];
|
---|
| 1254 | }
|
---|
| 1255 | }
|
---|
| 1256 | bdss.dserraccumulate(ref buf, ref y, ref dy);
|
---|
| 1257 | }
|
---|
| 1258 | bdss.dserrfinish(ref buf);
|
---|
| 1259 | relcls = buf[0];
|
---|
| 1260 | avgce = buf[1];
|
---|
| 1261 | rms = buf[2];
|
---|
| 1262 | avg = buf[3];
|
---|
| 1263 | avgrel = buf[4];
|
---|
| 1264 | }
|
---|
| 1265 |
|
---|
| 1266 |
|
---|
| 1267 | /*************************************************************************
|
---|
| 1268 | Internal bagging subroutine.
|
---|
| 1269 |
|
---|
| 1270 | -- ALGLIB --
|
---|
| 1271 | Copyright 19.02.2009 by Bochkanov Sergey
|
---|
| 1272 | *************************************************************************/
|
---|
| 1273 | private static void mlpebagginginternal(ref mlpensemble ensemble,
|
---|
| 1274 | ref double[,] xy,
|
---|
| 1275 | int npoints,
|
---|
| 1276 | double decay,
|
---|
| 1277 | int restarts,
|
---|
| 1278 | double wstep,
|
---|
| 1279 | int maxits,
|
---|
| 1280 | bool lmalgorithm,
|
---|
| 1281 | ref int info,
|
---|
| 1282 | ref mlptrain.mlpreport rep,
|
---|
| 1283 | ref mlptrain.mlpcvreport ooberrors)
|
---|
| 1284 | {
|
---|
| 1285 | double[,] xys = new double[0,0];
|
---|
| 1286 | bool[] s = new bool[0];
|
---|
| 1287 | double[,] oobbuf = new double[0,0];
|
---|
| 1288 | int[] oobcntbuf = new int[0];
|
---|
| 1289 | double[] x = new double[0];
|
---|
| 1290 | double[] y = new double[0];
|
---|
| 1291 | double[] dy = new double[0];
|
---|
| 1292 | double[] dsbuf = new double[0];
|
---|
| 1293 | int nin = 0;
|
---|
| 1294 | int nout = 0;
|
---|
| 1295 | int ccnt = 0;
|
---|
| 1296 | int pcnt = 0;
|
---|
| 1297 | int i = 0;
|
---|
| 1298 | int j = 0;
|
---|
| 1299 | int k = 0;
|
---|
| 1300 | double v = 0;
|
---|
| 1301 | mlptrain.mlpreport tmprep = new mlptrain.mlpreport();
|
---|
| 1302 | mlpbase.multilayerperceptron network = new mlpbase.multilayerperceptron();
|
---|
| 1303 | int i_ = 0;
|
---|
| 1304 | int i1_ = 0;
|
---|
| 1305 |
|
---|
| 1306 |
|
---|
| 1307 | //
|
---|
| 1308 | // Test for inputs
|
---|
| 1309 | //
|
---|
| 1310 | if( !lmalgorithm & (double)(wstep)==(double)(0) & maxits==0 )
|
---|
| 1311 | {
|
---|
| 1312 | info = -8;
|
---|
| 1313 | return;
|
---|
| 1314 | }
|
---|
| 1315 | if( npoints<=0 | restarts<1 | (double)(wstep)<(double)(0) | maxits<0 )
|
---|
| 1316 | {
|
---|
| 1317 | info = -1;
|
---|
| 1318 | return;
|
---|
| 1319 | }
|
---|
| 1320 | if( ensemble.issoftmax )
|
---|
| 1321 | {
|
---|
| 1322 | for(i=0; i<=npoints-1; i++)
|
---|
| 1323 | {
|
---|
| 1324 | if( (int)Math.Round(xy[i,ensemble.nin])<0 | (int)Math.Round(xy[i,ensemble.nin])>=ensemble.nout )
|
---|
| 1325 | {
|
---|
| 1326 | info = -2;
|
---|
| 1327 | return;
|
---|
| 1328 | }
|
---|
| 1329 | }
|
---|
| 1330 | }
|
---|
| 1331 |
|
---|
| 1332 | //
|
---|
| 1333 | // allocate temporaries
|
---|
| 1334 | //
|
---|
| 1335 | info = 2;
|
---|
| 1336 | rep.ngrad = 0;
|
---|
| 1337 | rep.nhess = 0;
|
---|
| 1338 | rep.ncholesky = 0;
|
---|
| 1339 | ooberrors.relclserror = 0;
|
---|
| 1340 | ooberrors.avgce = 0;
|
---|
| 1341 | ooberrors.rmserror = 0;
|
---|
| 1342 | ooberrors.avgerror = 0;
|
---|
| 1343 | ooberrors.avgrelerror = 0;
|
---|
| 1344 | nin = ensemble.nin;
|
---|
| 1345 | nout = ensemble.nout;
|
---|
| 1346 | if( ensemble.issoftmax )
|
---|
| 1347 | {
|
---|
| 1348 | ccnt = nin+1;
|
---|
| 1349 | pcnt = nin;
|
---|
| 1350 | }
|
---|
| 1351 | else
|
---|
| 1352 | {
|
---|
| 1353 | ccnt = nin+nout;
|
---|
| 1354 | pcnt = nin+nout;
|
---|
| 1355 | }
|
---|
| 1356 | xys = new double[npoints-1+1, ccnt-1+1];
|
---|
| 1357 | s = new bool[npoints-1+1];
|
---|
| 1358 | oobbuf = new double[npoints-1+1, nout-1+1];
|
---|
| 1359 | oobcntbuf = new int[npoints-1+1];
|
---|
| 1360 | x = new double[nin-1+1];
|
---|
| 1361 | y = new double[nout-1+1];
|
---|
| 1362 | if( ensemble.issoftmax )
|
---|
| 1363 | {
|
---|
| 1364 | dy = new double[0+1];
|
---|
| 1365 | }
|
---|
| 1366 | else
|
---|
| 1367 | {
|
---|
| 1368 | dy = new double[nout-1+1];
|
---|
| 1369 | }
|
---|
| 1370 | for(i=0; i<=npoints-1; i++)
|
---|
| 1371 | {
|
---|
| 1372 | for(j=0; j<=nout-1; j++)
|
---|
| 1373 | {
|
---|
| 1374 | oobbuf[i,j] = 0;
|
---|
| 1375 | }
|
---|
| 1376 | }
|
---|
| 1377 | for(i=0; i<=npoints-1; i++)
|
---|
| 1378 | {
|
---|
| 1379 | oobcntbuf[i] = 0;
|
---|
| 1380 | }
|
---|
| 1381 | mlpbase.mlpunserialize(ref ensemble.serializedmlp, ref network);
|
---|
| 1382 |
|
---|
| 1383 | //
|
---|
| 1384 | // main bagging cycle
|
---|
| 1385 | //
|
---|
| 1386 | for(k=0; k<=ensemble.ensemblesize-1; k++)
|
---|
| 1387 | {
|
---|
| 1388 |
|
---|
| 1389 | //
|
---|
| 1390 | // prepare dataset
|
---|
| 1391 | //
|
---|
| 1392 | for(i=0; i<=npoints-1; i++)
|
---|
| 1393 | {
|
---|
| 1394 | s[i] = false;
|
---|
| 1395 | }
|
---|
| 1396 | for(i=0; i<=npoints-1; i++)
|
---|
| 1397 | {
|
---|
| 1398 | j = AP.Math.RandomInteger(npoints);
|
---|
| 1399 | s[j] = true;
|
---|
| 1400 | for(i_=0; i_<=ccnt-1;i_++)
|
---|
| 1401 | {
|
---|
| 1402 | xys[i,i_] = xy[j,i_];
|
---|
| 1403 | }
|
---|
| 1404 | }
|
---|
| 1405 |
|
---|
| 1406 | //
|
---|
| 1407 | // train
|
---|
| 1408 | //
|
---|
| 1409 | if( lmalgorithm )
|
---|
| 1410 | {
|
---|
| 1411 | mlptrain.mlptrainlm(ref network, ref xys, npoints, decay, restarts, ref info, ref tmprep);
|
---|
| 1412 | }
|
---|
| 1413 | else
|
---|
| 1414 | {
|
---|
| 1415 | mlptrain.mlptrainlbfgs(ref network, ref xys, npoints, decay, restarts, wstep, maxits, ref info, ref tmprep);
|
---|
| 1416 | }
|
---|
| 1417 | if( info<0 )
|
---|
| 1418 | {
|
---|
| 1419 | return;
|
---|
| 1420 | }
|
---|
| 1421 |
|
---|
| 1422 | //
|
---|
| 1423 | // save results
|
---|
| 1424 | //
|
---|
| 1425 | rep.ngrad = rep.ngrad+tmprep.ngrad;
|
---|
| 1426 | rep.nhess = rep.nhess+tmprep.nhess;
|
---|
| 1427 | rep.ncholesky = rep.ncholesky+tmprep.ncholesky;
|
---|
| 1428 | i1_ = (0) - (k*ensemble.wcount);
|
---|
| 1429 | for(i_=k*ensemble.wcount; i_<=(k+1)*ensemble.wcount-1;i_++)
|
---|
| 1430 | {
|
---|
| 1431 | ensemble.weights[i_] = network.weights[i_+i1_];
|
---|
| 1432 | }
|
---|
| 1433 | i1_ = (0) - (k*pcnt);
|
---|
| 1434 | for(i_=k*pcnt; i_<=(k+1)*pcnt-1;i_++)
|
---|
| 1435 | {
|
---|
| 1436 | ensemble.columnmeans[i_] = network.columnmeans[i_+i1_];
|
---|
| 1437 | }
|
---|
| 1438 | i1_ = (0) - (k*pcnt);
|
---|
| 1439 | for(i_=k*pcnt; i_<=(k+1)*pcnt-1;i_++)
|
---|
| 1440 | {
|
---|
| 1441 | ensemble.columnsigmas[i_] = network.columnsigmas[i_+i1_];
|
---|
| 1442 | }
|
---|
| 1443 |
|
---|
| 1444 | //
|
---|
| 1445 | // OOB estimates
|
---|
| 1446 | //
|
---|
| 1447 | for(i=0; i<=npoints-1; i++)
|
---|
| 1448 | {
|
---|
| 1449 | if( !s[i] )
|
---|
| 1450 | {
|
---|
| 1451 | for(i_=0; i_<=nin-1;i_++)
|
---|
| 1452 | {
|
---|
| 1453 | x[i_] = xy[i,i_];
|
---|
| 1454 | }
|
---|
| 1455 | mlpbase.mlpprocess(ref network, ref x, ref y);
|
---|
| 1456 | for(i_=0; i_<=nout-1;i_++)
|
---|
| 1457 | {
|
---|
| 1458 | oobbuf[i,i_] = oobbuf[i,i_] + y[i_];
|
---|
| 1459 | }
|
---|
| 1460 | oobcntbuf[i] = oobcntbuf[i]+1;
|
---|
| 1461 | }
|
---|
| 1462 | }
|
---|
| 1463 | }
|
---|
| 1464 |
|
---|
| 1465 | //
|
---|
| 1466 | // OOB estimates
|
---|
| 1467 | //
|
---|
| 1468 | if( ensemble.issoftmax )
|
---|
| 1469 | {
|
---|
| 1470 | bdss.dserrallocate(nout, ref dsbuf);
|
---|
| 1471 | }
|
---|
| 1472 | else
|
---|
| 1473 | {
|
---|
| 1474 | bdss.dserrallocate(-nout, ref dsbuf);
|
---|
| 1475 | }
|
---|
| 1476 | for(i=0; i<=npoints-1; i++)
|
---|
| 1477 | {
|
---|
| 1478 | if( oobcntbuf[i]!=0 )
|
---|
| 1479 | {
|
---|
| 1480 | v = (double)(1)/(double)(oobcntbuf[i]);
|
---|
| 1481 | for(i_=0; i_<=nout-1;i_++)
|
---|
| 1482 | {
|
---|
| 1483 | y[i_] = v*oobbuf[i,i_];
|
---|
| 1484 | }
|
---|
| 1485 | if( ensemble.issoftmax )
|
---|
| 1486 | {
|
---|
| 1487 | dy[0] = xy[i,nin];
|
---|
| 1488 | }
|
---|
| 1489 | else
|
---|
| 1490 | {
|
---|
| 1491 | i1_ = (nin) - (0);
|
---|
| 1492 | for(i_=0; i_<=nout-1;i_++)
|
---|
| 1493 | {
|
---|
| 1494 | dy[i_] = v*xy[i,i_+i1_];
|
---|
| 1495 | }
|
---|
| 1496 | }
|
---|
| 1497 | bdss.dserraccumulate(ref dsbuf, ref y, ref dy);
|
---|
| 1498 | }
|
---|
| 1499 | }
|
---|
| 1500 | bdss.dserrfinish(ref dsbuf);
|
---|
| 1501 | ooberrors.relclserror = dsbuf[0];
|
---|
| 1502 | ooberrors.avgce = dsbuf[1];
|
---|
| 1503 | ooberrors.rmserror = dsbuf[2];
|
---|
| 1504 | ooberrors.avgerror = dsbuf[3];
|
---|
| 1505 | ooberrors.avgrelerror = dsbuf[4];
|
---|
| 1506 | }
|
---|
| 1507 | }
|
---|
| 1508 | }
|
---|