Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TMVAMulticlass.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_ml
3/// \notebook -nodraw
4/// This macro provides a simple example for the training and testing of the TMVA
5/// multiclass classification
6/// - Project : TMVA - a Root-integrated toolkit for multivariate data analysis
7/// - Package : TMVA
8/// - Root Macro: TMVAMulticlass
9///
10/// \macro_code
11/// \macro_output
12/// \author Andreas Hoecker
13
14#include <cstdlib>
15#include <iostream>
16#include <map>
17#include <string>
18
19#include "TFile.h"
20#include "TTree.h"
21#include "TString.h"
22#include "TSystem.h"
23#include "TROOT.h"
24
25
26#include "TMVA/Tools.h"
27#include "TMVA/Factory.h"
28#include "TMVA/DataLoader.h"
30
31
32using namespace TMVA;
33
35{
36
37 // This loads the library
39
40 // to get access to the GUI and all tmva macros
41 //
42 // gROOT->ProcessLine("TMVA::TMVAMultiClassGui()");
43
44
45 //---------------------------------------------------------------
46 // Default MVA methods to be trained + tested
47 std::map<std::string,int> Use;
48 Use["MLP"] = 1;
49 Use["BDTG"] = 1;
50#ifdef R__HAS_TMVAGPU
51 Use["DL_CPU"] = 1;
52 Use["DL_GPU"] = 1;
53#else
54 Use["DL_CPU"] = 1;
55 Use["DL_GPU"] = 0;
56#endif
57 Use["FDA_GA"] = 0;
58 Use["PDEFoam"] = 1;
59
60 //---------------------------------------------------------------
61
62 std::cout << std::endl;
63 std::cout << "==> Start TMVAMulticlass" << std::endl;
64
65 if (myMethodList != "") {
66 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) it->second = 0;
67
68 std::vector<TString> mlist = TMVA::gTools().SplitString( myMethodList, ',' );
69 for (UInt_t i=0; i<mlist.size(); i++) {
70 std::string regMethod(mlist[i]);
71
72 if (Use.find(regMethod) == Use.end()) {
73 std::cout << "Method \"" << regMethod << "\" not known in TMVA under this name. Choose among the following:" << std::endl;
74 for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) std::cout << it->first << " ";
75 std::cout << std::endl;
76 return;
77 }
78 Use[regMethod] = 1;
79 }
80 }
81
82 // Create a new root output file.
83 TString outfileName = "TMVAMulticlass.root";
84 TFile* outputFile = TFile::Open( outfileName, "RECREATE" );
85
86 TMVA::Factory *factory = new TMVA::Factory( "TMVAMulticlass", outputFile,
87 "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=multiclass" );
89
90 dataloader->AddVariable( "var1", 'F' );
91 dataloader->AddVariable( "var2", "Variable 2", "", 'F' );
92 dataloader->AddVariable( "var3", "Variable 3", "units", 'F' );
93 dataloader->AddVariable( "var4", "Variable 4", "units", 'F' );
94
95 TFile *input(nullptr);
96 TString fname = gROOT->GetTutorialDir() + "/machine_learning/data/tmva_multiclass_example.root";
97 if (!gSystem->AccessPathName( fname )) {
98 input = TFile::Open( fname ); // check if file in local directory exists
99 }
100 if (!input) {
101 std::cout << "ERROR: could not open data file" << std::endl;
102 exit(1);
103 }
104 std::cout << "--- TMVAMulticlass: Using input file: " << input->GetName() << std::endl;
105
106 TTree *signalTree = (TTree*)input->Get("TreeS");
107 TTree *background0 = (TTree*)input->Get("TreeB0");
108 TTree *background1 = (TTree*)input->Get("TreeB1");
109 TTree *background2 = (TTree*)input->Get("TreeB2");
110
111 gROOT->cd( outfileName+TString(":/") );
112 dataloader->AddTree (signalTree,"Signal");
113 dataloader->AddTree (background0,"bg0");
114 dataloader->AddTree (background1,"bg1");
115 dataloader->AddTree (background2,"bg2");
116
117 dataloader->PrepareTrainingAndTestTree( "", "SplitMode=Random:NormMode=NumEvents:!V" );
118
119 if (Use["BDTG"]) // gradient boosted decision trees
120 factory->BookMethod( dataloader, TMVA::Types::kBDT, "BDTG", "!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.10:UseBaggedBoost:BaggedSampleFraction=0.50:nCuts=20:MaxDepth=2");
121 if (Use["MLP"]) // neural network
122 factory->BookMethod( dataloader, TMVA::Types::kMLP, "MLP", "!H:!V:NeuronType=tanh:NCycles=1000:HiddenLayers=N+5,5:TestRate=5:EstimatorType=MSE");
123 if (Use["FDA_GA"]) // functional discriminant with GA minimizer
124 factory->BookMethod( dataloader, TMVA::Types::kFDA, "FDA_GA", "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1);(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=GA:PopSize=300:Cycles=3:Steps=20:Trim=True:SaveBestGen=1" );
125 if (Use["PDEFoam"]) // PDE-Foam approach
126 factory->BookMethod( dataloader, TMVA::Types::kPDEFoam, "PDEFoam", "!H:!V:TailCut=0.001:VolFrac=0.0666:nActiveCells=500:nSampl=2000:nBin=5:Nmin=100:Kernel=None:Compress=T" );
127
128
129 if (Use["DL_CPU"]) {
130 TString layoutString("Layout=TANH|100,TANH|50,TANH|10,LINEAR");
131 TString trainingStrategyString("TrainingStrategy=Optimizer=ADAM,LearningRate=1e-3,"
132 "TestRepetitions=1,ConvergenceSteps=10,BatchSize=100,MaxEpochs=20");
133 TString nnOptions("!H:V:ErrorStrategy=CROSSENTROPY:VarTransform=N:"
134 "WeightInitialization=XAVIERUNIFORM:Architecture=GPU");
135 nnOptions.Append(":");
136 nnOptions.Append(layoutString);
137 nnOptions.Append(":");
139 factory->BookMethod(dataloader, TMVA::Types::kDL, "DL_CPU", nnOptions);
140 }
141 if (Use["DL_GPU"]) {
142 TString layoutString("Layout=TANH|100,TANH|50,TANH|10,LINEAR");
143 TString trainingStrategyString("TrainingStrategy=Optimizer=ADAM,LearningRate=1e-3,"
144 "TestRepetitions=1,ConvergenceSteps=10,BatchSize=100,MaxEpochs=20");
145 TString nnOptions("!H:V:ErrorStrategy=CROSSENTROPY:VarTransform=N:"
146 "WeightInitialization=XAVIERUNIFORM:Architecture=GPU");
147 nnOptions.Append(":");
148 nnOptions.Append(layoutString);
149 nnOptions.Append(":");
151 factory->BookMethod(dataloader, TMVA::Types::kDL, "DL_GPU", nnOptions);
152 }
153
154
155 // Train MVAs using the set of training events
156 factory->TrainAllMethods();
157
158 // Evaluate all MVAs using the set of test events
159 factory->TestAllMethods();
160
161 // Evaluate and compare performance of all configured MVAs
162 factory->EvaluateAllMethods();
163
164 // --------------------------------------------------------------
165
166 // Save the output
167 outputFile->Close();
168
169 std::cout << "==> Wrote root file: " << outputFile->GetName() << std::endl;
170 std::cout << "==> TMVAMulticlass is done!" << std::endl;
171
172 delete factory;
173 delete dataloader;
174
175 // Launch the GUI for the root macros
176 if (!gROOT->IsBatch()) TMVAMultiClassGui( outfileName );
177
178
179}
180
181int main( int argc, char** argv )
182{
183 // Select methods (don't look at this code - not of interest)
185 for (int i=1; i<argc; i++) {
187 if(regMethod=="-b" || regMethod=="--batch") continue;
188 if (!methodList.IsNull()) methodList += TString(",");
190 }
192 return 0;
193}
194
int main()
Definition Prototype.cxx:12
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:60
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
#define gROOT
Definition TROOT.h:411
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:3765
This is the main MVA steering class.
Definition Factory.h:80
void TrainAllMethods()
Iterates through all booked methods and calls training.
Definition Factory.cxx:1109
void TestAllMethods()
Evaluates all booked methods on the testing data and adds the output to the Results in the corresponi...
Definition Factory.cxx:1266
void EvaluateAllMethods(void)
Iterates over all MVAs that have been booked, and calls their evaluation methods.
Definition Factory.cxx:1371
MethodBase * BookMethod(DataLoader *loader, MethodName theMethodName, TString methodTitle, TString theOption="")
Books an MVA classifier or regression method.
Definition Factory.cxx:358
static Tools & Instance()
Definition Tools.cxx:71
std::vector< TString > SplitString(const TString &theOpt, const char separator) const
splits the option string at 'separator' and fills the list 'splitV' with the primitive strings
Definition Tools.cxx:1199
@ kPDEFoam
Definition Types.h:94
Basic string class.
Definition TString.h:138
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition TSystem.cxx:1309
A TTree represents a columnar dataset.
Definition TTree.h:89
create variable transformations
Tools & gTools()
void TMVAMultiClassGui(const char *fName="TMVAMulticlass.root", TString dataset="")