86 fLocalTrainingTree(0),
88 fValidationFraction(0.5),
100 fLocalTrainingTree(0),
102 fValidationFraction(0.5),
103 fLearningMethod(
"" )
130 if (fMLP)
delete fMLP;
140 while (layerSpec.
Length()>0) {
142 if (layerSpec.
First(
',')<0) {
147 sToAdd = layerSpec(0,layerSpec.
First(
','));
148 layerSpec = layerSpec(layerSpec.
First(
',')+1,layerSpec.
Length());
152 nNodes += atoi(sToAdd);
153 fHiddenLayer =
TString::Format(
"%s%i:", (
const char*)fHiddenLayer, nNodes );
157 std::vector<TString>::iterator itrVar = (*fInputVars).begin();
158 std::vector<TString>::iterator itrVarEnd = (*fInputVars).end();
159 fMLPBuildOptions =
"";
160 for (; itrVar != itrVarEnd; ++itrVar) {
163 fMLPBuildOptions += myVar;
164 fMLPBuildOptions +=
",";
166 fMLPBuildOptions.
Chop();
169 fMLPBuildOptions += fHiddenLayer;
170 fMLPBuildOptions +=
"type";
172 Log() << kINFO <<
"Use " << fNcycles <<
" training cycles" <<
Endl;
173 Log() << kINFO <<
"Use configuration (nodes per hidden layer): " << fHiddenLayer <<
Endl;
193 DeclareOptionRef( fNcycles = 200,
"NCycles",
"Number of training cycles" );
194 DeclareOptionRef( fLayerSpec =
"N,N-1",
"HiddenLayers",
"Specification of hidden layer architecture (N stands for number of variables; any integers may also be used)" );
196 DeclareOptionRef( fValidationFraction = 0.5,
"ValidationFraction",
197 "Fraction of events in training tree used for cross validation" );
199 DeclareOptionRef( fLearningMethod =
"Stochastic",
"LearningMethod",
"Learning method" );
200 AddPreDefVal(
TString(
"Stochastic") );
201 AddPreDefVal(
TString(
"Batch") );
202 AddPreDefVal(
TString(
"SteepestDescent") );
203 AddPreDefVal(
TString(
"RibierePolak") );
204 AddPreDefVal(
TString(
"FletcherReeves") );
205 AddPreDefVal(
TString(
"BFGS") );
213 CreateMLPOptions(fLayerSpec);
215 if (IgnoreEventsWithNegWeightsInTraining()) {
216 Log() << kFATAL <<
"Mechanism to ignore events with negative weights in training not available for method"
217 << GetMethodTypeName()
218 <<
" --> please remove \"IgnoreNegWeightsInTraining\" option from booking string."
228 const Event* ev = GetEvent();
231 for (
UInt_t ivar = 0; ivar<Data()->GetNVariables(); ivar++) {
237 NoErrorCalc(err, errUpper);
260 const Long_t basketsize = 128000;
263 TTree *localTrainingTree =
new TTree(
"TMLPtrain",
"Local training tree for TMlpANN" );
264 localTrainingTree->
Branch(
"type", &
type,
"type/I", basketsize );
265 localTrainingTree->
Branch(
"weight", &weight,
"weight/F", basketsize );
267 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++) {
268 TString myVar = GetInternalVarName(ivar);
270 localTrainingTree->
Branch( myVar.
Data(), &vArr[ivar], myTyp.
Data(), basketsize );
273 for (
UInt_t ievt=0; ievt<Data()->GetNEvents(); ievt++) {
274 const Event *ev = GetEvent(ievt);
275 for (
UInt_t i=0; i<GetNvar(); i++) {
278 type = DataInfo().IsSignal( ev ) ? 1 : 0;
280 localTrainingTree->
Fill();
288 trainList += 1.0-fValidationFraction;
290 trainList += (
Int_t)Data()->GetNEvtSigTrain();
291 trainList +=
" || (Entry$>";
292 trainList += (
Int_t)Data()->GetNEvtSigTrain();
293 trainList +=
" && Entry$<";
294 trainList += (
Int_t)(Data()->GetNEvtSigTrain() + (1.0 - fValidationFraction)*Data()->GetNEvtBkgdTrain());
299 Log() << kHEADER <<
"Requirement for training events: \"" << trainList <<
"\"" <<
Endl;
300 Log() << kINFO <<
"Requirement for validation events: \"" << testList <<
"\"" <<
Endl;
305 if (fMLP) {
delete fMLP; fMLP =
nullptr; }
310 fMLP->SetEventWeight(
"weight" );
315 fLearningMethod.ToLower();
323 Log() << kFATAL <<
"Unknown Learning Method: \"" << fLearningMethod <<
"\"" <<
Endl;
325 fMLP->SetLearningMethod( learningMethod );
328 fMLP->Train(fNcycles,
"" );
332 delete localTrainingTree;
344 gTools().
AddAttr( arch,
"BuildOptions", fMLPBuildOptions.Data() );
347 const TString tmpfile=GetWeightFileDir()+
"/TMlp.nn.weights.temp";
348 fMLP->DumpWeights( tmpfile.
Data() );
349 std::ifstream inf( tmpfile.
Data() );
353 while (inf.getline(temp,256)) {
359 dummy = dummy(0,dummy.
First(
' '));
364 data += (dummy +
" ");
381 const TString fname = GetWeightFileDir()+
"/TMlp.nn.weights.temp";
382 std::ofstream fout( fname.
Data() );
383 double temp1=0,temp2=0;
386 std::stringstream content(nodecontent);
387 if (strcmp(
gTools().GetName(ch),
"input")==0) {
388 fout <<
"#input normalization" << std::endl;
389 while ((content >> temp1) &&(content >> temp2)) {
390 fout << temp1 <<
" " << temp2 << std::endl;
393 if (strcmp(
gTools().GetName(ch),
"output")==0) {
394 fout <<
"#output normalization" << std::endl;
395 while ((content >> temp1) &&(content >> temp2)) {
396 fout << temp1 <<
" " << temp2 << std::endl;
399 if (strcmp(
gTools().GetName(ch),
"neurons")==0) {
400 fout <<
"#neurons weights" << std::endl;
401 while (content >> temp1) {
402 fout << temp1 << std::endl;
405 if (strcmp(
gTools().GetName(ch),
"synapses")==0) {
406 fout <<
"#synapses weights" ;
407 while (content >> temp1) {
408 fout << std::endl << temp1 ;
421 TTree * dummyTree =
new TTree(
"dummy",
"Empty dummy tree", 1);
422 for (
UInt_t ivar = 0; ivar<Data()->GetNVariables(); ivar++) {
423 TString vn = DataInfo().GetVariableInfo(ivar).GetInternalName();
429 if (fMLP) {
delete fMLP; fMLP =
nullptr; }
431 fMLP->LoadWeights( fname );
441 std::ofstream fout(
"./TMlp.nn.weights.temp" );
442 fout << istr.rdbuf();
446 Log() << kINFO <<
"Load TMLP weights into " << fMLP <<
Endl;
451 TTree * dummyTree =
new TTree(
"dummy",
"Empty dummy tree", 1);
452 for (
UInt_t ivar = 0; ivar<Data()->GetNVariables(); ivar++) {
453 TString vn = DataInfo().GetVariableInfo(ivar).GetLabel();
459 if (fMLP) {
delete fMLP; fMLP =
nullptr; }
462 fMLP->LoadWeights(
"./TMlp.nn.weights.temp" );
476 if (theClassFileName ==
"")
477 classFileName = GetWeightFileDir() +
"/" + GetJobName() +
"_" + GetMethodName() +
".class";
479 classFileName = theClassFileName;
482 Log() << kINFO <<
"Creating specific (TMultiLayerPerceptron) standalone response class: " << classFileName <<
Endl;
483 fMLP->Export( classFileName.
Data() );
505 Log() <<
"This feed-forward multilayer perceptron neural network is the " <<
Endl;
506 Log() <<
"standard implementation distributed with ROOT (class TMultiLayerPerceptron)." <<
Endl;
508 Log() <<
"Detailed information is available here:" <<
Endl;
509 if (
gConfig().WriteOptionsReference()) {
510 Log() <<
"<a href=\"https://root.cern/doc/master/classTMultiLayerPerceptron.html\">";
511 Log() <<
"https://root.cern/doc/master/classTMultiLayerPerceptron.html</a>" <<
Endl;
513 else Log() <<
"https://root.cern/doc/master/classTMultiLayerPerceptron.html" <<
Endl;
#define REGISTER_METHOD(CLASS)
for example
const Bool_t EnforceNormalization__
bool Bool_t
Boolean (0=false, 1=true) (bool)
int Int_t
Signed integer 4 bytes (int)
long Long_t
Signed long integer 4 bytes (long). Size depends on architecture.
float Float_t
Float 4 bytes (float)
double Double_t
Double 8 bytes.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Class that contains all the data information.
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not.
Virtual base Class for all MVA method.
This is the TMVA TMultiLayerPerceptron interface class.
void ReadWeightsFromStream(std::istream &istr) override
read weights from stream since the MLP can not read from the stream, we 1st: write the weights to tem...
void MakeClass(const TString &classFileName=TString("")) const override
create reader class for classifier -> overwrites base class function create specific class for TMulti...
void Train(void) override
performs TMlpANN training available learning methods:
void ReadWeightsFromXML(void *wghtnode) override
rebuild temporary textfile from xml weightfile and load this file into MLP
void AddWeightsXMLTo(void *parent) const override
write weights to xml file
void DeclareOptions() override
define the options (their key words) that can be set in the option string
void CreateMLPOptions(TString)
translates options from option string into TMlpANN language
void ProcessOptions() override
builds the neural network as specified by the user
MethodTMlpANN(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption="3000:N-1:N-2")
standard constructor
Double_t GetMvaValue(Double_t *err=nullptr, Double_t *errUpper=nullptr) override
calculate the value of the neural net for the current event
void Init(void) override
default initialisations
virtual ~MethodTMlpANN(void)
destructor
Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets) override
TMlpANN can handle classification with 2 classes.
void GetHelpMessage() const override
get help message text
void MakeClassSpecific(std::ostream &, const TString &) const override
write specific classifier response nothing to do here - all taken care of by TMultiLayerPerceptron
Singleton class for Global types used by TMVA.
This class describes a neural network.
TSubString Strip(EStripType s=kTrailing, char c=' ') const
Return a substring of self stripped at beginning and/or end.
Ssiz_t First(char c) const
Find first occurrence of a character c.
const char * Data() const
TString & ReplaceAll(const TString &s1, const TString &s2)
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
TString & Remove(Ssiz_t pos)
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
A TTree represents a columnar dataset.
virtual Int_t Fill()
Fill all branches.
TBranch * Branch(const char *name, T *obj, Int_t bufsize=32000, Int_t splitlevel=99)
Add a new branch, and infer the data type from the type of obj being passed.
create variable transformations
MsgLogger & Endl(MsgLogger &ml)