27if "imt" in ROOT.gROOT.GetConfigFeatures():
29 ROOT.gSystem.Setenv(
"OMP_NUM_THREADS",
"1")
32 print(
"Running in serial mode since ROOT does not support MT")
51def MakeTimeData(n, ntime, ndim):
55 fname =
"time_data_t" + str(ntime) +
"_d" + str(ndim) +
".root"
59 for i
in range(ntime):
60 v1.append(ROOT.TH1D(
"h1_" + str(i),
"h1", ndim, 0, 10))
61 v2.append(ROOT.TH1D(
"h2_" + str(i),
"h2", ndim, 0, 10))
63 f1 = ROOT.TF1(
"f1",
"gaus")
64 f2 = ROOT.TF1(
"f2",
"gaus")
66 sgn = ROOT.TTree(
"sgn",
"sgn")
67 bkg = ROOT.TTree(
"bkg",
"bkg")
68 f =
TFile(fname,
"RECREATE")
73 for i
in range(ntime):
74 x1.append(ROOT.std.vector[
"float"](ndim))
75 x2.append(ROOT.std.vector[
"float"](ndim))
77 for i
in range(ntime):
78 bkg.Branch(
"vars_time" + str(i),
"std::vector<float>", x1[i])
79 sgn.Branch(
"vars_time" + str(i),
"std::vector<float>", x2[i])
83 ROOT.gRandom.SetSeed(0)
85 mean1 = ROOT.std.vector[
"double"](ntime)
86 mean2 = ROOT.std.vector[
"double"](ntime)
87 sigma1 = ROOT.std.vector[
"double"](ntime)
88 sigma2 = ROOT.std.vector[
"double"](ntime)
90 for j
in range(ntime):
91 mean1[j] = 5.0 + 0.2 * ROOT.TMath.Sin(ROOT.TMath.Pi() * j / float(ntime))
92 mean2[j] = 5.0 + 0.2 * ROOT.TMath.Cos(ROOT.TMath.Pi() * j / float(ntime))
93 sigma1[j] = 4 + 0.3 * ROOT.TMath.Sin(ROOT.TMath.Pi() * j / float(ntime))
94 sigma2[j] = 4 + 0.3 * ROOT.TMath.Cos(ROOT.TMath.Pi() * j / float(ntime))
98 print(
"Generating event ... %d", i)
100 for j
in range(ntime):
106 f1.SetParameters(1, mean1[j], sigma1[j])
107 f2.SetParameters(1, mean2[j], sigma2[j])
109 h1.FillRandom(
"f1", 1000)
110 h2.FillRandom(
"f2", 1000)
112 for k
in range(ntime):
114 x1[j][k] = h1.GetBinContent(k + 1) + ROOT.gRandom.Gaus(0, 10)
115 x2[j][k] = h2.GetBinContent(k + 1) + ROOT.gRandom.Gaus(0, 10)
123 for j
in range(ntime):
126 for j
in range(ntime):
162tf_spec = importlib.util.find_spec(
"tensorflow")
165 ROOT.Warning(
"TMVA_RNN_Classificaton",
"Skip using Keras since tensorflow is not installed")
168rnn_types = [
"RNN",
"LSTM",
"GRU"]
169use_rnn_type = [1, 1, 1]
172 use_rnn_type = [0, 0, 0]
173 use_rnn_type[use_type] = 1
177useGPU =
"tmva-gpu" in ROOT.gROOT.GetConfigFeatures()
178useTMVA_RNN = (
"tmva-cpu" in ROOT.gROOT.GetConfigFeatures())
or useGPU
182 "TMVA_RNN_Classification",
183 "TMVA is not build with GPU or CPU multi-thread support. Cannot use TMVA Deep Learning for RNN",
186archString =
"GPU" if useGPU
else "CPU"
188writeOutputFile =
True
192if "tmva-pymva" in ROOT.gROOT.GetConfigFeatures():
199inputFileName =
"time_data_t10_d30.root"
201fileDoesNotExist = ROOT.gSystem.AccessPathName(inputFileName)
205 MakeTimeData(nTotEvts, ntime, ninput)
210 raise ROOT.Error(
"Error opening input file %s - exit", inputFileName.Data())
213print(
"--- RNNClassification : Using input file: {}".
format(inputFile.GetName()))
216outfileName =
"data_RNN_" + archString +
".root"
221 outputFile =
TFile.Open(outfileName,
"RECREATE")
245 "TMVAClassification",
250 DrawProgressBar=
True,
251 Transformations=
None,
253 AnalysisType=
"Classification",
254 ModelPersistence=
True,
258signalTree = inputFile.Get(
"sgn")
259background = inputFile.Get(
"bkg")
264for i
in range(ntime):
265 dataloader.AddVariablesArray(
"vars_time" + str(i), ninput)
268dataloader.AddSignalTree(signalTree, 1.0)
269dataloader.AddBackgroundTree(background, 1.0)
272datainfo = dataloader.GetDataSetInfo()
273vars = datainfo.GetListOfVariables()
274print(
"number of variables is {}".
format(vars.size()))
280nTrainSig = 0.8 * nTotEvts
281nTrainBkg = 0.8 * nTotEvts
288dataloader.PrepareTrainingAndTestTree(
291 nTrain_Signal=nTrainSig,
292 nTrain_Background=nTrainBkg,
295 NormMode=
"NumEvents",
297 CalcCorrelations=
False,
300print(
"prepared DATA LOADER ")
310 if not use_rnn_type[i]:
313 rnn_type = rnn_types[i]
317 rnnLayout = str(rnn_type) +
"|10|" + str(ninput) +
"|" + str(ntime) +
"|0|1,RESHAPE|FLAT,DENSE|64|TANH,LINEAR"
320 trainingString1 =
"LearningRate=1e-3,Momentum=0.0,Repetitions=1,ConvergenceSteps=5,BatchSize=" + str(batchSize)
321 trainingString1 +=
",TestRepetitions=1,WeightDecay=1e-2,Regularization=None,MaxEpochs=" + str(maxepochs)
322 trainingString1 +=
"Optimizer=ADAM,DropConfig=0.0+0.+0.+0."
330 rnnName =
"TMVA_" + str(rnn_type)
337 ErrorStrategy=
"CROSSENTROPY",
339 WeightInitialization=
"XAVIERUNIFORM",
342 InputLayout=str(ntime) +
"|" + str(ninput),
344 TrainingStrategy=trainingString1,
345 Architecture=archString
353 trainingString1 = ROOT.TString(
354 "LearningRate=1e-3,Momentum=0.0,Repetitions=1,"
355 "ConvergenceSteps=10,BatchSize=256,TestRepetitions=1,"
356 "WeightDecay=1e-4,Regularization=None,MaxEpochs=20"
357 "DropConfig=0.0+0.+0.+0.,Optimizer=ADAM:"
360 trainingString1.Append(archString)
368 ErrorStrategy=
"CROSSENTROPY",
370 WeightInitialization=
"XAVIER",
372 InputLayout=
"1|1|" + str(ntime * ninput),
373 Layout=
"DENSE|64|TANH,DENSE|TANH|64,DENSE|TANH|64,LINEAR",
374 TrainingStrategy=trainingString1
386 modelName =
"model_" + rnn_types[i] +
".h5"
387 trainedModelName =
"trained_" + modelName
388 print(
"Building recurrent keras model using a", rnn_types[i],
"layer")
391 from tensorflow.keras.models
import Sequential
392 from tensorflow.keras.optimizers
import Adam
396 from tensorflow.keras.layers
import Input, Dense, Dropout, Flatten, SimpleRNN, GRU, LSTM, Reshape, BatchNormalization
399 model.add(Reshape((10, 30), input_shape=(10 * 30,)))
401 if rnn_types[i] ==
"LSTM":
402 model.add(LSTM(units=10, return_sequences=
True))
403 elif rnn_types[i] ==
"GRU":
404 model.add(GRU(units=10, return_sequences=
True))
406 model.add(SimpleRNN(units=10, return_sequences=
True))
409 model.add(Dense(64, activation=
"tanh"))
410 model.add(Dense(2, activation=
"sigmoid"))
411 model.compile(loss=
"binary_crossentropy", optimizer=Adam(learning_rate=0.001), weighted_metrics=[
"accuracy"])
412 model.save(modelName)
414 print(
"saved recurrent model", modelName)
416 if not os.path.exists(modelName):
418 print(
"Error creating Keras recurrent model file - Skip using Keras")
421 print(
"Booking Keras model ", rnn_types[i])
425 "PyKeras_" + rnn_types[i],
429 FilenameModel=modelName,
430 FilenameTrainedModel=
"trained_" + modelName,
433 GpuOptions=
"allow_growth=True",
438if not useKeras
or not useTMVA_BDT:
457 BaggedSampleFraction=0.5,
464factory.TrainAllMethods()
469factory.TestAllMethods()
472factory.EvaluateAllMethods()
477c1 = 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 a suite of consecutive data records (TKey instances) with a well defined format.
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.