Logo ROOT  
Reference Guide
 
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, TCut, gROOT
from subprocess import call
from os.path import isfile
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# 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')
def run():
with TFile.Open('TMVA_Classification_Keras.root', 'RECREATE') as output, TFile.Open(str(gROOT.GetTutorialDir()) + '/machine_learning/data/tmva_class_example.root') as data:
factory = TMVA.Factory('TMVAClassification', output,
'!V:!Silent:Color:DrawProgressBar:Transformations=D,G:AnalysisType=Classification')
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')
# 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
if __name__ == "__main__":
# Setup TMVA
# Create and store the ML model
# Run TMVA
run()
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.