89using std::stringstream;
142 MethodCFMlpANN_nsel(0)
159 MethodCFMlpANN_nsel(0)
179 DeclareOptionRef( fNcycles =3000,
"NCycles",
"Number of training cycles" );
180 DeclareOptionRef( fLayerSpec=
"N,N-1",
"HiddenLayers",
"Specification of hidden layer architecture" );
188 fNodes =
new Int_t[20];
190 Int_t currentHiddenLayer = 1;
192 while(layerSpec.
Length()>0) {
194 if (layerSpec.
First(
',')<0) {
199 sToAdd = layerSpec(0,layerSpec.
First(
','));
200 layerSpec = layerSpec(layerSpec.
First(
',')+1,layerSpec.
Length());
204 nNodes += atoi(sToAdd);
205 fNodes[currentHiddenLayer++] = nNodes;
208 fNodes[0] = GetNvar();
209 fNodes[fNlayers-1] = 2;
211 if (IgnoreEventsWithNegWeightsInTraining()) {
212 Log() << kFATAL <<
"Mechanism to ignore events with negative weights in training not yet available for method: "
213 << GetMethodTypeName()
214 <<
" --> please remove \"IgnoreNegWeightsInTraining\" option from booking string."
218 Log() << kINFO <<
"Use configuration (nodes per layer): in=";
219 for (
Int_t i=0; i<fNlayers-1; i++)
Log() << kINFO << fNodes[i] <<
":";
220 Log() << kINFO << fNodes[fNlayers-1] <<
"=out" <<
Endl;
223 Log() <<
"Use " << fNcycles <<
" training cycles" <<
Endl;
225 Int_t nEvtTrain = Data()->GetNTrainingEvents();
231 fData =
new TMatrix( nEvtTrain, GetNvar() );
232 fClass =
new std::vector<Int_t>( nEvtTrain );
237 for (
Int_t ievt=0; ievt<nEvtTrain; ievt++) {
238 const Event * ev = GetEvent(ievt);
241 (*fClass)[ievt] = DataInfo().IsSignal(ev) ? 1 : 2;
244 for (ivar=0; ivar<GetNvar(); ivar++) {
245 (*fData)( ievt, ivar ) = ev->
GetValue(ivar);
261 SetNormalised(
kTRUE );
264 MethodCFMlpANN_nsel = 0;
277 for (
Int_t i=0; i<fNlayers; i++)
delete[] fYNN[i];
289 Int_t ntrain(Data()->GetNTrainingEvents());
291 Int_t nvar(GetNvar());
292 Int_t nlayers(fNlayers);
294 Int_t ncycles(fNcycles);
296 for (
Int_t i=0; i<nlayers; i++) nodes[i] = fNodes[i];
299 for (
Int_t i=0; i<fNlayers; i++)
delete[] fYNN[i];
304 for (
Int_t layer=0; layer<nlayers; layer++)
305 fYNN[layer] =
new Double_t[fNodes[layer]];
309 Train_nn( &dumDat, &dumDat, &ntrain, &ntest, &nvar, &nlayers, nodes, &ncycles );
311 Log() << kWARNING <<
"<Train> sorry CFMlpANN does not run on Windows" <<
Endl;
326 const Event* ev = GetEvent();
329 std::vector<Double_t> inputVec( GetNvar() );
330 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++) inputVec[ivar] = ev->
GetValue(ivar);
332 Double_t myMVA = EvalANN( inputVec, isOK );
333 if (!isOK)
Log() << kFATAL <<
"EvalANN returns (!isOK) for event " <<
Endl;
336 NoErrorCalc(err, errUpper);
348 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++) xeev[ivar] = inVar[ivar];
352 for (
UInt_t jvar=0; jvar<GetNvar(); jvar++) {
354 if (fVarn_1.xmax[jvar] < xeev[jvar]) xeev[jvar] = fVarn_1.xmax[jvar];
355 if (fVarn_1.xmin[jvar] > xeev[jvar]) xeev[jvar] = fVarn_1.xmin[jvar];
356 if (fVarn_1.xmax[jvar] == fVarn_1.xmin[jvar]) {
361 xeev[jvar] = xeev[jvar] - ((fVarn_1.xmax[jvar] + fVarn_1.xmin[jvar])/2);
362 xeev[jvar] = xeev[jvar] / ((fVarn_1.xmax[jvar] - fVarn_1.xmin[jvar])/2);
368 Double_t retval = 0.5*(1.0 + fYNN[fParam_1.layerm-1][0]);
380 for (
Int_t ivar=0; ivar<fNeur_1.neuron[0]; ivar++) fYNN[0][ivar] = xeev[ivar];
382 for (
Int_t layer=1; layer<fParam_1.layerm; layer++) {
383 for (
Int_t j=1; j<=fNeur_1.neuron[layer]; j++) {
385 Double_t x = Ww_ref(fNeur_1.ww, layer+1,j);
387 for (
Int_t k=1; k<=fNeur_1.neuron[layer-1]; k++) {
388 x += fYNN[layer-1][k-1]*W_ref(fNeur_1.w, layer+1, j, k);
390 fYNN[layer][j-1] = NN_fonc( layer,
x );
402 if (u/fDel_1.temp[i] > 170)
f = +1;
403 else if (u/fDel_1.temp[i] < -170)
f = -1;
406 f = (1 - yy)/(1 + yy);
421 istr >> nva >> lclass;
423 if (GetNvar() != nva)
424 Log() << kFATAL <<
"<ReadWeightsFromFile> mismatch in number of variables" <<
Endl;
428 Log() << kFATAL <<
"<ReadWeightsFromFile> mismatch in number of classes" <<
Endl;
432 Log() << kFATAL <<
"<ReadWeightsFromStream> reached EOF prematurely " <<
Endl;
435 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++)
436 istr >> fVarn_1.xmax[ivar] >> fVarn_1.xmin[ivar];
439 istr >> fParam_1.layerm;
442 for (
Int_t i=0; i<fNlayers; i++)
delete[] fYNN[i];
446 fYNN =
new Double_t*[fParam_1.layerm];
447 for (
Int_t layer=0; layer<fParam_1.layerm; layer++) {
450 istr >> fNeur_1.neuron[layer];
451 fYNN[layer] =
new Double_t[fNeur_1.neuron[layer]];
455 const Int_t nchar( 100 );
456 char* dumchar =
new char[nchar];
459 for (
Int_t layer=1; layer<=fParam_1.layerm-1; layer++) {
461 Int_t nq = fNeur_1.neuron[layer]/10;
462 Int_t nr = fNeur_1.neuron[layer] - nq*10;
468 for (
Int_t k=1; k<=kk; k++) {
469 Int_t jmin = 10*k - 9;
471 if (fNeur_1.neuron[layer]<jmax) jmax = fNeur_1.neuron[layer];
472 for (
Int_t j=jmin; j<=jmax; j++) {
473 istr >> Ww_ref(fNeur_1.ww, layer+1, j);
475 for (
Int_t i=1; i<=fNeur_1.neuron[layer-1]; i++) {
476 for (
Int_t j=jmin; j<=jmax; j++) {
477 istr >> W_ref(fNeur_1.w, layer+1, j, i);
481 istr.getline( dumchar, nchar );
485 for (
Int_t layer=0; layer<fParam_1.layerm; layer++) {
488 istr.getline( dumchar, nchar );
489 istr.getline( dumchar, nchar );
491 istr >> fDel_1.temp[layer];
495 if ((
Int_t)GetNvar() != fNeur_1.neuron[0]) {
496 Log() << kFATAL <<
"<ReadWeightsFromFile> mismatch in zeroth layer:"
497 << GetNvar() <<
" " << fNeur_1.neuron[0] <<
Endl;
500 fNlayers = fParam_1.layerm;
518 Log() << kFATAL <<
"ERROR in MethodCFMlpANN_DataInterface zero pointer xpg" <<
Endl;
520 if (*nvar != (
Int_t)this->GetNvar()) {
521 Log() << kFATAL <<
"ERROR in MethodCFMlpANN_DataInterface mismatch in num of variables: "
522 << *nvar <<
" " << this->GetNvar() <<
Endl;
526 *iclass = (
int)this->
GetClass( MethodCFMlpANN_nsel );
527 for (
UInt_t ivar=0; ivar<this->GetNvar(); ivar++)
528 xpg[ivar] = (
double)this->GetData( MethodCFMlpANN_nsel, ivar );
530 ++MethodCFMlpANN_nsel;
547 for (
Int_t ivar=0; ivar<fParam_1.nvar; ivar++)
548 s << std::scientific << fVarn_1.xmin[ivar] <<
" " << fVarn_1.xmax[ivar] <<
" ";
553 for (
Int_t layer=0; layer<fParam_1.layerm; layer++)
554 n << std::scientific << fNeur_1.neuron[layer] <<
" ";
556 for (
Int_t layer=1; layer<fParam_1.layerm; layer++) {
558 gTools().
AddAttr(layernode,
"NNeurons",fNeur_1.neuron[layer]);
559 void* neuronnode=NULL;
560 for (
Int_t neuron=0; neuron<fNeur_1.neuron[layer]; neuron++) {
562 stringstream weights;
563 weights.precision( 16 );
564 weights << std::scientific << Ww_ref(fNeur_1.ww, layer+1, neuron+1);
565 for (
Int_t i=0; i<fNeur_1.neuron[layer-1]; i++) {
566 weights <<
" " << std::scientific << W_ref(fNeur_1.w, layer+1, neuron+1, i+1);
573 temp.precision( 16 );
574 for (
Int_t layer=0; layer<fParam_1.layerm; layer++) {
575 temp << std::scientific << fDel_1.temp[layer] <<
" ";
587 stringstream content(minmaxcontent);
588 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++)
589 content >> fVarn_1.xmin[ivar] >> fVarn_1.xmax[ivar];
591 for (
Int_t i=0; i<fNlayers; i++)
delete[] fYNN[i];
595 fYNN =
new Double_t*[fParam_1.layerm];
598 stringstream ncontent(neuronscontent);
599 for (
Int_t layer=0; layer<fParam_1.layerm; layer++) {
602 ncontent >> fNeur_1.neuron[layer];
603 fYNN[layer] =
new Double_t[fNeur_1.neuron[layer]];
605 for (
Int_t layer=1; layer<fParam_1.layerm; layer++) {
607 void* neuronnode=NULL;
609 for (
Int_t neuron=0; neuron<fNeur_1.neuron[layer]; neuron++) {
611 stringstream weights(neuronweights);
612 weights >> Ww_ref(fNeur_1.ww, layer+1, neuron+1);
613 for (
Int_t i=0; i<fNeur_1.neuron[layer-1]; i++) {
614 weights >> W_ref(fNeur_1.w, layer+1, neuron+1, i+1);
621 stringstream t(temp);
622 for (
Int_t layer=0; layer<fParam_1.layerm; layer++) {
623 t >> fDel_1.temp[layer];
625 fNlayers = fParam_1.layerm;
634 o <<
"Number of vars " << fParam_1.nvar << std::endl;
635 o <<
"Output nodes " << fParam_1.lclass << std::endl;
638 for (
Int_t ivar=0; ivar<fParam_1.nvar; ivar++)
639 o <<
"Var " << ivar <<
" [" << fVarn_1.xmin[ivar] <<
" - " << fVarn_1.xmax[ivar] <<
"]" << std::endl;
642 o <<
"Number of layers " << fParam_1.layerm << std::endl;
644 o <<
"Nodes per layer ";
645 for (
Int_t layer=0; layer<fParam_1.layerm; layer++)
647 o << fNeur_1.neuron[layer] <<
" ";
651 for (
Int_t layer=1; layer<=fParam_1.layerm-1; layer++) {
653 Int_t nq = fNeur_1.neuron[layer]/10;
654 Int_t nr = fNeur_1.neuron[layer] - nq*10;
660 for (
Int_t k=1; k<=kk; k++) {
661 Int_t jmin = 10*k - 9;
664 if (fNeur_1.neuron[layer]<jmax) jmax = fNeur_1.neuron[layer];
665 for (j=jmin; j<=jmax; j++) {
668 o << Ww_ref(fNeur_1.ww, layer+1, j) <<
" ";
673 for (i=1; i<=fNeur_1.neuron[layer-1]; i++) {
674 for (j=jmin; j<=jmax; j++) {
676 o << W_ref(fNeur_1.w, layer+1, j, i) <<
" ";
685 for (
Int_t layer=0; layer<fParam_1.layerm; layer++) {
686 o <<
"Del.temp in layer " << layer <<
" : " << fDel_1.temp[layer] << std::endl;
695 fout <<
" // not implemented for class: \"" << className <<
"\"" << std::endl;
696 fout <<
"};" << std::endl;
#define REGISTER_METHOD(CLASS)
for example
TMatrixT< Float_t > TMatrix
Class that contains all the data information.
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
Virtual base Class for all MVA method.
void SetLogger(MsgLogger *l)
Interface to Clermond-Ferrand artificial neural network.
void PrintWeights(std::ostream &o) const
write the weights of the neural net
void MakeClassSpecific(std::ostream &, const TString &) const
Double_t EvalANN(std::vector< Double_t > &, Bool_t &isOK)
evaluates NN value as function of input variables
Double_t GetMvaValue(Double_t *err=0, Double_t *errUpper=0)
returns CFMlpANN output (normalised within [0,1])
void DeclareOptions()
define the options (their key words) that can be set in the option string know options: NCycles=xx :t...
virtual Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t)
CFMlpANN can handle classification with 2 classes.
void NN_ava(Double_t *)
auxiliary functions
void AddWeightsXMLTo(void *parent) const
write weights to xml file
void ProcessOptions()
decode the options in the option string
void Train(void)
training of the Clement-Ferrand NN classifier
Double_t NN_fonc(Int_t, Double_t) const
activation function
void ReadWeightsFromStream(std::istream &istr)
read back the weight from the training from file (stream)
void MakeClassSpecificHeader(std::ostream &, const TString &="") const
write specific classifier response for header
virtual ~MethodCFMlpANN(void)
destructor
MethodCFMlpANN(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption="3000:N-1:N-2")
standard constructor
void Init(void)
default initialisation called by all constructors
Int_t DataInterface(Double_t *, Double_t *, Int_t *, Int_t *, Int_t *, Int_t *, Double_t *, Int_t *, Int_t *)
data interface function
void ReadWeightsFromXML(void *wghtnode)
read weights from xml file
void GetHelpMessage() const
get help message text
Singleton class for Global types used by TMVA.
Ssiz_t First(char c) const
Find first occurrence of a character c.
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
TString & Remove(Ssiz_t pos)
static constexpr double s
create variable transformations
MsgLogger & Endl(MsgLogger &ml)