76 #ifndef IClassifierReader__def 77 #define IClassifierReader__def 79 class IClassifierReader {
84 IClassifierReader() : fStatusIsClean(
true ) {}
85 virtual ~IClassifierReader() {}
88 virtual double GetMvaValue(
const std::vector<double>& inputValues )
const = 0;
91 bool IsStatusClean()
const {
return fStatusIsClean; }
100 class ReadMLP :
public IClassifierReader {
105 ReadMLP( std::vector<std::string>& theInputVars )
106 : IClassifierReader(),
107 fClassName(
"ReadMLP" ),
109 fIsNormalised(
false )
112 const char* inputVars[] = {
"var1",
"var2",
"var3",
"var4" };
115 if (theInputVars.size() <= 0) {
116 std::cout <<
"Problem in class \"" << fClassName <<
"\": empty input vector" << std::endl;
117 fStatusIsClean =
false;
120 if (theInputVars.size() != fNvars) {
121 std::cout <<
"Problem in class \"" << fClassName <<
"\": mismatch in number of input values: " 122 << theInputVars.size() <<
" != " << fNvars << std::endl;
123 fStatusIsClean =
false;
127 for (
size_t ivar = 0; ivar < theInputVars.size(); ivar++) {
128 if (theInputVars[ivar] != inputVars[ivar]) {
129 std::cout <<
"Problem in class \"" << fClassName <<
"\": mismatch in input variable names" << std::endl
130 <<
" for variable [" << ivar <<
"]: " << theInputVars[ivar].c_str() <<
" != " << inputVars[ivar] << std::endl;
131 fStatusIsClean =
false;
164 double GetMvaValue(
const std::vector<double>& inputValues )
const;
172 const char* fClassName;
175 size_t GetNvar()
const {
return fNvars; }
179 const bool fIsNormalised;
180 bool IsNormalised()
const {
return fIsNormalised; }
183 double NormVariable(
double x,
double xmin,
double xmax )
const {
185 return 2*(x -
xmin)/(xmax - xmin) - 1.0;
193 double GetMvaValue__(
const std::vector<double>& inputValues )
const;
197 double ActivationFnc(
double x)
const;
198 double OutputActivationFnc(
double x)
const;
202 double fWeightMatrix0to1[10][5];
203 double fWeightMatrix1to2[6][10];
204 double fWeightMatrix2to3[4][6];
206 double * fWeights[4];
213 fLayerSize[0] = 5; fWeights[0] =
new double[5];
214 fLayerSize[1] = 10; fWeights[1] =
new double[10];
215 fLayerSize[2] = 6; fWeights[2] =
new double[6];
216 fLayerSize[3] = 4; fWeights[3] =
new double[4];
218 fWeightMatrix0to1[0][0] = -0.38778850670751;
219 fWeightMatrix0to1[1][0] = 1.77075352763869;
220 fWeightMatrix0to1[2][0] = -0.495887946364255;
221 fWeightMatrix0to1[3][0] = -1.07287478074003;
222 fWeightMatrix0to1[4][0] = -1.27493222222149;
223 fWeightMatrix0to1[5][0] = -0.232068331688551;
224 fWeightMatrix0to1[6][0] = -0.0100184567840386;
225 fWeightMatrix0to1[7][0] = -0.504687461356197;
226 fWeightMatrix0to1[8][0] = -0.41456998867676;
227 fWeightMatrix0to1[0][1] = 0.344484078249326;
228 fWeightMatrix0to1[1][1] = 0.982416309163123;
229 fWeightMatrix0to1[2][1] = -0.0419129952225741;
230 fWeightMatrix0to1[3][1] = -0.703283427306373;
231 fWeightMatrix0to1[4][1] = -0.768397705168322;
232 fWeightMatrix0to1[5][1] = -0.166873265311981;
233 fWeightMatrix0to1[6][1] = 0.551561437331797;
234 fWeightMatrix0to1[7][1] = -2.49983402360569;
235 fWeightMatrix0to1[8][1] = 3.22680407145307;
236 fWeightMatrix0to1[0][2] = 0.384100424207592;
237 fWeightMatrix0to1[1][2] = 0.872517592990635;
238 fWeightMatrix0to1[2][2] = -0.0871975702555971;
239 fWeightMatrix0to1[3][2] = -1.52887190551762;
240 fWeightMatrix0to1[4][2] = -0.168358013186753;
241 fWeightMatrix0to1[5][2] = 1.50557472966874;
242 fWeightMatrix0to1[6][2] = -1.17198963689223;
243 fWeightMatrix0to1[7][2] = -0.0740450592612006;
244 fWeightMatrix0to1[8][2] = -0.157477458717336;
245 fWeightMatrix0to1[0][3] = -0.381185082592116;
246 fWeightMatrix0to1[1][3] = -3.17908830387991;
247 fWeightMatrix0to1[2][3] = -0.422381779159728;
248 fWeightMatrix0to1[3][3] = 2.54234963914718;
249 fWeightMatrix0to1[4][3] = 1.72413680540312;
250 fWeightMatrix0to1[5][3] = -1.03648108259518;
251 fWeightMatrix0to1[6][3] = 0.75010463707696;
252 fWeightMatrix0to1[7][3] = -0.120045773739285;
253 fWeightMatrix0to1[8][3] = 0.00520098351826005;
254 fWeightMatrix0to1[0][4] = -2.23811315621248;
255 fWeightMatrix0to1[1][4] = 0.299577800759728;
256 fWeightMatrix0to1[2][4] = 0.173737774048086;
257 fWeightMatrix0to1[3][4] = 2.01992950842827;
258 fWeightMatrix0to1[4][4] = 2.47067034525462;
259 fWeightMatrix0to1[5][4] = 2.69933718728504;
260 fWeightMatrix0to1[6][4] = 1.24979124792121;
261 fWeightMatrix0to1[7][4] = 4.37768351492713;
262 fWeightMatrix0to1[8][4] = -3.6655386905835;
264 fWeightMatrix1to2[0][0] = -2.47375060347935;
265 fWeightMatrix1to2[1][0] = 1.84237698308281;
266 fWeightMatrix1to2[2][0] = -0.915246344719473;
267 fWeightMatrix1to2[3][0] = 0.742030598942647;
268 fWeightMatrix1to2[4][0] = 1.54286295830321;
269 fWeightMatrix1to2[0][1] = 1.04760275173647;
270 fWeightMatrix1to2[1][1] = 0.251274036343028;
271 fWeightMatrix1to2[2][1] = 0.133311422450511;
272 fWeightMatrix1to2[3][1] = -0.362651699534128;
273 fWeightMatrix1to2[4][1] = -1.26560770951767;
274 fWeightMatrix1to2[0][2] = -0.542093135020647;
275 fWeightMatrix1to2[1][2] = -0.60714945425257;
276 fWeightMatrix1to2[2][2] = -0.429759617883957;
277 fWeightMatrix1to2[3][2] = 1.49053600605261;
278 fWeightMatrix1to2[4][2] = 0.186321270572984;
279 fWeightMatrix1to2[0][3] = -1.26885413818901;
280 fWeightMatrix1to2[1][3] = 0.403278533237819;
281 fWeightMatrix1to2[2][3] = -2.37515789916025;
282 fWeightMatrix1to2[3][3] = -0.682549634822121;
283 fWeightMatrix1to2[4][3] = 0.895671716449018;
284 fWeightMatrix1to2[0][4] = 1.00468665089631;
285 fWeightMatrix1to2[1][4] = 0.00294619057952214;
286 fWeightMatrix1to2[2][4] = -0.858376459553123;
287 fWeightMatrix1to2[3][4] = 2.31562800007637;
288 fWeightMatrix1to2[4][4] = 0.39209065116205;
289 fWeightMatrix1to2[0][5] = 0.453890573142123;
290 fWeightMatrix1to2[1][5] = -1.65086032608904;
291 fWeightMatrix1to2[2][5] = -1.38809189733852;
292 fWeightMatrix1to2[3][5] = 2.00030576315926;
293 fWeightMatrix1to2[4][5] = 1.28885335051608;
294 fWeightMatrix1to2[0][6] = 0.737008121249608;
295 fWeightMatrix1to2[1][6] = -2.07776319325285;
296 fWeightMatrix1to2[2][6] = -0.395007874498507;
297 fWeightMatrix1to2[3][6] = 0.71934456144467;
298 fWeightMatrix1to2[4][6] = 0.0157924737451731;
299 fWeightMatrix1to2[0][7] = 1.1303787673988;
300 fWeightMatrix1to2[1][7] = 0.245023395607365;
301 fWeightMatrix1to2[2][7] = 1.37767650460826;
302 fWeightMatrix1to2[3][7] = -2.94220491047599;
303 fWeightMatrix1to2[4][7] = -1.04119716294614;
304 fWeightMatrix1to2[0][8] = 0.230803202575464;
305 fWeightMatrix1to2[1][8] = 0.693532198172888;
306 fWeightMatrix1to2[2][8] = 1.05733862485925;
307 fWeightMatrix1to2[3][8] = -1.97011105977166;
308 fWeightMatrix1to2[4][8] = -0.897528569439193;
309 fWeightMatrix1to2[0][9] = 0.953931221485957;
310 fWeightMatrix1to2[1][9] = -1.26759017153591;
311 fWeightMatrix1to2[2][9] = 2.74759900060607;
312 fWeightMatrix1to2[3][9] = -0.656795897499413;
313 fWeightMatrix1to2[4][9] = -1.23312229444889;
315 fWeightMatrix2to3[0][0] = 0.809374089520293;
316 fWeightMatrix2to3[1][0] = -0.217255510142443;
317 fWeightMatrix2to3[2][0] = -0.452803002823739;
318 fWeightMatrix2to3[3][0] = -0.50457845423497;
319 fWeightMatrix2to3[0][1] = 0.782281118310111;
320 fWeightMatrix2to3[1][1] = -0.665086851180649;
321 fWeightMatrix2to3[2][1] = -1.10864578434138;
322 fWeightMatrix2to3[3][1] = 0.171136991984684;
323 fWeightMatrix2to3[0][2] = 0.0224037450653728;
324 fWeightMatrix2to3[1][2] = -0.514585812191245;
325 fWeightMatrix2to3[2][2] = 0.498674855353497;
326 fWeightMatrix2to3[3][2] = -0.0063816407743405;
327 fWeightMatrix2to3[0][3] = 0.0129312070692756;
328 fWeightMatrix2to3[1][3] = 0.048805104141024;
329 fWeightMatrix2to3[2][3] = 0.463802128086056;
330 fWeightMatrix2to3[3][3] = -0.526403437932214;
331 fWeightMatrix2to3[0][4] = 0.586629132427303;
332 fWeightMatrix2to3[1][4] = -0.56516260388251;
333 fWeightMatrix2to3[2][4] = -0.0193992692312397;
334 fWeightMatrix2to3[3][4] = -0.00243559497633667;
335 fWeightMatrix2to3[0][5] = 0.529319697003467;
336 fWeightMatrix2to3[1][5] = -0.466670469159831;
337 fWeightMatrix2to3[2][5] = -0.694783758016973;
338 fWeightMatrix2to3[3][5] = 1.17756234424668;
341 inline double ReadMLP::GetMvaValue__(
const std::vector<double>& inputValues )
const 343 if (inputValues.size() != (
unsigned int)fLayerSize[0]-1) {
344 std::cout <<
"Input vector needs to be of size " << fLayerSize[0]-1 << std::endl;
348 for (
int l=0;
l<fLayers;
l++)
349 for (
int i=0; i<fLayerSize[
l]; i++) fWeights[
l][i]=0;
351 for (
int l=0;
l<fLayers-1;
l++)
352 fWeights[
l][fLayerSize[
l]-1]=1;
354 for (
int i=0; i<fLayerSize[0]-1; i++)
355 fWeights[0][i]=inputValues[i];
358 for (
int o=0; o<fLayerSize[1]-1; o++) {
359 for (
int i=0; i<fLayerSize[0]; i++) {
360 double inputVal = fWeightMatrix0to1[o][i] * fWeights[0][i];
361 fWeights[1][o] += inputVal;
363 fWeights[1][o] = ActivationFnc(fWeights[1][o]);
366 for (
int o=0; o<fLayerSize[2]-1; o++) {
367 for (
int i=0; i<fLayerSize[1]; i++) {
368 double inputVal = fWeightMatrix1to2[o][i] * fWeights[1][i];
369 fWeights[2][o] += inputVal;
371 fWeights[2][o] = ActivationFnc(fWeights[2][o]);
374 for (
int o=0; o<fLayerSize[3]; o++) {
375 for (
int i=0; i<fLayerSize[2]; i++) {
376 double inputVal = fWeightMatrix2to3[o][i] * fWeights[2][i];
377 fWeights[3][o] += inputVal;
379 fWeights[3][o] = OutputActivationFnc(fWeights[3][o]);
382 return fWeights[3][0];
385 double ReadMLP::ActivationFnc(
double x)
const {
389 double ReadMLP::OutputActivationFnc(
double x)
const {
395 inline void ReadMLP::Clear()
398 for (
int lIdx = 0; lIdx < 4; lIdx++) {
399 delete[] fWeights[lIdx];
402 inline double ReadMLP::GetMvaValue(
const std::vector<double>& inputValues )
const 408 if (!IsStatusClean()) {
409 std::cout <<
"Problem in class \"" << fClassName <<
"\": cannot return classifier response" 410 <<
" because status is dirty" << std::endl;
414 if (IsNormalised()) {
416 std::vector<double> iV;
417 iV.reserve(inputValues.size());
419 for (std::vector<double>::const_iterator varIt = inputValues.begin();
420 varIt != inputValues.end(); varIt++, ivar++) {
421 iV.push_back(NormVariable( *varIt, fVmin[ivar], fVmax[ivar] ));
423 retval = GetMvaValue__( iV );
426 retval = GetMvaValue__( inputValues );
Type GetType(const std::string &Name)
void Initialize(Bool_t useTMVAStyle=kTRUE)