# Converting TMVA PyKeras output with lwtnn - variable normalization details

**URL:** <https://root-forum.cern.ch/t/converting-tmva-pykeras-output-with-lwtnn-variable-normalization-details/42830>\
**Category:** TMVA\
**Created:** [December 22, 2020, 3:55pm UTC](https://root-forum.cern.ch/t/converting-tmva-pykeras-output-with-lwtnn-variable-normalization-details/42830 "2020-12-22T15:55:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![janina.anna.krzysiak](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@janina.anna.krzysiak](https://root-forum.cern.ch/u/janina.anna.krzysiak)\
**Post date:** [December 22, 2020, 3:55pm UTC](https://root-forum.cern.ch/t/converting-tmva-pykeras-output-with-lwtnn-variable-normalization-details/42830/1 "2020-12-22T15:55:47Z")

</div>

Hi TMVA Experts,

I have a model trained using TMVA PyKeras that I need to apply within an analysis framework where tensorflow/keras are unavailable. Therefore, I’m trying to convert the model to a standard JSON format with the lwtnn keras2json converter. One of the things I need to do is make a variables.json file (templates are shown here: [https://github.com/lwtnn/lwtnn/wiki/Keras-Converter](https://github.com/lwtnn/lwtnn/wiki/Keras-Converter)) listing input variables together with parameters used for normalizing them in the training - offset and scale values. I think it should be possible to access these values using TMVA outputs (class.C and weights.xml files), but I cannot figure out how. What would be the easiest way to do this?

Cheers,  
Janina

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [December 23, 2020, 10:53am UTC](https://root-forum.cern.ch/t/converting-tmva-pykeras-output-with-lwtnn-variable-normalization-details/42830/2 "2020-12-23T10:53:43Z")

</div>

@moneta will help you as soon as we are back from vacation

---

<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:** [January 5, 2021, 10:54am UTC](https://root-forum.cern.ch/t/converting-tmva-pykeras-output-with-lwtnn-variable-normalization-details/42830/3 "2021-01-05T10:54:37Z")

</div>

Hi,  
The XML file should list all the variable names, their transformations and the trasformation parameters.  
However, TMVA does not diretly support the simple re-scaling transformation as supported by LWTNN,  
`x -> x' = (x+offset)*scale`

However you can implement this as a simple expression when adding variables in TMVA (`DataLoader::AddVariable`) after having computed mean and stddev for each variable. The variable expression is also stored in the XML file

Best regards

Lorenzo

---

<div class="post-metadata">

**Author:** ![janina.anna.krzysiak](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@janina.anna.krzysiak](https://root-forum.cern.ch/u/janina.anna.krzysiak)\
**Post date:** [January 5, 2021, 3:34pm UTC](https://root-forum.cern.ch/t/converting-tmva-pykeras-output-with-lwtnn-variable-normalization-details/42830/4 "2021-01-05T15:34:01Z")

</div>

Hi Lorenzo,

Thanks, I will try to implement the transformation at the AddVariable stage. I also took a look at the transformation parameters in the XML file, but I’m a bit confused about the contents. For example, for Transform Name=“Decorrelation” I see three different transformation matrices (I assume that’s what they are). I would expect to see only one?

Cheers,  
Janina

---

<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:** [January 5, 2021, 4:29pm UTC](https://root-forum.cern.ch/t/converting-tmva-pykeras-output-with-lwtnn-variable-normalization-details/42830/5 "2021-01-05T16:29:28Z")

</div>

Hi,  
You have probably a different decorrelation matrix for every class of events (e.g. signal, background1, background2).  
I would not apply decorrelation if you are using a neural network afterwards. It is better to let the network learn the correlations.

Lorenzo

---

<div class="post-metadata">

**Author:** ![janina.anna.krzysiak](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@janina.anna.krzysiak](https://root-forum.cern.ch/u/janina.anna.krzysiak)\
**Post date:** [January 5, 2021, 4:59pm UTC](https://root-forum.cern.ch/t/converting-tmva-pykeras-output-with-lwtnn-variable-normalization-details/42830/6 "2021-01-05T16:59:46Z")

</div>

Ok, got it!
