How to evaluate XML file for MVA trained with PyTorch within RDataFrame?


Referring to this thread, we are loading .xml files generated from TMVA using RReader and RDataFrame for our analysis. While it works fine for BDT, we are having trouble loading a neural network model trained with PyTorch.

Basically, I followed this TMVA PyTorch tutorial to train our model and make the .xml file. When we try to load it with RReader, we get the following error.

root [0] TMVA::Experimental::RReader model("dataset/weights/TMVAClassification_PyTorch.weights.xml");
<FATAL>                          : Unknown method in map: PyTorch
***> abort program execution
Error in <TRint::HandleTermInput()>: std::runtime_error caught: FATAL error

What might be causing this, and how would we find a way to load the .xml file?

Thanks in advance,

For information, the .xml file begins like this:

<?xml version="1.0"?>
<MethodSetup Method="PyTorch::PyTorch">
    <Info name="TMVA Release" value="4.2.1 [262657]"/>
    <Info name="ROOT Release" value="6.26/04 [399876]"/>
    <Info name="Creator" value="kyoon"/>
    <Info name="Date" value="Wed Nov  9 03:54:58 2022"/>
    <Info name="Host" value="Linux centos7-docker 4.18.0-365.el8.x86_64 #1 SMP Thu Feb 10 16:11:23 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux"/>

Hi @kyoon ,

and welcome to the ROOT forum! @moneta can correct me if I’m wrong, but I think RReader covers BDTs while for NN inference you need SOFIE, see the tutorials here (in particular the TMVA_SOFIE_RDataFrame.C).


Hi @eguiraud,

Thanks for the response. It seems like using SOFIE is the right place to start. Looking at the TMVA_SOFIE_RDataFrame.C tutorial, I see that I first need to generate a header file .hxx in order to load the data into RDataFrame with the trained model.

Since I am using PyTorch, I’ve also referred to this tutorial, TMVA_SOFIE_Pytorch.C, in order to generate the .hxx. I ran the example in SWAN, and this is the error message that I get.

SOFIE::RModel model = SOFIE::PyTorch::Parse("",inputShapesSequential);

Torch Version: 1.11.0a0+gitbc2c6ed
Python error message:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: _model_to_graph() got an unexpected keyword argument 'example_outputs'

When I try to do the same with my .pt model in my environment (ROOT version 6.26/04), I still get the identical error message.

root [3] TMVA::Experimental::SOFIE::RModel model = TMVA::Experimental::SOFIE::PyTorch::Parse("", inputShapesSequential);

Torch Version: 1.11.0a0+gitbc2c6ed
Python error message:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: _model_to_graph() got an unexpected keyword argument 'example_outputs'
Error in <TRint::HandleTermInput()>: std::runtime_error caught:
Failed to run python code: graph=_model_to_graph(model,dummyInputs,example_outputs=output)

Hi @kyoon ,

we need @moneta 's help for the SOFIE error, let’s ping him.


Can you please post your PyTorch model (the .pt file) and I can have look. It is possible we need to convert to ONNX, since the support for parsing from native PyTorch is limited to few operators.



Hi @moneta,

Since I cannot upload a .pt file here directly, I am putting it inside a .zip file. (16.5 KB)


With these simple python lines of code you can convert the model to ONNX:

import torch
model = torch.load("")
xinput = torch.zeros((1,11))   # define input shape (assume batch size = 1)

Afterwards you can use generate the header file to be used with RDataFrame in C++.
You can use these lines of code to generate the header file (from tutorial TMVA_SOFIE_ONNX.C) and “.dat” file containing the weights:

   std::string inputName = "trainedModelClassification.onnx";
   using namespace TMVA::Experimental;
   SOFIE::RModelParser_ONNX parser;  
   std::cout << "parsing file " << inputName << std::endl;
   SOFIE::RModel model = parser.Parse(inputName);
   std::cout << "output written " << std::endl;

and then integrate in RDataFrame as in the tutorial TMVA_SOFIE_RDataFrame.C.

You can also do all in Python (parsing ONNX file and compile header on the file) as shown in this tutorial,

There is only one issue, you would need to use the master version or one of the latest nightly builds, since one operator in your model (SoftMax) is not part of 6.26.

Best regards



Hi Lorenzo,

That method seems to work! I can now load the PyTorch model in RDataFrame and evaluate it.

Thank you for your help,