Debian Codegraph @

src/include/xgboost/metric.h

raw symbols SCIP
1 /**
2 * Copyright 2014-2023 by XGBoost Contributors
3 * \file metric.h
4 * \brief interface of evaluation metric function supported in xgboost.
5 * \author Tianqi Chen, Kailong Chen
6 */
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>
13 #include <xgboost/host_device_vector.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
25 /*!
26 * \brief interface of evaluation metric used to evaluate model performance.
27 * This has nothing to do with training, but merely act as evaluation purpose.
28 */
29 class Metric : public Configurable {
30 protected:
31 Context const* ctx_{nullptr};
32
33 public:
34 /*!
35 * \brief Configure the Metric with the specified parameters.
36 * \param args arguments to the objective function.
37 */
38 virtual void Configure(
39 const std::vector<std::pair<std::string, std::string> >&) {}
40 /*!
41 * \brief Load configuration from JSON object
42 * By default, metric has no internal configuration;
43 * override this function to maintain internal configuration
44 * \param in JSON object containing the configuration
45 */
46 void LoadConfig(Json const&) override {}
47 /*!
48 * \brief Save configuration to JSON object
49 * By default, metric has no internal configuration;
50 * override this function to maintain internal configuration
51 * \param out pointer to output JSON object
52 */
53 void SaveConfig(Json* p_out) const override {
54 auto& out = *p_out;
55 out["name"] = String(this->Name());
56 }
57
58 /**
59 * \brief Evaluate a metric with DMatrix as input.
60 *
61 * \param preds Prediction
62 * \param p_fmat DMatrix that contains related information like labels.
63 */
64 virtual double Evaluate(HostDeviceVector<bst_float> const& preds,
65 std::shared_ptr<DMatrix> p_fmat) = 0;
66
67 /*! \return name of metric */
68 virtual const char* Name() const = 0;
69 /*! \brief virtual destructor */
70 ~Metric() override = default;
71 /*!
72 * \brief create a metric according to name.
73 * \param name name of the metric.
74 * name can be in form metric[@]param and the name will be matched in the
75 * registry.
76 * \param ctx A global context
77 * \return the created metric.
78 */
79 static Metric* Create(const std::string& name, Context const* ctx);
80 };
81
82 /*!
83 * \brief Registry entry for Metric factory functions.
84 * The additional parameter const char* param gives the value after @, can be null.
85 * For example, metric map@3, then: param == "3".
86 */
87 struct MetricReg
88 : public dmlc::FunctionRegEntryBase<MetricReg,
89 std::function<Metric* (const char*)> > {
90 };
91
92 /*!
93 * \brief Macro to register metric.
94 *
95 * \code
96 * // example of registering a objective ndcg@k
97 * XGBOOST_REGISTER_METRIC(RMSE, "ndcg")
98 * .describe("Rooted mean square error.")
99 * .set_body([](const char* param) {
100 * int at_k = atoi(param);
101 * return new NDCG(at_k);
102 * });
103 * \endcode
104 */
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_