Belle II Software light-2405-quaxo
FastBDTExpert Class Reference

Expert for the FastBDT MVA method. More...

#include <FastBDT.h>

Inheritance diagram for FastBDTExpert:
Collaboration diagram for FastBDTExpert:

Public Member Functions

virtual void load (Weightfile &weightfile) override
 Load the expert from a Weightfile.
 
virtual std::vector< float > apply (Dataset &test_data) const override
 Apply this expert onto a dataset.
 
virtual std::vector< std::vector< float > > applyMulticlass (Dataset &test_data) const
 Apply this m_expert onto a dataset.
 

Protected Attributes

GeneralOptions m_general_options
 General options loaded from the weightfile.
 

Private Attributes

FastBDTOptions m_specific_options
 Method specific options.
 
FastBDT::Forest m_expert_forest
 Forest Expert.
 
std::vector< FastBDT::FeatureBinning< float > > m_expert_feature_binning
 Forest feature binning.
 

Detailed Description

Expert for the FastBDT MVA method.

Definition at line 122 of file FastBDT.h.

Member Function Documentation

◆ apply()

std::vector< float > apply ( Dataset test_data) const
overridevirtual

Apply this expert onto a dataset.

Parameters
test_datadataset

Implements Expert.

Definition at line 415 of file FastBDT.cc.

416 {
417
418 std::vector<float> probabilities(test_data.getNumberOfEvents());
419 for (unsigned int iEvent = 0; iEvent < test_data.getNumberOfEvents(); ++iEvent) {
420 test_data.loadEvent(iEvent);
421#if FastBDT_VERSION_MAJOR >= 3
422#if FastBDT_VERSION_MAJOR >= 5
423 if (m_use_simplified_interface)
424 probabilities[iEvent] = m_classifier.predict(test_data.m_input);
425 else
426 probabilities[iEvent] = m_expert_forest.Analyse(test_data.m_input);
427#else
428 probabilities[iEvent] = m_expert_forest.Analyse(test_data.m_input);
429#endif
430#else
431 std::vector<unsigned int> bins(m_expert_feature_binning.size());
432 for (unsigned int iFeature = 0; iFeature < m_expert_feature_binning.size(); ++iFeature) {
433 bins[iFeature] = m_expert_feature_binning[iFeature].ValueToBin(test_data.m_input[iFeature]);
434 }
435 probabilities[iEvent] = m_expert_forest.Analyse(bins);
436#endif
437 }
438
439 return probabilities;
440
441 }
std::vector< FastBDT::FeatureBinning< float > > m_expert_feature_binning
Forest feature binning.
Definition: FastBDT.h:147
FastBDT::Forest m_expert_forest
Forest Expert.
Definition: FastBDT.h:146

◆ applyMulticlass()

virtual std::vector< std::vector< float > > applyMulticlass ( Dataset test_data) const
inlinevirtualinherited

Apply this m_expert onto a dataset.

Multiclass mode. Not pure virtual, since not all derived classes to re-implement this.

Parameters
test_datadataset.
Returns
vector of size N=test_data.getNumberOfEvents() with N=m_classes.size() scores for each event in the dataset.

Reimplemented in PythonExpert, TMVAExpertMulticlass, and TrivialExpert.

Definition at line 56 of file Expert.h.

57 {
58
59 B2ERROR("Attempted to call applyMulticlass() of the abstract base class MVA::Expert. All methods that support multiclass classification should override this definition.");
60 (void) test_data;
61
62 return std::vector<std::vector<float>>();
63 };

◆ load()

void load ( Weightfile weightfile)
overridevirtual

Load the expert from a Weightfile.

Parameters
weightfilecontaining all information necessary to build the expert

Implements Expert.

Definition at line 322 of file FastBDT.cc.

323 {
324
325 std::string custom_weightfile = weightfile.generateFileName();
326 weightfile.getFile("FastBDT_Weightfile", custom_weightfile);
327 std::fstream file(custom_weightfile, std::ios_base::in);
328
329 int version = weightfile.getElement<int>("FastBDT_version", 0);
330 B2DEBUG(100, "FastBDT Weightfile Version " << version);
331 if (version < 2) {
332#if FastBDT_VERSION_MAJOR >= 3
333 std::stringstream s;
334 {
335 std::string t;
336 std::fstream file2(custom_weightfile, std::ios_base::in);
337 getline(file2, t);
338 s << t;
339 }
340 int dummy;
341 // Try to read to integers, if this is successful we have a old weightfile with a Feature Binning before the Tree.
342 if (!(s >> dummy >> dummy)) {
343 B2DEBUG(100, "FastBDT: I read a new weightfile of FastBDT using the new FastBDT version 3. Everything fine!");
344 // New format since version 3
345 m_expert_forest = FastBDT::readForestFromStream<float>(file);
346 } else {
347 B2INFO("FastBDT: I read an old weightfile of FastBDT using the new FastBDT version 3."
348 "I will convert your FastBDT on-the-fly to the new version."
349 "Retrain the classifier to get rid of this message");
350 // Old format before version 3
351 // We read in first the feature binnings and than rewrite the tree
352 std::vector<FastBDT::FeatureBinning<float>> feature_binnings;
353 file >> feature_binnings;
354 double F0;
355 file >> F0;
356 double shrinkage;
357 file >> shrinkage;
358 // This parameter was not available in the old version
359 bool transform2probability = true;
360 FastBDT::Forest<unsigned int> temp_forest(shrinkage, F0, transform2probability);
361 unsigned int size;
362 file >> size;
363 for (unsigned int i = 0; i < size; ++i) {
364 temp_forest.AddTree(FastBDT::readTreeFromStream<unsigned int>(file));
365 }
366
367 FastBDT::Forest<float> cleaned_forest(temp_forest.GetShrinkage(), temp_forest.GetF0(), temp_forest.GetTransform2Probability());
368 for (auto& tree : temp_forest.GetForest()) {
369 cleaned_forest.AddTree(FastBDT::removeFeatureBinningTransformationFromTree(tree, feature_binnings));
370 }
371 m_expert_forest = cleaned_forest;
372 }
373#else
374 B2INFO("FastBDT: I read an old weightfile of FastBDT using the old FastBDT version."
375 "I try to fix the weightfile first to avoid problems due to NaN and inf values."
376 "Consider to switch to the newer version of FastBDT (newer externals)");
377 // Check for nan or inf in file and replace with 0
378 std::stringstream s;
379 std::string t;
380 while (getline(file, t)) {
381 size_t f = 0;
382
383 while ((f = t.find("inf", f)) != std::string::npos) {
384 t.replace(f, std::string("inf").length(), std::string("0.0"));
385 f += std::string("0.0").length();
386 B2WARNING("Found infinity in FastBDT weightfile, I replace it with 0 to prevent horrible crashes, this is fixed in the newer version");
387 }
388 f = 0;
389 while ((f = t.find("nan", f)) != std::string::npos) {
390 t.replace(f, std::string("nan").length(), std::string("0.0"));
391 f += std::string("0.0").length();
392 B2WARNING("Found nan in FastBDT weightfile, I replace it with 0 to prevent horrible crashes, this is fixed in the newer version");
393 }
394 s << t + '\n';
395 }
397 m_expert_forest = FastBDT::readForestFromStream(s);
398#endif
399 }
400#if FastBDT_VERSION_MAJOR >= 5
401 else {
402 m_use_simplified_interface = true;
403 m_classifier = FastBDT::Classifier(file);
404 }
405#else
406 else {
407 B2ERROR("Unknown Version 2 of Weightfile, please use a more recent FastBDT version");
408 }
409#endif
410 file.close();
411
412 weightfile.getOptions(m_specific_options);
413 }
FastBDTOptions m_specific_options
Method specific options.
Definition: FastBDT.h:138

Member Data Documentation

◆ m_expert_feature_binning

std::vector<FastBDT::FeatureBinning<float> > m_expert_feature_binning
private

Forest feature binning.

Definition at line 147 of file FastBDT.h.

◆ m_expert_forest

FastBDT::Forest m_expert_forest
private

Forest Expert.

Definition at line 146 of file FastBDT.h.

◆ m_general_options

GeneralOptions m_general_options
protectedinherited

General options loaded from the weightfile.

Definition at line 70 of file Expert.h.

◆ m_specific_options

FastBDTOptions m_specific_options
private

Method specific options.

Definition at line 138 of file FastBDT.h.


The documentation for this class was generated from the following files: