41void MakeTimeData(
int n,
int ntime,
int ndim )
47 std::vector<TH1 *>
v1(ntime);
48 std::vector<TH1 *>
v2(ntime);
50 for (
int i = 0; i < ntime; ++i) {
55 auto f1 =
new TF1(
"f1",
"gaus");
56 auto f2 =
new TF1(
"f2",
"gaus");
58 TFile f(fname,
"RECREATE");
59 TTree sgn(
"sgn",
"sgn");
60 TTree bkg(
"bkg",
"bkg");
62 std::vector<std::vector<float>>
x1(ntime);
63 std::vector<std::vector<float>>
x2(ntime);
65 for (
int i = 0; i < ntime; ++i) {
66 x1[i] = std::vector<float>(ndim);
67 x2[i] = std::vector<float>(ndim);
70 for (
auto i = 0; i < ntime; i++) {
71 bkg.Branch(
Form(
"vars_time%d", i),
"std::vector<float>", &
x1[i]);
72 sgn.Branch(
Form(
"vars_time%d", i),
"std::vector<float>", &
x2[i]);
79 std::vector<double> mean1(ntime);
80 std::vector<double> mean2(ntime);
81 std::vector<double> sigma1(ntime);
82 std::vector<double> sigma2(ntime);
83 for (
int j = 0; j < ntime; ++j) {
84 mean1[j] = 5. + 0.2 *
sin(
TMath::Pi() * j /
double(ntime));
85 mean2[j] = 5. + 0.2 *
cos(
TMath::Pi() * j /
double(ntime));
86 sigma1[j] = 4 + 0.3 *
sin(
TMath::Pi() * j /
double(ntime));
87 sigma2[j] = 4 + 0.3 *
cos(
TMath::Pi() * j /
double(ntime));
89 for (
int i = 0; i <
n; ++i) {
92 std::cout <<
"Generating event ... " << i << std::endl;
94 for (
int j = 0; j < ntime; ++j) {
101 f2->SetParameters(1, mean2[j], sigma2[j]);
104 h2->FillRandom(
"f2", 1000);
106 for (
int k = 0; k < ndim; ++k) {
118 c1->Divide(ntime, 2);
119 for (
int j = 0; j < ntime; ++j) {
123 for (
int j = 0; j < ntime; ++j) {
124 c1->cd(ntime + j + 1);
149 const int ninput = 30;
150 const int ntime = 10;
151 const int batchSize = 100;
152 const int maxepochs = 20;
154 int nTotEvts = nevts;
156 bool useKeras =
true;
159 bool useTMVA_RNN =
true;
160 bool useTMVA_DNN =
true;
161 bool useTMVA_BDT =
false;
163 std::vector<std::string> rnn_types = {
"RNN",
"LSTM",
"GRU"};
164 std::vector<bool> use_rnn_type = {1, 1, 1};
165 if (use_type >=0 && use_type < 3) {
166 use_rnn_type = {0,0,0};
167 use_rnn_type[use_type] = 1;
171#ifndef R__HAS_TMVAGPU
173#ifndef R__HAS_TMVACPU
174 Warning(
"TMVA_RNN_Classification",
"TMVA is not build with GPU or CPU multi-thread support. Cannot use TMVA Deep Learning for RNN");
180 TString archString = (useGPU) ?
"GPU" :
"CPU";
182 bool writeOutputFile =
true;
186 const char *rnn_type =
"RNN";
197 if (num_threads >= 0) {
205 TString inputFileName =
"time_data_t10_d30.root";
211 MakeTimeData(nTotEvts,ntime, ninput);
217 Error(
"TMVA_RNN_Classification",
"Error opening input file %s - exit", inputFileName.
Data());
222 std::cout <<
"--- RNNClassification : Using input file: " << inputFile->GetName() << std::endl;
226 TFile *outputFile =
nullptr;
227 if (writeOutputFile) outputFile =
TFile::Open(outfileName,
"RECREATE");
252 "!V:!Silent:Color:DrawProgressBar:Transformations=None:!Correlations:"
253 "AnalysisType=Classification:ModelPersistence");
256 TTree *signalTree = (
TTree *)inputFile->Get(
"sgn");
257 TTree *background = (
TTree *)inputFile->Get(
"bkg");
259 const int nvar = ninput * ntime;
262 for (
auto i = 0; i < ntime; i++) {
272 std::cout <<
"number of variables is " << vars.size() << std::endl;
274 std::cout <<
v <<
",";
275 std::cout << std::endl;
277 int nTrainSig = 0.8 * nTotEvts;
278 int nTrainBkg = 0.8 * nTotEvts;
281 TString prepareOptions =
TString::Format(
"nTrain_Signal=%d:nTrain_Background=%d:SplitMode=Random:SplitSeed=100:NormMode=NumEvents:!V:!CalcCorrelations", nTrainSig, nTrainBkg);
289 std::cout <<
"prepared DATA LOADER " << std::endl;
300 for (
int i = 0; i < 3; ++i) {
302 if (!use_rnn_type[i])
305 const char *rnn_type = rnn_types[i].c_str();
319 TString layoutString =
TString(
"Layout=") + rnnLayout +
TString(
",RESHAPE|FLAT,DENSE|64|TANH,LINEAR");
323 "ConvergenceSteps=5,BatchSize=%d,TestRepetitions=1,"
324 "WeightDecay=1e-2,Regularization=None,MaxEpochs=%d,"
325 "Optimizer=ADAM,DropConfig=0.0+0.+0.+0.",
326 batchSize,maxepochs);
328 TString trainingStrategyString(
"TrainingStrategy=");
329 trainingStrategyString += trainingString1;
332 TString rnnOptions(
"!H:V:ErrorStrategy=CROSSENTROPY:VarTransform=None:"
333 "WeightInitialization=XAVIERUNIFORM:ValidationSize=0.2:RandomSeed=1234");
335 rnnOptions.Append(
":");
336 rnnOptions.Append(inputLayoutString);
337 rnnOptions.Append(
":");
338 rnnOptions.Append(layoutString);
339 rnnOptions.Append(
":");
340 rnnOptions.Append(trainingStrategyString);
341 rnnOptions.Append(
":");
359 TString layoutString(
"Layout=DENSE|64|TANH,DENSE|TANH|64,DENSE|TANH|64,LINEAR");
361 TString trainingString1(
"LearningRate=1e-3,Momentum=0.0,Repetitions=1,"
362 "ConvergenceSteps=10,BatchSize=256,TestRepetitions=1,"
363 "WeightDecay=1e-4,Regularization=None,MaxEpochs=20"
364 "DropConfig=0.0+0.+0.+0.,Optimizer=ADAM");
365 TString trainingStrategyString(
"TrainingStrategy=");
366 trainingStrategyString += trainingString1;
369 TString dnnOptions(
"!H:V:ErrorStrategy=CROSSENTROPY:VarTransform=None:"
370 "WeightInitialization=XAVIER:RandomSeed=0");
372 dnnOptions.Append(
":");
373 dnnOptions.Append(inputLayoutString);
374 dnnOptions.Append(
":");
375 dnnOptions.Append(layoutString);
376 dnnOptions.Append(
":");
377 dnnOptions.Append(trainingStrategyString);
378 dnnOptions.Append(
":");
379 dnnOptions.Append(archString);
394 for (
int i = 0; i < 3; i++) {
396 if (use_rnn_type[i]) {
401 Info(
"TMVA_RNN_Classification",
"Building recurrent keras model using a %s layer", rnn_types[i].c_str());
405 m.AddLine(
"import tensorflow");
406 m.AddLine(
"from tensorflow.keras.models import Sequential");
407 m.AddLine(
"from tensorflow.keras.optimizers import Adam");
408 m.AddLine(
"from tensorflow.keras.layers import Input, Dense, Dropout, Flatten, SimpleRNN, GRU, LSTM, Reshape, "
409 "BatchNormalization");
411 m.AddLine(
"model = Sequential() ");
412 m.AddLine(
"model.add(Reshape((10, 30), input_shape = (10*30, )))");
414 if (rnn_types[i] ==
"LSTM")
415 m.AddLine(
"model.add(LSTM(units=10, return_sequences=True) )");
416 else if (rnn_types[i] ==
"GRU")
417 m.AddLine(
"model.add(GRU(units=10, return_sequences=True) )");
419 m.AddLine(
"model.add(SimpleRNN(units=10, return_sequences=True) )");
422 m.AddLine(
"model.add(Flatten())");
423 m.AddLine(
"model.add(Dense(64, activation = 'tanh')) ");
424 m.AddLine(
"model.add(Dense(2, activation = 'sigmoid')) ");
426 "model.compile(loss = 'binary_crossentropy', optimizer = Adam(learning_rate = 0.001), weighted_metrics = ['accuracy'])");
428 m.AddLine(
"model.save(modelName)");
429 m.AddLine(
"model.summary()");
431 m.SaveSource(
"make_rnn_model.py");
433 auto ret = (
TString *)
gROOT->ProcessLine(
"TMVA::Python_Executable()");
434 TString python_exe = (ret) ? *(ret) :
"python";
438 Warning(
"TMVA_RNN_Classification",
"Error creating Keras recurrent model file - Skip using Keras");
442 Info(
"TMVA_RNN_Classification",
"Booking Keras %s model", rnn_types[i].c_str());
446 "FilenameTrainedModel=%s:GpuOptions=allow_growth=True:"
447 "NumEpochs=%d:BatchSize=%d",
448 modelName.
Data(), trainedModelName.
Data(), maxepochs, batchSize));
455 if (!useKeras || !useTMVA_BDT)
465 "!H:!V:NTrees=100:MinNodeSize=2.5%:BoostType=Grad:Shrinkage=0.10:UseBaggedBoost:"
466 "BaggedSampleFraction=0.5:nCuts=20:"
488 if (outputFile) outputFile->
Close();
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
R__EXTERN TRandom * gRandom
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
R__EXTERN TSystem * gSystem
A specialized string object used for TTree selections.
virtual void SetParameters(const Double_t *params)
A ROOT file is composed of a header, followed by consecutive data records (TKey instances) with a wel...
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.
void Close(Option_t *option="") override
Close a file.
void Draw(Option_t *chopt="") override
Draw this graph with its current attributes.
1-D histogram with a double per channel (see TH1 documentation)}
void Reset(Option_t *option="") override
Reset.
virtual void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng=nullptr)
Fill histogram following distribution in function fname.
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
static Config & Instance()
static function: returns TMVA instance
void AddVariablesArray(const TString &expression, int size, char type='F', Double_t min=0, Double_t max=0)
user inserts discriminating array of variables in data set info in case input tree provides an array ...
void AddSignalTree(TTree *signal, Double_t weight=1.0, Types::ETreeType treetype=Types::kMaxTreeType)
number of signal events (used to compute significance)
void PrepareTrainingAndTestTree(const TCut &cut, const TString &splitOpt)
prepare the training and test trees -> same cuts for signal and background
void AddBackgroundTree(TTree *background, Double_t weight=1.0, Types::ETreeType treetype=Types::kMaxTreeType)
number of signal events (used to compute significance)
DataSetInfo & GetDataSetInfo()
std::vector< TString > GetListOfVariables() const
returns list of variables
This is the main MVA steering class.
void TrainAllMethods()
Iterates through all booked methods and calls training.
MethodBase * BookMethod(DataLoader *loader, TString theMethodName, TString methodTitle, TString theOption="")
Book a classifier or regression method.
void TestAllMethods()
Evaluates all booked methods on the testing data and adds the output to the Results in the corresponi...
void EvaluateAllMethods(void)
Iterates over all MVAs that have been booked, and calls their evaluation methods.
TGraph * GetROCCurve(DataLoader *loader, TString theMethodName, Bool_t setTitles=kTRUE, UInt_t iClass=0, Types::ETreeType type=Types::kTesting)
Argument iClass specifies the class to generate the ROC curve in a multiclass setting.
static void PyInitialize()
Initialize Python interpreter.
Class supporting a collection of lines with C++ code.
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
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 Exec(const char *shellcmd)
Execute a command.
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
virtual void Setenv(const char *name, const char *value)
Set environment variable.
A TTree represents a columnar dataset.
RVec< PromoteType< T > > cos(const RVec< T > &v)
RVec< PromoteType< T > > sin(const RVec< T > &v)
void EnableImplicitMT(UInt_t numthreads=0)
Enable ROOT's implicit multi-threading for all objects and methods that provide an internal paralleli...
UInt_t GetThreadPoolSize()
Returns the size of ROOT's thread pool.