xgboost
math.h
Go to the documentation of this file.
1 
7 #ifndef XGBOOST_COMMON_MATH_H_
8 #define XGBOOST_COMMON_MATH_H_
9 
10 #include <xgboost/base.h>
11 
12 #include <utility>
13 #include <vector>
14 #include <cmath>
15 #include <algorithm>
16 #include <utility>
17 
18 namespace xgboost {
19 namespace common {
25 XGBOOST_DEVICE inline float Sigmoid(float x) {
26  return 1.0f / (1.0f + expf(-x));
27 }
28 
37 template <typename Iterator>
38 XGBOOST_DEVICE inline void Softmax(Iterator start, Iterator end) {
39  static_assert(std::is_same<bst_float,
40  typename std::remove_reference<
41  decltype(std::declval<Iterator>().operator*())>::type
42  >::value,
43  "Values should be of type bst_float");
44  bst_float wmax = *start;
45  for (Iterator i = start+1; i != end; ++i) {
46  wmax = fmaxf(*i, wmax);
47  }
48  double wsum = 0.0f;
49  for (Iterator i = start; i != end; ++i) {
50  *i = expf(*i - wmax);
51  wsum += *i;
52  }
53  for (Iterator i = start; i != end; ++i) {
54  *i /= static_cast<float>(wsum);
55  }
56 }
57 
65 template<typename Iterator>
66 XGBOOST_DEVICE inline Iterator FindMaxIndex(Iterator begin, Iterator end) {
67  Iterator maxit = begin;
68  for (Iterator it = begin; it != end; ++it) {
69  if (*it > *maxit) maxit = it;
70  }
71  return maxit;
72 }
73 
80 inline float LogSum(float x, float y) {
81  if (x < y) {
82  return y + std::log(std::exp(x - y) + 1.0f);
83  } else {
84  return x + std::log(std::exp(y - x) + 1.0f);
85  }
86 }
87 
95 template<typename Iterator>
96 inline float LogSum(Iterator begin, Iterator end) {
97  float mx = *begin;
98  for (Iterator it = begin; it != end; ++it) {
99  mx = std::max(mx, *it);
100  }
101  float sum = 0.0f;
102  for (Iterator it = begin; it != end; ++it) {
103  sum += std::exp(*it - mx);
104  }
105  return mx + std::log(sum);
106 }
107 
108 // comparator functions for sorting pairs in descending order
109 inline static bool CmpFirst(const std::pair<float, unsigned> &a,
110  const std::pair<float, unsigned> &b) {
111  return a.first > b.first;
112 }
113 inline static bool CmpSecond(const std::pair<float, unsigned> &a,
114  const std::pair<float, unsigned> &b) {
115  return a.second > b.second;
116 }
117 
118 #if XGBOOST_STRICT_R_MODE
119 // check nan
120 bool CheckNAN(double v);
121 #else
122 template<typename T>
123 inline bool CheckNAN(T v) {
124 #ifdef _MSC_VER
125  return (_isnan(v) != 0);
126 #else
127  return std::isnan(v);
128 #endif // _MSC_VER
129 }
130 #endif // XGBOOST_STRICT_R_MODE_
131 
132 // GPU version is not uploaded in CRAN anyway.
133 // Specialize only when using R with CPU.
134 #if XGBOOST_STRICT_R_MODE && !defined(XGBOOST_USE_CUDA)
135 double LogGamma(double v);
136 
137 #else // Not R or R with GPU.
138 
139 template<typename T>
140 XGBOOST_DEVICE inline T LogGamma(T v) {
141 #ifdef _MSC_VER
142 
143 #if _MSC_VER >= 1800
144  return lgamma(v);
145 #else
146 #pragma message("Warning: lgamma function was not available until VS2013"\
147  ", poisson regression will be disabled")
148  utils::Error("lgamma function was not available until VS2013");
149  return static_cast<T>(1.0);
150 #endif // _MSC_VER >= 1800
151 
152 #else
153  return lgamma(v);
154 #endif // _MSC_VER
155 }
156 
157 #endif // XGBOOST_STRICT_R_MODE && !defined(XGBOOST_USE_CUDA)
158 
159 } // namespace common
160 } // namespace xgboost
161 #endif // XGBOOST_COMMON_MATH_H_
float bst_float
float type, used for storing statistics
Definition: base.h:89
bool CheckNAN(T v)
Definition: math.h:123
float LogSum(float x, float y)
perform numerically safe logsum
Definition: math.h:80
XGBOOST_DEVICE void Softmax(Iterator start, Iterator end)
Do inplace softmax transformaton on start to end.
Definition: math.h:38
XGBOOST_DEVICE T LogGamma(T v)
Definition: math.h:140
XGBOOST_DEVICE Iterator FindMaxIndex(Iterator begin, Iterator end)
Find the maximum iterator within the iterators.
Definition: math.h:66
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:75
namespace of xgboost
Definition: base.h:79
defines configuration macros of xgboost.
XGBOOST_DEVICE float Sigmoid(float x)
calculate the sigmoid of the input.
Definition: math.h:25