# TMVA Keras: Inverse Transform of NN Regression Output

**URL:** <https://root-forum.cern.ch/t/tmva-keras-inverse-transform-of-nn-regression-output/53817>\
**Category:** TMVA\
**Created:** [February 28, 2023, 11:50am UTC](https://root-forum.cern.ch/t/tmva-keras-inverse-transform-of-nn-regression-output/53817 "2023-02-28T11:50:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndrejS](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ecc23a/32.png) [@AndrejS](https://root-forum.cern.ch/u/AndrejS)\
**Post date:** [February 28, 2023, 11:50am UTC](https://root-forum.cern.ch/t/tmva-keras-inverse-transform-of-nn-regression-output/53817/1 "2023-02-28T11:50:12Z")

</div>

Dear experts,

I’m using TMVA Keras to train a feed-forward NN to regress a feature.  
When performing the pre-processing with VariableTransform, I realized that the target distribution also gets transformed, e.g. Gaussian, Normalize. I use only one of those at a time.

I must undo the transformation to compare my output distribution with truth values and later perform a fit to theory. Is there an easy way to inverse-transform the output of the TMVA neural network during inference or after?

Kind regards,

Andrej

---

<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:** [February 28, 2023, 11:58am UTC](https://root-forum.cern.ch/t/tmva-keras-inverse-transform-of-nn-regression-output/53817/2 "2023-02-28T11:58:11Z")

</div>

Welcome to the ROOT Forum!  
I think @moneta can help you

---

<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:** [February 28, 2023, 2:59pm UTC](https://root-forum.cern.ch/t/tmva-keras-inverse-transform-of-nn-regression-output/53817/3 "2023-02-28T14:59:45Z")

</div>

Hi,  
Looking at the code of TMVA Keras, I see that an inverse transformation of the output is already done, see

> <https://github.com/root-project/root/blob/master/tmva/pymva/src/MethodPyKeras.cxx#L763>

Is this not working as expected ?

Best,

Lorenzo

---

<div class="post-metadata">

**Author:** ![Andrej-CMS](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@Andrej-CMS](https://root-forum.cern.ch/u/Andrej-CMS)\
**Post date:** [February 28, 2023, 3:26pm UTC](https://root-forum.cern.ch/t/tmva-keras-inverse-transform-of-nn-regression-output/53817/4 "2023-02-28T15:26:31Z")

</div>

Dear @moneta ,

Thank you very much for your quick reply.

- I run the training with VarTransform=D,G, or VarTransform=N
- Then I apply ROOT.TMVA\_SOFIE\_trainedModelRegression.Session().infer(x.data()) for the inference
- The output distribution is Gaussian or between -1 and 1 which is not correct since the regression target is defined between 0 and 1 in my case.

If it helps, I can provide the scripts and a small file with a few thousand events for cross-checks.

Kind regards,

Andrej

---

<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:** [February 28, 2023, 3:33pm UTC](https://root-forum.cern.ch/t/tmva-keras-inverse-transform-of-nn-regression-output/53817/5 "2023-02-28T15:33:31Z")

</div>

Hi Andrej,

I thought you were using `TMVA::MethodPyKeras.cxx` for the regression evaluation and then not SOFIE.  
In the case of SOFIE you need then to apply yourself the transformation, both for the input variables and for the output. So you would need to add the lines shown above( from 757 until 767) where `fOutput` will be the return vector of SOFIE.  
I could help you doing this, but I need to know how are you using SOFIE. Are you using within the Reader class ? Because you need access to the TransformationHandler and to the TMVA::Event class.

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![Andrej-CMS](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@Andrej-CMS](https://root-forum.cern.ch/u/Andrej-CMS)\
**Post date:** [February 28, 2023, 3:40pm UTC](https://root-forum.cern.ch/t/tmva-keras-inverse-transform-of-nn-regression-output/53817/6 "2023-02-28T15:40:07Z")

</div>

[ApplicationRegressionKeras.py](https://root-forum.cern.ch/uploads/short-url/3mtIRGOMhHOLc7qCG9SA3Zs5bo1.py) (4.1 KB)  
[RegressionKeras.py](https://root-forum.cern.ch/uploads/short-url/nvUmZGLI1GsHYzIcUTqm6tFWQgD.py) (3.5 KB)

Dear @moneta ,

Please find the regression and the application attached!  
Thanks a lot for the help!

Kind regards,  
Andrej

---

<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:** [February 28, 2023, 5:06pm UTC](https://root-forum.cern.ch/t/tmva-keras-inverse-transform-of-nn-regression-output/53817/7 "2023-02-28T17:06:45Z")

</div>

Hi,

Here is the code doing the inverse transformation after SOFIE:  
[ApplicationRegressionKeras.py](https://root-forum.cern.ch/uploads/short-url/8PFzifsOsSTqBY6AFsjNwJI3h9A.py) (4.0 KB)

Lorenzo

---

<div class="post-metadata">

**Author:** ![Andrej-CMS](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@Andrej-CMS](https://root-forum.cern.ch/u/Andrej-CMS)\
**Post date:** [March 1, 2023, 12:23pm UTC](https://root-forum.cern.ch/t/tmva-keras-inverse-transform-of-nn-regression-output/53817/8 "2023-03-01T12:23:09Z")

</div>

Hi @moneta

Thanks a lot for the help!  
The output is now as expected.

Just for my understanding: You mentioned above that we need also to do the transformation of the inputs manually. Does this still apply to the snippet you posted above?  
Do I need to transform the input distributions before calling SOFIE for the inference?

Cheers,  
Andrej

---

<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 1, 2023, 1:49pm UTC](https://root-forum.cern.ch/t/tmva-keras-inverse-transform-of-nn-regression-output/53817/9 "2023-03-01T13:49:21Z")

</div>

Hi,  
In the code you have sent to me there is input transformation is applied. You are not giving to SOFIE as input the variables directly from the TTree, but the values from the values from the `TMVA::Event` class which are transformed.  
One thing to do for us, given this use case, is to integrate the SOFIE inference in the `TMVA::Reader` class so the transformation are applied automatically.

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![Andrej-CMS](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@Andrej-CMS](https://root-forum.cern.ch/u/Andrej-CMS)\
**Post date:** [March 1, 2023, 2:05pm UTC](https://root-forum.cern.ch/t/tmva-keras-inverse-transform-of-nn-regression-output/53817/10 "2023-03-01T14:05:13Z")

</div>

Dear @moneta ,

Thank you very much for the clarification and the help! It works great now.

Kind regards,  
Andrej
