# TMVA generated class does not compile, and unable to read xml file when using Keras interface

**URL:** <https://root-forum.cern.ch/t/tmva-generated-class-does-not-compile-and-unable-to-read-xml-file-when-using-keras-interface/28457>\
**Category:** TMVA\
**Created:** [March 27, 2018, 10:53am UTC](https://root-forum.cern.ch/t/tmva-generated-class-does-not-compile-and-unable-to-read-xml-file-when-using-keras-interface/28457 "2018-03-27T10:53:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![willemv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/b19c9b/32.png) [@willemv](https://root-forum.cern.ch/u/willemv)\
**Post date:** [March 27, 2018, 10:53am UTC](https://root-forum.cern.ch/t/tmva-generated-class-does-not-compile-and-unable-to-read-xml-file-when-using-keras-interface/28457/1 "2018-03-27T10:53:48Z")

</div>

Dear experts,

I trained a neural network using the TMVA interface to Keras, closely following the example given on:

[https://root.cern.ch/doc/v608/ClassificationKeras\_8py\_source.html](https://root.cern.ch/doc/v608/ClassificationKeras_8py_source.html)

The training and evaluation of the method work fine, but I am experiencing problems when trying to use the method in later analysis. The first issues arise when trying to set up an MVA reader as follows:

```
reader->BookMVA("kerasDNN_newVars method", "leptonMvaTraining/dataset/weights/TMVAClassification_kerasDNN_newVars.weights.xml");

```

which prompts the error:

: Booking “kerasDNN\_newVars method” of type “PyKeras” from leptonMvaTraining/dataset/weights/TMVAClassification\_kerasDNN\_newVars.weights.xml.  
 : Unknown method in map: PyKeras

even though the xml file contains the name:

Does anyone know what is the reason for this? In order to avoid these issues I then turned to the standalone class, but this simply does not compile, giving the following errors  
:

/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:236:40: error: extra qualification on member ‘GetMvaValue’  
inline double ReadkerasDNN\_newVars::GetMvaValue( const std::vector& inputValues ) const  
~~~~~~~~~~~~~~~~~~~~~~ ^  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:236:40: error: class member cannot be redeclared  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:186:11: note: previous declaration is here  
double GetMvaValue( const std::vector& inputValues ) const;  
^  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:276:35: error: extra qualification on member ‘InitTransform\_1’  
inline void ReadkerasDNN\_newVars::InitTransform\_1()  
~~~~~~~~~~~~~~~~~~~~~~ ^  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:276:35: error: class member cannot be redeclared  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:204:9: note: previous declaration is here  
void InitTransform\_1();  
^  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:789:35: error: extra qualification on member ‘Transform\_1’  
inline void ReadkerasDNN\_newVars::Transform\_1( std::vector& iv, int cls) const  
~~~~~~~~~~~~~~~~~~~~~~ ^  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:789:35: error: class member cannot be redeclared  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:205:9: note: previous declaration is here  
void Transform\_1( std::vector & iv, int sigOrBgd ) const;  
^  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:847:35: error: extra qualification on member ‘InitTransform\_2’  
inline void ReadkerasDNN\_newVars::InitTransform\_2()  
~~~~~~~~~~~~~~~~~~~~~~ ^  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:847:35: error: class member cannot be redeclared  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:206:9: note: previous declaration is here  
void InitTransform\_2();  
^  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:157023:35: error: extra qualification on member ‘Transform\_2’  
inline void ReadkerasDNN\_newVars::Transform\_2( std::vector& iv, int clsIn) const  
~~~~~~~~~~~~~~~~~~~~~~ ^  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:157023:35: error: class member cannot be redeclared  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:207:9: note: previous declaration is here  
void Transform\_2( std::vector & iv, int sigOrBgd ) const;  
^  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:157118:35: error: extra qualification on member ‘InitTransform’  
inline void ReadkerasDNN\_newVars::InitTransform()  
~~~~~~~~~~~~~~~~~~~~~~ ^  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:157118:35: error: class member cannot be redeclared  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:208:9: note: previous declaration is here  
void InitTransform();  
^  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:157125:35: error: extra qualification on member ‘Transform’  
inline void ReadkerasDNN\_newVars::Transform( std::vector& iv, int sigOrBgd ) const  
~~~~~~~~~~~~~~~~~~~~~~ ^  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:157125:35: error: class member cannot be redeclared  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:209:9: note: previous declaration is here  
void Transform( std::vector & iv, int sigOrBgd ) const;  
^  
\<\<\< cling interactive line includer \>\>\>:1:1: error: expected ‘}’  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:93:55: note: to match this ‘{’  
class ReadkerasDNN\_newVars : public IClassifierReader {  
^  
/home/willem/Work/TMVAClassification\_kerasDNN\_newVars.class.C:157129:2: error: expected ‘;’ after class  
}

I tried to upload the xml and .C files, but apparently their size is too large for this website…  
Does anyone know how to solve these issues? Thanks in advance for the help!

regards,

Willem

---

<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:** [March 27, 2018, 12:50pm UTC](https://root-forum.cern.ch/t/tmva-generated-class-does-not-compile-and-unable-to-read-xml-file-when-using-keras-interface/28457/2 "2018-03-27T12:50:58Z")

</div>

Hi,

Have you added this line of code at the beginning of your macro ?

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

```

This is needed to have PyKeras available as a method in TMVA

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![willemv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/b19c9b/32.png) [@willemv](https://root-forum.cern.ch/u/willemv)\
**Post date:** [March 27, 2018, 12:53pm UTC](https://root-forum.cern.ch/t/tmva-generated-class-does-not-compile-and-unable-to-read-xml-file-when-using-keras-interface/28457/3 "2018-03-27T12:53:09Z")

</div>

Hi Lorenzo,

Thanks for the reply. To do this I assume I need the include statement #include “TMVA/PyMethodBase.h” ? Because when I try to do this I get the error “fatal error: TMVA/PyMethodBase.h: No such file or directory”. Do you know what might cause this?

regards,

Willem

---

<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:** [March 27, 2018, 12:58pm UTC](https://root-forum.cern.ch/t/tmva-generated-class-does-not-compile-and-unable-to-read-xml-file-when-using-keras-interface/28457/4 "2018-03-27T12:58:38Z")

</div>

Hi Willem,

yes you need to have the include statement if you are using a compiled macro.  
If the file is not found, it means probably that ROOT/TMVA has been built without Keras support.  
If you can, you should use the same ROOT version and installation you have used for the training.

Lorenzo

---

<div class="post-metadata">

**Author:** ![willemv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/b19c9b/32.png) [@willemv](https://root-forum.cern.ch/u/willemv)\
**Post date:** [March 27, 2018, 1:13pm UTC](https://root-forum.cern.ch/t/tmva-generated-class-does-not-compile-and-unable-to-read-xml-file-when-using-keras-interface/28457/5 "2018-03-27T13:13:25Z")

</div>

Hi Lorenzo,

When I try to add TMVA::PyMethodBase::PyInitialize(); to my code I get:

undefined reference to `TMVA::PyMethodBase::PyInitialize()’

regards,

Willem

---

<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:** [March 27, 2018, 1:18pm UTC](https://root-forum.cern.ch/t/tmva-generated-class-does-not-compile-and-unable-to-read-xml-file-when-using-keras-interface/28457/6 "2018-03-27T13:18:29Z")

</div>

Hi,

You might need to add -lPyMVA to the list of libraries to link

Lorenzo

---

<div class="post-metadata">

**Author:** ![willemv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/b19c9b/32.png) [@willemv](https://root-forum.cern.ch/u/willemv)\
**Post date:** [March 27, 2018, 1:21pm UTC](https://root-forum.cern.ch/t/tmva-generated-class-does-not-compile-and-unable-to-read-xml-file-when-using-keras-interface/28457/7 "2018-03-27T13:21:42Z")

</div>

Hi Lorenzo,

Sorry to keep bothering you. This indeed solved the compiler error, but now when I run my code I get:

: Failed to run python code: import sys; sys.argv = [’’]  
 : Python error message:  
RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa  
 : Set sys.argv faile

Any clue what the cause might be?

regards,

Willem

---

<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:** [March 27, 2018, 1:24pm UTC](https://root-forum.cern.ch/t/tmva-generated-class-does-not-compile-and-unable-to-read-xml-file-when-using-keras-interface/28457/8 "2018-03-27T13:24:43Z")

</div>

Hi,  
It looks to me there are some inconpatibilities in the Python/NumPy version. How did you install ROOT, building from source or from a binary installation ?

And how could you do the train in PyKeras ? Why it worked at that time ? You should set the correct environment as before if you can

Lorenzo

---

<div class="post-metadata">

**Author:** ![willemv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/b19c9b/32.png) [@willemv](https://root-forum.cern.ch/u/willemv)\
**Post date:** [March 27, 2018, 1:32pm UTC](https://root-forum.cern.ch/t/tmva-generated-class-does-not-compile-and-unable-to-read-xml-file-when-using-keras-interface/28457/9 "2018-03-27T13:32:57Z")

</div>

Hi Lorenzo,

Indeed, it seemed that I sourced a root release incompatible with what I used for the training. When using the same one things seem to work.

regards,

Willem

---

<div class="post-metadata">

**Author:** ![willemv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/b19c9b/32.png) [@willemv](https://root-forum.cern.ch/u/willemv)\
**Post date:** [March 27, 2018, 2:28pm UTC](https://root-forum.cern.ch/t/tmva-generated-class-does-not-compile-and-unable-to-read-xml-file-when-using-keras-interface/28457/10 "2018-03-27T14:28:11Z")

</div>

Hi Lorenzo,

Do you have any further suggestions on the standalone class for the pyKeras method? It seems it would be the easiest if I got this to work, since I plan to use the DNN in an environment where I can not set up a root release myself.

regards,

Willem

---

<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:** [March 27, 2018, 4:30pm UTC](https://root-forum.cern.ch/t/tmva-generated-class-does-not-compile-and-unable-to-read-xml-file-when-using-keras-interface/28457/11 "2018-03-27T16:30:29Z")

</div>

Hi,  
If you want to evaluate Keras standalone, you don’t need to use PyKeras, which is the ROOT interface to Keras and it requires ROOT, but just Keras from Python

Lorenzo

---

<div class="post-metadata">

**Author:** ![a.bragagnolo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@a.bragagnolo](https://root-forum.cern.ch/u/a.bragagnolo)\
**Post date:** [May 23, 2018, 3:07pm UTC](https://root-forum.cern.ch/t/tmva-generated-class-does-not-compile-and-unable-to-read-xml-file-when-using-keras-interface/28457/12 "2018-05-23T15:07:05Z")

</div>

@willemv  
Check my topic[1]

The class present a typo that prevents it to compile, sadly even fixing the typo does not allow to use the standalone class since the definitions of the functions  
`Initialize()`  
`GetMvaValue__(std::vector<double, std::allocator<double> > const&) const`  
`Clear()`  
are completely missing.

> [@Standalone C++ response classes for pyKeras does not work. Class presents a series of typos](https://root-forum.cern.ch/t/standalone-c-response-classes-for-pykeras-does-not-work-class-presents-a-series-of-typos/29110):
>
> Hi TMVA experts, I want to use TMVA as an interface for Keras. I have no problems in the training, but since my method needs to be deployed in a system where I do not control the root release (i.e. in a system with no tensorflow and keras available) I want to use the standalone c++ class option. The problem is that the above-mentioned class simply does not work. When I try to use it I get these error: /home/alberto/Desktop/training/dataset/weights/TMVAClassification\_DNN\_Barrel.class.C:291:34:…
