14 from keras.models import Sequential\n\
15 from keras.layers.core import Dense, Activation\n\
16 from keras import initializations\n\
17 from keras.optimizers import Adam\n\
19 model = Sequential()\n\
20 model.add(Dense(64, init=\"glorot_normal\", activation=\"relu\", input_dim=4))\n\
21 model.add(Dense(4, init=\"glorot_normal\", activation=\"softmax\"))\n\
22 model.compile(loss=\"categorical_crossentropy\", optimizer=Adam(), metrics=[\"accuracy\",])\n\
23 model.save(\"kerasModelMulticlass.h5\")\n";
27 std::cout <<
"Get test data..." << std::endl;
28 TString fname =
"./tmva_example_multiple_background.root";
30 std::cout <<
"Create multiclass test data..." << std::endl;
33 gROOT->ProcessLine(
"create_MultipleBackground(200)");
34 std::cout <<
"Created " << fname <<
" for tests of the multiclass features" << std::endl;
39 std::cout <<
"Generate keras model..." << std::endl;
41 ret =
gSystem->
Exec(
"echo '"+pythonSrc+
"' > generateKerasModelMulticlass.py");
43 std::cout <<
"[ERROR] Failed to write python code to file" << std::endl;
46 ret =
gSystem->
Exec(
"python generateKerasModelMulticlass.py");
48 std::cout <<
"[ERROR] Failed to generate model using python" << std::endl;
53 std::cout <<
"Setup TMVA..." << std::endl;
55 TFile* outputFile =
TFile::Open(
"ResultsTestPyKerasMulticlass.root",
"RECREATE");
57 "!V:Silent:Color:!DrawProgressBar:AnalysisType=multiclass");
66 dataloader->
AddTree(signal,
"Signal");
67 dataloader->
AddTree(background0,
"Background_0");
68 dataloader->
AddTree(background1,
"Background_1");
69 dataloader->
AddTree(background2,
"Background_2");
77 "SplitMode=Random:NormMode=NumEvents:!V");
81 "!H:!V:VarTransform=D,G:FilenameModel=kerasModelMulticlass.h5:FilenameTrainedModel=trainedKerasModelMulticlass.h5:NumEpochs=20:BatchSize=32:SaveBestOnly=false:Verbose=0");
82 std::cout <<
"Train model..." << std::endl;
92 std::cout <<
"Run reader and classify " << numEvents <<
" events..." << std::endl;
99 reader->
BookMVA(
"PyKeras",
"datasetTestPyKerasMulticlass/weights/testPyKerasMulticlass_PyKeras.weights.xml");
123 Float_t meanMvaBackground0 = 0;
124 Float_t meanMvaBackground1 = 0;
125 Float_t meanMvaBackground2 = 0;
126 for(
UInt_t i=0; i<numEvents; i++){
136 meanMvaSignal = meanMvaSignal/float(numEvents);
137 meanMvaBackground0 = meanMvaBackground0/float(numEvents);
138 meanMvaBackground1 = meanMvaBackground1/float(numEvents);
139 meanMvaBackground2 = meanMvaBackground2/float(numEvents);
142 std::cout <<
"Mean MVA response on signal: " << meanMvaSignal << std::endl;
143 if(meanMvaSignal < 0.3){
144 std::cout <<
"[ERROR] Mean response on signal is " << meanMvaSignal <<
" (<0.3)" << std::endl;
147 std::cout <<
"Mean MVA response on background 0: " << meanMvaBackground0 << std::endl;
148 if(meanMvaBackground0 < 0.3){
149 std::cout <<
"[ERROR] Mean response on background 0 is " << meanMvaBackground0 <<
" (<0.3)" << std::endl;
152 std::cout <<
"Mean MVA response on background 1: " << meanMvaBackground1 << std::endl;
153 if(meanMvaBackground0 < 0.3){
154 std::cout <<
"[ERROR] Mean response on background 1 is " << meanMvaBackground1 <<
" (<0.3)" << std::endl;
157 std::cout <<
"Mean MVA response on background 2: " << meanMvaBackground2 << std::endl;
158 if(meanMvaBackground0 < 0.3){
159 std::cout <<
"[ERROR] Mean response on background 2 is " << meanMvaBackground2 <<
" (<0.3)" << std::endl;
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
MethodBase * BookMethod(DataLoader *loader, TString theMethodName, TString methodTitle, TString theOption="")
Book a classifier or regression method.
void AddVariable(const TString &expression, Float_t *)
Add a float variable or expression to the reader.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
void TrainAllMethods()
Iterates through all booked methods and calls training.
void AddVariable(const TString &expression, const TString &title, const TString &unit, char type='F', Double_t min=0, Double_t max=0)
user inserts discriminating variable in data set info
static void PyInitialize()
Initialize Python interpreter.
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
const char * Data() const
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
IMethod * BookMVA(const TString &methodTag, const TString &weightfile)
read method name from weight file
int testPyKerasMulticlass()
R__EXTERN TSystem * gSystem
virtual Int_t Exec(const char *shellcmd)
Execute a command.
This is the main MVA steering class.
void AddTree(TTree *tree, const TString &className, Double_t weight=1.0, const TCut &cut="", Types::ETreeType tt=Types::kMaxTreeType)
void PrepareTrainingAndTestTree(const TCut &cut, const TString &splitOpt)
prepare the training and test trees -> same cuts for signal and background
The Reader class serves to use the MVAs in a specific analysis context.
A TTree object has a header with a name and a title.
int main(int argc, char **argv)
const std::vector< Float_t > & EvaluateMulticlass(const TString &methodTag, Double_t aux=0)
evaluates MVA for given set of input variables