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

Functions

 TMVA_SOFIE_PyTorch_HiggsModel.CreateModel (nlayers=4, nunits=64)    TMVA_SOFIE_PyTorch_HiggsModel.ExportModel (model, modelName)    TMVA_SOFIE_PyTorch_HiggsModel.GenerateCode (modelFile="model.onnx")    TMVA_SOFIE_PyTorch_HiggsModel.PrepareData ()    TMVA_SOFIE_PyTorch_HiggsModel.TrainModel (model, x, y, epochs=5, batch_size=50)     str TMVA_SOFIE_PyTorch_HiggsModel.modelHeaderFile = modelName + ".hxx"    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.
   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
= model(torch.from_numpy(x_check)).numpy()  

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.

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.6755
Epoch 2/5 - average loss: 0.6561
Epoch 3/5 - average loss: 0.6461
Epoch 4/5 - average loss: 0.6386
Epoch 5/5 - average loss: 0.6333
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.3763280510902405 using PyTorch = 0.37632805
input to model is [1.0965776 0.9103265 1.9756684 1.3508093 1.3468878 1.4005579 1.1609015]
-> output using SOFIE = 0.5212275385856628 using PyTorch = 0.5212275
input to model is [0.846992 0.9408182 0.98906 1.6148995 1.038698 1.2381754 1.0323234]
-> output using SOFIE = 0.6604689359664917 using PyTorch = 0.66046894
input to model is [1.897264 1.234499 0.98704207 0.708829 0.7279103 0.9053675
0.76521254]
-> output using SOFIE = 0.5919074416160583 using PyTorch = 0.59190744
input to model is [0.791873 0.9792347 0.9924756 0.9159218 1.1000326 0.94064647
0.79085195]
-> output using SOFIE = 0.6196779012680054 using PyTorch = 0.6196779
input to model is [0.9692043 0.6372814 0.9850732 0.9201175 0.72131383 0.8001433
0.7160924 ]
-> output using SOFIE = 0.5712547898292542 using PyTorch = 0.57125473
input to model is [1.5037444 1.1279533 0.9814414 1.5327642 0.7886151 1.1838427 1.0383142]
-> output using SOFIE = 0.5804571509361267 using PyTorch = 0.58045715
input to model is [1.2042431 1.0750061 1.5724212 1.1590953 1.367509 1.1043229
0.99204683]
-> output using SOFIE = 0.48698461055755615 using PyTorch = 0.4869846
input to model is [1.012179 0.76250947 0.9957243 0.48331824 0.4295301 0.55478483
0.7100585 ]
-> output using SOFIE = 0.35637032985687256 using PyTorch = 0.35637033
input to model is [0.8616951 1.1908345 0.99018383 1.2523934 1.1448306 1.0246366
0.9923519 ]
-> output using SOFIE = 0.6398386359214783 using PyTorch = 0.63983864
OK

Definition in file TMVA_SOFIE_PyTorch_HiggsModel.py.