objective_function.h 2.81 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
#ifndef LIGHTGBM_OBJECTIVE_FUNCTION_H_
#define LIGHTGBM_OBJECTIVE_FUNCTION_H_

#include <LightGBM/meta.h>
#include <LightGBM/config.h>
#include <LightGBM/dataset.h>
7
#include <functional>
Guolin Ke's avatar
Guolin Ke committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

namespace LightGBM {
/*!
* \brief The interface of Objective Function.
*/
class ObjectiveFunction {
public:
  /*! \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
26
27
  * \brief calculating first order derivative of loss function
  * \param score prediction score in this round
Guolin Ke's avatar
Guolin Ke committed
28
29
30
  * \gradients Output gradients
  * \hessians Output hessians
  */
31
  virtual void GetGradients(const double* score,
32
    score_t* gradients, score_t* hessians) const = 0;
Guolin Ke's avatar
Guolin Ke committed
33

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

36
37
  virtual bool IsConstantHessian() const { return false; }

38
  virtual bool IsRenewTreeOutput() const { return false; }
39

40
41
42
43
44
  virtual double RenewTreeOutput(double ori_output, const double*,
                                 const data_size_t*,
                                 const data_size_t*,
                                 data_size_t) const { return ori_output; }

Guolin Ke's avatar
Guolin Ke committed
45
46
47
48
49
50
51
  virtual double RenewTreeOutput(double ori_output, double,
                                  const data_size_t*,
                                  const data_size_t*,
                                  data_size_t) const {
                                  return ori_output;
  }

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

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

56
57
  virtual bool SkipEmptyClass() const { return false; }

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

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

62
63
64
  /*! \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
65
66
  virtual void ConvertOutput(const double* input, double* output) const {
    output[0] = input[0];
67
68
69
70
  }

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

Guolin Ke's avatar
Guolin Ke committed
71
72
73
74
75
  ObjectiveFunction() = default;
  /*! \brief Disable copy */
  ObjectiveFunction& operator=(const ObjectiveFunction&) = delete;
  /*! \brief Disable copy */
  ObjectiveFunction(const ObjectiveFunction&) = delete;
Guolin Ke's avatar
Guolin Ke committed
76
77
78
79
80
81

  /*!
  * \brief Create object of objective function
  * \param type Specific type of objective function
  * \param config Config for objective function
  */
82
  LIGHTGBM_EXPORT static ObjectiveFunction* CreateObjectiveFunction(const std::string& type,
Guolin Ke's avatar
Guolin Ke committed
83
    const Config& config);
84
85
86
87
88

  /*!
  * \brief Load objective function from string object
  */
  LIGHTGBM_EXPORT static ObjectiveFunction* CreateObjectiveFunction(const std::string& str);
Guolin Ke's avatar
Guolin Ke committed
89
90
91
92
};

}  // namespace LightGBM

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