13 from keras.models import Sequential\n\ 14 from keras.layers.core import Dense, Activation\n\ 15 from keras import initializations\n\ 16 from keras.optimizers import SGD\n\ 18 model = Sequential()\n\ 19 model.add(Dense(64, init=\"normal\", activation=\"relu\", input_dim=4))\n\ 20 model.add(Dense(2, init=\"normal\", activation=\"softmax\"))\n\ 21 model.compile(loss=\"categorical_crossentropy\", optimizer=SGD(lr=0.01), metrics=[\"accuracy\",])\n\ 22 model.save(\"kerasModelClassification.h5\")\n";
26 std::cout <<
"Get test data..." << std::endl;
27 TString fname =
"./tmva_class_example.root";
29 gSystem->
Exec(
"curl -O http://root.cern.ch/files/tmva_class_example.root");
33 std::cout <<
"Generate keras model..." << std::endl;
35 ret =
gSystem->
Exec(
"echo '"+pythonSrc+
"' > generateKerasModelClassification.py");
37 std::cout <<
"[ERROR] Failed to write python code to file" << std::endl;
40 ret =
gSystem->
Exec(
"python generateKerasModelClassification.py");
42 std::cout <<
"[ERROR] Failed to generate model using python" << std::endl;
47 std::cout <<
"Setup TMVA..." << std::endl;
49 TFile* outputFile =
TFile::Open(
"ResultsTestPyKerasClassification.root",
"RECREATE");
51 "!V:Silent:Color:!DrawProgressBar:AnalysisType=Classification");
67 "SplitMode=Random:NormMode=NumEvents:!V");
71 "!H:!V:VarTransform=D,G:FilenameModel=kerasModelClassification.h5:FilenameTrainedModel=trainedKerasModelClassification.h5:NumEpochs=10:BatchSize=32:SaveBestOnly=false:Verbose=0");
72 std::cout <<
"Train model..." << std::endl;
82 std::cout <<
"Run reader and classify " << numEvents <<
" events..." << std::endl;
89 reader->
BookMVA(
"PyKeras",
"datasetTestPyKerasClassification/weights/testPyKerasClassification_PyKeras.weights.xml");
104 for(
UInt_t i=0; i<numEvents; i++){
108 meanMvaBackground += reader->
EvaluateMVA(
"PyKeras");
110 meanMvaSignal = meanMvaSignal/float(numEvents);
111 meanMvaBackground = meanMvaBackground/float(numEvents);
114 std::cout <<
"Mean MVA response on signal: " << meanMvaSignal << std::endl;
115 if(meanMvaSignal < 0.6){
116 std::cout <<
"[ERROR] Mean response on signal is " << meanMvaSignal <<
" (<0.6)" << std::endl;
119 std::cout <<
"Mean MVA response on background: " << meanMvaBackground << std::endl;
120 if(meanMvaBackground > 0.4){
121 std::cout <<
"[ERROR] Mean response on background is " << meanMvaBackground <<
" (>0.4)" << std::endl;
void AddBackgroundTree(TTree *background, Double_t weight=1.0, Types::ETreeType treetype=Types::kMaxTreeType)
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="")
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)
static void PyInitialize()
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
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 testPyKerasClassification()
R__EXTERN TSystem * gSystem
virtual Int_t Exec(const char *shellcmd)
Execute a command.
void PrepareTrainingAndTestTree(const TCut &cut, const TString &splitOpt)
Double_t EvaluateMVA(const std::vector< Float_t > &, const TString &methodTag, Double_t aux=0)
Evaluate a std::vector<float> of input data for a given method The parameter aux is obligatory for th...
A TTree object has a header with a name and a title.
void AddSignalTree(TTree *signal, Double_t weight=1.0, Types::ETreeType treetype=Types::kMaxTreeType)