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.6760
Epoch 2/5 - average loss: 0.6523
Epoch 3/5 - average loss: 0.6400
Epoch 4/5 - average loss: 0.6313
Epoch 5/5 - average loss: 0.6272
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.35282161831855774 using PyTorch = 0.35282162
input to model is [1.0965776 0.9103265 1.9756684 1.3508093 1.3468878 1.4005579 1.1609015]
-> output using SOFIE = 0.5396692752838135 using PyTorch = 0.5396693
input to model is [0.846992 0.9408182 0.98906 1.6148995 1.038698 1.2381754 1.0323234]
-> output using SOFIE = 0.6710253357887268 using PyTorch = 0.6710254
input to model is [1.897264 1.234499 0.98704207 0.708829 0.7279103 0.9053675
0.76521254]
-> output using SOFIE = 0.6426116824150085 using PyTorch = 0.6426118
input to model is [0.791873 0.9792347 0.9924756 0.9159218 1.1000326 0.94064647
0.79085195]
-> output using SOFIE = 0.5542488694190979 using PyTorch = 0.5542488
input to model is [0.9692043 0.6372814 0.9850732 0.9201175 0.72131383 0.8001433
0.7160924 ]
-> output using SOFIE = 0.6300942897796631 using PyTorch = 0.6300943
input to model is [1.5037444 1.1279533 0.9814414 1.5327642 0.7886151 1.1838427 1.0383142]
-> output using SOFIE = 0.6688777208328247 using PyTorch = 0.6688777
input to model is [1.2042431 1.0750061 1.5724212 1.1590953 1.367509 1.1043229
0.99204683]
-> output using SOFIE = 0.47588080167770386 using PyTorch = 0.47588086
input to model is [1.012179 0.76250947 0.9957243 0.48331824 0.4295301 0.55478483
0.7100585 ]
-> output using SOFIE = 0.3623771667480469 using PyTorch = 0.36237717
input to model is [0.8616951 1.1908345 0.99018383 1.2523934 1.1448306 1.0246366
0.9923519 ]
-> output using SOFIE = 0.5826054215431213 using PyTorch = 0.5826054
OK

Definition in file TMVA_SOFIE_PyTorch_HiggsModel.py.