Treelite
pred_transform.cc
1 
8 #include "./pred_transform.h"
9 #include <treelite/tree.h>
10 #include <treelite/logging.h>
11 #include <string>
12 #include <unordered_map>
13 #include <cmath>
14 #include <cstddef>
15 
16 namespace treelite {
17 namespace gtil {
18 namespace pred_transform {
19 
20 std::size_t identity(const treelite::Model&, const float* in, float* out) {
21  *out = *in;
22  return 1;
23 }
24 
25 std::size_t signed_square(const treelite::Model&, const float* in, float* out) {
26  const float margin = *in;
27  *out = std::copysign(margin * margin, margin);
28  return 1;
29 }
30 
31 std::size_t hinge(const treelite::Model&, const float* in, float* out) {
32  *out = (*in > 0 ? 1.0f : 0.0f);
33  return 1;
34 }
35 
36 std::size_t sigmoid(const treelite::Model& model, const float* in, float* out) {
37  const float alpha = model.param.sigmoid_alpha;
38  TREELITE_CHECK(alpha > 0.0f) << "sigmoid: alpha must be strictly positive";
39  *out = 1.0f / (1.0f + std::exp(-alpha * *in));
40  return 1;
41 }
42 
43 std::size_t exponential(const treelite::Model&, const float* in, float* out) {
44  *out = std::exp(*in);
45  return 1;
46 }
47 
48 std::size_t exponential_standard_ratio(const treelite::Model& model, const float* in, float* out) {
49  const float ratio_c = model.param.ratio_c;
50  *out = std::exp2(- *in / ratio_c);
51  return 1;
52 }
53 
54 std::size_t logarithm_one_plus_exp(const treelite::Model&, const float* in, float* out) {
55  *out = std::log1p(std::exp(*in));
56  return 1;
57 }
58 
59 std::size_t identity_multiclass(const treelite::Model& model, const float* in, float* out) {
60  auto num_class = static_cast<std::size_t>(model.task_param.num_class);
61  TREELITE_CHECK(num_class > 1) << "model must be a multi-class classifier";
62  for (std::size_t i = 0; i < num_class; ++i) {
63  out[i] = in[i];
64  }
65  return num_class;
66 }
67 
68 std::size_t max_index(const treelite::Model& model, const float* in, float* out) {
69  auto num_class = static_cast<std::size_t>(model.task_param.num_class);
70  TREELITE_CHECK(num_class > 1) << "model must be a multi-class classifier";
71  std::size_t max_index = 0;
72  float max_margin = in[0];
73  for (std::size_t i = 1; i < num_class; ++i) {
74  if (in[i] > max_margin) {
75  max_margin = in[i];
76  max_index = i;
77  }
78  }
79  out[0] = static_cast<float>(max_index);
80  return 1;
81 }
82 
83 std::size_t softmax(const treelite::Model& model, const float* in, float* out) {
84  auto num_class = static_cast<std::size_t>(model.task_param.num_class);
85  TREELITE_CHECK(num_class > 1) << "model must be a multi-class classifier";
86  float max_margin = in[0];
87  double norm_const = 0.0;
88  float t;
89  for (std::size_t i = 1; i < num_class; ++i) {
90  if (in[i] > max_margin) {
91  max_margin = in[i];
92  }
93  }
94  for (std::size_t i = 0; i < num_class; ++i) {
95  t = std::exp(in[i] - max_margin);
96  norm_const += t;
97  out[i] = t;
98  }
99  for (std::size_t i = 0; i < num_class; ++i) {
100  out[i] /= static_cast<float>(norm_const);
101  }
102  return num_class;
103 }
104 
105 std::size_t multiclass_ova(const treelite::Model& model, const float* in, float* out) {
106  auto num_class = static_cast<std::size_t>(model.task_param.num_class);
107  TREELITE_CHECK(num_class > 1) << "model must be a multi-class classifier";
108  const float alpha = model.param.sigmoid_alpha;
109  TREELITE_CHECK(alpha > 0.0f) << "multiclass_ova: alpha must be strictly positive";
110  for (std::size_t i = 0; i < num_class; ++i) {
111  out[i] = 1.0f / (1.0f + std::exp(-alpha * in[i]));
112  }
113  return num_class;
114 }
115 
116 } // namespace pred_transform
117 
118 const std::unordered_map<std::string, PredTransformFuncType> pred_transform_func{
119  {"identity", pred_transform::identity},
120  {"signed_square", pred_transform::signed_square},
121  {"hinge", pred_transform::hinge},
122  {"sigmoid", pred_transform::sigmoid},
123  {"exponential", pred_transform::exponential},
124  {"exponential_standard_ratio", pred_transform::exponential_standard_ratio},
125  {"logarithm_one_plus_exp", pred_transform::logarithm_one_plus_exp},
126  {"identity_multiclass", pred_transform::identity_multiclass},
127  {"max_index", pred_transform::max_index},
128  {"softmax", pred_transform::softmax},
129  {"multiclass_ova", pred_transform::multiclass_ova}
130 };
131 
132 PredTransformFuncType LookupPredTransform(const std::string& name) {
133  return pred_transform_func.at(name);
134 }
135 
136 } // namespace gtil
137 } // namespace treelite
ModelParam param
extra parameters
Definition: tree.h:772
float sigmoid_alpha
scaling parameter for sigmoid function sigmoid(x) = 1 / (1 + exp(-alpha * x))
Definition: tree.h:676
model structure for tree ensemble
logging facility for Treelite
unsigned int num_class
The number of classes in the target label.
Definition: tree.h:193
float ratio_c
scaling parameter for exponential standard ratio transformation expstdratio(x) = exp2(-x / c) ...
Definition: tree.h:684
TaskParam task_param
Group of parameters that are specific to the particular task type.
Definition: tree.h:770
thin wrapper for tree ensemble model
Definition: tree.h:717