alglib 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (255) hide show
  1. data/History.txt +7 -0
  2. data/Manifest.txt +253 -0
  3. data/README.txt +33 -0
  4. data/Rakefile +27 -0
  5. data/ext/Rakefile +24 -0
  6. data/ext/alglib.i +24 -0
  7. data/ext/alglib/Makefile +157 -0
  8. data/ext/alglib/airyf.cpp +372 -0
  9. data/ext/alglib/airyf.h +81 -0
  10. data/ext/alglib/alglib.cpp +8558 -0
  11. data/ext/alglib/alglib_util.cpp +19 -0
  12. data/ext/alglib/alglib_util.h +14 -0
  13. data/ext/alglib/ap.cpp +877 -0
  14. data/ext/alglib/ap.english.html +364 -0
  15. data/ext/alglib/ap.h +666 -0
  16. data/ext/alglib/ap.russian.html +442 -0
  17. data/ext/alglib/apvt.h +754 -0
  18. data/ext/alglib/bdss.cpp +1500 -0
  19. data/ext/alglib/bdss.h +251 -0
  20. data/ext/alglib/bdsvd.cpp +1339 -0
  21. data/ext/alglib/bdsvd.h +164 -0
  22. data/ext/alglib/bessel.cpp +1226 -0
  23. data/ext/alglib/bessel.h +331 -0
  24. data/ext/alglib/betaf.cpp +105 -0
  25. data/ext/alglib/betaf.h +74 -0
  26. data/ext/alglib/bidiagonal.cpp +1328 -0
  27. data/ext/alglib/bidiagonal.h +350 -0
  28. data/ext/alglib/binomialdistr.cpp +247 -0
  29. data/ext/alglib/binomialdistr.h +153 -0
  30. data/ext/alglib/blas.cpp +576 -0
  31. data/ext/alglib/blas.h +132 -0
  32. data/ext/alglib/cblas.cpp +226 -0
  33. data/ext/alglib/cblas.h +57 -0
  34. data/ext/alglib/cdet.cpp +138 -0
  35. data/ext/alglib/cdet.h +92 -0
  36. data/ext/alglib/chebyshev.cpp +216 -0
  37. data/ext/alglib/chebyshev.h +76 -0
  38. data/ext/alglib/chisquaredistr.cpp +157 -0
  39. data/ext/alglib/chisquaredistr.h +144 -0
  40. data/ext/alglib/cholesky.cpp +285 -0
  41. data/ext/alglib/cholesky.h +86 -0
  42. data/ext/alglib/cinverse.cpp +298 -0
  43. data/ext/alglib/cinverse.h +111 -0
  44. data/ext/alglib/clu.cpp +337 -0
  45. data/ext/alglib/clu.h +120 -0
  46. data/ext/alglib/correlation.cpp +280 -0
  47. data/ext/alglib/correlation.h +77 -0
  48. data/ext/alglib/correlationtests.cpp +726 -0
  49. data/ext/alglib/correlationtests.h +134 -0
  50. data/ext/alglib/crcond.cpp +826 -0
  51. data/ext/alglib/crcond.h +148 -0
  52. data/ext/alglib/creflections.cpp +310 -0
  53. data/ext/alglib/creflections.h +165 -0
  54. data/ext/alglib/csolve.cpp +312 -0
  55. data/ext/alglib/csolve.h +99 -0
  56. data/ext/alglib/ctrinverse.cpp +387 -0
  57. data/ext/alglib/ctrinverse.h +98 -0
  58. data/ext/alglib/ctrlinsolve.cpp +297 -0
  59. data/ext/alglib/ctrlinsolve.h +81 -0
  60. data/ext/alglib/dawson.cpp +234 -0
  61. data/ext/alglib/dawson.h +74 -0
  62. data/ext/alglib/descriptivestatistics.cpp +436 -0
  63. data/ext/alglib/descriptivestatistics.h +112 -0
  64. data/ext/alglib/det.cpp +140 -0
  65. data/ext/alglib/det.h +94 -0
  66. data/ext/alglib/dforest.cpp +1819 -0
  67. data/ext/alglib/dforest.h +316 -0
  68. data/ext/alglib/elliptic.cpp +497 -0
  69. data/ext/alglib/elliptic.h +217 -0
  70. data/ext/alglib/estnorm.cpp +429 -0
  71. data/ext/alglib/estnorm.h +107 -0
  72. data/ext/alglib/expintegrals.cpp +422 -0
  73. data/ext/alglib/expintegrals.h +108 -0
  74. data/ext/alglib/faq.english.html +258 -0
  75. data/ext/alglib/faq.russian.html +272 -0
  76. data/ext/alglib/fdistr.cpp +202 -0
  77. data/ext/alglib/fdistr.h +163 -0
  78. data/ext/alglib/fresnel.cpp +211 -0
  79. data/ext/alglib/fresnel.h +91 -0
  80. data/ext/alglib/gammaf.cpp +338 -0
  81. data/ext/alglib/gammaf.h +104 -0
  82. data/ext/alglib/gqgengauss.cpp +235 -0
  83. data/ext/alglib/gqgengauss.h +92 -0
  84. data/ext/alglib/gqgenhermite.cpp +268 -0
  85. data/ext/alglib/gqgenhermite.h +63 -0
  86. data/ext/alglib/gqgenjacobi.cpp +297 -0
  87. data/ext/alglib/gqgenjacobi.h +72 -0
  88. data/ext/alglib/gqgenlaguerre.cpp +265 -0
  89. data/ext/alglib/gqgenlaguerre.h +69 -0
  90. data/ext/alglib/gqgenlegendre.cpp +300 -0
  91. data/ext/alglib/gqgenlegendre.h +62 -0
  92. data/ext/alglib/gqgenlobatto.cpp +305 -0
  93. data/ext/alglib/gqgenlobatto.h +97 -0
  94. data/ext/alglib/gqgenradau.cpp +232 -0
  95. data/ext/alglib/gqgenradau.h +95 -0
  96. data/ext/alglib/hbisinv.cpp +480 -0
  97. data/ext/alglib/hbisinv.h +183 -0
  98. data/ext/alglib/hblas.cpp +228 -0
  99. data/ext/alglib/hblas.h +64 -0
  100. data/ext/alglib/hcholesky.cpp +339 -0
  101. data/ext/alglib/hcholesky.h +91 -0
  102. data/ext/alglib/hermite.cpp +114 -0
  103. data/ext/alglib/hermite.h +49 -0
  104. data/ext/alglib/hessenberg.cpp +370 -0
  105. data/ext/alglib/hessenberg.h +152 -0
  106. data/ext/alglib/hevd.cpp +247 -0
  107. data/ext/alglib/hevd.h +107 -0
  108. data/ext/alglib/hsschur.cpp +1316 -0
  109. data/ext/alglib/hsschur.h +108 -0
  110. data/ext/alglib/htridiagonal.cpp +734 -0
  111. data/ext/alglib/htridiagonal.h +180 -0
  112. data/ext/alglib/ialglib.cpp +6 -0
  113. data/ext/alglib/ialglib.h +9 -0
  114. data/ext/alglib/ibetaf.cpp +960 -0
  115. data/ext/alglib/ibetaf.h +125 -0
  116. data/ext/alglib/igammaf.cpp +430 -0
  117. data/ext/alglib/igammaf.h +157 -0
  118. data/ext/alglib/inv.cpp +274 -0
  119. data/ext/alglib/inv.h +115 -0
  120. data/ext/alglib/inverseupdate.cpp +480 -0
  121. data/ext/alglib/inverseupdate.h +185 -0
  122. data/ext/alglib/jacobianelliptic.cpp +164 -0
  123. data/ext/alglib/jacobianelliptic.h +94 -0
  124. data/ext/alglib/jarquebera.cpp +2271 -0
  125. data/ext/alglib/jarquebera.h +80 -0
  126. data/ext/alglib/kmeans.cpp +356 -0
  127. data/ext/alglib/kmeans.h +76 -0
  128. data/ext/alglib/laguerre.cpp +94 -0
  129. data/ext/alglib/laguerre.h +48 -0
  130. data/ext/alglib/lbfgs.cpp +1167 -0
  131. data/ext/alglib/lbfgs.h +218 -0
  132. data/ext/alglib/lda.cpp +434 -0
  133. data/ext/alglib/lda.h +133 -0
  134. data/ext/alglib/ldlt.cpp +1130 -0
  135. data/ext/alglib/ldlt.h +124 -0
  136. data/ext/alglib/leastsquares.cpp +1252 -0
  137. data/ext/alglib/leastsquares.h +290 -0
  138. data/ext/alglib/legendre.cpp +107 -0
  139. data/ext/alglib/legendre.h +49 -0
  140. data/ext/alglib/linreg.cpp +1185 -0
  141. data/ext/alglib/linreg.h +380 -0
  142. data/ext/alglib/logit.cpp +1523 -0
  143. data/ext/alglib/logit.h +333 -0
  144. data/ext/alglib/lq.cpp +399 -0
  145. data/ext/alglib/lq.h +160 -0
  146. data/ext/alglib/lu.cpp +462 -0
  147. data/ext/alglib/lu.h +119 -0
  148. data/ext/alglib/mannwhitneyu.cpp +4490 -0
  149. data/ext/alglib/mannwhitneyu.h +115 -0
  150. data/ext/alglib/minlm.cpp +918 -0
  151. data/ext/alglib/minlm.h +312 -0
  152. data/ext/alglib/mlpbase.cpp +3375 -0
  153. data/ext/alglib/mlpbase.h +589 -0
  154. data/ext/alglib/mlpe.cpp +1369 -0
  155. data/ext/alglib/mlpe.h +552 -0
  156. data/ext/alglib/mlptrain.cpp +1056 -0
  157. data/ext/alglib/mlptrain.h +283 -0
  158. data/ext/alglib/nearunityunit.cpp +91 -0
  159. data/ext/alglib/nearunityunit.h +17 -0
  160. data/ext/alglib/normaldistr.cpp +377 -0
  161. data/ext/alglib/normaldistr.h +175 -0
  162. data/ext/alglib/nsevd.cpp +1869 -0
  163. data/ext/alglib/nsevd.h +140 -0
  164. data/ext/alglib/pca.cpp +168 -0
  165. data/ext/alglib/pca.h +87 -0
  166. data/ext/alglib/poissondistr.cpp +143 -0
  167. data/ext/alglib/poissondistr.h +130 -0
  168. data/ext/alglib/polinterpolation.cpp +685 -0
  169. data/ext/alglib/polinterpolation.h +206 -0
  170. data/ext/alglib/psif.cpp +173 -0
  171. data/ext/alglib/psif.h +88 -0
  172. data/ext/alglib/qr.cpp +414 -0
  173. data/ext/alglib/qr.h +168 -0
  174. data/ext/alglib/ratinterpolation.cpp +134 -0
  175. data/ext/alglib/ratinterpolation.h +72 -0
  176. data/ext/alglib/rcond.cpp +705 -0
  177. data/ext/alglib/rcond.h +140 -0
  178. data/ext/alglib/reflections.cpp +504 -0
  179. data/ext/alglib/reflections.h +165 -0
  180. data/ext/alglib/rotations.cpp +473 -0
  181. data/ext/alglib/rotations.h +128 -0
  182. data/ext/alglib/rsolve.cpp +221 -0
  183. data/ext/alglib/rsolve.h +99 -0
  184. data/ext/alglib/sbisinv.cpp +217 -0
  185. data/ext/alglib/sbisinv.h +171 -0
  186. data/ext/alglib/sblas.cpp +185 -0
  187. data/ext/alglib/sblas.h +64 -0
  188. data/ext/alglib/schur.cpp +156 -0
  189. data/ext/alglib/schur.h +102 -0
  190. data/ext/alglib/sdet.cpp +193 -0
  191. data/ext/alglib/sdet.h +101 -0
  192. data/ext/alglib/sevd.cpp +116 -0
  193. data/ext/alglib/sevd.h +99 -0
  194. data/ext/alglib/sinverse.cpp +672 -0
  195. data/ext/alglib/sinverse.h +138 -0
  196. data/ext/alglib/spddet.cpp +138 -0
  197. data/ext/alglib/spddet.h +96 -0
  198. data/ext/alglib/spdgevd.cpp +842 -0
  199. data/ext/alglib/spdgevd.h +200 -0
  200. data/ext/alglib/spdinverse.cpp +509 -0
  201. data/ext/alglib/spdinverse.h +122 -0
  202. data/ext/alglib/spdrcond.cpp +421 -0
  203. data/ext/alglib/spdrcond.h +118 -0
  204. data/ext/alglib/spdsolve.cpp +275 -0
  205. data/ext/alglib/spdsolve.h +105 -0
  206. data/ext/alglib/spline2d.cpp +1192 -0
  207. data/ext/alglib/spline2d.h +301 -0
  208. data/ext/alglib/spline3.cpp +1264 -0
  209. data/ext/alglib/spline3.h +290 -0
  210. data/ext/alglib/srcond.cpp +595 -0
  211. data/ext/alglib/srcond.h +127 -0
  212. data/ext/alglib/ssolve.cpp +895 -0
  213. data/ext/alglib/ssolve.h +139 -0
  214. data/ext/alglib/stdafx.h +0 -0
  215. data/ext/alglib/stest.cpp +131 -0
  216. data/ext/alglib/stest.h +94 -0
  217. data/ext/alglib/studenttdistr.cpp +222 -0
  218. data/ext/alglib/studenttdistr.h +115 -0
  219. data/ext/alglib/studentttests.cpp +377 -0
  220. data/ext/alglib/studentttests.h +178 -0
  221. data/ext/alglib/svd.cpp +620 -0
  222. data/ext/alglib/svd.h +126 -0
  223. data/ext/alglib/tdbisinv.cpp +2608 -0
  224. data/ext/alglib/tdbisinv.h +228 -0
  225. data/ext/alglib/tdevd.cpp +1229 -0
  226. data/ext/alglib/tdevd.h +115 -0
  227. data/ext/alglib/tridiagonal.cpp +594 -0
  228. data/ext/alglib/tridiagonal.h +171 -0
  229. data/ext/alglib/trigintegrals.cpp +490 -0
  230. data/ext/alglib/trigintegrals.h +131 -0
  231. data/ext/alglib/trinverse.cpp +345 -0
  232. data/ext/alglib/trinverse.h +98 -0
  233. data/ext/alglib/trlinsolve.cpp +926 -0
  234. data/ext/alglib/trlinsolve.h +73 -0
  235. data/ext/alglib/tsort.cpp +405 -0
  236. data/ext/alglib/tsort.h +54 -0
  237. data/ext/alglib/variancetests.cpp +245 -0
  238. data/ext/alglib/variancetests.h +134 -0
  239. data/ext/alglib/wsr.cpp +6285 -0
  240. data/ext/alglib/wsr.h +96 -0
  241. data/ext/ap.i +97 -0
  242. data/ext/correlation.i +24 -0
  243. data/ext/extconf.rb +6 -0
  244. data/ext/logit.i +89 -0
  245. data/lib/alglib.rb +71 -0
  246. data/lib/alglib/correlation.rb +26 -0
  247. data/lib/alglib/linearregression.rb +63 -0
  248. data/lib/alglib/logit.rb +42 -0
  249. data/test/test_alglib.rb +52 -0
  250. data/test/test_correlation.rb +44 -0
  251. data/test/test_correlationtest.rb +45 -0
  252. data/test/test_linreg.rb +35 -0
  253. data/test/test_logit.rb +43 -0
  254. data/test/test_pca.rb +27 -0
  255. metadata +326 -0
@@ -0,0 +1,1369 @@
1
+ /*************************************************************************
2
+ Copyright (c) 2007-2008, Sergey Bochkanov (ALGLIB project).
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are
6
+ met:
7
+
8
+ - Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+
11
+ - Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer listed
13
+ in this license in the documentation and/or other materials
14
+ provided with the distribution.
15
+
16
+ - Neither the name of the copyright holders nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ *************************************************************************/
32
+
33
+ #include <stdafx.h>
34
+ #include "mlpe.h"
35
+
36
+ static const int mlpntotaloffset = 3;
37
+ static const int mlpevnum = 9;
38
+
39
+ static void mlpeallerrors(mlpensemble& ensemble,
40
+ const ap::real_2d_array& xy,
41
+ int npoints,
42
+ double& relcls,
43
+ double& avgce,
44
+ double& rms,
45
+ double& avg,
46
+ double& avgrel);
47
+ static void mlpebagginginternal(mlpensemble& ensemble,
48
+ const ap::real_2d_array& xy,
49
+ int npoints,
50
+ double decay,
51
+ int restarts,
52
+ double wstep,
53
+ int maxits,
54
+ bool lmalgorithm,
55
+ int& info,
56
+ mlpreport& rep,
57
+ mlpcvreport& ooberrors);
58
+
59
+ /*************************************************************************
60
+ Like MLPCreate0, but for ensembles.
61
+
62
+ -- ALGLIB --
63
+ Copyright 18.02.2009 by Bochkanov Sergey
64
+ *************************************************************************/
65
+ void mlpecreate0(int nin, int nout, int ensemblesize, mlpensemble& ensemble)
66
+ {
67
+ multilayerperceptron net;
68
+
69
+ mlpcreate0(nin, nout, net);
70
+ mlpecreatefromnetwork(net, ensemblesize, ensemble);
71
+ }
72
+
73
+
74
+ /*************************************************************************
75
+ Like MLPCreate1, but for ensembles.
76
+
77
+ -- ALGLIB --
78
+ Copyright 18.02.2009 by Bochkanov Sergey
79
+ *************************************************************************/
80
+ void mlpecreate1(int nin,
81
+ int nhid,
82
+ int nout,
83
+ int ensemblesize,
84
+ mlpensemble& ensemble)
85
+ {
86
+ multilayerperceptron net;
87
+
88
+ mlpcreate1(nin, nhid, nout, net);
89
+ mlpecreatefromnetwork(net, ensemblesize, ensemble);
90
+ }
91
+
92
+
93
+ /*************************************************************************
94
+ Like MLPCreate2, but for ensembles.
95
+
96
+ -- ALGLIB --
97
+ Copyright 18.02.2009 by Bochkanov Sergey
98
+ *************************************************************************/
99
+ void mlpecreate2(int nin,
100
+ int nhid1,
101
+ int nhid2,
102
+ int nout,
103
+ int ensemblesize,
104
+ mlpensemble& ensemble)
105
+ {
106
+ multilayerperceptron net;
107
+
108
+ mlpcreate2(nin, nhid1, nhid2, nout, net);
109
+ mlpecreatefromnetwork(net, ensemblesize, ensemble);
110
+ }
111
+
112
+
113
+ /*************************************************************************
114
+ Like MLPCreateB0, but for ensembles.
115
+
116
+ -- ALGLIB --
117
+ Copyright 18.02.2009 by Bochkanov Sergey
118
+ *************************************************************************/
119
+ void mlpecreateb0(int nin,
120
+ int nout,
121
+ double b,
122
+ double d,
123
+ int ensemblesize,
124
+ mlpensemble& ensemble)
125
+ {
126
+ multilayerperceptron net;
127
+
128
+ mlpcreateb0(nin, nout, b, d, net);
129
+ mlpecreatefromnetwork(net, ensemblesize, ensemble);
130
+ }
131
+
132
+
133
+ /*************************************************************************
134
+ Like MLPCreateB1, but for ensembles.
135
+
136
+ -- ALGLIB --
137
+ Copyright 18.02.2009 by Bochkanov Sergey
138
+ *************************************************************************/
139
+ void mlpecreateb1(int nin,
140
+ int nhid,
141
+ int nout,
142
+ double b,
143
+ double d,
144
+ int ensemblesize,
145
+ mlpensemble& ensemble)
146
+ {
147
+ multilayerperceptron net;
148
+
149
+ mlpcreateb1(nin, nhid, nout, b, d, net);
150
+ mlpecreatefromnetwork(net, ensemblesize, ensemble);
151
+ }
152
+
153
+
154
+ /*************************************************************************
155
+ Like MLPCreateB2, but for ensembles.
156
+
157
+ -- ALGLIB --
158
+ Copyright 18.02.2009 by Bochkanov Sergey
159
+ *************************************************************************/
160
+ void mlpecreateb2(int nin,
161
+ int nhid1,
162
+ int nhid2,
163
+ int nout,
164
+ double b,
165
+ double d,
166
+ int ensemblesize,
167
+ mlpensemble& ensemble)
168
+ {
169
+ multilayerperceptron net;
170
+
171
+ mlpcreateb2(nin, nhid1, nhid2, nout, b, d, net);
172
+ mlpecreatefromnetwork(net, ensemblesize, ensemble);
173
+ }
174
+
175
+
176
+ /*************************************************************************
177
+ Like MLPCreateR0, but for ensembles.
178
+
179
+ -- ALGLIB --
180
+ Copyright 18.02.2009 by Bochkanov Sergey
181
+ *************************************************************************/
182
+ void mlpecreater0(int nin,
183
+ int nout,
184
+ double a,
185
+ double b,
186
+ int ensemblesize,
187
+ mlpensemble& ensemble)
188
+ {
189
+ multilayerperceptron net;
190
+
191
+ mlpcreater0(nin, nout, a, b, net);
192
+ mlpecreatefromnetwork(net, ensemblesize, ensemble);
193
+ }
194
+
195
+
196
+ /*************************************************************************
197
+ Like MLPCreateR1, but for ensembles.
198
+
199
+ -- ALGLIB --
200
+ Copyright 18.02.2009 by Bochkanov Sergey
201
+ *************************************************************************/
202
+ void mlpecreater1(int nin,
203
+ int nhid,
204
+ int nout,
205
+ double a,
206
+ double b,
207
+ int ensemblesize,
208
+ mlpensemble& ensemble)
209
+ {
210
+ multilayerperceptron net;
211
+
212
+ mlpcreater1(nin, nhid, nout, a, b, net);
213
+ mlpecreatefromnetwork(net, ensemblesize, ensemble);
214
+ }
215
+
216
+
217
+ /*************************************************************************
218
+ Like MLPCreateR2, but for ensembles.
219
+
220
+ -- ALGLIB --
221
+ Copyright 18.02.2009 by Bochkanov Sergey
222
+ *************************************************************************/
223
+ void mlpecreater2(int nin,
224
+ int nhid1,
225
+ int nhid2,
226
+ int nout,
227
+ double a,
228
+ double b,
229
+ int ensemblesize,
230
+ mlpensemble& ensemble)
231
+ {
232
+ multilayerperceptron net;
233
+
234
+ mlpcreater2(nin, nhid1, nhid2, nout, a, b, net);
235
+ mlpecreatefromnetwork(net, ensemblesize, ensemble);
236
+ }
237
+
238
+
239
+ /*************************************************************************
240
+ Like MLPCreateC0, but for ensembles.
241
+
242
+ -- ALGLIB --
243
+ Copyright 18.02.2009 by Bochkanov Sergey
244
+ *************************************************************************/
245
+ void mlpecreatec0(int nin, int nout, int ensemblesize, mlpensemble& ensemble)
246
+ {
247
+ multilayerperceptron net;
248
+
249
+ mlpcreatec0(nin, nout, net);
250
+ mlpecreatefromnetwork(net, ensemblesize, ensemble);
251
+ }
252
+
253
+
254
+ /*************************************************************************
255
+ Like MLPCreateC1, but for ensembles.
256
+
257
+ -- ALGLIB --
258
+ Copyright 18.02.2009 by Bochkanov Sergey
259
+ *************************************************************************/
260
+ void mlpecreatec1(int nin,
261
+ int nhid,
262
+ int nout,
263
+ int ensemblesize,
264
+ mlpensemble& ensemble)
265
+ {
266
+ multilayerperceptron net;
267
+
268
+ mlpcreatec1(nin, nhid, nout, net);
269
+ mlpecreatefromnetwork(net, ensemblesize, ensemble);
270
+ }
271
+
272
+
273
+ /*************************************************************************
274
+ Like MLPCreateC2, but for ensembles.
275
+
276
+ -- ALGLIB --
277
+ Copyright 18.02.2009 by Bochkanov Sergey
278
+ *************************************************************************/
279
+ void mlpecreatec2(int nin,
280
+ int nhid1,
281
+ int nhid2,
282
+ int nout,
283
+ int ensemblesize,
284
+ mlpensemble& ensemble)
285
+ {
286
+ multilayerperceptron net;
287
+
288
+ mlpcreatec2(nin, nhid1, nhid2, nout, net);
289
+ mlpecreatefromnetwork(net, ensemblesize, ensemble);
290
+ }
291
+
292
+
293
+ /*************************************************************************
294
+ Creates ensemble from network. Only network geometry is copied.
295
+
296
+ -- ALGLIB --
297
+ Copyright 17.02.2009 by Bochkanov Sergey
298
+ *************************************************************************/
299
+ void mlpecreatefromnetwork(const multilayerperceptron& network,
300
+ int ensemblesize,
301
+ mlpensemble& ensemble)
302
+ {
303
+ int i;
304
+ int ccount;
305
+ int rlen;
306
+
307
+ ap::ap_error::make_assertion(ensemblesize>0, "MLPECreate: incorrect ensemble size!");
308
+
309
+ //
310
+ // network properties
311
+ //
312
+ mlpproperties(network, ensemble.nin, ensemble.nout, ensemble.wcount);
313
+ if( mlpissoftmax(network) )
314
+ {
315
+ ccount = ensemble.nin;
316
+ }
317
+ else
318
+ {
319
+ ccount = ensemble.nin+ensemble.nout;
320
+ }
321
+ ensemble.postprocessing = false;
322
+ ensemble.issoftmax = mlpissoftmax(network);
323
+ ensemble.ensemblesize = ensemblesize;
324
+
325
+ //
326
+ // structure information
327
+ //
328
+ ensemble.structinfo.setbounds(0, network.structinfo(0)-1);
329
+ for(i = 0; i <= network.structinfo(0)-1; i++)
330
+ {
331
+ ensemble.structinfo(i) = network.structinfo(i);
332
+ }
333
+
334
+ //
335
+ // weights, means, sigmas
336
+ //
337
+ ensemble.weights.setbounds(0, ensemblesize*ensemble.wcount-1);
338
+ ensemble.columnmeans.setbounds(0, ensemblesize*ccount-1);
339
+ ensemble.columnsigmas.setbounds(0, ensemblesize*ccount-1);
340
+ for(i = 0; i <= ensemblesize*ensemble.wcount-1; i++)
341
+ {
342
+ ensemble.weights(i) = ap::randomreal()-0.5;
343
+ }
344
+ for(i = 0; i <= ensemblesize-1; i++)
345
+ {
346
+ ap::vmove(&ensemble.columnmeans(i*ccount), &network.columnmeans(0), ap::vlen(i*ccount,(i+1)*ccount-1));
347
+ ap::vmove(&ensemble.columnsigmas(i*ccount), &network.columnsigmas(0), ap::vlen(i*ccount,(i+1)*ccount-1));
348
+ }
349
+
350
+ //
351
+ // serialized part
352
+ //
353
+ mlpserialize(network, ensemble.serializedmlp, ensemble.serializedlen);
354
+
355
+ //
356
+ // temporaries, internal buffers
357
+ //
358
+ ensemble.tmpweights.setbounds(0, ensemble.wcount-1);
359
+ ensemble.tmpmeans.setbounds(0, ccount-1);
360
+ ensemble.tmpsigmas.setbounds(0, ccount-1);
361
+ ensemble.neurons.setbounds(0, ensemble.structinfo(mlpntotaloffset)-1);
362
+ ensemble.dfdnet.setbounds(0, ensemble.structinfo(mlpntotaloffset)-1);
363
+ ensemble.y.setbounds(0, ensemble.nout-1);
364
+ }
365
+
366
+
367
+ /*************************************************************************
368
+ Copying of MLPEnsemble strucure
369
+
370
+ INPUT PARAMETERS:
371
+ Ensemble1 - original
372
+
373
+ OUTPUT PARAMETERS:
374
+ Ensemble2 - copy
375
+
376
+ -- ALGLIB --
377
+ Copyright 17.02.2009 by Bochkanov Sergey
378
+ *************************************************************************/
379
+ void mlpecopy(const mlpensemble& ensemble1, mlpensemble& ensemble2)
380
+ {
381
+ int i;
382
+ int ssize;
383
+ int ccount;
384
+ int ntotal;
385
+ int nin;
386
+ int nout;
387
+ int wcount;
388
+
389
+
390
+ //
391
+ // Unload info
392
+ //
393
+ ssize = ensemble1.structinfo(0);
394
+ if( ensemble1.issoftmax )
395
+ {
396
+ ccount = ensemble1.nin;
397
+ }
398
+ else
399
+ {
400
+ ccount = ensemble1.nin+ensemble1.nout;
401
+ }
402
+ ntotal = ensemble1.structinfo(mlpntotaloffset);
403
+
404
+ //
405
+ // Allocate space
406
+ //
407
+ ensemble2.structinfo.setbounds(0, ssize-1);
408
+ ensemble2.weights.setbounds(0, ensemble1.ensemblesize*ensemble1.wcount-1);
409
+ ensemble2.columnmeans.setbounds(0, ensemble1.ensemblesize*ccount-1);
410
+ ensemble2.columnsigmas.setbounds(0, ensemble1.ensemblesize*ccount-1);
411
+ ensemble2.tmpweights.setbounds(0, ensemble1.wcount-1);
412
+ ensemble2.tmpmeans.setbounds(0, ccount-1);
413
+ ensemble2.tmpsigmas.setbounds(0, ccount-1);
414
+ ensemble2.serializedmlp.setbounds(0, ensemble1.serializedlen-1);
415
+ ensemble2.neurons.setbounds(0, ntotal-1);
416
+ ensemble2.dfdnet.setbounds(0, ntotal-1);
417
+ ensemble2.y.setbounds(0, ensemble1.nout-1);
418
+
419
+ //
420
+ // Copy
421
+ //
422
+ ensemble2.nin = ensemble1.nin;
423
+ ensemble2.nout = ensemble1.nout;
424
+ ensemble2.wcount = ensemble1.wcount;
425
+ ensemble2.ensemblesize = ensemble1.ensemblesize;
426
+ ensemble2.issoftmax = ensemble1.issoftmax;
427
+ ensemble2.postprocessing = ensemble1.postprocessing;
428
+ ensemble2.serializedlen = ensemble1.serializedlen;
429
+ for(i = 0; i <= ssize-1; i++)
430
+ {
431
+ ensemble2.structinfo(i) = ensemble1.structinfo(i);
432
+ }
433
+ ap::vmove(&ensemble2.weights(0), &ensemble1.weights(0), ap::vlen(0,ensemble1.ensemblesize*ensemble1.wcount-1));
434
+ ap::vmove(&ensemble2.columnmeans(0), &ensemble1.columnmeans(0), ap::vlen(0,ensemble1.ensemblesize*ccount-1));
435
+ ap::vmove(&ensemble2.columnsigmas(0), &ensemble1.columnsigmas(0), ap::vlen(0,ensemble1.ensemblesize*ccount-1));
436
+ ap::vmove(&ensemble2.serializedmlp(0), &ensemble1.serializedmlp(0), ap::vlen(0,ensemble1.serializedlen-1));
437
+ }
438
+
439
+
440
+ /*************************************************************************
441
+ Serialization of MLPEnsemble strucure
442
+
443
+ INPUT PARAMETERS:
444
+ Ensemble- original
445
+
446
+ OUTPUT PARAMETERS:
447
+ RA - array of real numbers which stores ensemble,
448
+ array[0..RLen-1]
449
+ RLen - RA lenght
450
+
451
+ -- ALGLIB --
452
+ Copyright 17.02.2009 by Bochkanov Sergey
453
+ *************************************************************************/
454
+ void mlpeserialize(mlpensemble& ensemble, ap::real_1d_array& ra, int& rlen)
455
+ {
456
+ int i;
457
+ int ssize;
458
+ int ntotal;
459
+ int ccount;
460
+ int hsize;
461
+ int offs;
462
+
463
+ hsize = 13;
464
+ ssize = ensemble.structinfo(0);
465
+ if( ensemble.issoftmax )
466
+ {
467
+ ccount = ensemble.nin;
468
+ }
469
+ else
470
+ {
471
+ ccount = ensemble.nin+ensemble.nout;
472
+ }
473
+ ntotal = ensemble.structinfo(mlpntotaloffset);
474
+ rlen = hsize+ssize+ensemble.ensemblesize*ensemble.wcount+2*ccount*ensemble.ensemblesize+ensemble.serializedlen;
475
+
476
+ //
477
+ // RA format:
478
+ // [0] RLen
479
+ // [1] Version (MLPEVNum)
480
+ // [2] EnsembleSize
481
+ // [3] NIn
482
+ // [4] NOut
483
+ // [5] WCount
484
+ // [6] IsSoftmax 0/1
485
+ // [7] PostProcessing 0/1
486
+ // [8] sizeof(StructInfo)
487
+ // [9] NTotal (sizeof(Neurons), sizeof(DFDNET))
488
+ // [10] CCount (sizeof(ColumnMeans), sizeof(ColumnSigmas))
489
+ // [11] data offset
490
+ // [12] SerializedLen
491
+ //
492
+ // [..] StructInfo
493
+ // [..] Weights
494
+ // [..] ColumnMeans
495
+ // [..] ColumnSigmas
496
+ //
497
+ ra.setbounds(0, rlen-1);
498
+ ra(0) = rlen;
499
+ ra(1) = mlpevnum;
500
+ ra(2) = ensemble.ensemblesize;
501
+ ra(3) = ensemble.nin;
502
+ ra(4) = ensemble.nout;
503
+ ra(5) = ensemble.wcount;
504
+ if( ensemble.issoftmax )
505
+ {
506
+ ra(6) = 1;
507
+ }
508
+ else
509
+ {
510
+ ra(6) = 0;
511
+ }
512
+ if( ensemble.postprocessing )
513
+ {
514
+ ra(7) = 1;
515
+ }
516
+ else
517
+ {
518
+ ra(7) = 9;
519
+ }
520
+ ra(8) = ssize;
521
+ ra(9) = ntotal;
522
+ ra(10) = ccount;
523
+ ra(11) = hsize;
524
+ ra(12) = ensemble.serializedlen;
525
+ offs = hsize;
526
+ for(i = offs; i <= offs+ssize-1; i++)
527
+ {
528
+ ra(i) = ensemble.structinfo(i-offs);
529
+ }
530
+ offs = offs+ssize;
531
+ ap::vmove(&ra(offs), &ensemble.weights(0), ap::vlen(offs,offs+ensemble.ensemblesize*ensemble.wcount-1));
532
+ offs = offs+ensemble.ensemblesize*ensemble.wcount;
533
+ ap::vmove(&ra(offs), &ensemble.columnmeans(0), ap::vlen(offs,offs+ensemble.ensemblesize*ccount-1));
534
+ offs = offs+ensemble.ensemblesize*ccount;
535
+ ap::vmove(&ra(offs), &ensemble.columnsigmas(0), ap::vlen(offs,offs+ensemble.ensemblesize*ccount-1));
536
+ offs = offs+ensemble.ensemblesize*ccount;
537
+ ap::vmove(&ra(offs), &ensemble.serializedmlp(0), ap::vlen(offs,offs+ensemble.serializedlen-1));
538
+ offs = offs+ensemble.serializedlen;
539
+ }
540
+
541
+
542
+ /*************************************************************************
543
+ Unserialization of MLPEnsemble strucure
544
+
545
+ INPUT PARAMETERS:
546
+ RA - real array which stores ensemble
547
+
548
+ OUTPUT PARAMETERS:
549
+ Ensemble- restored structure
550
+
551
+ -- ALGLIB --
552
+ Copyright 17.02.2009 by Bochkanov Sergey
553
+ *************************************************************************/
554
+ void mlpeunserialize(const ap::real_1d_array& ra, mlpensemble& ensemble)
555
+ {
556
+ int i;
557
+ int ssize;
558
+ int ntotal;
559
+ int ccount;
560
+ int hsize;
561
+ int offs;
562
+
563
+ ap::ap_error::make_assertion(ap::round(ra(1))==mlpevnum, "MLPEUnserialize: incorrect array!");
564
+
565
+ //
566
+ // load info
567
+ //
568
+ hsize = 13;
569
+ ensemble.ensemblesize = ap::round(ra(2));
570
+ ensemble.nin = ap::round(ra(3));
571
+ ensemble.nout = ap::round(ra(4));
572
+ ensemble.wcount = ap::round(ra(5));
573
+ ensemble.issoftmax = ap::round(ra(6))==1;
574
+ ensemble.postprocessing = ap::round(ra(7))==1;
575
+ ssize = ap::round(ra(8));
576
+ ntotal = ap::round(ra(9));
577
+ ccount = ap::round(ra(10));
578
+ offs = ap::round(ra(11));
579
+ ensemble.serializedlen = ap::round(ra(12));
580
+
581
+ //
582
+ // Allocate arrays
583
+ //
584
+ ensemble.structinfo.setbounds(0, ssize-1);
585
+ ensemble.weights.setbounds(0, ensemble.ensemblesize*ensemble.wcount-1);
586
+ ensemble.columnmeans.setbounds(0, ensemble.ensemblesize*ccount-1);
587
+ ensemble.columnsigmas.setbounds(0, ensemble.ensemblesize*ccount-1);
588
+ ensemble.tmpweights.setbounds(0, ensemble.wcount-1);
589
+ ensemble.tmpmeans.setbounds(0, ccount-1);
590
+ ensemble.tmpsigmas.setbounds(0, ccount-1);
591
+ ensemble.neurons.setbounds(0, ntotal-1);
592
+ ensemble.dfdnet.setbounds(0, ntotal-1);
593
+ ensemble.serializedmlp.setbounds(0, ensemble.serializedlen-1);
594
+ ensemble.y.setbounds(0, ensemble.nout-1);
595
+
596
+ //
597
+ // load data
598
+ //
599
+ for(i = offs; i <= offs+ssize-1; i++)
600
+ {
601
+ ensemble.structinfo(i-offs) = ap::round(ra(i));
602
+ }
603
+ offs = offs+ssize;
604
+ ap::vmove(&ensemble.weights(0), &ra(offs), ap::vlen(0,ensemble.ensemblesize*ensemble.wcount-1));
605
+ offs = offs+ensemble.ensemblesize*ensemble.wcount;
606
+ ap::vmove(&ensemble.columnmeans(0), &ra(offs), ap::vlen(0,ensemble.ensemblesize*ccount-1));
607
+ offs = offs+ensemble.ensemblesize*ccount;
608
+ ap::vmove(&ensemble.columnsigmas(0), &ra(offs), ap::vlen(0,ensemble.ensemblesize*ccount-1));
609
+ offs = offs+ensemble.ensemblesize*ccount;
610
+ ap::vmove(&ensemble.serializedmlp(0), &ra(offs), ap::vlen(0,ensemble.serializedlen-1));
611
+ offs = offs+ensemble.serializedlen;
612
+ }
613
+
614
+
615
+ /*************************************************************************
616
+ Randomization of MLP ensemble
617
+
618
+ -- ALGLIB --
619
+ Copyright 17.02.2009 by Bochkanov Sergey
620
+ *************************************************************************/
621
+ void mlperandomize(mlpensemble& ensemble)
622
+ {
623
+ int i;
624
+
625
+ for(i = 0; i <= ensemble.ensemblesize*ensemble.wcount-1; i++)
626
+ {
627
+ ensemble.weights(i) = ap::randomreal()-0.5;
628
+ }
629
+ }
630
+
631
+
632
+ /*************************************************************************
633
+ Return ensemble properties (number of inputs and outputs).
634
+
635
+ -- ALGLIB --
636
+ Copyright 17.02.2009 by Bochkanov Sergey
637
+ *************************************************************************/
638
+ void mlpeproperties(const mlpensemble& ensemble, int& nin, int& nout)
639
+ {
640
+
641
+ nin = ensemble.nin;
642
+ nout = ensemble.nout;
643
+ }
644
+
645
+
646
+ /*************************************************************************
647
+ Return normalization type (whether ensemble is SOFTMAX-normalized or not).
648
+
649
+ -- ALGLIB --
650
+ Copyright 17.02.2009 by Bochkanov Sergey
651
+ *************************************************************************/
652
+ bool mlpeissoftmax(const mlpensemble& ensemble)
653
+ {
654
+ bool result;
655
+
656
+ result = ensemble.issoftmax;
657
+ return result;
658
+ }
659
+
660
+
661
+ /*************************************************************************
662
+ Procesing
663
+
664
+ INPUT PARAMETERS:
665
+ Ensemble- neural networks ensemble
666
+ X - input vector, array[0..NIn-1].
667
+
668
+ OUTPUT PARAMETERS:
669
+ Y - result. Regression estimate when solving regression task,
670
+ vector of posterior probabilities for classification task.
671
+ Subroutine does not allocate memory for this vector, it is
672
+ responsibility of a caller to allocate it. Array must be
673
+ at least [0..NOut-1].
674
+
675
+ -- ALGLIB --
676
+ Copyright 17.02.2009 by Bochkanov Sergey
677
+ *************************************************************************/
678
+ void mlpeprocess(mlpensemble& ensemble,
679
+ const ap::real_1d_array& x,
680
+ ap::real_1d_array& y)
681
+ {
682
+ int i;
683
+ int es;
684
+ int wc;
685
+ int cc;
686
+ double v;
687
+
688
+ es = ensemble.ensemblesize;
689
+ wc = ensemble.wcount;
690
+ if( ensemble.issoftmax )
691
+ {
692
+ cc = ensemble.nin;
693
+ }
694
+ else
695
+ {
696
+ cc = ensemble.nin+ensemble.nout;
697
+ }
698
+ v = double(1)/double(es);
699
+ for(i = 0; i <= ensemble.nout-1; i++)
700
+ {
701
+ y(i) = 0;
702
+ }
703
+ for(i = 0; i <= es-1; i++)
704
+ {
705
+ ap::vmove(&ensemble.tmpweights(0), &ensemble.weights(i*wc), ap::vlen(0,wc-1));
706
+ ap::vmove(&ensemble.tmpmeans(0), &ensemble.columnmeans(i*cc), ap::vlen(0,cc-1));
707
+ ap::vmove(&ensemble.tmpsigmas(0), &ensemble.columnsigmas(i*cc), ap::vlen(0,cc-1));
708
+ mlpinternalprocessvector(ensemble.structinfo, ensemble.tmpweights, ensemble.tmpmeans, ensemble.tmpsigmas, ensemble.neurons, ensemble.dfdnet, x, ensemble.y);
709
+ ap::vadd(&y(0), &ensemble.y(0), ap::vlen(0,ensemble.nout-1), v);
710
+ }
711
+ }
712
+
713
+
714
+ /*************************************************************************
715
+ Relative classification error on the test set
716
+
717
+ INPUT PARAMETERS:
718
+ Ensemble- ensemble
719
+ XY - test set
720
+ NPoints - test set size
721
+
722
+ RESULT:
723
+ percent of incorrectly classified cases.
724
+ Works both for classifier betwork and for regression networks which
725
+ are used as classifiers.
726
+
727
+ -- ALGLIB --
728
+ Copyright 17.02.2009 by Bochkanov Sergey
729
+ *************************************************************************/
730
+ double mlperelclserror(mlpensemble& ensemble,
731
+ const ap::real_2d_array& xy,
732
+ int npoints)
733
+ {
734
+ double result;
735
+ double relcls;
736
+ double avgce;
737
+ double rms;
738
+ double avg;
739
+ double avgrel;
740
+
741
+ mlpeallerrors(ensemble, xy, npoints, relcls, avgce, rms, avg, avgrel);
742
+ result = relcls;
743
+ return result;
744
+ }
745
+
746
+
747
+ /*************************************************************************
748
+ Average cross-entropy (in bits per element) on the test set
749
+
750
+ INPUT PARAMETERS:
751
+ Ensemble- ensemble
752
+ XY - test set
753
+ NPoints - test set size
754
+
755
+ RESULT:
756
+ CrossEntropy/(NPoints*LN(2)).
757
+ Zero if ensemble solves regression task.
758
+
759
+ -- ALGLIB --
760
+ Copyright 17.02.2009 by Bochkanov Sergey
761
+ *************************************************************************/
762
+ double mlpeavgce(mlpensemble& ensemble,
763
+ const ap::real_2d_array& xy,
764
+ int npoints)
765
+ {
766
+ double result;
767
+ double relcls;
768
+ double avgce;
769
+ double rms;
770
+ double avg;
771
+ double avgrel;
772
+
773
+ mlpeallerrors(ensemble, xy, npoints, relcls, avgce, rms, avg, avgrel);
774
+ result = avgce;
775
+ return result;
776
+ }
777
+
778
+
779
+ /*************************************************************************
780
+ RMS error on the test set
781
+
782
+ INPUT PARAMETERS:
783
+ Ensemble- ensemble
784
+ XY - test set
785
+ NPoints - test set size
786
+
787
+ RESULT:
788
+ root mean square error.
789
+ Its meaning for regression task is obvious. As for classification task
790
+ RMS error means error when estimating posterior probabilities.
791
+
792
+ -- ALGLIB --
793
+ Copyright 17.02.2009 by Bochkanov Sergey
794
+ *************************************************************************/
795
+ double mlpermserror(mlpensemble& ensemble,
796
+ const ap::real_2d_array& xy,
797
+ int npoints)
798
+ {
799
+ double result;
800
+ double relcls;
801
+ double avgce;
802
+ double rms;
803
+ double avg;
804
+ double avgrel;
805
+
806
+ mlpeallerrors(ensemble, xy, npoints, relcls, avgce, rms, avg, avgrel);
807
+ result = rms;
808
+ return result;
809
+ }
810
+
811
+
812
+ /*************************************************************************
813
+ Average error on the test set
814
+
815
+ INPUT PARAMETERS:
816
+ Ensemble- ensemble
817
+ XY - test set
818
+ NPoints - test set size
819
+
820
+ RESULT:
821
+ Its meaning for regression task is obvious. As for classification task
822
+ it means average error when estimating posterior probabilities.
823
+
824
+ -- ALGLIB --
825
+ Copyright 17.02.2009 by Bochkanov Sergey
826
+ *************************************************************************/
827
+ double mlpeavgerror(mlpensemble& ensemble,
828
+ const ap::real_2d_array& xy,
829
+ int npoints)
830
+ {
831
+ double result;
832
+ double relcls;
833
+ double avgce;
834
+ double rms;
835
+ double avg;
836
+ double avgrel;
837
+
838
+ mlpeallerrors(ensemble, xy, npoints, relcls, avgce, rms, avg, avgrel);
839
+ result = avg;
840
+ return result;
841
+ }
842
+
843
+
844
+ /*************************************************************************
845
+ Average relative error on the test set
846
+
847
+ INPUT PARAMETERS:
848
+ Ensemble- ensemble
849
+ XY - test set
850
+ NPoints - test set size
851
+
852
+ RESULT:
853
+ Its meaning for regression task is obvious. As for classification task
854
+ it means average relative error when estimating posterior probabilities.
855
+
856
+ -- ALGLIB --
857
+ Copyright 17.02.2009 by Bochkanov Sergey
858
+ *************************************************************************/
859
+ double mlpeavgrelerror(mlpensemble& ensemble,
860
+ const ap::real_2d_array& xy,
861
+ int npoints)
862
+ {
863
+ double result;
864
+ double relcls;
865
+ double avgce;
866
+ double rms;
867
+ double avg;
868
+ double avgrel;
869
+
870
+ mlpeallerrors(ensemble, xy, npoints, relcls, avgce, rms, avg, avgrel);
871
+ result = avgrel;
872
+ return result;
873
+ }
874
+
875
+
876
+ /*************************************************************************
877
+ Training neural networks ensemble using bootstrap aggregating (bagging).
878
+ Modified Levenberg-Marquardt algorithm is used as base training method.
879
+
880
+ INPUT PARAMETERS:
881
+ Ensemble - model with initialized geometry
882
+ XY - training set
883
+ NPoints - training set size
884
+ Decay - weight decay coefficient, >=0.001
885
+ Restarts - restarts, >0.
886
+
887
+ OUTPUT PARAMETERS:
888
+ Ensemble - trained model
889
+ Info - return code:
890
+ * -2, if there is a point with class number
891
+ outside of [0..NClasses-1].
892
+ * -1, if incorrect parameters was passed
893
+ (NPoints<0, Restarts<1).
894
+ * 2, if task has been solved.
895
+ Rep - training report.
896
+ OOBErrors - out-of-bag generalization error estimate
897
+
898
+ -- ALGLIB --
899
+ Copyright 17.02.2009 by Bochkanov Sergey
900
+ *************************************************************************/
901
+ void mlpebagginglm(mlpensemble& ensemble,
902
+ const ap::real_2d_array& xy,
903
+ int npoints,
904
+ double decay,
905
+ int restarts,
906
+ int& info,
907
+ mlpreport& rep,
908
+ mlpcvreport& ooberrors)
909
+ {
910
+
911
+ mlpebagginginternal(ensemble, xy, npoints, decay, restarts, 0.0, 0, true, info, rep, ooberrors);
912
+ }
913
+
914
+
915
+ /*************************************************************************
916
+ Training neural networks ensemble using bootstrap aggregating (bagging).
917
+ L-BFGS algorithm is used as base training method.
918
+
919
+ INPUT PARAMETERS:
920
+ Ensemble - model with initialized geometry
921
+ XY - training set
922
+ NPoints - training set size
923
+ Decay - weight decay coefficient, >=0.001
924
+ Restarts - restarts, >0.
925
+ WStep - stopping criterion, same as in MLPTrainLBFGS
926
+ MaxIts - stopping criterion, same as in MLPTrainLBFGS
927
+
928
+ OUTPUT PARAMETERS:
929
+ Ensemble - trained model
930
+ Info - return code:
931
+ * -8, if both WStep=0 and MaxIts=0
932
+ * -2, if there is a point with class number
933
+ outside of [0..NClasses-1].
934
+ * -1, if incorrect parameters was passed
935
+ (NPoints<0, Restarts<1).
936
+ * 2, if task has been solved.
937
+ Rep - training report.
938
+ OOBErrors - out-of-bag generalization error estimate
939
+
940
+ -- ALGLIB --
941
+ Copyright 17.02.2009 by Bochkanov Sergey
942
+ *************************************************************************/
943
+ void mlpebagginglbfgs(mlpensemble& ensemble,
944
+ const ap::real_2d_array& xy,
945
+ int npoints,
946
+ double decay,
947
+ int restarts,
948
+ double wstep,
949
+ int maxits,
950
+ int& info,
951
+ mlpreport& rep,
952
+ mlpcvreport& ooberrors)
953
+ {
954
+
955
+ mlpebagginginternal(ensemble, xy, npoints, decay, restarts, wstep, maxits, false, info, rep, ooberrors);
956
+ }
957
+
958
+
959
+ /*************************************************************************
960
+ Training neural networks ensemble using early stopping.
961
+
962
+ INPUT PARAMETERS:
963
+ Ensemble - model with initialized geometry
964
+ XY - training set
965
+ NPoints - training set size
966
+ Decay - weight decay coefficient, >=0.001
967
+ Restarts - restarts, >0.
968
+
969
+ OUTPUT PARAMETERS:
970
+ Ensemble - trained model
971
+ Info - return code:
972
+ * -2, if there is a point with class number
973
+ outside of [0..NClasses-1].
974
+ * -1, if incorrect parameters was passed
975
+ (NPoints<0, Restarts<1).
976
+ * 2, if task has been solved.
977
+ Rep - training report.
978
+ OOBErrors - out-of-bag generalization error estimate
979
+
980
+ -- ALGLIB --
981
+ Copyright 10.03.2009 by Bochkanov Sergey
982
+ *************************************************************************/
983
+ void mlpetraines(mlpensemble& ensemble,
984
+ const ap::real_2d_array& xy,
985
+ int npoints,
986
+ double decay,
987
+ int restarts,
988
+ int& info,
989
+ mlpreport& rep)
990
+ {
991
+ int i;
992
+ int k;
993
+ int ccount;
994
+ int pcount;
995
+ ap::real_2d_array trnxy;
996
+ ap::real_2d_array valxy;
997
+ int trnsize;
998
+ int valsize;
999
+ multilayerperceptron network;
1000
+ int tmpinfo;
1001
+ mlpreport tmprep;
1002
+
1003
+ if( npoints<2||restarts<1||decay<0 )
1004
+ {
1005
+ info = -1;
1006
+ return;
1007
+ }
1008
+ if( ensemble.issoftmax )
1009
+ {
1010
+ for(i = 0; i <= npoints-1; i++)
1011
+ {
1012
+ if( ap::round(xy(i,ensemble.nin))<0||ap::round(xy(i,ensemble.nin))>=ensemble.nout )
1013
+ {
1014
+ info = -2;
1015
+ return;
1016
+ }
1017
+ }
1018
+ }
1019
+ info = 6;
1020
+
1021
+ //
1022
+ // allocate
1023
+ //
1024
+ if( ensemble.issoftmax )
1025
+ {
1026
+ ccount = ensemble.nin+1;
1027
+ pcount = ensemble.nin;
1028
+ }
1029
+ else
1030
+ {
1031
+ ccount = ensemble.nin+ensemble.nout;
1032
+ pcount = ensemble.nin+ensemble.nout;
1033
+ }
1034
+ trnxy.setbounds(0, npoints-1, 0, ccount-1);
1035
+ valxy.setbounds(0, npoints-1, 0, ccount-1);
1036
+ mlpunserialize(ensemble.serializedmlp, network);
1037
+ rep.ngrad = 0;
1038
+ rep.nhess = 0;
1039
+ rep.ncholesky = 0;
1040
+
1041
+ //
1042
+ // train networks
1043
+ //
1044
+ for(k = 0; k <= ensemble.ensemblesize-1; k++)
1045
+ {
1046
+
1047
+ //
1048
+ // Split set
1049
+ //
1050
+ do
1051
+ {
1052
+ trnsize = 0;
1053
+ valsize = 0;
1054
+ for(i = 0; i <= npoints-1; i++)
1055
+ {
1056
+ if( ap::randomreal()<0.66 )
1057
+ {
1058
+
1059
+ //
1060
+ // Assign sample to training set
1061
+ //
1062
+ ap::vmove(&trnxy(trnsize, 0), &xy(i, 0), ap::vlen(0,ccount-1));
1063
+ trnsize = trnsize+1;
1064
+ }
1065
+ else
1066
+ {
1067
+
1068
+ //
1069
+ // Assign sample to validation set
1070
+ //
1071
+ ap::vmove(&valxy(valsize, 0), &xy(i, 0), ap::vlen(0,ccount-1));
1072
+ valsize = valsize+1;
1073
+ }
1074
+ }
1075
+ }
1076
+ while(!(trnsize!=0&&valsize!=0));
1077
+
1078
+ //
1079
+ // Train
1080
+ //
1081
+ mlptraines(network, trnxy, trnsize, valxy, valsize, decay, restarts, tmpinfo, tmprep);
1082
+ if( tmpinfo<0 )
1083
+ {
1084
+ info = tmpinfo;
1085
+ return;
1086
+ }
1087
+
1088
+ //
1089
+ // save results
1090
+ //
1091
+ ap::vmove(&ensemble.weights(k*ensemble.wcount), &network.weights(0), ap::vlen(k*ensemble.wcount,(k+1)*ensemble.wcount-1));
1092
+ ap::vmove(&ensemble.columnmeans(k*pcount), &network.columnmeans(0), ap::vlen(k*pcount,(k+1)*pcount-1));
1093
+ ap::vmove(&ensemble.columnsigmas(k*pcount), &network.columnsigmas(0), ap::vlen(k*pcount,(k+1)*pcount-1));
1094
+ rep.ngrad = rep.ngrad+tmprep.ngrad;
1095
+ rep.nhess = rep.nhess+tmprep.nhess;
1096
+ rep.ncholesky = rep.ncholesky+tmprep.ncholesky;
1097
+ }
1098
+ }
1099
+
1100
+
1101
+ /*************************************************************************
1102
+ Calculation of all types of errors
1103
+
1104
+ -- ALGLIB --
1105
+ Copyright 17.02.2009 by Bochkanov Sergey
1106
+ *************************************************************************/
1107
+ static void mlpeallerrors(mlpensemble& ensemble,
1108
+ const ap::real_2d_array& xy,
1109
+ int npoints,
1110
+ double& relcls,
1111
+ double& avgce,
1112
+ double& rms,
1113
+ double& avg,
1114
+ double& avgrel)
1115
+ {
1116
+ int i;
1117
+ ap::real_1d_array buf;
1118
+ ap::real_1d_array workx;
1119
+ ap::real_1d_array y;
1120
+ ap::real_1d_array dy;
1121
+
1122
+ workx.setbounds(0, ensemble.nin-1);
1123
+ y.setbounds(0, ensemble.nout-1);
1124
+ if( ensemble.issoftmax )
1125
+ {
1126
+ dy.setbounds(0, 0);
1127
+ dserrallocate(ensemble.nout, buf);
1128
+ }
1129
+ else
1130
+ {
1131
+ dy.setbounds(0, ensemble.nout-1);
1132
+ dserrallocate(-ensemble.nout, buf);
1133
+ }
1134
+ for(i = 0; i <= npoints-1; i++)
1135
+ {
1136
+ ap::vmove(&workx(0), &xy(i, 0), ap::vlen(0,ensemble.nin-1));
1137
+ mlpeprocess(ensemble, workx, y);
1138
+ if( ensemble.issoftmax )
1139
+ {
1140
+ dy(0) = xy(i,ensemble.nin);
1141
+ }
1142
+ else
1143
+ {
1144
+ ap::vmove(&dy(0), &xy(i, ensemble.nin), ap::vlen(0,ensemble.nout-1));
1145
+ }
1146
+ dserraccumulate(buf, y, dy);
1147
+ }
1148
+ dserrfinish(buf);
1149
+ relcls = buf(0);
1150
+ avgce = buf(1);
1151
+ rms = buf(2);
1152
+ avg = buf(3);
1153
+ avgrel = buf(4);
1154
+ }
1155
+
1156
+
1157
+ /*************************************************************************
1158
+ Internal bagging subroutine.
1159
+
1160
+ -- ALGLIB --
1161
+ Copyright 19.02.2009 by Bochkanov Sergey
1162
+ *************************************************************************/
1163
+ static void mlpebagginginternal(mlpensemble& ensemble,
1164
+ const ap::real_2d_array& xy,
1165
+ int npoints,
1166
+ double decay,
1167
+ int restarts,
1168
+ double wstep,
1169
+ int maxits,
1170
+ bool lmalgorithm,
1171
+ int& info,
1172
+ mlpreport& rep,
1173
+ mlpcvreport& ooberrors)
1174
+ {
1175
+ ap::real_2d_array xys;
1176
+ ap::boolean_1d_array s;
1177
+ ap::real_2d_array oobbuf;
1178
+ ap::integer_1d_array oobcntbuf;
1179
+ ap::real_1d_array x;
1180
+ ap::real_1d_array y;
1181
+ ap::real_1d_array dy;
1182
+ ap::real_1d_array dsbuf;
1183
+ int nin;
1184
+ int nout;
1185
+ int ccnt;
1186
+ int pcnt;
1187
+ int i;
1188
+ int j;
1189
+ int k;
1190
+ double v;
1191
+ mlpreport tmprep;
1192
+ multilayerperceptron network;
1193
+
1194
+
1195
+ //
1196
+ // Test for inputs
1197
+ //
1198
+ if( !lmalgorithm&&wstep==0&&maxits==0 )
1199
+ {
1200
+ info = -8;
1201
+ return;
1202
+ }
1203
+ if( npoints<=0||restarts<1||wstep<0||maxits<0 )
1204
+ {
1205
+ info = -1;
1206
+ return;
1207
+ }
1208
+ if( ensemble.issoftmax )
1209
+ {
1210
+ for(i = 0; i <= npoints-1; i++)
1211
+ {
1212
+ if( ap::round(xy(i,ensemble.nin))<0||ap::round(xy(i,ensemble.nin))>=ensemble.nout )
1213
+ {
1214
+ info = -2;
1215
+ return;
1216
+ }
1217
+ }
1218
+ }
1219
+
1220
+ //
1221
+ // allocate temporaries
1222
+ //
1223
+ info = 2;
1224
+ rep.ngrad = 0;
1225
+ rep.nhess = 0;
1226
+ rep.ncholesky = 0;
1227
+ ooberrors.relclserror = 0;
1228
+ ooberrors.avgce = 0;
1229
+ ooberrors.rmserror = 0;
1230
+ ooberrors.avgerror = 0;
1231
+ ooberrors.avgrelerror = 0;
1232
+ nin = ensemble.nin;
1233
+ nout = ensemble.nout;
1234
+ if( ensemble.issoftmax )
1235
+ {
1236
+ ccnt = nin+1;
1237
+ pcnt = nin;
1238
+ }
1239
+ else
1240
+ {
1241
+ ccnt = nin+nout;
1242
+ pcnt = nin+nout;
1243
+ }
1244
+ xys.setbounds(0, npoints-1, 0, ccnt-1);
1245
+ s.setbounds(0, npoints-1);
1246
+ oobbuf.setbounds(0, npoints-1, 0, nout-1);
1247
+ oobcntbuf.setbounds(0, npoints-1);
1248
+ x.setbounds(0, nin-1);
1249
+ y.setbounds(0, nout-1);
1250
+ if( ensemble.issoftmax )
1251
+ {
1252
+ dy.setbounds(0, 0);
1253
+ }
1254
+ else
1255
+ {
1256
+ dy.setbounds(0, nout-1);
1257
+ }
1258
+ for(i = 0; i <= npoints-1; i++)
1259
+ {
1260
+ for(j = 0; j <= nout-1; j++)
1261
+ {
1262
+ oobbuf(i,j) = 0;
1263
+ }
1264
+ }
1265
+ for(i = 0; i <= npoints-1; i++)
1266
+ {
1267
+ oobcntbuf(i) = 0;
1268
+ }
1269
+ mlpunserialize(ensemble.serializedmlp, network);
1270
+
1271
+ //
1272
+ // main bagging cycle
1273
+ //
1274
+ for(k = 0; k <= ensemble.ensemblesize-1; k++)
1275
+ {
1276
+
1277
+ //
1278
+ // prepare dataset
1279
+ //
1280
+ for(i = 0; i <= npoints-1; i++)
1281
+ {
1282
+ s(i) = false;
1283
+ }
1284
+ for(i = 0; i <= npoints-1; i++)
1285
+ {
1286
+ j = ap::randominteger(npoints);
1287
+ s(j) = true;
1288
+ ap::vmove(&xys(i, 0), &xy(j, 0), ap::vlen(0,ccnt-1));
1289
+ }
1290
+
1291
+ //
1292
+ // train
1293
+ //
1294
+ if( lmalgorithm )
1295
+ {
1296
+ mlptrainlm(network, xys, npoints, decay, restarts, info, tmprep);
1297
+ }
1298
+ else
1299
+ {
1300
+ mlptrainlbfgs(network, xys, npoints, decay, restarts, wstep, maxits, info, tmprep);
1301
+ }
1302
+ if( info<0 )
1303
+ {
1304
+ return;
1305
+ }
1306
+
1307
+ //
1308
+ // save results
1309
+ //
1310
+ rep.ngrad = rep.ngrad+tmprep.ngrad;
1311
+ rep.nhess = rep.nhess+tmprep.nhess;
1312
+ rep.ncholesky = rep.ncholesky+tmprep.ncholesky;
1313
+ ap::vmove(&ensemble.weights(k*ensemble.wcount), &network.weights(0), ap::vlen(k*ensemble.wcount,(k+1)*ensemble.wcount-1));
1314
+ ap::vmove(&ensemble.columnmeans(k*pcnt), &network.columnmeans(0), ap::vlen(k*pcnt,(k+1)*pcnt-1));
1315
+ ap::vmove(&ensemble.columnsigmas(k*pcnt), &network.columnsigmas(0), ap::vlen(k*pcnt,(k+1)*pcnt-1));
1316
+
1317
+ //
1318
+ // OOB estimates
1319
+ //
1320
+ for(i = 0; i <= npoints-1; i++)
1321
+ {
1322
+ if( !s(i) )
1323
+ {
1324
+ ap::vmove(&x(0), &xy(i, 0), ap::vlen(0,nin-1));
1325
+ mlpprocess(network, x, y);
1326
+ ap::vadd(&oobbuf(i, 0), &y(0), ap::vlen(0,nout-1));
1327
+ oobcntbuf(i) = oobcntbuf(i)+1;
1328
+ }
1329
+ }
1330
+ }
1331
+
1332
+ //
1333
+ // OOB estimates
1334
+ //
1335
+ if( ensemble.issoftmax )
1336
+ {
1337
+ dserrallocate(nout, dsbuf);
1338
+ }
1339
+ else
1340
+ {
1341
+ dserrallocate(-nout, dsbuf);
1342
+ }
1343
+ for(i = 0; i <= npoints-1; i++)
1344
+ {
1345
+ if( oobcntbuf(i)!=0 )
1346
+ {
1347
+ v = double(1)/double(oobcntbuf(i));
1348
+ ap::vmove(&y(0), &oobbuf(i, 0), ap::vlen(0,nout-1), v);
1349
+ if( ensemble.issoftmax )
1350
+ {
1351
+ dy(0) = xy(i,nin);
1352
+ }
1353
+ else
1354
+ {
1355
+ ap::vmove(&dy(0), &xy(i, nin), ap::vlen(0,nout-1), v);
1356
+ }
1357
+ dserraccumulate(dsbuf, y, dy);
1358
+ }
1359
+ }
1360
+ dserrfinish(dsbuf);
1361
+ ooberrors.relclserror = dsbuf(0);
1362
+ ooberrors.avgce = dsbuf(1);
1363
+ ooberrors.rmserror = dsbuf(2);
1364
+ ooberrors.avgerror = dsbuf(3);
1365
+ ooberrors.avgrelerror = dsbuf(4);
1366
+ }
1367
+
1368
+
1369
+