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 <algorithm>
13 #include <cmath>
14 #include <limits>
15 #include <utility>
16 #include <vector>
17 
18 namespace xgboost {
19 namespace common {
25 XGBOOST_DEVICE inline float Sigmoid(float x) {
26  float constexpr kEps = 1e-16; // avoid 0 div
27  x = std::min(-x, 88.7f); // avoid exp overflow
28  auto denom = expf(x) + 1.0f + kEps;
29  auto y = 1.0f / denom;
30  return y;
31 }
32 
33 template <typename T>
34 XGBOOST_DEVICE inline static T Sqr(T a) { return a * a; }
35 
39 template <typename T, typename U>
40 XGBOOST_DEVICE constexpr bool CloseTo(T a, U b) {
41  using Casted =
42  typename std::conditional<
43  std::is_floating_point<T>::value || std::is_floating_point<U>::value,
44  double,
45  typename std::conditional<
46  std::is_signed<T>::value || std::is_signed<U>::value,
47  int64_t,
48  uint64_t>::type>::type;
49  return std::is_floating_point<Casted>::value ?
50  std::abs(static_cast<Casted>(a) -static_cast<Casted>(b)) < 1e-6 : a == b;
51 }
52 
61 template <typename Iterator>
62 XGBOOST_DEVICE inline void Softmax(Iterator start, Iterator end) {
63  static_assert(std::is_same<bst_float,
64  typename std::remove_reference<
65  decltype(std::declval<Iterator>().operator*())>::type
66  >::value,
67  "Values should be of type bst_float");
68  bst_float wmax = *start;
69  for (Iterator i = start+1; i != end; ++i) {
70  wmax = fmaxf(*i, wmax);
71  }
72  double wsum = 0.0f;
73  for (Iterator i = start; i != end; ++i) {
74  *i = expf(*i - wmax);
75  wsum += *i;
76  }
77  for (Iterator i = start; i != end; ++i) {
78  *i /= static_cast<float>(wsum);
79  }
80 }
81 
89 template<typename Iterator>
90 XGBOOST_DEVICE inline Iterator FindMaxIndex(Iterator begin, Iterator end) {
91  Iterator maxit = begin;
92  for (Iterator it = begin; it != end; ++it) {
93  if (*it > *maxit) maxit = it;
94  }
95  return maxit;
96 }
97 
104 inline float LogSum(float x, float y) {
105  if (x < y) {
106  return y + std::log(std::exp(x - y) + 1.0f);
107  } else {
108  return x + std::log(std::exp(y - x) + 1.0f);
109  }
110 }
111 
119 template<typename Iterator>
120 inline float LogSum(Iterator begin, Iterator end) {
121  float mx = *begin;
122  for (Iterator it = begin; it != end; ++it) {
123  mx = std::max(mx, *it);
124  }
125  float sum = 0.0f;
126  for (Iterator it = begin; it != end; ++it) {
127  sum += std::exp(*it - mx);
128  }
129  return mx + std::log(sum);
130 }
131 
132 // comparator functions for sorting pairs in descending order
133 inline static bool CmpFirst(const std::pair<float, unsigned> &a,
134  const std::pair<float, unsigned> &b) {
135  return a.first > b.first;
136 }
137 inline static bool CmpSecond(const std::pair<float, unsigned> &a,
138  const std::pair<float, unsigned> &b) {
139  return a.second > b.second;
140 }
141 
142 // Redefined here to workaround a VC bug that doesn't support overloading for integer
143 // types.
144 template <typename T>
145 XGBOOST_DEVICE typename std::enable_if<
146  std::numeric_limits<T>::is_integer, bool>::type
148  return false;
149 }
150 
151 #if XGBOOST_STRICT_R_MODE && !defined(__CUDA_ARCH__)
152 
153 bool CheckNAN(double v);
154 
155 #else
156 
157 XGBOOST_DEVICE bool inline CheckNAN(float x) {
158 #if defined(__CUDA_ARCH__)
159  return isnan(x);
160 #else
161  return std::isnan(x);
162 #endif // defined(__CUDA_ARCH__)
163 }
164 
165 XGBOOST_DEVICE bool inline CheckNAN(double x) {
166 #if defined(__CUDA_ARCH__)
167  return isnan(x);
168 #else
169  return std::isnan(x);
170 #endif // defined(__CUDA_ARCH__)
171 }
172 
173 #endif // XGBOOST_STRICT_R_MODE && !defined(__CUDA_ARCH__)
174 // GPU version is not uploaded in CRAN anyway.
175 // Specialize only when using R with CPU.
176 #if XGBOOST_STRICT_R_MODE && !defined(XGBOOST_USE_CUDA)
177 double LogGamma(double v);
178 
179 #else // Not R or R with GPU.
180 
181 template<typename T>
182 XGBOOST_DEVICE inline T LogGamma(T v) {
183 #ifdef _MSC_VER
184 
185 #if _MSC_VER >= 1800
186  return lgamma(v);
187 #else
188 #pragma message("Warning: lgamma function was not available until VS2013"\
189  ", poisson regression will be disabled")
190  utils::Error("lgamma function was not available until VS2013");
191  return static_cast<T>(1.0);
192 #endif // _MSC_VER >= 1800
193 
194 #else
195  return lgamma(v);
196 #endif // _MSC_VER
197 }
198 
199 #endif // XGBOOST_STRICT_R_MODE && !defined(XGBOOST_USE_CUDA)
200 
201 } // namespace common
202 } // namespace xgboost
203 #endif // XGBOOST_COMMON_MATH_H_
xgboost::common::FindMaxIndex
XGBOOST_DEVICE Iterator FindMaxIndex(Iterator begin, Iterator end)
Find the maximum iterator within the iterators.
Definition: math.h:90
base.h
defines configuration macros of xgboost.
xgboost::common::Softmax
XGBOOST_DEVICE void Softmax(Iterator start, Iterator end)
Do inplace softmax transformaton on start to end.
Definition: math.h:62
xgboost::common::LogSum
float LogSum(float x, float y)
perform numerically safe logsum
Definition: math.h:104
xgboost::common::CloseTo
constexpr XGBOOST_DEVICE bool CloseTo(T a, U b)
Equality test for both integer and floating point.
Definition: math.h:40
xgboost::common::CheckNAN
XGBOOST_DEVICE std::enable_if< std::numeric_limits< T >::is_integer, bool >::type CheckNAN(T)
Definition: math.h:147
xgboost::common::aft::kEps
constexpr double kEps
Definition: survival_util.h:47
XGBOOST_DEVICE
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition: base.h:84
xgboost::common::LogGamma
XGBOOST_DEVICE T LogGamma(T v)
Definition: math.h:182
xgboost::common::Sigmoid
XGBOOST_DEVICE float Sigmoid(float x)
calculate the sigmoid of the input.
Definition: math.h:25
xgboost
namespace of xgboost
Definition: base.h:110
xgboost::bst_float
float bst_float
float type, used for storing statistics
Definition: base.h:119