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

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

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

Hi,

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

TMVA::PyMethodBase::PyInitialize();

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

Cheers

Lorenzo

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

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

Hi Lorenzo,

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

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

regards,

Willem

Hi,

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

Lorenzo

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

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

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

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

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

@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.