Skip to content

Commit 5760f78

Browse files
committed
Format TMVA SOFIE tutorial code
User-facing code like tutorials should be nicely formatted, in particular avoiding non-standard indentations with three spaces that can annoy users when copy-pasting code.
1 parent b53ce7c commit 5760f78

4 files changed

Lines changed: 300 additions & 301 deletions

File tree

tutorials/machine_learning/TMVA_SOFIE_Keras.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
### \author Sanjiban Sengupta and Lorenzo Moneta
1010

1111

12-
1312
import ROOT
1413

1514
# Enable ROOT in batch mode (same effect as -nodraw)
@@ -23,22 +22,22 @@
2322
from tensorflow.keras.layers import Activation, Dense, Input, Softmax
2423
from tensorflow.keras.models import Model
2524

26-
input=Input(shape=(4,),batch_size=2)
27-
x=Dense(32)(input)
28-
x=Activation('relu')(x)
29-
x=Dense(16,activation='relu')(x)
30-
x=Dense(8,activation='relu')(x)
31-
x=Dense(2)(x)
32-
output=Softmax()(x)
33-
model=Model(inputs=input,outputs=output)
25+
input = Input(shape=(4,), batch_size=2)
26+
x = Dense(32)(input)
27+
x = Activation("relu")(x)
28+
x = Dense(16, activation="relu")(x)
29+
x = Dense(8, activation="relu")(x)
30+
x = Dense(2)(x)
31+
output = Softmax()(x)
32+
model = Model(inputs=input, outputs=output)
3433

35-
randomGenerator=np.random.RandomState(0)
36-
x_train=randomGenerator.rand(4,4)
37-
y_train=randomGenerator.rand(4,2)
34+
randomGenerator = np.random.RandomState(0)
35+
x_train = randomGenerator.rand(4, 4)
36+
y_train = randomGenerator.rand(4, 2)
3837

39-
model.compile(loss='mse', optimizer='adam')
38+
model.compile(loss="mse", optimizer="adam")
4039
model.fit(x_train, y_train, epochs=3, batch_size=2)
41-
model.save('KerasModel.keras')
40+
model.save("KerasModel.keras")
4241
model.summary()
4342

4443
# -----------------------------------------------------------------------------
@@ -54,7 +53,7 @@
5453
# Generate inference code
5554
model.Generate()
5655
model.OutputGenerated()
57-
#print generated code
56+
# print generated code
5857
print("\n**************************************************")
5958
print(" Generated code")
6059
print("**************************************************\n")
@@ -69,14 +68,13 @@
6968
# Step 3: Run inference
7069
# -----------------------------------------------------------------------------
7170

72-
#instantiate SOFIE session class
71+
# instantiate SOFIE session class
7372
session = ROOT.TMVA_SOFIE_KerasModel.Session()
7473

7574
# Input tensor (same shape as training input)
76-
x = np.array([[0.1, 0.2, 0.3, 0.4],[0.5, 0.6, 0.7, 0.8]], dtype=np.float32)
75+
x = np.array([[0.1, 0.2, 0.3, 0.4], [0.5, 0.6, 0.7, 0.8]], dtype=np.float32)
7776

7877
# Run inference
7978
y = session.infer(x)
8079

8180
print("Inference output:", y)
82-

tutorials/machine_learning/TMVA_SOFIE_Keras_HiggsModel.py

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -16,82 +16,85 @@
1616
from 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

9194
x_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)
115118
session = 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)
118121
y = 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

126129
print("OK")
127-
128-
129-

0 commit comments

Comments
 (0)