# Evaluating multiple TMVA (pyKeras) weights using the same intree variables?

**URL:** <https://root-forum.cern.ch/t/evaluating-multiple-tmva-pykeras-weights-using-the-same-intree-variables/63179>\
**Category:** TMVA\
**Created:** [March 21, 2025, 11:46am UTC](https://root-forum.cern.ch/t/evaluating-multiple-tmva-pykeras-weights-using-the-same-intree-variables/63179 "2025-03-21T11:46:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![abayyari](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/abayyari/32/35034_2.png) [@abayyari](https://root-forum.cern.ch/u/abayyari)\
**Post date:** [March 21, 2025, 11:46am UTC](https://root-forum.cern.ch/t/evaluating-multiple-tmva-pykeras-weights-using-the-same-intree-variables/63179/1 "2025-03-21T11:46:45Z")

</div>

I have trained multiple CNN models using the TMVA Classification code. I can evaluate a single CNN model weight in my code just fine, and I’m happy with the results it’s producing. I’m now trying to add multiple weights, the goal is to use a combination of selection cuts from multiple CNN weights for my analysis. However, whenever I try to evaluate more than one weight, my code crashes. It uses the same variables in my input ROOT file TTree, so I suspect I’m doing something incorrectly. I tried using multiple outtrees and reader variables, but still no luck. Here’s a snippet of my current code:

```auto
//skipping header files 

#include "/home/software/root/root_src/tmva/tmva/inc/TMVA/Factory.h"
#include "/home/software/root/root_src/tmva/tmva/inc/TMVA/Tools.h"
#include "/home/software/root/root_src/tmva/tmva/inc/TMVA/Reader.h"
#include "/home/software/root/root_src/tmva/tmva/inc/TMVA/MethodCuts.h"
#include "/home/software/root/root_src/tmva/pymva/inc/TMVA/PyMethodBase.h"

#include <TSystem.h>

  //reading input file
  TChain* intree = new TChain("data");

  float elost00xlad00; intree->SetBranchAddress("elost00xlad00",&elost00xlad00);
  float elost00xlad01; intree->SetBranchAddress("elost00xlad01",&elost00xlad01);
...
  float elost19xlad16; intree->SetBranchAddress("elost19xlad16",&elost19xlad16);
  float elost19xlad17; intree->SetBranchAddress("elost19xlad17",&elost19xlad17);

  TFile *outfile = new TFile(Form("%s",outfname.Data()), "RECREATE", "", ROOT::CompressionSettings(ROOT::kLZMA, 8));
  TTree *outtree = new TTree("cleandata", "cleandata");

outtree->Branch("var1",&elost00xlad00);
outtree->Branch("var2",&elost00xlad01);
...
outtree->Branch("var359",&elost00xlad00);
outtree->Branch("var360",&elost19xlad17);

// PyKeras response value
float pykeras; outtree->Branch("pykeras",&pykeras); 
float pykeras2; outtree->Branch("pykeras2",&pykeras2); 

 // Setup TMVA for PyKeras
 
   TMVA::Tools::Instance();
   TMVA::PyMethodBase::PyInitialize();

// Create a TMVA reader object for pykeras CNN models
TMVA::Reader* reader_py = new TMVA::Reader("!Color:!Silent");

reader_py->AddVariable("var1",&elost00xlad00);
reader_py->AddVariable("var2",&elost00xlad01);
...
reader_py->AddVariable("var359",&elost19xlad16);
reader_py->AddVariable("var360",&elost19xlad17);

//Reading CNN models
  reader_py->BookMVA("PyKeras", "mod1.weights.xml");
  reader_py->BookMVA("PyKeras2", "mod2.weights.xml"); //Tried using different reader variables, but still crashes. Keeping same reader variable 

  //looping over event
  long int Nev = intree->GetEntries();
  for(long int iev=0; iev<Nev; iev++)
  {

	  pykeras = reader_py->EvaluateMVA("PyKeras");
        pykeras2 = reader_py->EvaluateMVA("PyKeras2");

outtree->Fill(); //Fill tree

  }

```

And here’s the crash message:

```auto
<WARNING> <WARNING> : Failed to run python code: for i,p in enumerate(model.predict(vals, verbose=0)): output[i]=p
<WARNING> <WARNING> : 
<WARNING> <WARNING> : Python error message:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/abayyari/.local/lib/python3.10/site-packages/keras/src/engine/training_v1.py", line 1059, in predict
    return func.predict(
  File "/home/abayyari/.local/lib/python3.10/site-packages/keras/src/engine/training_arrays_v1.py", line 801, in predict
    return predict_loop(
  File "/home/abayyari/.local/lib/python3.10/site-packages/keras/src/engine/training_arrays_v1.py", line 421, in model_iteration
    batch_outs = f(ins_batch)
  File "/home/abayyari/.local/lib/python3.10/site-packages/keras/src/backend.py", line 4609, in __call__
    fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)
  File "/home/abayyari/.local/lib/python3.10/site-packages/tensorflow/python/client/session.py", line 1482, in __call__
    ret = tf_session.TF_SessionRunCallable(self._session._session,
tensorflow.python.framework.errors_impl.FailedPreconditionError: Could not find variable dense_1/bias. This could mean that the variable has been deleted. In TF1, it can also mean the variable is uninitialized. Debug info: container=localhost, status error message=Resource localhost/dense_1/bias/N10tensorflow3VarE does not exist.
	 [[{{node dense_1/BiasAdd/ReadVariableOp}}]]
<FATAL> : Failed to get predictions
***> abort program execution
terminate called after throwing an instance of 'std::runtime_error'
  what(): FATAL error

```

Any help would be greatly appreciated it!

---

<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:** [March 21, 2025, 11:49am UTC](https://root-forum.cern.ch/t/evaluating-multiple-tmva-pykeras-weights-using-the-same-intree-variables/63179/2 "2025-03-21T11:49:20Z")

</div>

Maybe @moneta can help

---

<div class="post-metadata">

**Author:** ![jhchoi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jhchoi/32/38759_2.png) [@jhchoi](https://root-forum.cern.ch/u/jhchoi)\
**Post date:** [April 6, 2025, 2:18pm UTC](https://root-forum.cern.ch/t/evaluating-multiple-tmva-pykeras-weights-using-the-same-intree-variables/63179/3 "2025-04-06T14:18:14Z")

</div>

Hello. I found the same problem with ROOT6.30. Keras models got broken during running multiple pykeras.

Now I am using ROOT 6.26 which doesn’t have that problem. I guess it is a problem of ROOT6.30 only.

---

<div class="post-metadata">

**Author:** ![abayyari](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/abayyari/32/35034_2.png) [@abayyari](https://root-forum.cern.ch/u/abayyari)\
**Post date:** [April 6, 2025, 10:44pm UTC](https://root-forum.cern.ch/t/evaluating-multiple-tmva-pykeras-weights-using-the-same-intree-variables/63179/4 "2025-04-06T22:44:32Z")

</div>

Thank you, I’ll try this. Did you use different reader objects for the different weight responses or the same one for all of them?

---

<div class="post-metadata">

**Author:** ![jhchoi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jhchoi/32/38759_2.png) [@jhchoi](https://root-forum.cern.ch/u/jhchoi)\
**Post date:** [April 7, 2025, 11:25am UTC](https://root-forum.cern.ch/t/evaluating-multiple-tmva-pykeras-weights-using-the-same-intree-variables/63179/5 "2025-04-07T11:25:26Z")

</div>

I created three different TMVA::Reader objects for three different keras models.  
Then, I called the following member functions on each reader:  
AddVariable(), BookMVA(), and EvaluateMVA().  
(like,  
reader1-\>EvalueateMVA(“PyKeras::DNN”);  
reader2-\>EvalueateMVA(“PyKeras::DNN”);  
reader3-\>EvalueateMVA(“PyKeras::DNN”);  
)

Actually, I tested them in CMSSW enviroments. It works for CMSSW\_13\_X\_X whose root version is 6.26. It doesn’t work and keras model is broken for CMSSW\_14\_X\_X whose root version is 6.30.

That’s why I said I guess it’s a problem of ROOT version.
