xgboost
metric.h
Go to the documentation of this file.
1 
7 #ifndef XGBOOST_METRIC_H_
8 #define XGBOOST_METRIC_H_
9 
10 #include <dmlc/registry.h>
11 #include <xgboost/base.h>
12 #include <xgboost/data.h>
14 #include <xgboost/model.h>
15 
16 #include <functional>
17 #include <memory> // shared_ptr
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 namespace xgboost {
23 struct Context;
24 
29 class Metric : public Configurable {
30  protected:
31  Context const* ctx_{nullptr};
32 
33  public:
38  virtual void Configure(
39  const std::vector<std::pair<std::string, std::string> >&) {}
46  void LoadConfig(Json const&) override {}
53  void SaveConfig(Json* p_out) const override {
54  auto& out = *p_out;
55  out["name"] = String(this->Name());
56  }
57 
64  virtual double Evaluate(HostDeviceVector<bst_float> const& preds,
65  std::shared_ptr<DMatrix> p_fmat) = 0;
66 
68  virtual const char* Name() const = 0;
70  ~Metric() override = default;
79  static Metric* Create(const std::string& name, Context const* ctx);
80 };
81 
87 struct MetricReg
88  : public dmlc::FunctionRegEntryBase<MetricReg,
89  std::function<Metric* (const char*)> > {
90 };
91 
105 #define XGBOOST_REGISTER_METRIC(UniqueId, Name) \
106  ::xgboost::MetricReg& __make_ ## MetricReg ## _ ## UniqueId ## __ = \
107  ::dmlc::Registry< ::xgboost::MetricReg>::Get()->__REGISTER__(Name)
108 } // namespace xgboost
109 #endif // XGBOOST_METRIC_H_
Defines configuration macros and basic types for xgboost.
Data structure representing JSON format.
Definition: json.h:357
interface of evaluation metric used to evaluate model performance. This has nothing to do with traini...
Definition: metric.h:29
void SaveConfig(Json *p_out) const override
Save configuration to JSON object By default, metric has no internal configuration; override this fun...
Definition: metric.h:53
virtual void Configure(const std::vector< std::pair< std::string, std::string > > &)
Configure the Metric with the specified parameters.
Definition: metric.h:38
void LoadConfig(Json const &) override
Load configuration from JSON object By default, metric has no internal configuration; override this f...
Definition: metric.h:46
virtual const char * Name() const =0
static Metric * Create(const std::string &name, Context const *ctx)
create a metric according to name.
Context const * ctx_
Definition: metric.h:31
~Metric() override=default
virtual destructor
virtual double Evaluate(HostDeviceVector< bst_float > const &preds, std::shared_ptr< DMatrix > p_fmat)=0
Evaluate a metric with DMatrix as input.
The input data structure of xgboost.
A device-and-host vector abstraction layer.
Defines the abstract interface for different components in XGBoost.
namespace of xgboost
Definition: base.h:90
JsonString String
Definition: json.h:595
Definition: model.h:31
Runtime context for XGBoost. Contains information like threads and device.
Definition: context.h:84
Registry entry for Metric factory functions. The additional parameter const char* param gives the val...
Definition: metric.h:89