Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RBDT.hxx
Go to the documentation of this file.
1/**********************************************************************************
2 * Project: ROOT - a Root-integrated toolkit for multivariate data analysis *
3 * Package: TMVA *
4 * *
5 * *
6 * Description: *
7 * *
8 * Authors: *
9 * Stefan Wunsch (stefan.wunsch@cern.ch) *
10 * Jonas Rembser (jonas.rembser@cern.ch) *
11 * *
12 * Copyright (c) 2024: *
13 * CERN, Switzerland *
14 * *
15 * Redistribution and use in source and binary forms, with or without *
16 * modification, are permitted according to the terms listed in LICENSE *
17 * (see tmva/doc/LICENSE) *
18 **********************************************************************************/
19
20#ifndef TMVA_RBDT
21#define TMVA_RBDT
22
23#include <ROOT/RSpan.hxx>
24#include <TMVA/RTensor.hxx>
25
26#include <array>
27#include <istream>
28#include <string>
29#include <unordered_map>
30#include <vector>
31
32namespace TMVA {
33
34namespace Experimental {
35
36class RBDT final {
37public:
38 typedef float Value_t;
39
40 /// Compute model prediction on a single event.
41 ///
42 /// The method is intended to be used with std::vectors-like containers,
43 /// for example RVecs.
44 template <typename Vector>
45 Vector Compute(const Vector &x) const
46 {
47 std::size_t nOut = fBaseResponses.size() > 2 ? fBaseResponses.size() : 1;
48 Vector y(nOut);
49 ComputeImpl(x.data(), y.data());
50 return y;
51 }
52
53 /// Compute model prediction on a single event.
54 inline std::vector<Value_t> Compute(std::vector<Value_t> const &x) const { return Compute<std::vector<Value_t>>(x); }
55
57
58 static RBDT LoadXGBoost(std::string const &jsonPath);
59
60private:
61 /// Private default constructor, used by the public LoadXGBoost() factory.
62 RBDT() = default;
63
64 /// Map from XGBoost to RBDT indices.
65 using IndexMap = std::unordered_map<int, int>;
66
67 void Softmax(const Value_t *array, Value_t *out) const;
68 void ComputeImpl(const Value_t *array, Value_t *out) const;
69 Value_t EvaluateBinary(const Value_t *array) const;
70 static void correctIndices(std::span<int> indices, IndexMap const &nodeIndices, IndexMap const &leafIndices);
73
74 std::vector<int> fRootIndices;
75 std::vector<unsigned int> fCutIndices;
76 std::vector<Value_t> fCutValues;
77 std::vector<int> fLeftIndices;
78 std::vector<int> fRightIndices;
79 std::vector<Value_t> fResponses;
80 std::vector<int> fTreeNumbers;
81 std::vector<Value_t> fBaseResponses;
83 bool fLogistic = false;
84};
85
86} // namespace Experimental
87
88} // namespace TMVA
89
90#endif // TMVA_RBDT
std::vector< Value_t > fCutValues
Definition RBDT.hxx:76
static void terminateTree(TMVA::Experimental::RBDT &ff, int &nPreviousNodes, int &nPreviousLeaves, IndexMap &nodeIndices, IndexMap &leafIndices, int &treesSkipped)
Definition RBDT.cxx:202
RBDT()=default
Private default constructor, used by the public LoadXGBoost() factory.
static RBDT LoadXGBoost(std::string const &jsonPath)
Construct an RBDT from an XGBoost model in its native JSON serialization.
Definition RBDT.cxx:231
static void correctIndices(std::span< int > indices, IndexMap const &nodeIndices, IndexMap const &leafIndices)
RBDT uses a more efficient representation of the BDT in flat arrays.
Definition RBDT.cxx:181
std::vector< int > fRightIndices
Definition RBDT.hxx:78
std::unordered_map< int, int > IndexMap
Map from XGBoost to RBDT indices.
Definition RBDT.hxx:65
void Softmax(const Value_t *array, Value_t *out) const
Definition RBDT.cxx:119
std::vector< int > fTreeNumbers
Definition RBDT.hxx:80
Value_t EvaluateBinary(const Value_t *array) const
Definition RBDT.cxx:159
std::vector< Value_t > fResponses
Definition RBDT.hxx:79
std::vector< Value_t > fBaseResponses
Definition RBDT.hxx:81
std::vector< Value_t > Compute(std::vector< Value_t > const &x) const
Compute model prediction on a single event.
Definition RBDT.hxx:54
Vector Compute(const Vector &x) const
Compute model prediction on a single event.
Definition RBDT.hxx:45
std::vector< unsigned int > fCutIndices
Definition RBDT.hxx:75
void ComputeImpl(const Value_t *array, Value_t *out) const
Definition RBDT.cxx:146
std::vector< int > fRootIndices
Definition RBDT.hxx:74
std::vector< int > fLeftIndices
Definition RBDT.hxx:77
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
create variable transformations