Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TMVA_SOFIE_PyTorch_HiggsModel.py File Reference

Functions

 TMVA_SOFIE_PyTorch_HiggsModel.GenerateCode (modelFile="model.onnx")    TMVA_SOFIE_PyTorch_HiggsModel.PrepareData ()    TMVA_SOFIE_PyTorch_HiggsModel.TrainModel (x_train, y_train, x_check, name)      TMVA_SOFIE_PyTorch_HiggsModel.modelName = GenerateCode(modelFile)  Step 2 : Parse model and generate inference code with SOFIE.
   TMVA_SOFIE_PyTorch_HiggsModel.session = sofie.Session()    TMVA_SOFIE_PyTorch_HiggsModel.sofie
= getattr(ROOT, "TMVA_SOFIE_" + modelName)  Step 3 : Compile the generated C++ model code.
  str TMVA_SOFIE_PyTorch_HiggsModel.TRAIN_SCRIPT    TMVA_SOFIE_PyTorch_HiggsModel.x_check
= x_test[:10]    TMVA_SOFIE_PyTorch_HiggsModel.x_test  Step 1 : Create and train the model, export it to ONNX.
   TMVA_SOFIE_PyTorch_HiggsModel.x_train  Step 1 : Create and train the model, export it to ONNX.
   TMVA_SOFIE_PyTorch_HiggsModel.y
= session.infer(x_check[i])    TMVA_SOFIE_PyTorch_HiggsModel.y_test  Step 1 : Create and train the model, export it to ONNX.
   TMVA_SOFIE_PyTorch_HiggsModel.y_train  Step 1 : Create and train the model, export it to ONNX.
   TMVA_SOFIE_PyTorch_HiggsModel.ytorch  

Detailed Description

View in nbviewer Open in SWAN
This macro trains a simple deep neural network on the Higgs dataset with PyTorch, exports the model to ONNX and runs the SOFIE parser on it to generate and compile C++ inference code.

The trained model is saved as HiggsModel.onnx and is used as input by other SOFIE tutorials (e.g. TMVA_SOFIE_RDataFrame.C), so this macro needs to be run before them.

The PyTorch export and ROOT's SOFIE parser are both linked against protobuf, but usually against different versions, so loading them in the same process leads to a symbol clash. We therefore run the PyTorch training and ONNX export in a separate Python process and only use ROOT before and afterwards.

size of data 10000
Sequential(
(0): Linear(in_features=7, out_features=64, bias=True)
(1): ReLU()
(2): Linear(in_features=64, out_features=64, bias=True)
(3): ReLU()
(4): Linear(in_features=64, out_features=1, bias=True)
(5): Sigmoid()
)
Epoch 1/5 - average loss: 0.6714
Epoch 2/5 - average loss: 0.6533
Epoch 3/5 - average loss: 0.6429
Epoch 4/5 - average loss: 0.6362
Epoch 5/5 - average loss: 0.6300
calling torch.onnx.export with parameters {'input_names': ['input'], 'output_names': ['output'], 'external_data': False, 'dynamo': True}
[torch.onnx] Obtain model graph for `Sequential([...]` with `torch.export.export(..., strict=False)`...
[torch.onnx] Obtain model graph for `Sequential([...]` with `torch.export.export(..., strict=False)`... ✅
[torch.onnx] Run decompositions...
[torch.onnx] Run decompositions... ✅
[torch.onnx] Translate the graph into ONNX...
[torch.onnx] Translate the graph into ONNX... ✅
[torch.onnx] Optimize the ONNX graph...
[torch.onnx] Optimize the ONNX graph... ✅
model exported to ONNX as HiggsModel.onnx
input to model is [1.3551283 1.0198661 0.98278755 0.5504138 1.2055093 0.91609305
0.93084896]
-> output using SOFIE = 0.3520871698856354 using PyTorch = 0.35208717
input to model is [1.0965776 0.9103265 1.9756684 1.3508093 1.3468878 1.4005579 1.1609015]
-> output using SOFIE = 0.386409193277359 using PyTorch = 0.3864092
input to model is [0.846992 0.9408182 0.98906 1.6148995 1.038698 1.2381754 1.0323234]
-> output using SOFIE = 0.6477437615394592 using PyTorch = 0.64774376
input to model is [1.897264 1.234499 0.98704207 0.708829 0.7279103 0.9053675
0.76521254]
-> output using SOFIE = 0.6026788949966431 using PyTorch = 0.6026789
input to model is [0.791873 0.9792347 0.9924756 0.9159218 1.1000326 0.94064647
0.79085195]
-> output using SOFIE = 0.5594281554222107 using PyTorch = 0.55942816
input to model is [0.9692043 0.6372814 0.9850732 0.9201175 0.72131383 0.8001433
0.7160924 ]
-> output using SOFIE = 0.605823278427124 using PyTorch = 0.60582334
input to model is [1.5037444 1.1279533 0.9814414 1.5327642 0.7886151 1.1838427 1.0383142]
-> output using SOFIE = 0.6124486923217773 using PyTorch = 0.6124487
input to model is [1.2042431 1.0750061 1.5724212 1.1590953 1.367509 1.1043229
0.99204683]
-> output using SOFIE = 0.4066299796104431 using PyTorch = 0.40663
input to model is [1.012179 0.76250947 0.9957243 0.48331824 0.4295301 0.55478483
0.7100585 ]
-> output using SOFIE = 0.4313146770000458 using PyTorch = 0.43131468
input to model is [0.8616951 1.1908345 0.99018383 1.2523934 1.1448306 1.0246366
0.9923519 ]
-> output using SOFIE = 0.5673179030418396 using PyTorch = 0.5673179
OK

Definition in file TMVA_SOFIE_PyTorch_HiggsModel.py.