TMVA Keras: Inverse Transform of NN Regression Output

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

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

1 Like

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

Is this not working as expected ?

Best,

Lorenzo

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

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

ApplicationRegressionKeras.py (4.1 KB)
RegressionKeras.py (3.5 KB)

Dear @moneta ,

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

Kind regards,
Andrej

Hi,

Here is the code doing the inverse transformation after SOFIE:
ApplicationRegressionKeras.py (4.0 KB)

Lorenzo

1 Like

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

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

1 Like

Dear @moneta ,

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

Kind regards,
Andrej