# PyKeras training has a strange behaviour compared to BDT

**URL:** <https://root-forum.cern.ch/t/pykeras-training-has-a-strange-behaviour-compared-to-bdt/40591>\
**Category:** TMVA\
**Created:** [July 23, 2020, 5:42pm UTC](https://root-forum.cern.ch/t/pykeras-training-has-a-strange-behaviour-compared-to-bdt/40591 "2020-07-23T17:42:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tommaso\_Diotalevi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tommaso_diotalevi/32/35658_2.png) [@Tommaso\_Diotalevi](https://root-forum.cern.ch/u/Tommaso_Diotalevi)\
**Post date:** [July 23, 2020, 5:42pm UTC](https://root-forum.cern.ch/t/pykeras-training-has-a-strange-behaviour-compared-to-bdt/40591/1 "2020-07-23T17:42:30Z")

</div>

Dear TMVA experts,  
I’m using TMVA for a signal/background classification problem. In particular, I’m using several trees with signal samples and a single tree with background samples (drell-yan).  
In the classification macro, I try different methods for training:

- PyKeras (with a python script that defines the model architecture)
- BDTs

Using the BDT approach, I see something like that:  
 ![canvas2](https://root-forum.cern.ch/uploads/default/original/3X/f/2/f256c96bef3f7c3a1d066b11e7bd62d82656c782.png)

The code for the BDT option is:

```auto
factory.BookMethod(dataloader, TMVA.Types.kBDT, "BDT",
                           "!H:!V:NTrees=1000:MinNodeSize=2.5%:MaxDepth=6:BoostType=AdaBoost:AdaBoostBeta=0.3:UseBaggedBoost:BaggedSampleFraction=0.3:SeparationType=GiniIndex:nCuts=20:VarTransform=D+N" )

```

When I use the PyKeras code, instead, I see something like:  
 ![canvas1](https://root-forum.cern.ch/uploads/default/original/3X/9/e/9e30c071d79c3bc62e06d13ac9e0be660c350ad4.png)

From this plot, I don’t understand why the green line is stuck at 0 (not showing) and why the blue and red line start at zero. In the BDT plot, instead, with no cut, the efficiency is 1 which makes more sense to me.

The code where the network is defined is:

```auto
        factory.BookMethod(dataloader, TMVA.Types.kPyKeras, 'PyKeras_deep', 'VarTransform=N:FilenameModel=model_deep.h5:UserCode=metrics.py:NumEpochs=10#:BatchSize=500')

```

and the Keras architecture is quite straightforward:

```auto
model=Sequential()
model.add(Dense(150, input_dim=13, kernel_initializer='random_normal', activation='relu'))
model.add(Dropout(rate=0.1))
model.add(Dense(100, activation='relu'))
model.add(Dropout(rate=0.1))
model.add(Dense(100, activation='relu'))
model.add(Dropout(rate=0.1))
model.add(Dense(70, activation='relu'))
model.add(Dropout(rate=0.1))
model.add(Dense(70, activation='relu'))
model.add(Dropout(rate=0.1))
model.add(Dense(50, activation='relu'))
model.add(Dropout(rate=0.1))
model.add(Dense(50, activation='relu'))
model.add(Dropout(rate=0.1))
model.add(Dense(20, activation='relu'))
model.add(Dropout(rate=0.1))
model.add(Dense(20, activation='relu'))
model.add(Dropout(rate=0.1))
model.add(Dense(10, activation='relu'))
model.add(Dropout(rate=0.1))
model.add(Dense(10, activation='relu'))
model.add(Dropout(rate=0.1))
model.add(Dense(2, activation='sigmoid'))

# Set loss function and optimizer algorithm
model.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.001), metrics = ['accuracy', precision, recall])

model.save('model_deep.h5')

```

To run this code, I use ROOT 6.23/01 built from source in my lxplus account with a version of TMVA slightly changed to add also other metrics (precision and recall).

In your opinion, is this a problem in PyKeras or the issue is in the Neural Network approach by itself?

Best,  
Tommaso

PS: You can find the data and the macros in [https://cernbox.cern.ch/index.php/s/EvdftAa3FK6F17Q](https://cernbox.cern.ch/index.php/s/EvdftAa3FK6F17Q)

---

<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:** [July 24, 2020, 1:43pm UTC](https://root-forum.cern.ch/t/pykeras-training-has-a-strange-behaviour-compared-to-bdt/40591/2 "2020-07-24T13:43:17Z")

</div>

Hi,

It looks to me a problem in the plot. How do they look the output distribution of the BDT and the Keras NN for signal and background ? This is the plot that can be easily made with the GUI

Lorenzo

---

<div class="post-metadata">

**Author:** ![Tommaso\_Diotalevi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tommaso_diotalevi/32/35658_2.png) [@Tommaso\_Diotalevi](https://root-forum.cern.ch/u/Tommaso_Diotalevi)\
**Post date:** [July 24, 2020, 2:10pm UTC](https://root-forum.cern.ch/t/pykeras-training-has-a-strange-behaviour-compared-to-bdt/40591/3 "2020-07-24T14:10:35Z")

</div>

Hi Lorenzo,

This is the plot for the output distribution in the BDT model:

![canvas2](https://root-forum.cern.ch/uploads/default/original/3X/4/2/42a83ac0ddc15efb4090f6cb87c9b568868e37b8.png)

And this is the plot for the output distribution in the PyKeras model:

![canvas1](https://root-forum.cern.ch/uploads/default/original/3X/6/5/651ce856a35872cb34c236598d5a5e62493f40f3.png)

Tommaso

---

<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:** [July 24, 2020, 3:03pm UTC](https://root-forum.cern.ch/t/pykeras-training-has-a-strange-behaviour-compared-to-bdt/40591/4 "2020-07-24T15:03:48Z")

</div>

Hi,  
It looks like the output of PyKeras is inverted, not sure why. I am not seeing this with standard PyKeras example  
Lorenzo

---

<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:** [July 24, 2020, 3:25pm UTC](https://root-forum.cern.ch/t/pykeras-training-has-a-strange-behaviour-compared-to-bdt/40591/5 "2020-07-24T15:25:18Z")

</div>

Are you getting the same inverted plot (background picking at 1 instead of zero) also when running the tutorial `tutorials/tmva/TMVA_CNN_Classification.C` ?

Lorenzo

---

<div class="post-metadata">

**Author:** ![Tommaso\_Diotalevi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tommaso_diotalevi/32/35658_2.png) [@Tommaso\_Diotalevi](https://root-forum.cern.ch/u/Tommaso_Diotalevi)\
**Post date:** [July 27, 2020, 9:24am UTC](https://root-forum.cern.ch/t/pykeras-training-has-a-strange-behaviour-compared-to-bdt/40591/6 "2020-07-27T09:24:01Z")

</div>

Hi,

running the tutorial, I get these distributions:

 ![Schermata 2020-07-27 alle 11.21.10](https://root-forum.cern.ch/uploads/default/original/3X/f/7/f7f457865ec6836be2259f37e37c51e90fe1dbf0.png)

And these are the cut efficiencies:

 ![Schermata 2020-07-27 alle 11.26.35](https://root-forum.cern.ch/uploads/default/original/3X/c/b/cb743833d5f35b7daf736457c57e6f55fd398f46.png)

So, the distribution does not seem to be inverted and the significance is visibile (green line). I don’t know why our plots are wrong at this point!  
Tommaso

---

<div class="post-metadata">

**Author:** ![Tommaso\_Diotalevi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tommaso_diotalevi/32/35658_2.png) [@Tommaso\_Diotalevi](https://root-forum.cern.ch/u/Tommaso_Diotalevi)\
**Post date:** [July 30, 2020, 11:15am UTC](https://root-forum.cern.ch/t/pykeras-training-has-a-strange-behaviour-compared-to-bdt/40591/7 "2020-07-30T11:15:35Z")

</div>

Dear @moneta,

I discovered the issue. Basically if you load, in the Classification macro, the Background with AddBackgroundTree() **before** the Signal, like I did, the output is completely specular. This means that the roc is actually 1-roc and the significance curve was not physical (it shows up correctly if you use the correct order). Also the output distribution for signal and background is now correct and the signal/background efficiencies now make sense.

Best,  
Tommaso

---

<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:** [August 11, 2020, 8:58am UTC](https://root-forum.cern.ch/t/pykeras-training-has-a-strange-behaviour-compared-to-bdt/40591/8 "2020-08-11T08:58:32Z")

</div>

Hi Tommaso,

Thank you very much for your finding. I will investigate the reason and eventually correct it in TMVA code

Best regards

Lorenzo
