sse_loss.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_XGBOOST_LOSS_FUNCTIONS_SSE_LOSS_HPP
14 #define MLPACK_METHODS_XGBOOST_LOSS_FUNCTIONS_SSE_LOSS_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 namespace mlpack {
19 namespace ensemble {
20 
29 class SSELoss
30 {
31  public:
32  // Default constructor---No regularization.
33  SSELoss() : alpha(0), lambda(0) { /* Nothing to do. */}
34 
35  SSELoss(const double alpha, const double lambda):
36  alpha(alpha), lambda(lambda)
37  {
38  // Nothing to do.
39  }
40 
44  template<typename VecType>
45  typename VecType::elem_type InitialPrediction(const VecType& values)
46  {
47  // Sanity check for empty vector.
48  if (values.n_elem == 0)
49  return 0;
50 
51  return arma::accu(values) / (typename VecType::elem_type) values.n_elem;
52  }
53 
57  template<typename MatType, typename WeightVecType>
58  double OutputLeafValue(const MatType& /* input */,
59  const WeightVecType& /* weights */)
60  {
61  return -ApplyL1(arma::accu(gradients)) / (arma::accu(hessians) + lambda);
62  }
63 
70  double Evaluate(const size_t begin, const size_t end)
71  {
72  return std::pow(ApplyL1(arma::accu(gradients.subvec(begin, end))), 2) /
73  (arma::accu(hessians.subvec(begin, end)) + lambda);
74  }
75 
86  template<bool UseWeights, typename MatType, typename WeightVecType>
87  double Evaluate(const MatType& input, const WeightVecType& /* weights */)
88  {
89  // Calculate gradients and hessians.
90  gradients = (input.row(1) - input.row(0)).t();
91  hessians = arma::vec(input.n_cols, arma::fill::ones);
92 
93  return std::pow(ApplyL1(arma::accu(gradients)), 2) /
94  (arma::accu(hessians) + lambda);
95  }
96  private:
98  const double lambda;
100  const double alpha;
102  arma::vec gradients;
104  arma::vec hessians;
105 
107  double ApplyL1(const double sumGradients)
108  {
109  if (sumGradients > alpha)
110  {
111  return sumGradients - alpha;
112  }
113  else if (sumGradients < - alpha)
114  {
115  return sumGradients + alpha;
116  }
117 
118  return 0;
119  }
120 };
121 
122 } // namespace ensemble
123 } // namespace mlpack
124 
125 #endif
VecType::elem_type InitialPrediction(const VecType &values)
Returns the initial predition for gradient boosting.
Definition: sse_loss.hpp:45
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
double Evaluate(const MatType &input, const WeightVecType &)
Calculates the gain of the node before splitting.
Definition: sse_loss.hpp:87
double OutputLeafValue(const MatType &, const WeightVecType &)
Returns the output value for the leaf in the tree.
Definition: sse_loss.hpp:58
The SSE (Sum of Squared Errors) loss is a loss function to measure the quality of prediction of respo...
Definition: sse_loss.hpp:29
SSELoss(const double alpha, const double lambda)
Definition: sse_loss.hpp:35
double Evaluate(const size_t begin, const size_t end)
Calculates the gain from begin to end.
Definition: sse_loss.hpp:70