Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TMVA_SOFIE_Keras_HiggsModel.py
Go to the documentation of this file.
1### \file
2### \ingroup tutorial_ml
3### \notebook -nodraw
4### This macro run the SOFIE parser on the Keras model
5### obtaining running TMVA_Higgs_Classification.C
6### You need to run that macro before this one
7###
8### \author Lorenzo Moneta
9
10
11from os.path import exists
12
13import numpy as np
14import ROOT
15from keras import layers, models
16from sklearn.model_selection import train_test_split
17
18
19def CreateModel(nlayers = 4, nunits = 64):
20 input = layers.Input(shape=(7,))
21 x = input
22 for i in range(1,nlayers) :
23 y = layers.Dense(nunits, activation='relu')(x)
24 x = y
25
26 output = layers.Dense(1, activation='sigmoid')(x)
27 model = models.Model(input, output)
28 model.compile(loss = 'binary_crossentropy', optimizer = 'adam', weighted_metrics = ['accuracy'])
30 return model
31
32def PrepareData() :
33 #get the input data
34 inputFile = str(ROOT.gROOT.GetTutorialDir()) + "/machine_learning/data/Higgs_data.root"
35
36 df1 = ROOT.RDataFrame("sig_tree", inputFile)
37 sigData = df1.AsNumpy(columns=['m_jj', 'm_jjj', 'm_lv', 'm_jlv', 'm_bb', 'm_wbb', 'm_wwbb'])
38 #print(sigData)
39
40 # stack all the 7 numpy array in a single array (nevents x nvars)
41 xsig = np.column_stack(list(sigData.values()))
42 data_sig_size = xsig.shape[0]
43 print("size of data", data_sig_size)
44
45 # make SOFIE inference on background data
46 df2 = ROOT.RDataFrame("bkg_tree", inputFile)
47 bkgData = df2.AsNumpy(columns=['m_jj', 'm_jjj', 'm_lv', 'm_jlv', 'm_bb', 'm_wbb', 'm_wwbb'])
48 xbkg = np.column_stack(list(bkgData.values()))
49 data_bkg_size = xbkg.shape[0]
50
51 ysig = np.ones(data_sig_size)
52 ybkg = np.zeros(data_bkg_size)
53 inputs_data = np.concatenate((xsig,xbkg),axis=0)
54 inputs_targets = np.concatenate((ysig,ybkg),axis=0)
55
56 #split data in training and test data
57
58 x_train, x_test, y_train, y_test = train_test_split(
59 inputs_data, inputs_targets, test_size=0.50, random_state=1234)
60
61 return x_train, y_train, x_test, y_test
62
63def TrainModel(model, x, y, name) :
64 model.fit(x,y,epochs=5,batch_size=50)
65 modelFile = name + '.keras'
66 model.save(modelFile)
67 return model, modelFile
68
69
70def GenerateCode(modelFile = "model.keras") :
71
72 #check if the input file exists
73 if not exists(modelFile):
74 raise FileNotFoundError("INput model file not existing. You need to run TMVA_Higgs_Classification.C to generate the Keras trained model")
75
76
77 #parse the input Keras model into RModel object (force batch size to be 1)
79
80 #Generating inference code
83
84 modelName = modelFile.replace(".keras","")
85 return modelName
86
87###################################################################
88## Step 1 : Create and Train model
89###################################################################
90
91x_train, y_train, x_test, y_test = PrepareData()
92#create dense model with 3 layers of 64 units
93model = CreateModel(3,64)
94model, modelFile = TrainModel(model,x_train, y_train, 'HiggsModel')
95
96###################################################################
97## Step 2 : Parse model and generate inference code with SOFIE
98###################################################################
99
100modelName = GenerateCode(modelFile)
101modelHeaderFile = modelName + ".hxx"
102
103###################################################################
104## Step 3 : Compile the generated C++ model code
105###################################################################
106
107ROOT.gInterpreter.Declare('#include "' + modelHeaderFile + '"')
108
109###################################################################
110## Step 4: Evaluate the model
111###################################################################
112
113#get first the SOFIE session namespace
114sofie = getattr(ROOT, 'TMVA_SOFIE_' + modelName)
115session = sofie.Session()
116
118y = session.infer(x)
119ykeras = model(x.reshape(1,7)).numpy()
120
121print("input to model is ",x, "\n\t -> output using SOFIE = ", y[0], " using Keras = ", ykeras[0])
122
123if (abs(y[0]-ykeras[0]) > 0.01) :
124 raise RuntimeError('ERROR: Result is different between SOFIE and Keras')
125
126print("OK")
127
128
129
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...