# TMVA Reader With PyTorch CNN

**URL:** <https://root-forum.cern.ch/t/tmva-reader-with-pytorch-cnn/62300>\
**Category:** TMVA\
**Created:** [November 24, 2024, 1:30am UTC](https://root-forum.cern.ch/t/tmva-reader-with-pytorch-cnn/62300 "2024-11-24T01:30:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [November 24, 2024, 1:30am UTC](https://root-forum.cern.ch/t/tmva-reader-with-pytorch-cnn/62300/1 "2024-11-24T01:30:36Z")

</div>

Hi, ROOT experts,

I have two questions, there’s this CNN tutorial:  
[https://root.cern/doc/master/TMVA\_\_CNN\_\_Classification\_8C.html](https://root.cern/doc/master/TMVA __CNN__ Classification_8C.html)  
It also has a Python version:  
[https://root.cern/doc/master/TMVA\_\_CNN\_\_Classification\_8py.html](https://root.cern/doc/master/TMVA __CNN__ Classification_8py.html)

And I found that there’s an example applying the reader to the trained CNN with respect to the above tutorial in this post below:

> [@TMVA read image data for application of a CNN model](https://root-forum.cern.ch/t/tmva-read-image-data-for-application-of-a-cnn-model/54181/4):
>
> Hi, For the RReader class one needs to modify the Compute functor passed to RDataFrame to handle vector data. Attached you find the example code, defining a new functor to use with RRdataFrame. I also attach the example code for using the Reader class in this case. Since AddVariableArray is missing one needs to declare each single vector element, as shown in the attached code. I will add this missing function in the next release. Best Regards Lorenzo [TMVA\_CNN\_RReader.C](https://root-forum.cern.ch/uploads/short-url/gVjUtYzQnsAo74rAEkVXFAdRnk7.C) (908 Bytes) [TMVA\_CN…](https://root-forum.cern.ch/uploads/short-url/o0sdeFTMnFnfXNOEekCqQXgQVm6.C)

I was trying to recreate a Python version of the reader application but I failed because in the CNN classification tutorial the variables were saved in a vector for each entry and then were given to the TMVA methods by using:

```auto
loader.AddVariablesArray("vars", imgSize);

```

However, in the reader application example each bin is added to the TMVA reader as a variable:

```auto
std::vector<float> vars(16*16);
   for (unsigned int i = 0; i < vars.size(); i++) {
      std::string varName = "vars";
      reader->AddVariable(varName.c_str(), &vars[i]);
   }

```

This line below is used in C++ to reconcile the difference:

```auto
std::copy(userVars->begin(), userVars->end(), vars.begin());

```

But I couldn’t make it work in Python, is there a way to do it in Python? (First question)

My Second question is how to use the reader to EvaluateMVA on a test sample by using a trained PyTorch CNN model? We see that there are the PyTorch CNN and the Keras CNN in the tutorial **TMVA\_CNN\_Classification.C** , but in the reader example we only see the three TMVA built-in methods TMVA\_CNN, TMVA\_DNN, and BDT are used, is there a way to also apply the reader while using PyTorch? (In either C++ or Python, or both)

Thank you!

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [November 24, 2024, 8:13am UTC](https://root-forum.cern.ch/t/tmva-reader-with-pytorch-cnn/62300/2 "2024-11-24T08:13:18Z")

</div>

Hello,

Thanks for the post. Let me add @moneta in the loop.

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [November 24, 2024, 4:51pm UTC](https://root-forum.cern.ch/t/tmva-reader-with-pytorch-cnn/62300/3 "2024-11-24T16:51:48Z")

</div>

Hi @Danilo  
Thank you for tagging @moneta for me!

---

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [November 26, 2024, 7:15pm UTC](https://root-forum.cern.ch/t/tmva-reader-with-pytorch-cnn/62300/4 "2024-11-26T19:15:59Z")

</div>

I am asking the second question because I am facing an issue when I run this line below in C++ (this is my actual second question):

```auto
TMVA::PyMethodBase::PyInitialize();

```

```auto
cling::DynamicLibraryManager::loadLibrary(): dlopen(/Users/martin/opt/anaconda3/envs/deep_learning/lib/libPyMVA.6.28.00.so, 0x0009): symbol not found in flat namespace (_PyCapsule_Type)
Error in <AutoloadLibraryMU>: Failed to load library /Users/martin/opt/anaconda3/envs/deep_learning/lib/libPyMVA.6.28.00.socling JIT session error: Failed to materialize symbols: { (main, { __ZN4TMVA12PyMethodBase12PyInitializeEv }) }

```

But it doesn’t have this issue when I use Python; however, without knowing how to get the CNN reader application example work in Python, I can’t really proceed to do anything with a trained PyTorch model (my first question).

So the difficulties I have are:  
In C++ ROOT interface I’ve failed to load  
`TMVA::PyMethodBase::PyInitialize()`  
I can’t proceed to use a trained PyTorch model in this case.  
While in Python I can load  
`TMVA.PyMethodBase.PyInitialize()`  
But I can’t reconcile the issue of connecting `AddVariable()` for each var (bin) for the reader and the “vars” saved in  
`vars = ROOT.std.vector["float"](ntot)` for each entry with the use of `AddVariableArray()`.

---

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [November 28, 2024, 7:10am UTC](https://root-forum.cern.ch/t/tmva-reader-with-pytorch-cnn/62300/5 "2024-11-28T07:10:21Z")

</div>

Update:  
I’ve used a different way to install PyTorch so now I can use  
`TMVA::PyMethodBase::PyInitialize();`  
with no problem in ROOT, but then I’ve run into a different issue:

```auto
==> Start TMVAClassificationApplication
                         : Booking "PyTorch" of type "PyTorch" from s13/2_stages/dataLoader_s13/weights/TMVA_CNN_Classification_PyTorch.weights.xml.
<FATAL> : Unknown method in map: PyTorch
***> abort program execution
libc++abi: terminating with uncaught exception of type std::runtime_error: FATAL error

```

PyTorch still cannot be used when I am working in the ROOT interface. That’s why I really want to use the reader in Python; however, without having the function `AddVariableArray()` in the reader class, I can’t make the CNN work 🥲

---

<div class="post-metadata">

**Author:** ![Jackhammer0102](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@Jackhammer0102](https://root-forum.cern.ch/u/Jackhammer0102)\
**Post date:** [November 28, 2024, 6:24pm UTC](https://root-forum.cern.ch/t/tmva-reader-with-pytorch-cnn/62300/6 "2024-11-28T18:24:20Z")

</div>

Update 2:  
I just fixed the issue and now I can use PyTorch in the C++ ROOT interface, so it means that everything is good now, I can use the reader for the PyTorch CNN in C++, but it still doesn’t work in Python without having the function `AddVariableArray()` in the reader class though.
