17from os.path
import exists
19ROOT.TMVA.PyMethodBase.PyInitialize()
25from tensorflow.keras.models
import Sequential
26from tensorflow.keras.layers
import Dense
27from tensorflow.keras.optimizers
import Adam
29from sklearn.model_selection
import train_test_split
31def CreateModel(nlayers = 4, nunits = 64):
33 model.add(Dense(nunits, activation=
'relu',input_dim=7))
34 for i
in range(1,nlayers) :
35 model.add(Dense(nunits, activation=
'relu'))
37 model.add(Dense(1, activation=
'sigmoid'))
38 model.compile(loss =
'binary_crossentropy', optimizer = Adam(learning_rate = 0.001), weighted_metrics = [
'accuracy'])
44 inputFileName =
"Higgs_data.root"
45 inputFile =
"http://root.cern.ch/files/" + inputFileName
48 sigData = df1.AsNumpy(columns=[
'm_jj',
'm_jjj',
'm_lv',
'm_jlv',
'm_bb',
'm_wbb',
'm_wwbb'])
52 xsig = np.column_stack(list(sigData.values()))
53 data_sig_size = xsig.shape[0]
54 print(
"size of data", data_sig_size)
58 bkgData = df2.AsNumpy(columns=[
'm_jj',
'm_jjj',
'm_lv',
'm_jlv',
'm_bb',
'm_wbb',
'm_wwbb'])
59 xbkg = np.column_stack(list(bkgData.values()))
60 data_bkg_size = xbkg.shape[0]
62 ysig = np.ones(data_sig_size)
63 ybkg = np.zeros(data_bkg_size)
64 inputs_data = np.concatenate((xsig,xbkg),axis=0)
65 inputs_targets = np.concatenate((ysig,ybkg),axis=0)
69 x_train, x_test, y_train, y_test = train_test_split(
70 inputs_data, inputs_targets, test_size=0.50, random_state=1234)
72 return x_train, y_train, x_test, y_test
74def TrainModel(model, x, y, name) :
75 model.fit(x,y,epochs=10,batch_size=50)
76 modelFile = name +
'.h5'
82x_train, y_train, x_test, y_test = PrepareData()
86model1 = TrainModel(CreateModel(4,64),x_train, y_train,
'Higgs_Model_4L_50')
87model2 = TrainModel(CreateModel(4,64),x_train, y_train,
'Higgs_Model_4L_200')
88model3 = TrainModel(CreateModel(4,64),x_train, y_train,
'Higgs_Model_2L_500')
93def GenerateModelCode(modelFile, generatedHeaderFile):
94 model = ROOT.TMVA.Experimental.SOFIE.PyKeras.Parse(modelFile)
96 print(
"Generating inference code for the Keras model from ",modelFile,
"in the header ", generatedHeaderFile)
98 model.Generate(ROOT.TMVA.Experimental.SOFIE.Options.kRootBinaryWeightFile)
100 model.OutputGenerated(generatedHeaderFile,
True)
102 return generatedHeaderFile
105generatedHeaderFile =
"Higgs_Model.hxx"
108if (os.path.exists(generatedHeaderFile)):
109 weightFile =
"Higgs_Model.root"
110 print(
"removing existing files", generatedHeaderFile,weightFile)
111 os.remove(generatedHeaderFile)
112 os.remove(weightFile)
114GenerateModelCode(model1, generatedHeaderFile)
115GenerateModelCode(model2, generatedHeaderFile)
116GenerateModelCode(model3, generatedHeaderFile)
120ROOT.gInterpreter.Declare(
'#include "' + generatedHeaderFile +
'"')
124session1 = ROOT.TMVA_SOFIE_Higgs_Model_4L_50.Session(
"Higgs_Model.root")
125session2 = ROOT.TMVA_SOFIE_Higgs_Model_4L_200.Session(
"Higgs_Model.root")
126session3 = ROOT.TMVA_SOFIE_Higgs_Model_2L_500.Session(
"Higgs_Model.root")
128hs1 = ROOT.TH1D(
"hs1",
"Signal result 4L 50",100,0,1)
129hs2 = ROOT.TH1D(
"hs2",
"Signal result 4L 200",100,0,1)
130hs3 = ROOT.TH1D(
"hs3",
"Signal result 2L 500",100,0,1)
132hb1 = ROOT.TH1D(
"hb1",
"Background result 4L 50",100,0,1)
133hb2 = ROOT.TH1D(
"hb2",
"Background result 4L 200",100,0,1)
134hb3 = ROOT.TH1D(
"hb3",
"Background result 2L 500",100,0,1)
136def EvalModel(session, x) :
137 result = session.infer(x)
140for i
in range(0,x_test.shape[0]):
141 result1 = EvalModel(session1, x_test[i,:])
142 result2 = EvalModel(session2, x_test[i,:])
143 result3 = EvalModel(session3, x_test[i,:])
144 if (y_test[i] == 1) :
153def PlotHistos(hs,hb):
154 hs.SetLineColor(ROOT.kRed)
155 hb.SetLineColor(ROOT.kBlue)
173 x = ROOT.std.vector[
'float'](n)
174 w = ROOT.std.vector[
'float'](n)
176 x[i] = h.GetBinCenter(i+1)
177 w[i] = h.GetBinContent(i+1)
180def MakeROCCurve(hs, hb) :
181 xs,ws = GetContent(hs)
182 xb,wb = GetContent(hb)
183 roc = ROOT.TMVA.ROCCurve(xs,xb,ws,wb)
184 print(
"ROC integral for ",hs.GetName(), roc.GetROCIntegral())
185 curve = roc.GetROCCurve()
186 curve.SetName(hs.GetName())
191r1,curve1 = MakeROCCurve(hs1,hb1)
192curve1.SetLineColor(ROOT.kRed)
195r2,curve2 = MakeROCCurve(hs2,hb2)
196curve2.SetLineColor(ROOT.kBlue)
199r3,curve3 = MakeROCCurve(hs3,hb3)
200curve3.SetLineColor(ROOT.kGreen)
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...