13from ROOT
import TMVA, TFile, TTree, TCut
14from subprocess
import call
15from os.path
import isfile
17from tensorflow.keras.models
import Sequential
18from tensorflow.keras.layers
import Dense, Activation
19from tensorflow.keras.optimizers
import SGD
25output =
TFile.Open(
'TMVA_Regression_Keras.root',
'RECREATE')
27 '!V:!Silent:Color:DrawProgressBar:Transformations=D,G:AnalysisType=Regression')
30if not isfile(
'tmva_reg_example.root'):
31 call([
'curl',
'-L',
'-O',
'http://root.cern/files/tmva_reg_example.root'])
34tree = data.Get(
'TreeR')
37for branch
in tree.GetListOfBranches():
38 name = branch.GetName()
40 dataloader.AddVariable(name)
41dataloader.AddTarget(
'fvalue')
43dataloader.AddRegressionTree(tree, 1.0)
45dataloader.PrepareTrainingAndTestTree(
TCut(
''),
46 'nTrain_Regression=1000:SplitMode=Random:NormMode=NumEvents:!V')
52model.add(Dense(64, activation=
'tanh', input_dim=2))
53model.add(Dense(1, activation=
'linear'))
56model.compile(loss=
'mean_squared_error', optimizer=SGD(learning_rate=0.01), weighted_metrics=[])
59model.save(
'modelRegression.h5')
63factory.BookMethod(dataloader, TMVA.Types.kPyKeras,
'PyKeras',
64 'H:!V:VarTransform=D,G:FilenameModel=modelRegression.h5:FilenameTrainedModel=trainedModelRegression.h5:NumEpochs=20:BatchSize=32')
65factory.BookMethod(dataloader, TMVA.Types.kBDT,
'BDTG',
66 '!H:!V:VarTransform=D,G:NTrees=1000:BoostType=Grad:Shrinkage=0.1:UseBaggedBoost:BaggedSampleFraction=0.5:nCuts=20:MaxDepth=4')
69factory.TrainAllMethods()
70factory.TestAllMethods()
71factory.EvaluateAllMethods()
A specialized string object used for TTree selections.
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.
This is the main MVA steering class.
static void PyInitialize()
Initialize Python interpreter.