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] = -2.56827151871145;
219 fWeightMatrix0to1[1][0] = 1.9675350707796;
220 fWeightMatrix0to1[2][0] = 0.0945211430434372;
221 fWeightMatrix0to1[3][0] = -0.534652842263711;
222 fWeightMatrix0to1[4][0] = -1.20582392866552;
223 fWeightMatrix0to1[5][0] = -1.15295712224926;
224 fWeightMatrix0to1[6][0] = 0.062068122332932;
225 fWeightMatrix0to1[7][0] = 1.77909967887342;
226 fWeightMatrix0to1[8][0] = -0.465417129592263;
227 fWeightMatrix0to1[0][1] = -1.14980631440935;
228 fWeightMatrix0to1[1][1] = 0.997243902140403;
229 fWeightMatrix0to1[2][1] = 0.23295461257945;
230 fWeightMatrix0to1[3][1] = 0.597331762575396;
231 fWeightMatrix0to1[4][1] = -1.07812121147322;
232 fWeightMatrix0to1[5][1] = -0.904010711693679;
233 fWeightMatrix0to1[6][1] = 0.763987509344495;
234 fWeightMatrix0to1[7][1] = -0.42730495957135;
235 fWeightMatrix0to1[8][1] = 1.98468529543297;
236 fWeightMatrix0to1[0][2] = 0.255102649182503;
237 fWeightMatrix0to1[1][2] = 1.33042896557683;
238 fWeightMatrix0to1[2][2] = -1.01135742504311;
239 fWeightMatrix0to1[3][2] = -0.163148696405109;
240 fWeightMatrix0to1[4][2] = -0.874674280440262;
241 fWeightMatrix0to1[5][2] = -0.605186137715753;
242 fWeightMatrix0to1[6][2] = -1.29503636367852;
243 fWeightMatrix0to1[7][2] = 0.0166465979160239;
244 fWeightMatrix0to1[8][2] = -0.0838533085354363;
245 fWeightMatrix0to1[0][3] = -0.298765733434783;
246 fWeightMatrix0to1[1][3] = -3.61669821007974;
247 fWeightMatrix0to1[2][3] = 0.230635988926395;
248 fWeightMatrix0to1[3][3] = 1.41124122416393;
249 fWeightMatrix0to1[4][3] = 2.50578787969626;
250 fWeightMatrix0to1[5][3] = 1.24044263856194;
251 fWeightMatrix0to1[6][3] = 0.444402811598472;
252 fWeightMatrix0to1[7][3] = -0.0135466357087942;
253 fWeightMatrix0to1[8][3] = 0.0114644267404459;
254 fWeightMatrix0to1[0][4] = -2.80469640642522;
255 fWeightMatrix0to1[1][4] = 2.03677268622447;
256 fWeightMatrix0to1[2][4] = -1.2252550607933;
257 fWeightMatrix0to1[3][4] = -0.400363817658847;
258 fWeightMatrix0to1[4][4] = 1.74778853696658;
259 fWeightMatrix0to1[5][4] = 3.02549270492086;
260 fWeightMatrix0to1[6][4] = 2.62632004739209;
261 fWeightMatrix0to1[7][4] = -1.05661183010273;
262 fWeightMatrix0to1[8][4] = -2.27738442555204;
264 fWeightMatrix1to2[0][0] = -1.22570337882136;
265 fWeightMatrix1to2[1][0] = -0.0931186540811387;
266 fWeightMatrix1to2[2][0] = 0.0272542341322508;
267 fWeightMatrix1to2[3][0] = 2.20635629364256;
268 fWeightMatrix1to2[4][0] = 0.0433728010577296;
269 fWeightMatrix1to2[0][1] = 0.273962369245395;
270 fWeightMatrix1to2[1][1] = 0.0930582916641537;
271 fWeightMatrix1to2[2][1] = -1.19664292914285;
272 fWeightMatrix1to2[3][1] = 0.157749723688285;
273 fWeightMatrix1to2[4][1] = -4.93727193311647;
274 fWeightMatrix1to2[0][2] = -0.715714091172125;
275 fWeightMatrix1to2[1][2] = 0.867788095978986;
276 fWeightMatrix1to2[2][2] = -1.96198302938044;
277 fWeightMatrix1to2[3][2] = -0.464404359512948;
278 fWeightMatrix1to2[4][2] = 0.0595675643343206;
279 fWeightMatrix1to2[0][3] = 0.758204927770786;
280 fWeightMatrix1to2[1][3] = -2.33053777721601;
281 fWeightMatrix1to2[2][3] = -0.0596569719756188;
282 fWeightMatrix1to2[3][3] = -0.437338637677644;
283 fWeightMatrix1to2[4][3] = 0.166445159553785;
284 fWeightMatrix1to2[0][4] = 0.254272880836908;
285 fWeightMatrix1to2[1][4] = -1.35815091193781;
286 fWeightMatrix1to2[2][4] = -0.364973793826042;
287 fWeightMatrix1to2[3][4] = 0.461639459085397;
288 fWeightMatrix1to2[4][4] = 2.61649708222458;
289 fWeightMatrix1to2[0][5] = -0.607652087990045;
290 fWeightMatrix1to2[1][5] = -1.59745372261442;
291 fWeightMatrix1to2[2][5] = -0.497497443898413;
292 fWeightMatrix1to2[3][5] = 1.55870109321722;
293 fWeightMatrix1to2[4][5] = 1.76169097043581;
294 fWeightMatrix1to2[0][6] = 0.636994330715913;
295 fWeightMatrix1to2[1][6] = -0.437626501220008;
296 fWeightMatrix1to2[2][6] = -1.64640596581009;
297 fWeightMatrix1to2[3][6] = 1.81557603834963;
298 fWeightMatrix1to2[4][6] = 0.213460988352797;
299 fWeightMatrix1to2[0][7] = 2.87318117712971;
300 fWeightMatrix1to2[1][7] = -0.3992286418755;
301 fWeightMatrix1to2[2][7] = -0.0828774633250926;
302 fWeightMatrix1to2[3][7] = -2.32908344616873;
303 fWeightMatrix1to2[4][7] = -0.00287196406204667;
304 fWeightMatrix1to2[0][8] = 3.18538225792951;
305 fWeightMatrix1to2[1][8] = 0.00204371301044828;
306 fWeightMatrix1to2[2][8] = 0.00883819846358683;
307 fWeightMatrix1to2[3][8] = -3.25311571046186;
308 fWeightMatrix1to2[4][8] = 0.0863656137054146;
309 fWeightMatrix1to2[0][9] = -2.58613271127948;
310 fWeightMatrix1to2[1][9] = -1.61491969085433;
311 fWeightMatrix1to2[2][9] = 1.46349048563992;
312 fWeightMatrix1to2[3][9] = -3.52557440843157;
313 fWeightMatrix1to2[4][9] = 0.774477596633972;
315 fWeightMatrix2to3[0][0] = 1.00887795674738;
316 fWeightMatrix2to3[1][0] = 0.901934446106593;
317 fWeightMatrix2to3[2][0] = 0.0103134082552144;
318 fWeightMatrix2to3[3][0] = -4.15846758201171;
319 fWeightMatrix2to3[0][1] = -1.76663219370744;
320 fWeightMatrix2to3[1][1] = -3.16703807597721;
321 fWeightMatrix2to3[2][1] = -0.721676163411028;
322 fWeightMatrix2to3[3][1] = 2.88184594216196;
323 fWeightMatrix2to3[0][2] = 1.05464986697278;
324 fWeightMatrix2to3[1][2] = -1.33953128311584;
325 fWeightMatrix2to3[2][2] = 2.06932178843474;
326 fWeightMatrix2to3[3][2] = -0.566502440872972;
327 fWeightMatrix2to3[0][3] = 1.1754576459947;
328 fWeightMatrix2to3[1][3] = 1.04229738415628;
329 fWeightMatrix2to3[2][3] = 0.409096508127509;
330 fWeightMatrix2to3[3][3] = -4.91383178074721;
331 fWeightMatrix2to3[0][4] = 2.89032187783364;
332 fWeightMatrix2to3[1][4] = -0.640305503795561;
333 fWeightMatrix2to3[2][4] = -3.0600188888794;
334 fWeightMatrix2to3[3][4] = 0.412788618104668;
335 fWeightMatrix2to3[0][5] = 0.441316337386854;
336 fWeightMatrix2to3[1][5] = -0.0238371766698862;
337 fWeightMatrix2to3[2][5] = 0.903246146264539;
338 fWeightMatrix2to3[3][5] = 0.705588849148349;
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)