Use matrix for gradient. (#9508)
- Use the `linalg::Matrix` for storing gradients. - New API for the custom objective. - Custom objective for multi-class/multi-target is now required to return the correct shape. - Custom objective for Python can accept arrays with any strides. (row-major, column-major)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*!
|
||||
* Copyright 2015-2022 by Contributors
|
||||
/**
|
||||
* Copyright 2015-2023, XGBoost Contributors
|
||||
* \file custom_metric.cc
|
||||
* \brief This is an example to define plugin of xgboost.
|
||||
* This plugin defines the additional metric function.
|
||||
@@ -9,9 +9,7 @@
|
||||
#include <xgboost/objective.h>
|
||||
#include <xgboost/json.h>
|
||||
|
||||
namespace xgboost {
|
||||
namespace obj {
|
||||
|
||||
namespace xgboost::obj {
|
||||
// This is a helpful data structure to define parameters
|
||||
// You do not have to use it.
|
||||
// see http://dmlc-core.readthedocs.org/en/latest/parameter.html
|
||||
@@ -33,38 +31,38 @@ class MyLogistic : public ObjFunction {
|
||||
public:
|
||||
void Configure(const Args& args) override { param_.UpdateAllowUnknown(args); }
|
||||
|
||||
ObjInfo Task() const override { return ObjInfo::kRegression; }
|
||||
[[nodiscard]] ObjInfo Task() const override { return ObjInfo::kRegression; }
|
||||
|
||||
void GetGradient(const HostDeviceVector<bst_float>& preds, const MetaInfo& info, int32_t /*iter*/,
|
||||
HostDeviceVector<GradientPair>* out_gpair) override {
|
||||
out_gpair->Resize(preds.Size());
|
||||
const std::vector<bst_float>& preds_h = preds.HostVector();
|
||||
std::vector<GradientPair>& out_gpair_h = out_gpair->HostVector();
|
||||
void GetGradient(const HostDeviceVector<float>& preds, MetaInfo const& info,
|
||||
std::int32_t /*iter*/, linalg::Matrix<GradientPair>* out_gpair) override {
|
||||
out_gpair->Reshape(info.num_row_, 1);
|
||||
const std::vector<float>& preds_h = preds.HostVector();
|
||||
auto out_gpair_h = out_gpair->HostView();
|
||||
auto const labels_h = info.labels.HostView();
|
||||
for (size_t i = 0; i < preds_h.size(); ++i) {
|
||||
bst_float w = info.GetWeight(i);
|
||||
float w = info.GetWeight(i);
|
||||
// scale the negative examples!
|
||||
if (labels_h(i) == 0.0f) w *= param_.scale_neg_weight;
|
||||
// logistic transformation
|
||||
bst_float p = 1.0f / (1.0f + std::exp(-preds_h[i]));
|
||||
float p = 1.0f / (1.0f + std::exp(-preds_h[i]));
|
||||
// this is the gradient
|
||||
bst_float grad = (p - labels_h(i)) * w;
|
||||
float grad = (p - labels_h(i)) * w;
|
||||
// this is the second order gradient
|
||||
bst_float hess = p * (1.0f - p) * w;
|
||||
out_gpair_h.at(i) = GradientPair(grad, hess);
|
||||
float hess = p * (1.0f - p) * w;
|
||||
out_gpair_h(i) = GradientPair(grad, hess);
|
||||
}
|
||||
}
|
||||
const char* DefaultEvalMetric() const override {
|
||||
[[nodiscard]] const char* DefaultEvalMetric() const override {
|
||||
return "logloss";
|
||||
}
|
||||
void PredTransform(HostDeviceVector<bst_float> *io_preds) const override {
|
||||
void PredTransform(HostDeviceVector<float> *io_preds) const override {
|
||||
// transform margin value to probability.
|
||||
std::vector<bst_float> &preds = io_preds->HostVector();
|
||||
std::vector<float> &preds = io_preds->HostVector();
|
||||
for (auto& pred : preds) {
|
||||
pred = 1.0f / (1.0f + std::exp(-pred));
|
||||
}
|
||||
}
|
||||
bst_float ProbToMargin(bst_float base_score) const override {
|
||||
[[nodiscard]] float ProbToMargin(float base_score) const override {
|
||||
// transform probability to margin value
|
||||
return -std::log(1.0f / base_score - 1.0f);
|
||||
}
|
||||
@@ -89,5 +87,4 @@ XGBOOST_REGISTER_OBJECTIVE(MyLogistic, "mylogistic")
|
||||
.describe("User defined logistic regression plugin")
|
||||
.set_body([]() { return new MyLogistic(); });
|
||||
|
||||
} // namespace obj
|
||||
} // namespace xgboost
|
||||
} // namespace xgboost::obj
|
||||
|
||||
Reference in New Issue
Block a user