25#include <nlohmann/json.hpp>
36template <
class Value_t>
43 for (
int i = 1; i <
nOut; ++i) {
46 for (
int i = 0; i <
nOut; ++i) {
51 for (
int i = 0; i <
nOut; ++i) {
52 out[i] /=
static_cast<float>(
norm);
58template <
class NumericType>
59struct NumericAfterSubstrOutput {
60 explicit NumericAfterSubstrOutput()
72template <
class NumericType>
79 std::size_t found = str.find(
substr);
80 if (found != std::string::npos) {
82 std::stringstream
ss(str.substr(found +
substr.size(), str.size() - found +
substr.size()));
85 output.failed =
false;
86 output.rest =
ss.str();
102 const std::size_t
rows =
x.GetShape()[0];
103 const std::size_t
cols =
x.GetShape()[1];
121 std::size_t
nOut = fBaseResponses.size() > 2 ? fBaseResponses.size() : 1;
123 throw std::runtime_error(
124 "Error in RBDT::softmax : binary classification models don't support softmax evaluation. Plase set "
125 "the number of classes in the RBDT-creating function if this is a multiclassification model.");
128 for (std::size_t i = 0; i <
nOut; ++i) {
129 out[i] = fBaseScore + fBaseResponses[i];
133 for (
int index : fRootIndices) {
135 int r = fRightIndices[
index];
136 int l = fLeftIndices[
index];
148 std::size_t
nOut = fBaseResponses.size() > 2 ? fBaseResponses.size() : 1;
152 out[0] = EvaluateBinary(array);
154 out[0] = 1.0 / (1.0 + std::exp(-out[0]));
161 Value_t out = fBaseScore + fBaseResponses[0];
167 int r = fRightIndices[
index];
168 int l = fLeftIndices[
index];
171 out += fResponses[-
index];
184 for (
int &idx : indices) {
196 errMsg <<
"RBDT: something is wrong in the node structure - node with index " << idx <<
" doesn't exist";
197 throw std::runtime_error(
errMsg.str());
214 ff.fBaseResponses[
treeNumbers %
ff.fBaseResponses.size()] +=
ff.fResponses.back();
215 ff.fResponses.pop_back();
233 const std::string
info =
"constructing RBDT from '" +
jsonPath +
"': ";
236 throw std::runtime_error(
info +
"file does not exist");
245 auto const &
learner =
j.at(
"learner");
250 static const std::unordered_map<std::string, std::string>
objectiveMap{
251 {
"multi:softprob",
"softmax"},
252 {
"binary:logistic",
"logistic"},
253 {
"reg:linear",
"identity"},
254 {
"reg:squarederror",
"identity"},
262 throw std::runtime_error(
info +
"XGBoost model has unsupported objective \"" +
xgbObjective +
263 "\". Supported objectives are " +
supported +
".");
276 throw std::runtime_error(
info +
"model contains multiple base scores, which is not supported. This "
277 "typically occurs with XGBoost >= 3.1.0, which supports multi-target base "
299 auto const &
trees =
learner.at(
"gradient_booster").at(
"model").at(
"trees");
313 for (
auto const &tree :
trees) {
320 for (std::size_t i = 0; i <
nNodes; ++i) {
331 ff.fLeftIndices.push_back(left);
342 std::stringstream
ss;
343 ss <<
info <<
"Forest has " <<
ff.fRootIndices.size() <<
" trees, which is not compatible with " <<
nClasses
345 throw std::runtime_error(
ss.str());
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t wmax
R__EXTERN TSystem * gSystem
const_iterator begin() const
const_iterator end() const
static void terminateTree(TMVA::Experimental::RBDT &ff, int &nPreviousNodes, int &nPreviousLeaves, IndexMap &nodeIndices, IndexMap &leafIndices, int &treesSkipped)
static RBDT LoadXGBoost(std::string const &jsonPath)
Construct an RBDT from an XGBoost model in its native JSON serialization.
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.
std::unordered_map< int, int > IndexMap
Map from XGBoost to RBDT indices.
void Softmax(const Value_t *array, Value_t *out) const
Value_t EvaluateBinary(const Value_t *array) const
std::vector< Value_t > fBaseResponses
Vector Compute(const Vector &x) const
Compute model prediction on a single event.
void ComputeImpl(const Value_t *array, Value_t *out) const
RTensor is a container with contiguous memory and shape information.
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.