14 from keras.models import Sequential\n\ 15 from keras.layers.core import Dense, Activation\n\ 16 from keras.optimizers import Adam\n\ 18 model = Sequential()\n\ 19 model.add(Dense(64, activation=\"relu\", input_dim=4))\n\ 20 model.add(Dense(4, activation=\"softmax\"))\n\ 21 model.compile(loss=\"categorical_crossentropy\", optimizer=Adam(), metrics=[\"accuracy\",])\n\ 22 model.save(\"kerasModelMulticlass.h5\")\n";
26 std::cout <<
"Get test data..." << std::endl;
27 TString fname =
"./tmva_example_multiple_background.root";
29 std::cout <<
"Create multiclass test data..." << std::endl;
32 gROOT->ProcessLine(
"create_MultipleBackground(200)");
33 std::cout <<
"Created " << fname <<
" for tests of the multiclass features" << std::endl;
38 std::cout <<
"Generate keras model..." << std::endl;
40 ret =
gSystem->
Exec(
"echo '"+pythonSrc+
"' > generateKerasModelMulticlass.py");
42 std::cout <<
"[ERROR] Failed to write python code to file" << std::endl;
45 ret =
gSystem->
Exec(
"python generateKerasModelMulticlass.py");
47 std::cout <<
"[ERROR] Failed to generate model using python" << std::endl;
52 std::cout <<
"Setup TMVA..." << std::endl;
54 TFile* outputFile =
TFile::Open(
"ResultsTestPyKerasMulticlass.root",
"RECREATE");
56 "!V:Silent:Color:!DrawProgressBar:AnalysisType=multiclass");
65 dataloader->
AddTree(signal,
"Signal");
66 dataloader->
AddTree(background0,
"Background_0");
67 dataloader->
AddTree(background1,
"Background_1");
68 dataloader->
AddTree(background2,
"Background_2");
76 "SplitMode=Random:NormMode=NumEvents:!V");
80 "!H:!V:VarTransform=D,G:FilenameModel=kerasModelMulticlass.h5:FilenameTrainedModel=trainedKerasModelMulticlass.h5:NumEpochs=20:BatchSize=32:SaveBestOnly=false:Verbose=0");
81 std::cout <<
"Train model..." << std::endl;
91 std::cout <<
"Run reader and classify " << numEvents <<
" events..." << std::endl;
98 reader->
BookMVA(
"PyKeras",
"datasetTestPyKerasMulticlass/weights/testPyKerasMulticlass_PyKeras.weights.xml");
122 Float_t meanMvaBackground0 = 0;
123 Float_t meanMvaBackground1 = 0;
124 Float_t meanMvaBackground2 = 0;
125 for(
UInt_t i=0; i<numEvents; i++){
135 meanMvaSignal = meanMvaSignal/float(numEvents);
136 meanMvaBackground0 = meanMvaBackground0/float(numEvents);
137 meanMvaBackground1 = meanMvaBackground1/float(numEvents);
138 meanMvaBackground2 = meanMvaBackground2/float(numEvents);
141 std::cout <<
"Mean MVA response on signal: " << meanMvaSignal << std::endl;
142 if(meanMvaSignal < 0.3){
143 std::cout <<
"[ERROR] Mean response on signal is " << meanMvaSignal <<
" (<0.3)" << std::endl;
146 std::cout <<
"Mean MVA response on background 0: " << meanMvaBackground0 << std::endl;
147 if(meanMvaBackground0 < 0.3){
148 std::cout <<
"[ERROR] Mean response on background 0 is " << meanMvaBackground0 <<
" (<0.3)" << std::endl;
151 std::cout <<
"Mean MVA response on background 1: " << meanMvaBackground1 << std::endl;
152 if(meanMvaBackground0 < 0.3){
153 std::cout <<
"[ERROR] Mean response on background 1 is " << meanMvaBackground1 <<
" (<0.3)" << std::endl;
156 std::cout <<
"Mean MVA response on background 2: " << meanMvaBackground2 << std::endl;
157 if(meanMvaBackground0 < 0.3){
158 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.
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.
const std::vector< Float_t > & EvaluateMulticlass(const TString &methodTag, Double_t aux=0)
evaluates MVA for given set of input variables
const char * Data() const