1616from sklearn .model_selection import train_test_split
1717
1818
19- def 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
19+ def 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
2525
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' ])
29- model .summary ()
30- return model
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" ])
29+ model .summary ()
30+ return model
3131
32- def PrepareData () :
33- #get the input data
34- inputFile = str (ROOT .gROOT .GetTutorialDir ()) + "/machine_learning/data/Higgs_data.root"
3532
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)
33+ def PrepareData ():
34+ # get the input data
35+ inputFile = str ( ROOT . gROOT . GetTutorialDir ()) + "/machine_learning/data/Higgs_data.root"
3936
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 )
37+ df1 = ROOT .RDataFrame ("sig_tree" , inputFile )
38+ sigData = df1 .AsNumpy (columns = ["m_jj" , "m_jjj" , "m_lv" , "m_jlv" , "m_bb" , "m_wbb" , "m_wwbb" ])
39+ # print(sigData)
4440
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 ]
41+ # stack all the 7 numpy array in a single array (nevents x nvars)
42+ xsig = np .column_stack (list (sigData .values ()))
43+ data_sig_size = xsig .shape [0 ]
44+ print ("size of data" , data_sig_size )
5045
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 )
46+ # make SOFIE inference on background data
47+ df2 = ROOT .RDataFrame ("bkg_tree" , inputFile )
48+ bkgData = df2 .AsNumpy (columns = ["m_jj" , "m_jjj" , "m_lv" , "m_jlv" , "m_bb" , "m_wbb" , "m_wwbb" ])
49+ xbkg = np .column_stack (list (bkgData .values ()))
50+ data_bkg_size = xbkg .shape [0 ]
5551
56- #split data in training and test data
52+ ysig = np .ones (data_sig_size )
53+ ybkg = np .zeros (data_bkg_size )
54+ inputs_data = np .concatenate ((xsig , xbkg ), axis = 0 )
55+ inputs_targets = np .concatenate ((ysig , ybkg ), axis = 0 )
5756
58- x_train , x_test , y_train , y_test = train_test_split (
59- inputs_data , inputs_targets , test_size = 0.50 , random_state = 1234 )
57+ # split data in training and test data
6058
61- return x_train , y_train , x_test , y_test
59+ x_train , x_test , y_train , y_test = train_test_split ( inputs_data , inputs_targets , test_size = 0.50 , random_state = 1234 )
6260
63- def 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
61+ return x_train , y_train , x_test , y_test
6862
6963
70- def GenerateCode (modelFile = "model.keras" ) :
64+ def TrainModel (model , x , y , name ):
65+ model .fit (x , y , epochs = 5 , batch_size = 50 )
66+ modelFile = name + ".keras"
67+ model .save (modelFile )
68+ return model , modelFile
7169
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" )
7570
71+ def GenerateCode (modelFile = "model.keras" ):
7672
77- #parse the input Keras model into RModel object (force batch size to be 1)
78- model = ROOT .TMVA .Experimental .SOFIE .PyKeras .Parse (modelFile )
73+ # check if the input file exists
74+ if not exists (modelFile ):
75+ raise FileNotFoundError (
76+ "INput model file not existing. You need to run TMVA_Higgs_Classification.C to generate the Keras trained model"
77+ )
7978
80- #Generating inference code
81- model .Generate ()
82- model .OutputGenerated ()
79+ # parse the input Keras model into RModel object (force batch size to be 1)
80+ model = ROOT .TMVA .Experimental .SOFIE .PyKeras .Parse (modelFile )
81+
82+ # Generating inference code
83+ model .Generate ()
84+ model .OutputGenerated ()
85+
86+ modelName = modelFile .replace (".keras" , "" )
87+ return modelName
8388
84- modelName = modelFile .replace (".keras" ,"" )
85- return modelName
8689
8790###################################################################
8891## Step 1 : Create and Train model
8992###################################################################
9093
9194x_train , y_train , x_test , y_test = PrepareData ()
92- #create dense model with 3 layers of 64 units
93- model = CreateModel (3 ,64 )
94- model , modelFile = TrainModel (model ,x_train , y_train , ' HiggsModel' )
95+ # create dense model with 3 layers of 64 units
96+ model = CreateModel (3 , 64 )
97+ model , modelFile = TrainModel (model , x_train , y_train , " HiggsModel" )
9598
9699###################################################################
97100## Step 2 : Parse model and generate inference code with SOFIE
@@ -110,20 +113,17 @@ def GenerateCode(modelFile = "model.keras") :
110113## Step 4: Evaluate the model
111114###################################################################
112115
113- #get first the SOFIE session namespace
114- sofie = getattr (ROOT , ' TMVA_SOFIE_' + modelName )
116+ # get first the SOFIE session namespace
117+ sofie = getattr (ROOT , " TMVA_SOFIE_" + modelName )
115118session = sofie .Session ()
116119
117- x = np .random .normal (0 ,1 , 7 ).astype (np .float32 )
120+ x = np .random .normal (0 , 1 , 7 ).astype (np .float32 )
118121y = session .infer (x )
119- ykeras = model (x .reshape (1 ,7 )).numpy ()
122+ ykeras = model (x .reshape (1 , 7 )).numpy ()
120123
121- print ("input to model is " ,x , "\n \t -> output using SOFIE = " , y [0 ], " using Keras = " , ykeras [0 ])
124+ print ("input to model is " , x , "\n \t -> output using SOFIE = " , y [0 ], " using Keras = " , ykeras [0 ])
122125
123- if ( abs (y [0 ]- ykeras [0 ]) > 0.01 ) :
124- raise RuntimeError (' ERROR: Result is different between SOFIE and Keras' )
126+ if abs (y [0 ] - ykeras [0 ]) > 0.01 :
127+ raise RuntimeError (" ERROR: Result is different between SOFIE and Keras" )
125128
126129print ("OK" )
127-
128-
129-
0 commit comments