Medial Code Documentation
Loading...
Searching...
No Matches
test_multiclass_metric.h
1// Copyright by Contributors
2#include <xgboost/metric.h>
3#include <string>
4
5#include "../helpers.h"
6
7namespace xgboost {
8namespace metric {
9
10inline void CheckDeterministicMetricMultiClass(StringView name, int32_t device) {
11 auto ctx = MakeCUDACtx(device);
12 std::unique_ptr<Metric> metric{Metric::Create(name.c_str(), &ctx)};
13
14 HostDeviceVector<float> predts;
15 auto p_fmat = EmptyDMatrix();
16 MetaInfo& info = p_fmat->Info();
17 auto &h_predts = predts.HostVector();
18
19 SimpleLCG lcg;
20
21 size_t n_samples = 2048, n_classes = 4;
22
23 info.labels.Reshape(n_samples);
24 auto &h_labels = info.labels.Data()->HostVector();
25 h_predts.resize(n_samples * n_classes);
26
27 {
28 SimpleRealUniformDistribution<float> dist{0.0f, static_cast<float>(n_classes)};
29 for (size_t i = 0; i < n_samples; ++i) {
30 h_labels[i] = dist(&lcg);
31 }
32 }
33
34 {
35 SimpleRealUniformDistribution<float> dist{0.0f, 1.0f};
36 for (size_t i = 0; i < n_samples * n_classes; ++i) {
37 h_predts[i] = dist(&lcg);
38 }
39 }
40
41 auto result = metric->Evaluate(predts, p_fmat);
42 for (size_t i = 0; i < 8; ++i) {
43 ASSERT_EQ(metric->Evaluate(predts, p_fmat), result);
44 }
45}
46
47inline void TestMultiClassError(int device, DataSplitMode data_split_mode) {
48 auto ctx = MakeCUDACtx(device);
49 xgboost::Metric * metric = xgboost::Metric::Create("merror", &ctx);
50 metric->Configure({});
51 ASSERT_STREQ(metric->Name(), "merror");
52 EXPECT_ANY_THROW(GetMetricEval(metric, {0}, {0, 0}, {}, {}, data_split_mode));
53 EXPECT_NEAR(GetMetricEval(
54 metric, {1, 0, 0, 0, 1, 0, 0, 0, 1}, {0, 1, 2}, {}, {}, data_split_mode), 0, 1e-10);
55 EXPECT_NEAR(GetMetricEval(metric,
56 {0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f},
57 {0, 1, 2}, {}, {}, data_split_mode),
58 0.666f, 0.001f);
59 delete metric;
60}
61
62inline void VerifyMultiClassError(DataSplitMode data_split_mode = DataSplitMode::kRow) {
63 TestMultiClassError(GPUIDX, data_split_mode);
64 CheckDeterministicMetricMultiClass(StringView{"merror"}, GPUIDX);
65}
66
67inline void TestMultiClassLogLoss(int device, DataSplitMode data_split_mode) {
68 auto ctx = MakeCUDACtx(device);
69 xgboost::Metric * metric = xgboost::Metric::Create("mlogloss", &ctx);
70 metric->Configure({});
71 ASSERT_STREQ(metric->Name(), "mlogloss");
72 EXPECT_ANY_THROW(GetMetricEval(metric, {0}, {0, 0}, {}, {}, data_split_mode));
73 EXPECT_NEAR(GetMetricEval(
74 metric, {1, 0, 0, 0, 1, 0, 0, 0, 1}, {0, 1, 2}, {}, {}, data_split_mode), 0, 1e-10);
75 EXPECT_NEAR(GetMetricEval(metric,
76 {0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f},
77 {0, 1, 2}, {}, {}, data_split_mode),
78 2.302f, 0.001f);
79
80 delete metric;
81}
82
83inline void VerifyMultiClassLogLoss(DataSplitMode data_split_mode = DataSplitMode::kRow) {
84 TestMultiClassLogLoss(GPUIDX, data_split_mode);
85 CheckDeterministicMetricMultiClass(StringView{"mlogloss"}, GPUIDX);
86}
87
88} // namespace metric
89} // namespace xgboost
interface of evaluation metric used to evaluate model performance. This has nothing to do with traini...
Definition metric.h:29
static Metric * Create(const std::string &name, Context const *ctx)
create a metric according to name.
Definition metric.cc:46
virtual void Configure(const std::vector< std::pair< std::string, std::string > > &)
Configure the Metric with the specified parameters.
Definition metric.h:38
virtual const char * Name() const =0
namespace of xgboost
Definition base.h:90
Context MakeCUDACtx(std::int32_t device)
Make a context that uses CUDA if device >= 0.
Definition helpers.h:410
Copyright 2014-2023 by XGBoost Contributors.