5 namespace pred_transform {
8 inline std::string identity(
const Model& model) {
10 "static inline float pred_transform(float margin) {\n" 15 inline std::string sigmoid(
const Model& model) {
16 const float alpha = model.param.sigmoid_alpha;
17 CHECK_GT(alpha, 0.0f) <<
"sigmoid: alpha must be strictly positive";
18 std::ostringstream oss;
19 oss <<
"static inline float pred_transform(float margin) {\n" 20 <<
" const float alpha = (float)" << common::ToString(alpha) <<
";\n" 21 <<
" return 1.0f / (1 + expf(-alpha * margin));\n" 26 inline std::string exponential(
const Model& model) {
28 "static inline float pred_transform(float margin) {\n" 29 " return expf(margin);\n" 33 inline std::string logarithm_one_plus_exp(
const Model& model) {
35 "static inline float pred_transform(float margin) {\n" 36 " return log1pf(expf(margin));\n" 40 inline std::string identity_multiclass(
const Model& model) {
41 CHECK(model.num_output_group > 1)
42 <<
"identity_multiclass: model is not a proper multi-class classifier";
43 const int num_class = model.num_output_group;
44 std::ostringstream oss;
45 oss <<
"static inline size_t pred_transform(float* pred) {\n" 46 <<
" const size_t num_class = " << num_class <<
";\n" 47 <<
" return num_class;\n" 52 inline std::string max_index(
const Model& model) {
53 CHECK(model.num_output_group > 1)
54 <<
"max_index: model is not a proper multi-class classifier";
55 const int num_class = model.num_output_group;
56 std::ostringstream oss;
57 oss <<
"static inline size_t pred_transform(float* pred) {\n" 58 <<
" const int num_class = " << num_class <<
";\n" 59 <<
" int max_index = 0;\n" 60 <<
" float max_margin = pred[0];\n" 61 <<
" for (int k = 1; k < num_class; ++k) {\n" 62 <<
" if (pred[k] > max_margin) {\n" 63 <<
" max_margin = pred[k];\n" 64 <<
" max_index = k;\n" 67 <<
" pred[0] = (float)max_index;\n" 73 inline std::string softmax(
const Model& model) {
74 CHECK(model.num_output_group > 1)
75 <<
"softmax: model is not a proper multi-class classifier";
76 const int num_class = model.num_output_group;
77 std::ostringstream oss;
78 oss <<
"static inline size_t pred_transform(float* pred) {\n" 79 <<
" const int num_class = " << num_class <<
";\n" 80 <<
" float max_margin = pred[0];\n" 81 <<
" double norm_const = 0.0;\n" 83 <<
" for (int k = 1; k < num_class; ++k) {\n" 84 <<
" if (pred[k] > max_margin) {\n" 85 <<
" max_margin = pred[k];\n" 88 <<
" for (int k = 0; k < num_class; ++k) {\n" 89 <<
" t = expf(pred[k] - max_margin);\n" 90 <<
" norm_const += t;\n" 93 <<
" for (int k = 0; k < num_class; ++k) {\n" 94 <<
" pred[k] /= (float)norm_const;\n" 96 <<
" return (size_t)num_class;\n" 101 inline std::string multiclass_ova(
const Model& model) {
102 CHECK(model.num_output_group > 1)
103 <<
"multiclass_ova: model is not a proper multi-class classifier";
104 const int num_class = model.num_output_group;
105 const float alpha = model.param.sigmoid_alpha;
106 CHECK_GT(alpha, 0.0f) <<
"multiclass_ova: alpha must be strictly positive";
107 std::ostringstream oss;
108 oss <<
"static inline size_t pred_transform(float* pred) {\n" 109 <<
" const float alpha = (float)" << common::ToString(alpha) <<
";\n" 110 <<
" const int num_class = " << num_class <<
";\n" 111 <<
" for (int k = 0; k < num_class; ++k) {\n" 112 <<
" pred[k] = 1.0f / (1.0f + expf(-alpha * pred[k]));\n" 114 <<
" return (size_t)num_class;\n"