objective_function.h 3.56 KB
Newer Older
1
2
3
4
/*!
 * Copyright (c) 2016 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
Guolin Ke's avatar
Guolin Ke committed
5
6
7
8
9
#ifndef LIGHTGBM_OBJECTIVE_FUNCTION_H_
#define LIGHTGBM_OBJECTIVE_FUNCTION_H_

#include <LightGBM/config.h>
#include <LightGBM/dataset.h>
10
11
#include <LightGBM/meta.h>

12
13
14
#include <string>
#include <functional>

Guolin Ke's avatar
Guolin Ke committed
15
16
17
18
19
namespace LightGBM {
/*!
* \brief The interface of Objective Function.
*/
class ObjectiveFunction {
Nikita Titov's avatar
Nikita Titov committed
20
 public:
Guolin Ke's avatar
Guolin Ke committed
21
22
23
24
25
26
27
28
29
30
31
  /*! \brief virtual destructor */
  virtual ~ObjectiveFunction() {}

  /*!
  * \brief Initialize
  * \param metadata Label data
  * \param num_data Number of data
  */
  virtual void Init(const Metadata& metadata, data_size_t num_data) = 0;

  /*!
Qiwei Ye's avatar
Qiwei Ye committed
32
33
  * \brief calculating first order derivative of loss function
  * \param score prediction score in this round
Guolin Ke's avatar
Guolin Ke committed
34
35
36
  * \gradients Output gradients
  * \hessians Output hessians
  */
37
  virtual void GetGradients(const double* score,
38
    score_t* gradients, score_t* hessians) const = 0;
Guolin Ke's avatar
Guolin Ke committed
39

Guolin Ke's avatar
Guolin Ke committed
40
41
  virtual const char* GetName() const = 0;

42
43
  virtual bool IsConstantHessian() const { return false; }

44
  virtual bool IsRenewTreeOutput() const { return false; }
45

Guolin Ke's avatar
Guolin Ke committed
46
  virtual double RenewTreeOutput(double ori_output, std::function<double(const label_t*, int)>,
47
48
49
50
                                 const data_size_t*,
                                 const data_size_t*,
                                 data_size_t) const { return ori_output; }

51
52
53
  virtual void RenewTreeOutputCUDA(const double* /*score*/, const data_size_t* /*data_indices_in_leaf*/, const data_size_t* /*num_data_in_leaf*/,
    const data_size_t* /*data_start_in_leaf*/, const int /*num_leaves*/, double* /*leaf_value*/) const {}

54
55
56
  virtual double BoostFromScore(int /*class_id*/) const { return 0.0; }

  virtual bool ClassNeedTrain(int /*class_id*/) const { return true; }
57

58
59
  virtual bool SkipEmptyClass() const { return false; }

Guolin Ke's avatar
Guolin Ke committed
60
  virtual int NumModelPerIteration() const { return 1; }
61

Guolin Ke's avatar
Guolin Ke committed
62
  virtual int NumPredictOneRow() const { return 1; }
63

64
65
66
  /*! \brief The prediction should be accurate or not. True will disable early stopping for prediction. */
  virtual bool NeedAccuratePrediction() const { return true; }

Guolin Ke's avatar
Guolin Ke committed
67
68
69
  /*! \brief Return the number of positive samples. Return 0 if no binary classification tasks.*/
  virtual data_size_t NumPositiveData() const { return 0; }

Guolin Ke's avatar
Guolin Ke committed
70
71
  virtual void ConvertOutput(const double* input, double* output) const {
    output[0] = input[0];
72
73
74
75
  }

  virtual std::string ToString() const = 0;

Guolin Ke's avatar
Guolin Ke committed
76
77
78
79
80
  ObjectiveFunction() = default;
  /*! \brief Disable copy */
  ObjectiveFunction& operator=(const ObjectiveFunction&) = delete;
  /*! \brief Disable copy */
  ObjectiveFunction(const ObjectiveFunction&) = delete;
Guolin Ke's avatar
Guolin Ke committed
81
82
83
84
85
86

  /*!
  * \brief Create object of objective function
  * \param type Specific type of objective function
  * \param config Config for objective function
  */
87
  LIGHTGBM_EXPORT static ObjectiveFunction* CreateObjectiveFunction(const std::string& type,
Guolin Ke's avatar
Guolin Ke committed
88
    const Config& config);
89
90
91
92
93

  /*!
  * \brief Load objective function from string object
  */
  LIGHTGBM_EXPORT static ObjectiveFunction* CreateObjectiveFunction(const std::string& str);
94
95
96
97
98

  /*!
  * \brief Whether boosting is done on CUDA
  */
  virtual bool IsCUDAObjective() const { return false; }
99

100
  #ifdef USE_CUDA
101
  /*!
102
  * \brief Convert output for CUDA version
103
  */
104
  virtual const double* ConvertOutputCUDA(data_size_t /*num_data*/, const double* input, double* /*output*/) const {
105
    return input;
106
  }
107
108
109

  virtual bool NeedConvertOutputCUDA () const { return false; }

110
  #endif  // USE_CUDA
Guolin Ke's avatar
Guolin Ke committed
111
112
113
114
};

}  // namespace LightGBM

Guolin Ke's avatar
Guolin Ke committed
115
#endif   // LightGBM_OBJECTIVE_FUNCTION_H_