|
30 | 30 | ## - The third argument is a string option defining some general configuration for the TMVA session. For example all TMVA output can be suppressed by removing the "!" (not) in front of the "Silent" argument in the option string
|
31 | 31 |
|
32 | 32 | import ROOT
|
| 33 | +import os |
33 | 34 |
|
34 | 35 | TMVA = ROOT.TMVA
|
35 | 36 | TFile = ROOT.TFile
|
36 | 37 |
|
37 |
| - |
38 | 38 | TMVA.Tools.Instance()
|
39 |
| -## For PYMVA methods |
40 |
| -TMVA.PyMethodBase.PyInitialize() |
41 |
| - |
42 | 39 |
|
43 | 40 | # options to control used methods
|
44 | 41 | useLikelihood = True # likelihood based discriminant
|
|
47 | 44 | useMLP = False # Multi Layer Perceptron (old TMVA NN implementation)
|
48 | 45 | useBDT = True # Boosted Decision Tree
|
49 | 46 | useDL = True # TMVA Deep learning ( CPU or GPU)
|
| 47 | +useKeras = True # Use Keras Deep Learning via PyMVA |
| 48 | + |
| 49 | +if ROOT.gSystem.GetFromPipe("root-config --has-tmva-pymva") == "yes": |
| 50 | + TMVA.PyMethodBase.PyInitialize() |
| 51 | +else: |
| 52 | + useKeras = False # cannot use Keras if PYMVA is not available |
| 53 | + |
| 54 | +if useKeras: |
| 55 | + try: |
| 56 | + import tensorflow |
| 57 | + except: |
| 58 | + ROOT.Warning("TMVA_Higgs_Classification", "Skip using Keras since tensorflow is not available") |
| 59 | + useKeras = False |
50 | 60 |
|
51 |
| -TMVA.Tools.Instance() |
52 | 61 | outputFile = TFile.Open("Higgs_ClassificationOutput.root", "RECREATE")
|
53 | 62 | factory = TMVA.Factory(
|
54 | 63 | "TMVA_Higgs_Classification", outputFile, V=False, ROC=True, Silent=False, Color=True, AnalysisType="Classification"
|
|
276 | 285 | training1 = ROOT.TString(
|
277 | 286 | "LearningRate=1e-3,Momentum=0.9,"
|
278 | 287 | "ConvergenceSteps=10,BatchSize=128,TestRepetitions=1,"
|
279 |
| - "MaxEpochs=30,WeightDecay=1e-4,Regularization=None," |
| 288 | + "MaxEpochs=20,WeightDecay=1e-4,Regularization=None," |
280 | 289 | "Optimizer=ADAM,ADAM_beta1=0.9,ADAM_beta2=0.999,ADAM_eps=1.E-7," # ADAM default parameters
|
281 | 290 | "DropConfig=0.0+0.0+0.0+0."
|
282 | 291 | )
|
|
310 | 319 | Architecture=arch,
|
311 | 320 | )
|
312 | 321 |
|
| 322 | +#Keras DL |
| 323 | +if useKeras: |
| 324 | + ROOT.Info("TMVA_Higgs_Classification", "Building Deep Learning keras model") |
| 325 | + # create Keras model with 4 layers of 64 units and relu activations |
| 326 | + import tensorflow |
| 327 | + from tensorflow.keras.models import Sequential |
| 328 | + from tensorflow.keras.optimizers import Adam |
| 329 | + from tensorflow.keras.layers import Input, Dense |
| 330 | + |
| 331 | + model = Sequential() |
| 332 | + model.add(Dense(64, activation='relu',input_dim=7)) |
| 333 | + model.add(Dense(64, activation='relu')) |
| 334 | + model.add(Dense(64, activation='relu')) |
| 335 | + model.add(Dense(64, activation='relu')) |
| 336 | + model.add(Dense(2, activation='sigmoid')) |
| 337 | + model.compile(loss = 'binary_crossentropy', optimizer = Adam(learning_rate = 0.001), metrics = ['accuracy']) |
| 338 | + model.save('model_higgs.h5') |
| 339 | + model.summary() |
| 340 | + |
| 341 | + if not os.path.exists("model_higgs.h5"): |
| 342 | + raise FileNotFoundError("Error creating Keras model file - skip using Keras") |
| 343 | + else: |
| 344 | + # book PyKeras method only if Keras model could be created |
| 345 | + ROOT.Info("TMVA_Higgs_Classification", "Booking Deep Learning keras model") |
| 346 | + factory.BookMethod( |
| 347 | + loader, |
| 348 | + TMVA.Types.kPyKeras, |
| 349 | + "PyKeras", |
| 350 | + H=True, |
| 351 | + V=False, |
| 352 | + VarTransform=None, |
| 353 | + FilenameModel="model_higgs.h5", |
| 354 | + FilenameTrainedModel="trained_model_higgs.h5", |
| 355 | + NumEpochs=20, |
| 356 | + BatchSize=100 ) |
| 357 | +# GpuOptions="allow_growth=True", |
| 358 | +# ) # needed for RTX NVidia card and to avoid TF allocates all GPU memory |
| 359 | + |
| 360 | + |
313 | 361 | ## Train Methods
|
314 | 362 |
|
315 | 363 | # Here we train all the previously booked methods.
|
|
0 commit comments