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
ClassificationKeras.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 classification 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_Classification_Keras.root'
,
'RECREATE'
)
26
factory =
TMVA.Factory
(
'TMVAClassification'
, output,
27
'!V:!Silent:Color:DrawProgressBar:Transformations=D,G:AnalysisType=Classification'
)
28
29
# Load data
30
data =
TFile.Open
(str(
gROOT.GetTutorialDir
()) +
'/tmva/data/tmva_class_example.root'
)
31
signal =
data.Get
(
'TreeS'
)
32
background =
data.Get
(
'TreeB'
)
33
34
dataloader =
TMVA.DataLoader
(
'dataset'
)
35
for
branch
in
signal.GetListOfBranches
():
36
dataloader.AddVariable
(
branch.GetName
())
37
38
dataloader.AddSignalTree
(signal, 1.0)
39
dataloader.AddBackgroundTree
(background, 1.0)
40
dataloader.PrepareTrainingAndTestTree
(
TCut
(
''
),
41
'nTrain_Signal=4000:nTrain_Background=4000:SplitMode=Random:NormMode=NumEvents:!V'
)
42
43
# Generate model
44
45
# Define model
46
model =
Sequential
()
47
model.add
(
Dense
(64, activation=
'relu'
, input_dim=4))
48
model.add
(
Dense
(2, activation=
'softmax'
))
49
50
# Set loss and optimizer
51
model.compile
(loss=
'categorical_crossentropy'
,
52
optimizer=
SGD
(learning_rate=0.01), weighted_metrics=[
'accuracy'
, ])
53
54
# Store model to file
55
model.save
(
'modelClassification.h5'
)
56
model.summary
()
57
58
# Book methods
59
factory.BookMethod
(dataloader,
TMVA.Types.kFisher
,
'Fisher'
,
60
'!H:!V:Fisher:VarTransform=D,G'
)
61
factory.BookMethod
(dataloader,
TMVA.Types.kPyKeras
,
'PyKeras'
,
62
'H:!V:VarTransform=D,G:FilenameModel=modelClassification.h5:FilenameTrainedModel=trainedModelClassification.h5:NumEpochs=20:BatchSize=32'
)
63
64
# Run training, test and evaluation
65
factory.TrainAllMethods
()
66
factory.TestAllMethods
()
67
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
ClassificationKeras.py
ROOT tags/v6-32-12 - Reference Guide Generated on Tue Apr 8 2025 06:01:07 (GVA Time) using Doxygen 1.10.0