ROOT
Version v6.32
master
v6.34
v6.30
v6.28
v6.26
v6.24
v6.22
v6.20
v6.18
v6.16
v6.14
v6.12
v6.10
v6.08
v6.06
Reference Guide
►
ROOT
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Modules
Pages
Loading...
Searching...
No Matches
RegressionKeras.py
Go to the documentation of this file.
1
#!/usr/bin/env python
2
## \file
3
## \ingroup tutorial_tmva_keras
4
## \notebook -nodraw
5
## This tutorial shows how to do regression in TMVA with neural networks
6
## trained with keras.
7
##
8
## \macro_code
9
##
10
## \date 2017
11
## \author TMVA Team
12
13
from
ROOT
import
TMVA, TFile, TTree, TCut, gROOT
14
from
subprocess
import
call
15
from
os.path
import
isfile
16
17
from
tensorflow.keras.models
import
Sequential
18
from
tensorflow.keras.layers
import
Dense, Activation
19
from
tensorflow.keras.optimizers
import
SGD
20
21
# Setup TMVA
22
TMVA.Tools.Instance
()
23
TMVA.PyMethodBase.PyInitialize
()
24
25
output =
TFile.Open
(
'TMVA_Regression_Keras.root'
,
'RECREATE'
)
26
factory =
TMVA.Factory
(
'TMVARegression'
, output,
27
'!V:!Silent:Color:DrawProgressBar:Transformations=D,G:AnalysisType=Regression'
)
28
29
# Load data
30
if
not
isfile
(
'tmva_reg_example.root'
):
31
call([
'curl'
,
'-L'
,
'-O'
,
'http://root.cern/files/tmva_reg_example.root'
])
32
33
data =
TFile.Open
(str(
gROOT.GetTutorialDir
()) +
'/tmva/data/tmva_reg_example.root'
)
34
tree =
data.Get
(
'TreeR'
)
35
36
dataloader =
TMVA.DataLoader
(
'dataset'
)
37
for
branch
in
tree.GetListOfBranches
():
38
name =
branch.GetName
()
39
if
name !=
'fvalue'
:
40
dataloader.AddVariable
(name)
41
dataloader.AddTarget
(
'fvalue'
)
42
43
dataloader.AddRegressionTree
(tree, 1.0)
44
#use only 1000 events since evaluation is very slow (especially on MacOS). Increase it to get meaningful results
45
dataloader.PrepareTrainingAndTestTree
(
TCut
(
''
),
46
'nTrain_Regression=1000:SplitMode=Random:NormMode=NumEvents:!V'
)
47
48
# Generate model
49
50
# Define model
51
model =
Sequential
()
52
model.add
(
Dense
(64, activation=
'tanh'
, input_dim=2))
53
model.add
(
Dense
(1, activation=
'linear'
))
54
55
# Set loss and optimizer
56
model.compile
(loss=
'mean_squared_error'
, optimizer=
SGD
(learning_rate=0.01), weighted_metrics=[])
57
58
# Store model to file
59
model.save
(
'modelRegression.h5'
)
60
model.summary
()
61
62
# Book methods
63
factory.BookMethod
(dataloader,
TMVA.Types.kPyKeras
,
'PyKeras'
,
64
'H:!V:VarTransform=D,G:FilenameModel=modelRegression.h5:FilenameTrainedModel=trainedModelRegression.h5:NumEpochs=20:BatchSize=32'
)
65
factory.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'
)
67
68
# Run TMVA
69
factory.TrainAllMethods
()
70
factory.TestAllMethods
()
71
factory.EvaluateAllMethods
()
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:358
ROOT::Detail::TRangeCast
Definition
TCollection.h:311
TCut
A specialized string object used for TTree selections.
Definition
TCut.h:25
TMVA::DataLoader
Definition
DataLoader.h:50
TMVA::Factory
This is the main MVA steering class.
Definition
Factory.h:80
tutorials
tmva
keras
RegressionKeras.py
ROOT v6-32 - Reference Guide Generated on Sat Apr 12 2025 15:11:09 (GVA Time) using Doxygen 1.10.0