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

**URL:** <https://root-forum.cern.ch/t/how-to-evaluate-xml-file-for-mva-trained-with-pytorch-within-rdataframe/52299>\
**Category:** TMVA\
**Tags:** rdataframe\
**Created:** [November 10, 2022, 1:19am UTC](https://root-forum.cern.ch/t/how-to-evaluate-xml-file-for-mva-trained-with-pytorch-within-rdataframe/52299 "2022-11-10T01:19:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyoon](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/e47c2d/32.png) [@kyoon](https://root-forum.cern.ch/u/kyoon)\
**Post date:** [November 10, 2022, 1:19am UTC](https://root-forum.cern.ch/t/how-to-evaluate-xml-file-for-mva-trained-with-pytorch-within-rdataframe/52299/1 "2022-11-10T01:19:52Z")

</div>

Hello,

Referring to [this thread](https://root-forum.cern.ch/t/evaluating-mva-within-root-data-frame/49238/4), 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](https://root.cern/doc/master/ClassificationPyTorch_8py.html) to train our model and make the .xml file. When we try to load it with RReader, we get the following error.

```auto
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,  
Kevin

For information, the .xml file begins like this:

```auto
<?xml version="1.0"?>
<MethodSetup Method="PyTorch::PyTorch">
  <GeneralInfo>
    <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"/>

```

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [November 10, 2022, 10:48am UTC](https://root-forum.cern.ch/t/how-to-evaluate-xml-file-for-mva-trained-with-pytorch-within-rdataframe/52299/2 "2022-11-10T10:48:09Z")

</div>

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](https://root.cern/doc/master/group __tutorial__ tmva.html) (in particular the `	TMVA_SOFIE_RDataFrame.C`).

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![kyoon](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/e47c2d/32.png) [@kyoon](https://root-forum.cern.ch/u/kyoon)\
**Post date:** [November 11, 2022, 1:17am UTC](https://root-forum.cern.ch/t/how-to-evaluate-xml-file-for-mva-trained-with-pytorch-within-rdataframe/52299/3 "2022-11-11T01:17:13Z")

</div>

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](https://root.cern/doc/master/TMVA __SOFIE__ RDataFrame_8C.html), 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](https://root.cern/doc/v626/TMVA __SOFIE__ PyTorch_8C.html), in order to generate the .hxx. I ran the example in SWAN, and this is the error message that I get.

```auto
SOFIE::RModel model = SOFIE::PyTorch::Parse("PyTorchModel.pt",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.

```auto
root [3] TMVA::Experimental::SOFIE::RModel model = TMVA::Experimental::SOFIE::PyTorch::Parse("trainedModelClassification.pt", 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)

```

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [November 11, 2022, 11:09am UTC](https://root-forum.cern.ch/t/how-to-evaluate-xml-file-for-mva-trained-with-pytorch-within-rdataframe/52299/4 "2022-11-11T11:09:37Z")

</div>

Hi @kyoon ,

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

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [November 11, 2022, 11:12am UTC](https://root-forum.cern.ch/t/how-to-evaluate-xml-file-for-mva-trained-with-pytorch-within-rdataframe/52299/5 "2022-11-11T11:12:31Z")

</div>

Hi,  
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.

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![kyoon](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/e47c2d/32.png) [@kyoon](https://root-forum.cern.ch/u/kyoon)\
**Post date:** [November 13, 2022, 2:53pm UTC](https://root-forum.cern.ch/t/how-to-evaluate-xml-file-for-mva-trained-with-pytorch-within-rdataframe/52299/6 "2022-11-13T14:53:20Z")

</div>

Hi @moneta,

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

[trainedModelClassification.zip](https://root-forum.cern.ch/uploads/short-url/alY0OPfBoagFNovrXbpZd76VKBM.zip) (16.5 KB)

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [November 14, 2022, 8:18am UTC](https://root-forum.cern.ch/t/how-to-evaluate-xml-file-for-mva-trained-with-pytorch-within-rdataframe/52299/7 "2022-11-14T08:18:47Z")

</div>

Hi,

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

```auto
import torch
model = torch.load("trainedModelClassification.pt")
xinput = torch.zeros((1,11)) # define input shape (assume batch size = 1)
torch.onnx.export(model,xinput,"trainedModelClassification.onnx",export_params=True)

```

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:

```auto
{
   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);
   model.Generate();
   model.OutputGenerated();//outputName);
   std::cout << "output written " << std::endl;
}

```

and then integrate in RDataFrame as in the tutorial [TMVA\_SOFIE\_RDataFrame.C](https://root.cern/doc/master/TMVA __SOFIE__ RDataFrame_8C.html).

You can also do all in Python (parsing ONNX file and compile header on the file) as shown in this tutorial, [TMVA\_SOFIE\_RDataFrame.py](https://root.cern/doc/master/TMVA __SOFIE__ RDataFrame_8py.html).

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

Lorenzo

---

<div class="post-metadata">

**Author:** ![kyoon](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/e47c2d/32.png) [@kyoon](https://root-forum.cern.ch/u/kyoon)\
**Post date:** [November 15, 2022, 3:16pm UTC](https://root-forum.cern.ch/t/how-to-evaluate-xml-file-for-mva-trained-with-pytorch-within-rdataframe/52299/8 "2022-11-15T15:16:31Z")

</div>

Hi Lorenzo,

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

Thank you for your help,  
Kevin
