# PyKeras with event-by-event weights

**URL:** <https://root-forum.cern.ch/t/pykeras-with-event-by-event-weights/33586>\
**Category:** TMVA\
**Created:** [April 13, 2019, 12:18pm UTC](https://root-forum.cern.ch/t/pykeras-with-event-by-event-weights/33586 "2019-04-13T12:18:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Archil](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@Archil](https://root-forum.cern.ch/u/Archil)\
**Post date:** [April 13, 2019, 12:18pm UTC](https://root-forum.cern.ch/t/pykeras-with-event-by-event-weights/33586/1 "2019-04-13T12:18:27Z")

</div>

Hello,

Can you please tell me how I can use event-by-event weights in the PyKeras training?

I found the following example of PyKeras usage, but it use trees without event weights:  
[https://nbviewer.jupyter.org/github/iml-wg/tmvatutorials/blob/master/TMVA\_PyMVA.ipynbachine\_learning/TMVA\_PyMVA.ipynb](https://nbviewer.jupyter.org/github/iml-wg/tmvatutorials/blob/master/TMVA_PyMVA.ipynbachine_learning/TMVA_PyMVA.ipynb)

Thanks,  
Archil

---

<div class="post-metadata">

**Author:** ![oshadura](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/oshadura/32/14460_2.png) [@oshadura](https://root-forum.cern.ch/u/oshadura)\
**Post date:** [April 14, 2019, 6:18am UTC](https://root-forum.cern.ch/t/pykeras-with-event-by-event-weights/33586/2 "2019-04-14T06:18:14Z")

</div>

@kialbert, @moneta please maybe you can help here?

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [April 16, 2019, 4:37pm UTC](https://root-forum.cern.ch/t/pykeras-with-event-by-event-weights/33586/3 "2019-04-16T16:37:27Z")

</div>

Hi,

That _should_ be the same as in “normal” TMVA training. Please see the TMVAClassification/Regression tutorial here.

In short:

```auto
dataloader = TMVA.DataLoader("dataset_name")
dataloader.SetWeightExpression("branch_name")

# or if you have different weights for sig/bkg
dataloader.SetSignalWeightExpression("branch_name")
dataloader.SetBackgroundWeightExpression("branch_name")

```

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![Archil](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@Archil](https://root-forum.cern.ch/u/Archil)\
**Post date:** [April 16, 2019, 7:40pm UTC](https://root-forum.cern.ch/t/pykeras-with-event-by-event-weights/33586/4 "2019-04-16T19:40:11Z")

</div>

Hello,

Thanks a lot for the reply,

I have tried that method and it works perfectly well for other TMVA methods, but for PyKeras method I am getting the discrepancy between the PyKeras variable from the TrainTree and the PyKeras distribution from the histogram stored in the output root file.

If I do not use the event weights then the PyKeras variable from the TrainTree is consistent with the histogram in the output root file.  
So, I thought there was other method to treat event-by-event weights for PyKeras.

Regards,  
Archil

---

<div class="post-metadata">

**Author:** ![Archil](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@Archil](https://root-forum.cern.ch/u/Archil)\
**Post date:** [April 17, 2019, 5:45pm UTC](https://root-forum.cern.ch/t/pykeras-with-event-by-event-weights/33586/5 "2019-04-17T17:45:44Z")

</div>

Hello,

Each time I run the training with unchanged code I am getting the different results:  
in most cases PyKeras output distribution for signal obtained from the TrainTree differs from the “MVA\_PyKeras\_Train\_S” histogram in the output root file. Only few times, histogram obtained from the TrainTree matches to the “MVA\_PyKeras\_Train\_S” histogram, as it should always be.

If I do not use event-by-event weights then everything is OK.

Regards,  
Archil

---

<div class="post-metadata">

**Author:** ![Archil](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/df705f/32.png) [@Archil](https://root-forum.cern.ch/u/Archil)\
**Post date:** [April 22, 2019, 10:08am UTC](https://root-forum.cern.ch/t/pykeras-with-event-by-event-weights/33586/6 "2019-04-22T10:08:08Z")

</div>

It seems that the TrainTree contains results from the last epoch, while the histogram in the output root file contains results from the epoch with smallest validation loss.

If I set the parameter SaveBestOnly=false, then TrainTree and histograms in the root file have same results.

So, my problem was not in the usage of event-by-event weights.

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [April 23, 2019, 11:29am UTC](https://root-forum.cern.ch/t/pykeras-with-event-by-event-weights/33586/7 "2019-04-23T11:29:52Z")

</div>

Thank you for clarifying!
