[OBJ] Add basic objective function and registry
This commit is contained in:
107
src/common/math.h
Normal file
107
src/common/math.h
Normal file
@@ -0,0 +1,107 @@
|
||||
/*!
|
||||
* Copyright 2015 by Contributors
|
||||
* \file math.h
|
||||
* \brief additional math utils
|
||||
* \author Tianqi Chen
|
||||
*/
|
||||
#ifndef XGBOOST_COMMON_MATH_H_
|
||||
#define XGBOOST_COMMON_MATH_H_
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
namespace xgboost {
|
||||
namespace common {
|
||||
/*!
|
||||
* \brief calculate the sigmoid of the input.
|
||||
* \param x input parameter
|
||||
* \return the transformed value.
|
||||
*/
|
||||
inline float Sigmoid(float x) {
|
||||
return 1.0f / (1.0f + std::exp(-x));
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief do inplace softmax transformaton on p_rec
|
||||
* \param p_rec the input/output vector of the values.
|
||||
*/
|
||||
inline void Softmax(std::vector<float>* p_rec) {
|
||||
std::vector<float> &rec = *p_rec;
|
||||
float wmax = rec[0];
|
||||
for (size_t i = 1; i < rec.size(); ++i) {
|
||||
wmax = std::max(rec[i], wmax);
|
||||
}
|
||||
double wsum = 0.0f;
|
||||
for (size_t i = 0; i < rec.size(); ++i) {
|
||||
rec[i] = std::exp(rec[i] - wmax);
|
||||
wsum += rec[i];
|
||||
}
|
||||
for (size_t i = 0; i < rec.size(); ++i) {
|
||||
rec[i] /= static_cast<float>(wsum);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Find the maximum iterator within the iterators
|
||||
* \param begin The begining iterator.
|
||||
* \param end The end iterator.
|
||||
* \return the iterator point to the maximum value.
|
||||
* \tparam Iterator The type of the iterator.
|
||||
*/
|
||||
template<typename Iterator>
|
||||
inline Iterator FindMaxIndex(Iterator begin, Iterator end) {
|
||||
Iterator maxit = begin;
|
||||
for (Iterator it = begin; it != end; ++it) {
|
||||
if (*it > *maxit) maxit = it;
|
||||
}
|
||||
return maxit;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief perform numerically safe logsum
|
||||
* \param x left input operand
|
||||
* \param y right input operand
|
||||
* \return log(exp(x) + exp(y))
|
||||
*/
|
||||
inline float LogSum(float x, float y) {
|
||||
if (x < y) {
|
||||
return y + std::log(std::exp(x - y) + 1.0f);
|
||||
} else {
|
||||
return x + std::log(std::exp(y - x) + 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief perform numerically safe logsum
|
||||
* \param begin The begining iterator.
|
||||
* \param end The end iterator.
|
||||
* \return the iterator point to the maximum value.
|
||||
* \tparam Iterator The type of the iterator.
|
||||
*/
|
||||
template<typename Iterator>
|
||||
inline float LogSum(Iterator begin, Iterator end) {
|
||||
float mx = *begin;
|
||||
for (Iterator it = begin; it != end; ++it) {
|
||||
mx = std::max(mx, *it);
|
||||
}
|
||||
float sum = 0.0f;
|
||||
for (Iterator it = begin; it != end; ++it) {
|
||||
sum += std::exp(*it - mx);
|
||||
}
|
||||
return mx + std::log(sum);
|
||||
}
|
||||
|
||||
// comparator functions for sorting pairs in descending order
|
||||
inline static bool CmpFirst(const std::pair<float, unsigned> &a,
|
||||
const std::pair<float, unsigned> &b) {
|
||||
return a.first > b.first;
|
||||
}
|
||||
inline static bool CmpSecond(const std::pair<float, unsigned> &a,
|
||||
const std::pair<float, unsigned> &b) {
|
||||
return a.second > b.second;
|
||||
}
|
||||
} // namespace common
|
||||
} // namespace xgboost
|
||||
#endif // XGBOOST_COMMON_MATH_H_
|
||||
Reference in New Issue
Block a user