27if "imt" in ROOT.gROOT.GetConfigFeatures():
30 ROOT.gSystem.Setenv(
"OMP_NUM_THREADS",
"1")
33 print(
"Running in serial mode since ROOT does not support MT")
52def MakeTimeData(n, ntime, ndim):
56 fname =
"time_data_t" + str(ntime) +
"_d" + str(ndim) +
".root"
60 for i
in range(ntime):
61 v1.append(ROOT.TH1D(
"h1_" + str(i),
"h1", ndim, 0, 10))
62 v2.append(ROOT.TH1D(
"h2_" + str(i),
"h2", ndim, 0, 10))
64 f1 = ROOT.TF1(
"f1",
"gaus")
65 f2 = ROOT.TF1(
"f2",
"gaus")
67 sgn = ROOT.TTree(
"sgn",
"sgn")
68 bkg = ROOT.TTree(
"bkg",
"bkg")
69 f =
TFile(fname,
"RECREATE")
74 for i
in range(ntime):
75 x1.append(ROOT.std.vector[
"float"](ndim))
76 x2.append(ROOT.std.vector[
"float"](ndim))
78 for i
in range(ntime):
79 bkg.Branch(
"vars_time" + str(i),
"std::vector<float>", x1[i])
80 sgn.Branch(
"vars_time" + str(i),
"std::vector<float>", x2[i])
84 ROOT.gRandom.SetSeed(0)
86 mean1 = ROOT.std.vector[
"double"](ntime)
87 mean2 = ROOT.std.vector[
"double"](ntime)
88 sigma1 = ROOT.std.vector[
"double"](ntime)
89 sigma2 = ROOT.std.vector[
"double"](ntime)
91 for j
in range(ntime):
92 mean1[j] = 5.0 + 0.2 * ROOT.TMath.Sin(ROOT.TMath.Pi() * j / float(ntime))
93 mean2[j] = 5.0 + 0.2 * ROOT.TMath.Cos(ROOT.TMath.Pi() * j / float(ntime))
94 sigma1[j] = 4 + 0.3 * ROOT.TMath.Sin(ROOT.TMath.Pi() * j / float(ntime))
95 sigma2[j] = 4 + 0.3 * ROOT.TMath.Cos(ROOT.TMath.Pi() * j / float(ntime))
99 print(
"Generating event ... %d", i)
101 for j
in range(ntime):
107 f1.SetParameters(1, mean1[j], sigma1[j])
108 f2.SetParameters(1, mean2[j], sigma2[j])
110 h1.FillRandom(
"f1", 1000)
111 h2.FillRandom(
"f2", 1000)
113 for k
in range(ntime):
115 x1[j][k] = h1.GetBinContent(k + 1) + ROOT.gRandom.Gaus(0, 10)
116 x2[j][k] = h2.GetBinContent(k + 1) + ROOT.gRandom.Gaus(0, 10)
124 for j
in range(ntime):
127 for j
in range(ntime):
163tf_spec = importlib.util.find_spec(
"tensorflow")
166 ROOT.Warning(
"TMVA_RNN_Classificaton",
"Skip using Keras since tensorflow is not installed")
169rnn_types = [
"RNN",
"LSTM",
"GRU"]
170use_rnn_type = [1, 1, 1]
173 use_rnn_type = [0, 0, 0]
174 use_rnn_type[use_type] = 1
178useGPU =
"tmva-gpu" in ROOT.gROOT.GetConfigFeatures()
179useTMVA_RNN = (
"tmva-cpu" in ROOT.gROOT.GetConfigFeatures())
or useGPU
183 "TMVA_RNN_Classification",
184 "TMVA is not build with GPU or CPU multi-thread support. Cannot use TMVA Deep Learning for RNN",
187archString =
"GPU" if useGPU
else "CPU"
189writeOutputFile =
True
193if "tmva-pymva" in ROOT.gROOT.GetConfigFeatures():
200inputFileName =
"time_data_t10_d30.root"
202fileDoesNotExist = ROOT.gSystem.AccessPathName(inputFileName)
206 MakeTimeData(nTotEvts, ntime, ninput)
211 raise ROOT.Error(
"Error opening input file %s - exit", inputFileName.Data())
214print(
"--- RNNClassification : Using input file: {}".
format(inputFile.GetName()))
217outfileName =
"data_RNN_" + archString +
".root"
222 outputFile =
TFile.Open(outfileName,
"RECREATE")
246 "TMVAClassification",
251 DrawProgressBar=
True,
252 Transformations=
None,
254 AnalysisType=
"Classification",
255 ModelPersistence=
True,
259signalTree = inputFile.Get(
"sgn")
260background = inputFile.Get(
"bkg")
265for i
in range(ntime):
266 dataloader.AddVariablesArray(
"vars_time" + str(i), ninput)
269dataloader.AddSignalTree(signalTree, 1.0)
270dataloader.AddBackgroundTree(background, 1.0)
273datainfo = dataloader.GetDataSetInfo()
274vars = datainfo.GetListOfVariables()
275print(
"number of variables is {}".
format(vars.size()))
281nTrainSig = 0.8 * nTotEvts
282nTrainBkg = 0.8 * nTotEvts
289dataloader.PrepareTrainingAndTestTree(
292 nTrain_Signal=nTrainSig,
293 nTrain_Background=nTrainBkg,
296 NormMode=
"NumEvents",
298 CalcCorrelations=
False,
301print(
"prepared DATA LOADER ")
311 if not use_rnn_type[i]:
314 rnn_type = rnn_types[i]
318 rnnLayout = str(rnn_type) +
"|10|" + str(ninput) +
"|" + str(ntime) +
"|0|1,RESHAPE|FLAT,DENSE|64|TANH,LINEAR"
321 trainingString1 =
"LearningRate=1e-3,Momentum=0.0,Repetitions=1,ConvergenceSteps=5,BatchSize=" + str(batchSize)
322 trainingString1 +=
",TestRepetitions=1,WeightDecay=1e-2,Regularization=None,MaxEpochs=" + str(maxepochs)
323 trainingString1 +=
"Optimizer=ADAM,DropConfig=0.0+0.+0.+0."
331 rnnName =
"TMVA_" + str(rnn_type)
338 ErrorStrategy=
"CROSSENTROPY",
340 WeightInitialization=
"XAVIERUNIFORM",
343 InputLayout=str(ntime) +
"|" + str(ninput),
345 TrainingStrategy=trainingString1,
346 Architecture=archString
354 trainingString1 = ROOT.TString(
355 "LearningRate=1e-3,Momentum=0.0,Repetitions=1,"
356 "ConvergenceSteps=10,BatchSize=256,TestRepetitions=1,"
357 "WeightDecay=1e-4,Regularization=None,MaxEpochs=20"
358 "DropConfig=0.0+0.+0.+0.,Optimizer=ADAM:"
361 trainingString1.Append(archString)
369 ErrorStrategy=
"CROSSENTROPY",
371 WeightInitialization=
"XAVIER",
373 InputLayout=
"1|1|" + str(ntime * ninput),
374 Layout=
"DENSE|64|TANH,DENSE|TANH|64,DENSE|TANH|64,LINEAR",
375 TrainingStrategy=trainingString1
387 modelName =
"model_" + rnn_types[i] +
".h5"
388 trainedModelName =
"trained_" + modelName
389 print(
"Building recurrent keras model using a", rnn_types[i],
"layer")
392 from tensorflow.keras.models
import Sequential
393 from tensorflow.keras.optimizers
import Adam
397 from tensorflow.keras.layers
import Input, Dense, Dropout, Flatten, SimpleRNN, GRU, LSTM, Reshape, BatchNormalization
400 model.add(Reshape((10, 30), input_shape=(10 * 30,)))
402 if rnn_types[i] ==
"LSTM":
403 model.add(LSTM(units=10, return_sequences=
True))
404 elif rnn_types[i] ==
"GRU":
405 model.add(GRU(units=10, return_sequences=
True))
407 model.add(SimpleRNN(units=10, return_sequences=
True))
410 model.add(Dense(64, activation=
"tanh"))
411 model.add(Dense(2, activation=
"sigmoid"))
412 model.compile(loss=
"binary_crossentropy", optimizer=Adam(learning_rate=0.001), weighted_metrics=[
"accuracy"])
413 model.save(modelName)
415 print(
"saved recurrent model", modelName)
417 if not os.path.exists(modelName):
419 print(
"Error creating Keras recurrent model file - Skip using Keras")
422 print(
"Booking Keras model ", rnn_types[i])
426 "PyKeras_" + rnn_types[i],
430 FilenameModel=modelName,
431 FilenameTrainedModel=
"trained_" + modelName,
434 GpuOptions=
"allow_growth=True",
439if not useKeras
or not useTMVA_BDT:
458 BaggedSampleFraction=0.5,
465factory.TrainAllMethods()
470factory.TestAllMethods()
473factory.EvaluateAllMethods()
478c1 = factory.GetROCCurve(dataloader)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t format
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
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.
static Config & Instance()
static function: returns TMVA instance
This is the main MVA steering class.
static void PyInitialize()
Initialize Python interpreter.
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.