Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
ClassificationKeras.py File Reference

Detailed Description

View in nbviewer Open in SWAN
This tutorial shows how to do classification in TMVA with neural networks trained with keras.

from ROOT import TMVA, TFile, TTree, TCut, gROOT
from subprocess import call
from os.path import isfile
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
# Setup TMVA
output = TFile.Open('TMVA_Classification_Keras.root', 'RECREATE')
factory = TMVA.Factory('TMVAClassification', output,
'!V:!Silent:Color:DrawProgressBar:Transformations=D,G:AnalysisType=Classification')
# Load data
data = TFile.Open(str(gROOT.GetTutorialDir()) + '/tmva/data/tmva_class_example.root')
signal = data.Get('TreeS')
background = data.Get('TreeB')
dataloader = TMVA.DataLoader('dataset')
for branch in signal.GetListOfBranches():
'nTrain_Signal=4000:nTrain_Background=4000:SplitMode=Random:NormMode=NumEvents:!V')
# Generate model
# Define model
model = Sequential()
model.add(Dense(64, activation='relu', input_dim=4))
model.add(Dense(2, activation='softmax'))
# Set loss and optimizer
model.compile(loss='categorical_crossentropy',
optimizer=SGD(learning_rate=0.01), weighted_metrics=['accuracy', ])
# Store model to file
model.save('modelClassification.h5')
# Book methods
factory.BookMethod(dataloader, TMVA.Types.kFisher, 'Fisher',
'!H:!V:Fisher:VarTransform=D,G')
factory.BookMethod(dataloader, TMVA.Types.kPyKeras, 'PyKeras',
'H:!V:VarTransform=D,G:FilenameModel=modelClassification.h5:FilenameTrainedModel=trainedModelClassification.h5:NumEpochs=20:BatchSize=32')
# Run training, test and evaluation
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
A specialized string object used for TTree selections.
Definition TCut.h:25
This is the main MVA steering class.
Definition Factory.h:80
Date
2017
Author
TMVA Team

Definition in file ClassificationKeras.py.