Add Model and Configurable interface. (#4945)
* Apply Configurable to objective functions. * Apply Model to Learner and Regtree, gbm. * Add Load/SaveConfig to objs. * Refactor obj tests to use smart pointer. * Dummy methods for Save/Load Model.
This commit is contained in:
parent
9fc681001a
commit
ae536756ae
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
#include <dmlc/parameter.h>
|
#include <dmlc/parameter.h>
|
||||||
#include <xgboost/logging.h>
|
#include <xgboost/logging.h>
|
||||||
#include <xgboost/enum_class_param.h>
|
#include <xgboost/parameter.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|||||||
@ -322,6 +322,9 @@ class Json {
|
|||||||
static void Dump(Json json, std::ostream* stream,
|
static void Dump(Json json, std::ostream* stream,
|
||||||
bool pretty = ConsoleLogger::ShouldLog(
|
bool pretty = ConsoleLogger::ShouldLog(
|
||||||
ConsoleLogger::LogVerbosity::kDebug));
|
ConsoleLogger::LogVerbosity::kDebug));
|
||||||
|
static void Dump(Json json, std::string* out,
|
||||||
|
bool pretty = ConsoleLogger::ShouldLog(
|
||||||
|
ConsoleLogger::LogVerbosity::kDebug));
|
||||||
|
|
||||||
Json() : ptr_{new JsonNull} {}
|
Json() : ptr_{new JsonNull} {}
|
||||||
|
|
||||||
@ -400,6 +403,13 @@ class Json {
|
|||||||
return *ptr_ == *(rhs.ptr_);
|
return *ptr_ == *(rhs.ptr_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
friend std::ostream& operator<<(std::ostream& os, Json const& j) {
|
||||||
|
std::string str;
|
||||||
|
Json::Dump(j, &str);
|
||||||
|
os << str;
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<Value> ptr_;
|
std::shared_ptr<Value> ptr_;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
#include <xgboost/objective.h>
|
#include <xgboost/objective.h>
|
||||||
#include <xgboost/feature_map.h>
|
#include <xgboost/feature_map.h>
|
||||||
#include <xgboost/generic_parameters.h>
|
#include <xgboost/generic_parameters.h>
|
||||||
|
#include <xgboost/model.h>
|
||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <map>
|
#include <map>
|
||||||
@ -41,7 +42,7 @@ namespace xgboost {
|
|||||||
*
|
*
|
||||||
* \endcode
|
* \endcode
|
||||||
*/
|
*/
|
||||||
class Learner : public rabit::Serializable {
|
class Learner : public Model, public rabit::Serializable {
|
||||||
public:
|
public:
|
||||||
/*! \brief virtual destructor */
|
/*! \brief virtual destructor */
|
||||||
~Learner() override = default;
|
~Learner() override = default;
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
/*!
|
/*!
|
||||||
* Copyright (c) 2015-2019 by Contributors
|
* Copyright (c) 2015-2019 by Contributors
|
||||||
* \file logging.h
|
* \file logging.h
|
||||||
* \brief defines console logging options for xgboost.
|
*
|
||||||
* Use to enforce unified print behavior.
|
* \brief defines console logging options for xgboost. Use to enforce unified print
|
||||||
* For debug loggers, use LOG(INFO) and LOG(ERROR).
|
* behavior.
|
||||||
*/
|
*/
|
||||||
#ifndef XGBOOST_LOGGING_H_
|
#ifndef XGBOOST_LOGGING_H_
|
||||||
#define XGBOOST_LOGGING_H_
|
#define XGBOOST_LOGGING_H_
|
||||||
|
|||||||
44
include/xgboost/model.h
Normal file
44
include/xgboost/model.h
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/*!
|
||||||
|
* Copyright (c) 2019 by Contributors
|
||||||
|
* \file model.h
|
||||||
|
* \brief Defines the abstract interface for different components in XGBoost.
|
||||||
|
*/
|
||||||
|
#ifndef XGBOOST_MODEL_H_
|
||||||
|
#define XGBOOST_MODEL_H_
|
||||||
|
|
||||||
|
namespace dmlc {
|
||||||
|
class Stream;
|
||||||
|
} // namespace dmlc
|
||||||
|
|
||||||
|
namespace xgboost {
|
||||||
|
|
||||||
|
class Json;
|
||||||
|
|
||||||
|
struct Model {
|
||||||
|
/*!
|
||||||
|
* \brief Save the model to stream.
|
||||||
|
* \param fo output write stream
|
||||||
|
*/
|
||||||
|
virtual void SaveModel(dmlc::Stream* fo) const = 0;
|
||||||
|
/*!
|
||||||
|
* \brief Load the model from stream.
|
||||||
|
* \param fi input read stream
|
||||||
|
*/
|
||||||
|
virtual void LoadModel(dmlc::Stream* fi) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Configurable {
|
||||||
|
/*!
|
||||||
|
* \brief Load configuration from JSON object
|
||||||
|
* \param in JSON object containing the configuration
|
||||||
|
*/
|
||||||
|
virtual void LoadConfig(Json const& in) = 0;
|
||||||
|
/*!
|
||||||
|
* \brief Save configuration to JSON object
|
||||||
|
* \param out pointer to output JSON object
|
||||||
|
*/
|
||||||
|
virtual void SaveConfig(Json* out) const = 0;
|
||||||
|
};
|
||||||
|
} // namespace xgboost
|
||||||
|
|
||||||
|
#endif // XGBOOST_MODEL_H_
|
||||||
@ -10,6 +10,7 @@
|
|||||||
#include <dmlc/registry.h>
|
#include <dmlc/registry.h>
|
||||||
#include <xgboost/base.h>
|
#include <xgboost/base.h>
|
||||||
#include <xgboost/data.h>
|
#include <xgboost/data.h>
|
||||||
|
#include <xgboost/model.h>
|
||||||
#include <xgboost/generic_parameters.h>
|
#include <xgboost/generic_parameters.h>
|
||||||
#include <xgboost/host_device_vector.h>
|
#include <xgboost/host_device_vector.h>
|
||||||
|
|
||||||
@ -21,7 +22,7 @@
|
|||||||
namespace xgboost {
|
namespace xgboost {
|
||||||
|
|
||||||
/*! \brief interface of objective function */
|
/*! \brief interface of objective function */
|
||||||
class ObjFunction {
|
class ObjFunction : public Configurable {
|
||||||
protected:
|
protected:
|
||||||
GenericParameter const* tparam_;
|
GenericParameter const* tparam_;
|
||||||
|
|
||||||
|
|||||||
@ -5,10 +5,11 @@
|
|||||||
* \author Hyunsu Philip Cho
|
* \author Hyunsu Philip Cho
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef XGBOOST_ENUM_CLASS_PARAM_H_
|
#ifndef XGBOOST_PARAMETER_H_
|
||||||
#define XGBOOST_ENUM_CLASS_PARAM_H_
|
#define XGBOOST_PARAMETER_H_
|
||||||
|
|
||||||
#include <dmlc/parameter.h>
|
#include <dmlc/parameter.h>
|
||||||
|
#include <xgboost/base.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
@ -78,4 +79,27 @@ class FieldEntry<EnumClass> : public FieldEntry<int> { \
|
|||||||
} /* namespace parameter */ \
|
} /* namespace parameter */ \
|
||||||
} /* namespace dmlc */
|
} /* namespace dmlc */
|
||||||
|
|
||||||
#endif // XGBOOST_ENUM_CLASS_PARAM_H_
|
namespace xgboost {
|
||||||
|
template <typename Type>
|
||||||
|
struct XGBoostParameter : public dmlc::Parameter<Type> {
|
||||||
|
protected:
|
||||||
|
bool initialised_ {false};
|
||||||
|
|
||||||
|
public:
|
||||||
|
template <typename Container>
|
||||||
|
Args UpdateAllowUnknown(Container const& kwargs, bool* out_changed = nullptr) {
|
||||||
|
if (initialised_) {
|
||||||
|
return dmlc::Parameter<Type>::UpdateAllowUnknown(kwargs, out_changed);
|
||||||
|
} else {
|
||||||
|
auto unknown = dmlc::Parameter<Type>::InitAllowUnknown(kwargs);
|
||||||
|
if (out_changed) {
|
||||||
|
*out_changed = true;
|
||||||
|
}
|
||||||
|
initialised_ = true;
|
||||||
|
return unknown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace xgboost
|
||||||
|
|
||||||
|
#endif // XGBOOST_PARAMETER_H_
|
||||||
@ -14,6 +14,7 @@
|
|||||||
#include <xgboost/data.h>
|
#include <xgboost/data.h>
|
||||||
#include <xgboost/logging.h>
|
#include <xgboost/logging.h>
|
||||||
#include <xgboost/feature_map.h>
|
#include <xgboost/feature_map.h>
|
||||||
|
#include <xgboost/model.h>
|
||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -93,7 +94,7 @@ struct RTreeNodeStat {
|
|||||||
* \brief define regression tree to be the most common tree model.
|
* \brief define regression tree to be the most common tree model.
|
||||||
* This is the data structure used in xgboost's major tree models.
|
* This is the data structure used in xgboost's major tree models.
|
||||||
*/
|
*/
|
||||||
class RegTree {
|
class RegTree : public Model {
|
||||||
public:
|
public:
|
||||||
/*! \brief auxiliary statistics of node to help tree building */
|
/*! \brief auxiliary statistics of node to help tree building */
|
||||||
using SplitCondT = bst_float;
|
using SplitCondT = bst_float;
|
||||||
@ -289,38 +290,17 @@ class RegTree {
|
|||||||
const RTreeNodeStat& Stat(int nid) const {
|
const RTreeNodeStat& Stat(int nid) const {
|
||||||
return stats_[nid];
|
return stats_[nid];
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief load model from stream
|
* \brief load model from stream
|
||||||
* \param fi input stream
|
* \param fi input stream
|
||||||
*/
|
*/
|
||||||
void Load(dmlc::Stream* fi) {
|
void LoadModel(dmlc::Stream* fi) override;
|
||||||
CHECK_EQ(fi->Read(¶m, sizeof(TreeParam)), sizeof(TreeParam));
|
|
||||||
nodes_.resize(param.num_nodes);
|
|
||||||
stats_.resize(param.num_nodes);
|
|
||||||
CHECK_NE(param.num_nodes, 0);
|
|
||||||
CHECK_EQ(fi->Read(dmlc::BeginPtr(nodes_), sizeof(Node) * nodes_.size()),
|
|
||||||
sizeof(Node) * nodes_.size());
|
|
||||||
CHECK_EQ(fi->Read(dmlc::BeginPtr(stats_), sizeof(RTreeNodeStat) * stats_.size()),
|
|
||||||
sizeof(RTreeNodeStat) * stats_.size());
|
|
||||||
// chg deleted nodes
|
|
||||||
deleted_nodes_.resize(0);
|
|
||||||
for (int i = param.num_roots; i < param.num_nodes; ++i) {
|
|
||||||
if (nodes_[i].IsDeleted()) deleted_nodes_.push_back(i);
|
|
||||||
}
|
|
||||||
CHECK_EQ(static_cast<int>(deleted_nodes_.size()), param.num_deleted);
|
|
||||||
}
|
|
||||||
/*!
|
/*!
|
||||||
* \brief save model to stream
|
* \brief save model to stream
|
||||||
* \param fo output stream
|
* \param fo output stream
|
||||||
*/
|
*/
|
||||||
void Save(dmlc::Stream* fo) const {
|
void SaveModel(dmlc::Stream* fo) const override;
|
||||||
CHECK_EQ(param.num_nodes, static_cast<int>(nodes_.size()));
|
|
||||||
CHECK_EQ(param.num_nodes, static_cast<int>(stats_.size()));
|
|
||||||
fo->Write(¶m, sizeof(TreeParam));
|
|
||||||
CHECK_NE(param.num_nodes, 0);
|
|
||||||
fo->Write(dmlc::BeginPtr(nodes_), sizeof(Node) * nodes_.size());
|
|
||||||
fo->Write(dmlc::BeginPtr(stats_), sizeof(RTreeNodeStat) * nodes_.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(const RegTree& b) const {
|
bool operator==(const RegTree& b) const {
|
||||||
return nodes_ == b.nodes_ && stats_ == b.stats_ &&
|
return nodes_ == b.nodes_ && stats_ == b.stats_ &&
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*!
|
/*!
|
||||||
* Copyright 2015 by Contributors
|
* Copyright 2015-2019 by Contributors
|
||||||
* \file custom_metric.cc
|
* \file custom_metric.cc
|
||||||
* \brief This is an example to define plugin of xgboost.
|
* \brief This is an example to define plugin of xgboost.
|
||||||
* This plugin defines the additional metric function.
|
* This plugin defines the additional metric function.
|
||||||
@ -7,6 +7,7 @@
|
|||||||
#include <xgboost/base.h>
|
#include <xgboost/base.h>
|
||||||
#include <dmlc/parameter.h>
|
#include <dmlc/parameter.h>
|
||||||
#include <xgboost/objective.h>
|
#include <xgboost/objective.h>
|
||||||
|
#include <xgboost/json.h>
|
||||||
|
|
||||||
namespace xgboost {
|
namespace xgboost {
|
||||||
namespace obj {
|
namespace obj {
|
||||||
@ -69,6 +70,16 @@ class MyLogistic : public ObjFunction {
|
|||||||
return -std::log(1.0f / base_score - 1.0f);
|
return -std::log(1.0f / base_score - 1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SaveConfig(Json* p_out) const override {
|
||||||
|
auto& out = *p_out;
|
||||||
|
out["name"] = String("my_logistic");
|
||||||
|
out["my_logistic_param"] = toJson(param_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LoadConfig(Json const& in) override {
|
||||||
|
fromJson(in["my_logistic_param"], ¶m_);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MyLogisticParam param_;
|
MyLogisticParam param_;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -718,5 +718,13 @@ void Json::Dump(Json json, std::ostream *stream, bool pretty) {
|
|||||||
writer.Save(json);
|
writer.Save(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Json::Dump(Json json, std::string* str, bool pretty) {
|
||||||
|
GlobalCLocale guard;
|
||||||
|
std::stringstream ss;
|
||||||
|
JsonWriter writer(&ss, pretty);
|
||||||
|
writer.Save(json);
|
||||||
|
*str = ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
Json& Json::operator=(Json const &other) = default;
|
Json& Json::operator=(Json const &other) = default;
|
||||||
} // namespace xgboost
|
} // namespace xgboost
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
#include <dmlc/parameter.h>
|
#include <dmlc/parameter.h>
|
||||||
#include <xgboost/base.h>
|
#include <xgboost/base.h>
|
||||||
#include <xgboost/feature_map.h>
|
#include <xgboost/feature_map.h>
|
||||||
|
#include <xgboost/model.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@ -34,7 +35,7 @@ struct GBLinearModelParam : public dmlc::Parameter<GBLinearModelParam> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// model for linear booster
|
// model for linear booster
|
||||||
class GBLinearModel {
|
class GBLinearModel : public Model {
|
||||||
public:
|
public:
|
||||||
// parameter
|
// parameter
|
||||||
GBLinearModelParam param;
|
GBLinearModelParam param;
|
||||||
@ -57,6 +58,17 @@ class GBLinearModel {
|
|||||||
CHECK_EQ(fi->Read(¶m, sizeof(param)), sizeof(param));
|
CHECK_EQ(fi->Read(¶m, sizeof(param)), sizeof(param));
|
||||||
fi->Read(&weight);
|
fi->Read(&weight);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LoadModel(dmlc::Stream* fi) override {
|
||||||
|
// They are the same right now until we can split up the saved parameter from model.
|
||||||
|
this->Load(fi);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SaveModel(dmlc::Stream* fo) const override {
|
||||||
|
// They are the same right now until we can split up the saved parameter from model.
|
||||||
|
this->Save(fo);
|
||||||
|
}
|
||||||
|
|
||||||
// model bias
|
// model bias
|
||||||
inline bst_float* bias() {
|
inline bst_float* bias() {
|
||||||
return &weight[param.num_feature * param.num_output_group];
|
return &weight[param.num_feature * param.num_output_group];
|
||||||
|
|||||||
@ -14,7 +14,7 @@
|
|||||||
#include <xgboost/gbm.h>
|
#include <xgboost/gbm.h>
|
||||||
#include <xgboost/predictor.h>
|
#include <xgboost/predictor.h>
|
||||||
#include <xgboost/tree_updater.h>
|
#include <xgboost/tree_updater.h>
|
||||||
#include <xgboost/enum_class_param.h>
|
#include <xgboost/parameter.h>
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <dmlc/parameter.h>
|
#include <dmlc/parameter.h>
|
||||||
#include <dmlc/io.h>
|
#include <dmlc/io.h>
|
||||||
|
#include <xgboost/model.h>
|
||||||
#include <xgboost/tree_model.h>
|
#include <xgboost/tree_model.h>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@ -61,7 +62,7 @@ struct GBTreeModelParam : public dmlc::Parameter<GBTreeModelParam> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct GBTreeModel {
|
struct GBTreeModel : public Model {
|
||||||
explicit GBTreeModel(bst_float base_margin) : base_margin(base_margin) {}
|
explicit GBTreeModel(bst_float base_margin) : base_margin(base_margin) {}
|
||||||
void Configure(const Args& cfg) {
|
void Configure(const Args& cfg) {
|
||||||
// initialize model parameters if not yet been initialized.
|
// initialize model parameters if not yet been initialized.
|
||||||
@ -81,6 +82,15 @@ struct GBTreeModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LoadModel(dmlc::Stream* fi) override {
|
||||||
|
// They are the same right now until we can split up the saved parameter from model.
|
||||||
|
this->Load(fi);
|
||||||
|
}
|
||||||
|
void SaveModel(dmlc::Stream* fo) const override {
|
||||||
|
// They are the same right now until we can split up the saved parameter from model.
|
||||||
|
this->Save(fo);
|
||||||
|
}
|
||||||
|
|
||||||
void Load(dmlc::Stream* fi) {
|
void Load(dmlc::Stream* fi) {
|
||||||
CHECK_EQ(fi->Read(¶m, sizeof(param)), sizeof(param))
|
CHECK_EQ(fi->Read(¶m, sizeof(param)), sizeof(param))
|
||||||
<< "GBTree: invalid model file";
|
<< "GBTree: invalid model file";
|
||||||
@ -88,7 +98,7 @@ struct GBTreeModel {
|
|||||||
trees_to_update.clear();
|
trees_to_update.clear();
|
||||||
for (int i = 0; i < param.num_trees; ++i) {
|
for (int i = 0; i < param.num_trees; ++i) {
|
||||||
std::unique_ptr<RegTree> ptr(new RegTree());
|
std::unique_ptr<RegTree> ptr(new RegTree());
|
||||||
ptr->Load(fi);
|
ptr->LoadModel(fi);
|
||||||
trees.push_back(std::move(ptr));
|
trees.push_back(std::move(ptr));
|
||||||
}
|
}
|
||||||
tree_info.resize(param.num_trees);
|
tree_info.resize(param.num_trees);
|
||||||
@ -103,7 +113,7 @@ struct GBTreeModel {
|
|||||||
CHECK_EQ(param.num_trees, static_cast<int>(trees.size()));
|
CHECK_EQ(param.num_trees, static_cast<int>(trees.size()));
|
||||||
fo->Write(¶m, sizeof(param));
|
fo->Write(¶m, sizeof(param));
|
||||||
for (const auto & tree : trees) {
|
for (const auto & tree : trees) {
|
||||||
tree->Save(fo);
|
tree->SaveModel(fo);
|
||||||
}
|
}
|
||||||
if (tree_info.size() != 0) {
|
if (tree_info.size() != 0) {
|
||||||
fo->Write(dmlc::BeginPtr(tree_info), sizeof(int) * tree_info.size());
|
fo->Write(dmlc::BeginPtr(tree_info), sizeof(int) * tree_info.size());
|
||||||
|
|||||||
@ -9,6 +9,7 @@
|
|||||||
#include <dmlc/any.h>
|
#include <dmlc/any.h>
|
||||||
#include <xgboost/feature_map.h>
|
#include <xgboost/feature_map.h>
|
||||||
#include <xgboost/learner.h>
|
#include <xgboost/learner.h>
|
||||||
|
#include <xgboost/base.h>
|
||||||
#include <xgboost/logging.h>
|
#include <xgboost/logging.h>
|
||||||
#include <xgboost/generic_parameters.h>
|
#include <xgboost/generic_parameters.h>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@ -196,6 +197,16 @@ class LearnerImpl : public Learner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LoadModel(dmlc::Stream* fi) override {
|
||||||
|
// They are the same right now until we can split up the saved parameter from model.
|
||||||
|
this->Load(fi);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SaveModel(dmlc::Stream* fo) const override {
|
||||||
|
// They are the same right now until we can split up the saved parameter from model.
|
||||||
|
this->Save(fo);
|
||||||
|
}
|
||||||
|
|
||||||
void Load(dmlc::Stream* fi) override {
|
void Load(dmlc::Stream* fi) override {
|
||||||
generic_param_.InitAllowUnknown(Args{});
|
generic_param_.InitAllowUnknown(Args{});
|
||||||
tparam_.Init(std::vector<std::pair<std::string, std::string>>{});
|
tparam_.Init(std::vector<std::pair<std::string, std::string>>{});
|
||||||
|
|||||||
@ -1,10 +1,11 @@
|
|||||||
/*!
|
/*!
|
||||||
* Copyright 2018 by Contributors
|
* Copyright 2018-2019 by Contributors
|
||||||
* \file hinge.cc
|
* \file hinge.cc
|
||||||
* \brief Provides an implementation of the hinge loss function
|
* \brief Provides an implementation of the hinge loss function
|
||||||
* \author Henry Gouk
|
* \author Henry Gouk
|
||||||
*/
|
*/
|
||||||
#include "xgboost/objective.h"
|
#include "xgboost/objective.h"
|
||||||
|
#include "xgboost/json.h"
|
||||||
#include "xgboost/span.h"
|
#include "xgboost/span.h"
|
||||||
#include "xgboost/host_device_vector.h"
|
#include "xgboost/host_device_vector.h"
|
||||||
|
|
||||||
@ -76,6 +77,12 @@ class HingeObj : public ObjFunction {
|
|||||||
const char* DefaultEvalMetric() const override {
|
const char* DefaultEvalMetric() const override {
|
||||||
return "error";
|
return "error";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SaveConfig(Json* p_out) const override {
|
||||||
|
auto& out = *p_out;
|
||||||
|
out["name"] = String("binary:hinge");
|
||||||
|
}
|
||||||
|
void LoadConfig(Json const& in) override {}
|
||||||
};
|
};
|
||||||
|
|
||||||
// register the objective functions
|
// register the objective functions
|
||||||
|
|||||||
@ -14,6 +14,7 @@
|
|||||||
#include <limits>
|
#include <limits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include "xgboost/json.h"
|
||||||
#include "../common/common.h"
|
#include "../common/common.h"
|
||||||
#include "../common/math.h"
|
#include "../common/math.h"
|
||||||
#include "../common/transform.h"
|
#include "../common/transform.h"
|
||||||
@ -25,7 +26,7 @@ namespace obj {
|
|||||||
DMLC_REGISTRY_FILE_TAG(multiclass_obj_gpu);
|
DMLC_REGISTRY_FILE_TAG(multiclass_obj_gpu);
|
||||||
#endif // defined(XGBOOST_USE_CUDA)
|
#endif // defined(XGBOOST_USE_CUDA)
|
||||||
|
|
||||||
struct SoftmaxMultiClassParam : public dmlc::Parameter<SoftmaxMultiClassParam> {
|
struct SoftmaxMultiClassParam : public XGBoostParameter<SoftmaxMultiClassParam> {
|
||||||
int num_class;
|
int num_class;
|
||||||
// declare parameters
|
// declare parameters
|
||||||
DMLC_DECLARE_PARAMETER(SoftmaxMultiClassParam) {
|
DMLC_DECLARE_PARAMETER(SoftmaxMultiClassParam) {
|
||||||
@ -37,10 +38,10 @@ struct SoftmaxMultiClassParam : public dmlc::Parameter<SoftmaxMultiClassParam> {
|
|||||||
class SoftmaxMultiClassObj : public ObjFunction {
|
class SoftmaxMultiClassObj : public ObjFunction {
|
||||||
public:
|
public:
|
||||||
explicit SoftmaxMultiClassObj(bool output_prob)
|
explicit SoftmaxMultiClassObj(bool output_prob)
|
||||||
: output_prob_(output_prob) {
|
: output_prob_(output_prob) {}
|
||||||
}
|
|
||||||
void Configure(const std::vector<std::pair<std::string, std::string> >& args) override {
|
void Configure(Args const& args) override {
|
||||||
param_.InitAllowUnknown(args);
|
param_.UpdateAllowUnknown(args);
|
||||||
}
|
}
|
||||||
void GetGradient(const HostDeviceVector<bst_float>& preds,
|
void GetGradient(const HostDeviceVector<bst_float>& preds,
|
||||||
const MetaInfo& info,
|
const MetaInfo& info,
|
||||||
@ -155,6 +156,20 @@ class SoftmaxMultiClassObj : public ObjFunction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SaveConfig(Json* p_out) const override {
|
||||||
|
auto& out = *p_out;
|
||||||
|
if (this->output_prob_) {
|
||||||
|
out["name"] = String("multi:softprob");
|
||||||
|
} else {
|
||||||
|
out["name"] = String("multi:softmax");
|
||||||
|
}
|
||||||
|
out["softmax_multiclass_param"] = toJson(param_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LoadConfig(Json const& in) override {
|
||||||
|
fromJson(in["softmax_multiclass_param"], ¶m_);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// output probability
|
// output probability
|
||||||
bool output_prob_;
|
bool output_prob_;
|
||||||
|
|||||||
@ -6,6 +6,8 @@
|
|||||||
#include <xgboost/objective.h>
|
#include <xgboost/objective.h>
|
||||||
#include <dmlc/registry.h>
|
#include <dmlc/registry.h>
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
#include "xgboost/host_device_vector.h"
|
#include "xgboost/host_device_vector.h"
|
||||||
|
|
||||||
namespace dmlc {
|
namespace dmlc {
|
||||||
@ -17,10 +19,12 @@ namespace xgboost {
|
|||||||
ObjFunction* ObjFunction::Create(const std::string& name, GenericParameter const* tparam) {
|
ObjFunction* ObjFunction::Create(const std::string& name, GenericParameter const* tparam) {
|
||||||
auto *e = ::dmlc::Registry< ::xgboost::ObjFunctionReg>::Get()->Find(name);
|
auto *e = ::dmlc::Registry< ::xgboost::ObjFunctionReg>::Get()->Find(name);
|
||||||
if (e == nullptr) {
|
if (e == nullptr) {
|
||||||
|
std::stringstream ss;
|
||||||
for (const auto& entry : ::dmlc::Registry< ::xgboost::ObjFunctionReg>::List()) {
|
for (const auto& entry : ::dmlc::Registry< ::xgboost::ObjFunctionReg>::List()) {
|
||||||
LOG(INFO) << "Objective candidate: " << entry->name;
|
ss << "Objective candidate: " << entry->name << "\n";
|
||||||
}
|
}
|
||||||
LOG(FATAL) << "Unknown objective function " << name;
|
LOG(FATAL) << "Unknown objective function: `" << name << "`\n"
|
||||||
|
<< ss.str();
|
||||||
}
|
}
|
||||||
auto pobj = (e->body)();
|
auto pobj = (e->body)();
|
||||||
pobj->tparam_ = tparam;
|
pobj->tparam_ = tparam;
|
||||||
|
|||||||
@ -10,6 +10,10 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include "xgboost/json.h"
|
||||||
|
#include "xgboost/parameter.h"
|
||||||
|
|
||||||
#include "../common/math.h"
|
#include "../common/math.h"
|
||||||
#include "../common/random.h"
|
#include "../common/random.h"
|
||||||
|
|
||||||
@ -18,7 +22,7 @@ namespace obj {
|
|||||||
|
|
||||||
DMLC_REGISTRY_FILE_TAG(rank_obj);
|
DMLC_REGISTRY_FILE_TAG(rank_obj);
|
||||||
|
|
||||||
struct LambdaRankParam : public dmlc::Parameter<LambdaRankParam> {
|
struct LambdaRankParam : public XGBoostParameter<LambdaRankParam> {
|
||||||
int num_pairsample;
|
int num_pairsample;
|
||||||
float fix_list_weight;
|
float fix_list_weight;
|
||||||
// declare parameters
|
// declare parameters
|
||||||
@ -35,7 +39,7 @@ struct LambdaRankParam : public dmlc::Parameter<LambdaRankParam> {
|
|||||||
class LambdaRankObj : public ObjFunction {
|
class LambdaRankObj : public ObjFunction {
|
||||||
public:
|
public:
|
||||||
void Configure(const std::vector<std::pair<std::string, std::string> >& args) override {
|
void Configure(const std::vector<std::pair<std::string, std::string> >& args) override {
|
||||||
param_.InitAllowUnknown(args);
|
param_.UpdateAllowUnknown(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetGradient(const HostDeviceVector<bst_float>& preds,
|
void GetGradient(const HostDeviceVector<bst_float>& preds,
|
||||||
@ -170,7 +174,16 @@ class LambdaRankObj : public ObjFunction {
|
|||||||
virtual void GetLambdaWeight(const std::vector<ListEntry> &sorted_list,
|
virtual void GetLambdaWeight(const std::vector<ListEntry> &sorted_list,
|
||||||
std::vector<LambdaPair> *io_pairs) = 0;
|
std::vector<LambdaPair> *io_pairs) = 0;
|
||||||
|
|
||||||
private:
|
void SaveConfig(Json* p_out) const override {
|
||||||
|
auto& out = *p_out;
|
||||||
|
out["name"] = String("LambdaRankObj");
|
||||||
|
out["lambda_rank_param"] = Object();
|
||||||
|
for (auto const& kv : param_.__DICT__()) {
|
||||||
|
out["lambda_rank_param"][kv.first] = kv.second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
LambdaRankParam param_;
|
LambdaRankParam param_;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -178,6 +191,15 @@ class PairwiseRankObj: public LambdaRankObj{
|
|||||||
protected:
|
protected:
|
||||||
void GetLambdaWeight(const std::vector<ListEntry> &sorted_list,
|
void GetLambdaWeight(const std::vector<ListEntry> &sorted_list,
|
||||||
std::vector<LambdaPair> *io_pairs) override {}
|
std::vector<LambdaPair> *io_pairs) override {}
|
||||||
|
|
||||||
|
void SaveConfig(Json* p_out) const override {
|
||||||
|
auto& out = *p_out;
|
||||||
|
out["name"] = String("rank:pairwise");
|
||||||
|
out["lambda_rank_param"] = toJson(LambdaRankObj::param_);
|
||||||
|
}
|
||||||
|
void LoadConfig(Json const& in) override {
|
||||||
|
fromJson(in["lambda_rank_param"], &(LambdaRankObj::param_));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// beta version: NDCG lambda rank
|
// beta version: NDCG lambda rank
|
||||||
@ -228,6 +250,14 @@ class LambdaRankObjNDCG : public LambdaRankObj {
|
|||||||
}
|
}
|
||||||
return static_cast<bst_float>(sumdcg);
|
return static_cast<bst_float>(sumdcg);
|
||||||
}
|
}
|
||||||
|
void SaveConfig(Json* p_out) const override {
|
||||||
|
auto& out = *p_out;
|
||||||
|
out["name"] = String("rank:ndcg");
|
||||||
|
out["lambda_rank_param"] = toJson(LambdaRankObj::param_);
|
||||||
|
}
|
||||||
|
void LoadConfig(Json const& in) override {
|
||||||
|
fromJson(in["lambda_rank_param"], &(LambdaRankObj::param_));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class LambdaRankObjMAP : public LambdaRankObj {
|
class LambdaRankObjMAP : public LambdaRankObj {
|
||||||
@ -315,6 +345,15 @@ class LambdaRankObjMAP : public LambdaRankObj {
|
|||||||
pair.neg_index, &map_stats);
|
pair.neg_index, &map_stats);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SaveConfig(Json* p_out) const override {
|
||||||
|
auto& out = *p_out;
|
||||||
|
out["name"] = String("rank:map");
|
||||||
|
out["lambda_rank_param"] = toJson(LambdaRankObj::param_);
|
||||||
|
}
|
||||||
|
void LoadConfig(Json const& in) override {
|
||||||
|
fromJson(in["lambda_rank_param"], &(LambdaRankObj::param_));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// register the objective functions
|
// register the objective functions
|
||||||
|
|||||||
@ -34,6 +34,8 @@ struct LinearSquareLoss {
|
|||||||
static bst_float ProbToMargin(bst_float base_score) { return base_score; }
|
static bst_float ProbToMargin(bst_float base_score) { return base_score; }
|
||||||
static const char* LabelErrorMsg() { return ""; }
|
static const char* LabelErrorMsg() { return ""; }
|
||||||
static const char* DefaultEvalMetric() { return "rmse"; }
|
static const char* DefaultEvalMetric() { return "rmse"; }
|
||||||
|
|
||||||
|
static const char* Name() { return "reg:squarederror"; }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SquaredLogError {
|
struct SquaredLogError {
|
||||||
@ -57,6 +59,8 @@ struct SquaredLogError {
|
|||||||
return "label must be greater than -1 for rmsle so that log(label + 1) can be valid.";
|
return "label must be greater than -1 for rmsle so that log(label + 1) can be valid.";
|
||||||
}
|
}
|
||||||
static const char* DefaultEvalMetric() { return "rmsle"; }
|
static const char* DefaultEvalMetric() { return "rmsle"; }
|
||||||
|
|
||||||
|
static const char* Name() { return "reg:squaredlogerror"; }
|
||||||
};
|
};
|
||||||
|
|
||||||
// logistic loss for probability regression task
|
// logistic loss for probability regression task
|
||||||
@ -83,18 +87,21 @@ struct LogisticRegression {
|
|||||||
}
|
}
|
||||||
static bst_float ProbToMargin(bst_float base_score) {
|
static bst_float ProbToMargin(bst_float base_score) {
|
||||||
CHECK(base_score > 0.0f && base_score < 1.0f)
|
CHECK(base_score > 0.0f && base_score < 1.0f)
|
||||||
<< "base_score must be in (0,1) for logistic loss";
|
<< "base_score must be in (0,1) for logistic loss, got: " << base_score;
|
||||||
return -logf(1.0f / base_score - 1.0f);
|
return -logf(1.0f / base_score - 1.0f);
|
||||||
}
|
}
|
||||||
static const char* LabelErrorMsg() {
|
static const char* LabelErrorMsg() {
|
||||||
return "label must be in [0,1] for logistic regression";
|
return "label must be in [0,1] for logistic regression";
|
||||||
}
|
}
|
||||||
static const char* DefaultEvalMetric() { return "rmse"; }
|
static const char* DefaultEvalMetric() { return "rmse"; }
|
||||||
|
|
||||||
|
static const char* Name() { return "reg:logistic"; }
|
||||||
};
|
};
|
||||||
|
|
||||||
// logistic loss for binary classification task
|
// logistic loss for binary classification task
|
||||||
struct LogisticClassification : public LogisticRegression {
|
struct LogisticClassification : public LogisticRegression {
|
||||||
static const char* DefaultEvalMetric() { return "error"; }
|
static const char* DefaultEvalMetric() { return "error"; }
|
||||||
|
static const char* Name() { return "binary:logistic"; }
|
||||||
};
|
};
|
||||||
|
|
||||||
// logistic loss, but predict un-transformed margin
|
// logistic loss, but predict un-transformed margin
|
||||||
@ -125,6 +132,8 @@ struct LogisticRaw : public LogisticRegression {
|
|||||||
return std::max(predt * (T(1.0f) - predt), eps);
|
return std::max(predt * (T(1.0f) - predt), eps);
|
||||||
}
|
}
|
||||||
static const char* DefaultEvalMetric() { return "auc"; }
|
static const char* DefaultEvalMetric() { return "auc"; }
|
||||||
|
|
||||||
|
static const char* Name() { return "binary:logitraw"; }
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace obj
|
} // namespace obj
|
||||||
|
|||||||
@ -12,8 +12,10 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "xgboost/span.h"
|
|
||||||
#include "xgboost/host_device_vector.h"
|
#include "xgboost/host_device_vector.h"
|
||||||
|
#include "xgboost/json.h"
|
||||||
|
#include "xgboost/parameter.h"
|
||||||
|
#include "xgboost/span.h"
|
||||||
|
|
||||||
#include "../common/transform.h"
|
#include "../common/transform.h"
|
||||||
#include "../common/common.h"
|
#include "../common/common.h"
|
||||||
@ -27,7 +29,7 @@ namespace obj {
|
|||||||
DMLC_REGISTRY_FILE_TAG(regression_obj_gpu);
|
DMLC_REGISTRY_FILE_TAG(regression_obj_gpu);
|
||||||
#endif // defined(XGBOOST_USE_CUDA)
|
#endif // defined(XGBOOST_USE_CUDA)
|
||||||
|
|
||||||
struct RegLossParam : public dmlc::Parameter<RegLossParam> {
|
struct RegLossParam : public XGBoostParameter<RegLossParam> {
|
||||||
float scale_pos_weight;
|
float scale_pos_weight;
|
||||||
// declare parameters
|
// declare parameters
|
||||||
DMLC_DECLARE_PARAMETER(RegLossParam) {
|
DMLC_DECLARE_PARAMETER(RegLossParam) {
|
||||||
@ -45,7 +47,7 @@ class RegLossObj : public ObjFunction {
|
|||||||
RegLossObj() = default;
|
RegLossObj() = default;
|
||||||
|
|
||||||
void Configure(const std::vector<std::pair<std::string, std::string> >& args) override {
|
void Configure(const std::vector<std::pair<std::string, std::string> >& args) override {
|
||||||
param_.InitAllowUnknown(args);
|
param_.UpdateAllowUnknown(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetGradient(const HostDeviceVector<bst_float>& preds,
|
void GetGradient(const HostDeviceVector<bst_float>& preds,
|
||||||
@ -114,6 +116,16 @@ class RegLossObj : public ObjFunction {
|
|||||||
return Loss::ProbToMargin(base_score);
|
return Loss::ProbToMargin(base_score);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SaveConfig(Json* p_out) const override {
|
||||||
|
auto& out = *p_out;
|
||||||
|
out["name"] = String(Loss::Name());
|
||||||
|
out["reg_loss_param"] = toJson(param_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LoadConfig(Json const& in) override {
|
||||||
|
fromJson(in["reg_loss_param"], ¶m_);
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
RegLossParam param_;
|
RegLossParam param_;
|
||||||
};
|
};
|
||||||
@ -121,23 +133,23 @@ class RegLossObj : public ObjFunction {
|
|||||||
// register the objective functions
|
// register the objective functions
|
||||||
DMLC_REGISTER_PARAMETER(RegLossParam);
|
DMLC_REGISTER_PARAMETER(RegLossParam);
|
||||||
|
|
||||||
XGBOOST_REGISTER_OBJECTIVE(SquaredLossRegression, "reg:squarederror")
|
XGBOOST_REGISTER_OBJECTIVE(SquaredLossRegression, LinearSquareLoss::Name())
|
||||||
.describe("Regression with squared error.")
|
.describe("Regression with squared error.")
|
||||||
.set_body([]() { return new RegLossObj<LinearSquareLoss>(); });
|
.set_body([]() { return new RegLossObj<LinearSquareLoss>(); });
|
||||||
|
|
||||||
XGBOOST_REGISTER_OBJECTIVE(SquareLogError, "reg:squaredlogerror")
|
XGBOOST_REGISTER_OBJECTIVE(SquareLogError, SquaredLogError::Name())
|
||||||
.describe("Regression with root mean squared logarithmic error.")
|
.describe("Regression with root mean squared logarithmic error.")
|
||||||
.set_body([]() { return new RegLossObj<SquaredLogError>(); });
|
.set_body([]() { return new RegLossObj<SquaredLogError>(); });
|
||||||
|
|
||||||
XGBOOST_REGISTER_OBJECTIVE(LogisticRegression, "reg:logistic")
|
XGBOOST_REGISTER_OBJECTIVE(LogisticRegression, LogisticRegression::Name())
|
||||||
.describe("Logistic regression for probability regression task.")
|
.describe("Logistic regression for probability regression task.")
|
||||||
.set_body([]() { return new RegLossObj<LogisticRegression>(); });
|
.set_body([]() { return new RegLossObj<LogisticRegression>(); });
|
||||||
|
|
||||||
XGBOOST_REGISTER_OBJECTIVE(LogisticClassification, "binary:logistic")
|
XGBOOST_REGISTER_OBJECTIVE(LogisticClassification, LogisticClassification::Name())
|
||||||
.describe("Logistic regression for binary classification task.")
|
.describe("Logistic regression for binary classification task.")
|
||||||
.set_body([]() { return new RegLossObj<LogisticClassification>(); });
|
.set_body([]() { return new RegLossObj<LogisticClassification>(); });
|
||||||
|
|
||||||
XGBOOST_REGISTER_OBJECTIVE(LogisticRaw, "binary:logitraw")
|
XGBOOST_REGISTER_OBJECTIVE(LogisticRaw, LogisticRaw::Name())
|
||||||
.describe("Logistic regression for classification, output score "
|
.describe("Logistic regression for classification, output score "
|
||||||
"before logistic transformation.")
|
"before logistic transformation.")
|
||||||
.set_body([]() { return new RegLossObj<LogisticRaw>(); });
|
.set_body([]() { return new RegLossObj<LogisticRaw>(); });
|
||||||
@ -151,7 +163,7 @@ XGBOOST_REGISTER_OBJECTIVE(LinearRegression, "reg:linear")
|
|||||||
// End deprecated
|
// End deprecated
|
||||||
|
|
||||||
// declare parameter
|
// declare parameter
|
||||||
struct PoissonRegressionParam : public dmlc::Parameter<PoissonRegressionParam> {
|
struct PoissonRegressionParam : public XGBoostParameter<PoissonRegressionParam> {
|
||||||
float max_delta_step;
|
float max_delta_step;
|
||||||
DMLC_DECLARE_PARAMETER(PoissonRegressionParam) {
|
DMLC_DECLARE_PARAMETER(PoissonRegressionParam) {
|
||||||
DMLC_DECLARE_FIELD(max_delta_step).set_lower_bound(0.0f).set_default(0.7f)
|
DMLC_DECLARE_FIELD(max_delta_step).set_lower_bound(0.0f).set_default(0.7f)
|
||||||
@ -165,7 +177,7 @@ class PoissonRegression : public ObjFunction {
|
|||||||
public:
|
public:
|
||||||
// declare functions
|
// declare functions
|
||||||
void Configure(const std::vector<std::pair<std::string, std::string> >& args) override {
|
void Configure(const std::vector<std::pair<std::string, std::string> >& args) override {
|
||||||
param_.InitAllowUnknown(args);
|
param_.UpdateAllowUnknown(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GetGradient(const HostDeviceVector<bst_float>& preds,
|
void GetGradient(const HostDeviceVector<bst_float>& preds,
|
||||||
@ -227,6 +239,16 @@ class PoissonRegression : public ObjFunction {
|
|||||||
return "poisson-nloglik";
|
return "poisson-nloglik";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SaveConfig(Json* p_out) const override {
|
||||||
|
auto& out = *p_out;
|
||||||
|
out["name"] = String("count:poisson");
|
||||||
|
out["poisson_regression_param"] = toJson(param_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LoadConfig(Json const& in) override {
|
||||||
|
fromJson(in["poisson_regression_param"], ¶m_);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PoissonRegressionParam param_;
|
PoissonRegressionParam param_;
|
||||||
HostDeviceVector<int> label_correct_;
|
HostDeviceVector<int> label_correct_;
|
||||||
@ -321,6 +343,12 @@ class CoxRegression : public ObjFunction {
|
|||||||
const char* DefaultEvalMetric() const override {
|
const char* DefaultEvalMetric() const override {
|
||||||
return "cox-nloglik";
|
return "cox-nloglik";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SaveConfig(Json* p_out) const override {
|
||||||
|
auto& out = *p_out;
|
||||||
|
out["name"] = String("survival:cox");
|
||||||
|
}
|
||||||
|
void LoadConfig(Json const&) override {}
|
||||||
};
|
};
|
||||||
|
|
||||||
// register the objective function
|
// register the objective function
|
||||||
@ -391,6 +419,11 @@ class GammaRegression : public ObjFunction {
|
|||||||
const char* DefaultEvalMetric() const override {
|
const char* DefaultEvalMetric() const override {
|
||||||
return "gamma-nloglik";
|
return "gamma-nloglik";
|
||||||
}
|
}
|
||||||
|
void SaveConfig(Json* p_out) const override {
|
||||||
|
auto& out = *p_out;
|
||||||
|
out["name"] = String("reg:gamma");
|
||||||
|
}
|
||||||
|
void LoadConfig(Json const&) override {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
HostDeviceVector<int> label_correct_;
|
HostDeviceVector<int> label_correct_;
|
||||||
@ -403,7 +436,7 @@ XGBOOST_REGISTER_OBJECTIVE(GammaRegression, "reg:gamma")
|
|||||||
|
|
||||||
|
|
||||||
// declare parameter
|
// declare parameter
|
||||||
struct TweedieRegressionParam : public dmlc::Parameter<TweedieRegressionParam> {
|
struct TweedieRegressionParam : public XGBoostParameter<TweedieRegressionParam> {
|
||||||
float tweedie_variance_power;
|
float tweedie_variance_power;
|
||||||
DMLC_DECLARE_PARAMETER(TweedieRegressionParam) {
|
DMLC_DECLARE_PARAMETER(TweedieRegressionParam) {
|
||||||
DMLC_DECLARE_FIELD(tweedie_variance_power).set_range(1.0f, 2.0f).set_default(1.5f)
|
DMLC_DECLARE_FIELD(tweedie_variance_power).set_range(1.0f, 2.0f).set_default(1.5f)
|
||||||
@ -416,7 +449,7 @@ class TweedieRegression : public ObjFunction {
|
|||||||
public:
|
public:
|
||||||
// declare functions
|
// declare functions
|
||||||
void Configure(const std::vector<std::pair<std::string, std::string> >& args) override {
|
void Configure(const std::vector<std::pair<std::string, std::string> >& args) override {
|
||||||
param_.InitAllowUnknown(args);
|
param_.UpdateAllowUnknown(args);
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
os << "tweedie-nloglik@" << param_.tweedie_variance_power;
|
os << "tweedie-nloglik@" << param_.tweedie_variance_power;
|
||||||
metric_ = os.str();
|
metric_ = os.str();
|
||||||
@ -485,6 +518,15 @@ class TweedieRegression : public ObjFunction {
|
|||||||
return metric_.c_str();
|
return metric_.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SaveConfig(Json* p_out) const override {
|
||||||
|
auto& out = *p_out;
|
||||||
|
out["name"] = String("reg:tweedie");
|
||||||
|
out["tweedie_regression_param"] = toJson(param_);
|
||||||
|
}
|
||||||
|
void LoadConfig(Json const& in) override {
|
||||||
|
fromJson(in["tweedie_regression_param"], ¶m_);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string metric_;
|
std::string metric_;
|
||||||
TweedieRegressionParam param_;
|
TweedieRegressionParam param_;
|
||||||
|
|||||||
@ -617,6 +617,35 @@ std::string RegTree::DumpModel(const FeatureMap& fmap,
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RegTree::LoadModel(dmlc::Stream* fi) {
|
||||||
|
CHECK_EQ(fi->Read(¶m, sizeof(TreeParam)), sizeof(TreeParam));
|
||||||
|
nodes_.resize(param.num_nodes);
|
||||||
|
stats_.resize(param.num_nodes);
|
||||||
|
CHECK_NE(param.num_nodes, 0);
|
||||||
|
CHECK_EQ(fi->Read(dmlc::BeginPtr(nodes_), sizeof(Node) * nodes_.size()),
|
||||||
|
sizeof(Node) * nodes_.size());
|
||||||
|
CHECK_EQ(fi->Read(dmlc::BeginPtr(stats_), sizeof(RTreeNodeStat) * stats_.size()),
|
||||||
|
sizeof(RTreeNodeStat) * stats_.size());
|
||||||
|
// chg deleted nodes
|
||||||
|
deleted_nodes_.resize(0);
|
||||||
|
for (int i = param.num_roots; i < param.num_nodes; ++i) {
|
||||||
|
if (nodes_[i].IsDeleted()) deleted_nodes_.push_back(i);
|
||||||
|
}
|
||||||
|
CHECK_EQ(static_cast<int>(deleted_nodes_.size()), param.num_deleted);
|
||||||
|
}
|
||||||
|
/*!
|
||||||
|
* \brief save model to stream
|
||||||
|
* \param fo output stream
|
||||||
|
*/
|
||||||
|
void RegTree::SaveModel(dmlc::Stream* fo) const {
|
||||||
|
CHECK_EQ(param.num_nodes, static_cast<int>(nodes_.size()));
|
||||||
|
CHECK_EQ(param.num_nodes, static_cast<int>(stats_.size()));
|
||||||
|
fo->Write(¶m, sizeof(TreeParam));
|
||||||
|
CHECK_NE(param.num_nodes, 0);
|
||||||
|
fo->Write(dmlc::BeginPtr(nodes_), sizeof(Node) * nodes_.size());
|
||||||
|
fo->Write(dmlc::BeginPtr(stats_), sizeof(RTreeNodeStat) * nodes_.size());
|
||||||
|
}
|
||||||
|
|
||||||
void RegTree::FillNodeMeanValues() {
|
void RegTree::FillNodeMeanValues() {
|
||||||
size_t num_nodes = this->param.num_nodes;
|
size_t num_nodes = this->param.num_nodes;
|
||||||
if (this->node_mean_values_.size() == num_nodes) {
|
if (this->node_mean_values_.size() == num_nodes) {
|
||||||
|
|||||||
@ -1053,12 +1053,12 @@ class GPUHistMakerSpecialised {
|
|||||||
common::MemoryBufferStream fs(&s_model);
|
common::MemoryBufferStream fs(&s_model);
|
||||||
int rank = rabit::GetRank();
|
int rank = rabit::GetRank();
|
||||||
if (rank == 0) {
|
if (rank == 0) {
|
||||||
local_trees.front().Save(&fs);
|
local_trees.front().SaveModel(&fs);
|
||||||
}
|
}
|
||||||
fs.Seek(0);
|
fs.Seek(0);
|
||||||
rabit::Broadcast(&s_model, 0);
|
rabit::Broadcast(&s_model, 0);
|
||||||
RegTree reference_tree{};
|
RegTree reference_tree{};
|
||||||
reference_tree.Load(&fs);
|
reference_tree.LoadModel(&fs);
|
||||||
for (const auto& tree : local_trees) {
|
for (const auto& tree : local_trees) {
|
||||||
CHECK(tree == reference_tree);
|
CHECK(tree == reference_tree);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,13 +35,13 @@ class TreeSyncher: public TreeUpdater {
|
|||||||
int rank = rabit::GetRank();
|
int rank = rabit::GetRank();
|
||||||
if (rank == 0) {
|
if (rank == 0) {
|
||||||
for (auto tree : trees) {
|
for (auto tree : trees) {
|
||||||
tree->Save(&fs);
|
tree->SaveModel(&fs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fs.Seek(0);
|
fs.Seek(0);
|
||||||
rabit::Broadcast(&s_model, 0);
|
rabit::Broadcast(&s_model, 0);
|
||||||
for (auto tree : trees) {
|
for (auto tree : trees) {
|
||||||
tree->Load(&fs);
|
tree->LoadModel(&fs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
#include <dmlc/parameter.h>
|
#include <dmlc/parameter.h>
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <xgboost/enum_class_param.h>
|
#include <xgboost/parameter.h>
|
||||||
|
|
||||||
enum class Foo : int {
|
enum class Foo : int {
|
||||||
kBar = 0, kFrog = 1, kCat = 2, kDog = 3
|
kBar = 0, kFrog = 1, kCat = 2, kDog = 3
|
||||||
@ -3,6 +3,10 @@
|
|||||||
*/
|
*/
|
||||||
#include <dmlc/filesystem.h>
|
#include <dmlc/filesystem.h>
|
||||||
#include <xgboost/logging.h>
|
#include <xgboost/logging.h>
|
||||||
|
#include <xgboost/json.h>
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <cinttypes>
|
#include <cinttypes>
|
||||||
#include "./helpers.h"
|
#include "./helpers.h"
|
||||||
@ -36,7 +40,7 @@ void CreateBigTestData(const std::string& filename, size_t n_entries) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckObjFunctionImpl(xgboost::ObjFunction * obj,
|
void CheckObjFunctionImpl(std::unique_ptr<xgboost::ObjFunction> const& obj,
|
||||||
std::vector<xgboost::bst_float> preds,
|
std::vector<xgboost::bst_float> preds,
|
||||||
std::vector<xgboost::bst_float> labels,
|
std::vector<xgboost::bst_float> labels,
|
||||||
std::vector<xgboost::bst_float> weights,
|
std::vector<xgboost::bst_float> weights,
|
||||||
@ -59,7 +63,7 @@ void CheckObjFunctionImpl(xgboost::ObjFunction * obj,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckObjFunction(xgboost::ObjFunction * obj,
|
void CheckObjFunction(std::unique_ptr<xgboost::ObjFunction> const& obj,
|
||||||
std::vector<xgboost::bst_float> preds,
|
std::vector<xgboost::bst_float> preds,
|
||||||
std::vector<xgboost::bst_float> labels,
|
std::vector<xgboost::bst_float> labels,
|
||||||
std::vector<xgboost::bst_float> weights,
|
std::vector<xgboost::bst_float> weights,
|
||||||
@ -73,7 +77,27 @@ void CheckObjFunction(xgboost::ObjFunction * obj,
|
|||||||
CheckObjFunctionImpl(obj, preds, labels, weights, info, out_grad, out_hess);
|
CheckObjFunctionImpl(obj, preds, labels, weights, info, out_grad, out_hess);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheckRankingObjFunction(xgboost::ObjFunction * obj,
|
xgboost::Json CheckConfigReloadImpl(xgboost::Configurable* const configurable,
|
||||||
|
std::string name) {
|
||||||
|
xgboost::Json config_0 { xgboost::Object() };
|
||||||
|
configurable->SaveConfig(&config_0);
|
||||||
|
configurable->LoadConfig(config_0);
|
||||||
|
|
||||||
|
xgboost::Json config_1 { xgboost::Object() };
|
||||||
|
configurable->SaveConfig(&config_1);
|
||||||
|
|
||||||
|
std::string str_0, str_1;
|
||||||
|
xgboost::Json::Dump(config_0, &str_0);
|
||||||
|
xgboost::Json::Dump(config_1, &str_1);
|
||||||
|
EXPECT_EQ(str_0, str_1);
|
||||||
|
|
||||||
|
if (name != "") {
|
||||||
|
EXPECT_EQ(xgboost::get<xgboost::String>(config_1["name"]), name);
|
||||||
|
}
|
||||||
|
return config_1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CheckRankingObjFunction(std::unique_ptr<xgboost::ObjFunction> const& obj,
|
||||||
std::vector<xgboost::bst_float> preds,
|
std::vector<xgboost::bst_float> preds,
|
||||||
std::vector<xgboost::bst_float> labels,
|
std::vector<xgboost::bst_float> labels,
|
||||||
std::vector<xgboost::bst_float> weights,
|
std::vector<xgboost::bst_float> weights,
|
||||||
|
|||||||
@ -17,6 +17,7 @@
|
|||||||
#include <xgboost/base.h>
|
#include <xgboost/base.h>
|
||||||
#include <xgboost/objective.h>
|
#include <xgboost/objective.h>
|
||||||
#include <xgboost/metric.h>
|
#include <xgboost/metric.h>
|
||||||
|
#include <xgboost/json.h>
|
||||||
#include <xgboost/predictor.h>
|
#include <xgboost/predictor.h>
|
||||||
#include <xgboost/generic_parameters.h>
|
#include <xgboost/generic_parameters.h>
|
||||||
|
|
||||||
@ -46,14 +47,24 @@ void CreateSimpleTestData(const std::string& filename);
|
|||||||
|
|
||||||
void CreateBigTestData(const std::string& filename, size_t n_entries);
|
void CreateBigTestData(const std::string& filename, size_t n_entries);
|
||||||
|
|
||||||
void CheckObjFunction(xgboost::ObjFunction * obj,
|
void CheckObjFunction(std::unique_ptr<xgboost::ObjFunction> const& obj,
|
||||||
std::vector<xgboost::bst_float> preds,
|
std::vector<xgboost::bst_float> preds,
|
||||||
std::vector<xgboost::bst_float> labels,
|
std::vector<xgboost::bst_float> labels,
|
||||||
std::vector<xgboost::bst_float> weights,
|
std::vector<xgboost::bst_float> weights,
|
||||||
std::vector<xgboost::bst_float> out_grad,
|
std::vector<xgboost::bst_float> out_grad,
|
||||||
std::vector<xgboost::bst_float> out_hess);
|
std::vector<xgboost::bst_float> out_hess);
|
||||||
|
|
||||||
void CheckRankingObjFunction(xgboost::ObjFunction * obj,
|
xgboost::Json CheckConfigReloadImpl(xgboost::Configurable* const configurable,
|
||||||
|
std::string name);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
xgboost::Json CheckConfigReload(std::unique_ptr<T> const& configurable,
|
||||||
|
std::string name = "") {
|
||||||
|
return CheckConfigReloadImpl(dynamic_cast<xgboost::Configurable*>(configurable.get()),
|
||||||
|
name);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CheckRankingObjFunction(std::unique_ptr<xgboost::ObjFunction> const& obj,
|
||||||
std::vector<xgboost::bst_float> preds,
|
std::vector<xgboost::bst_float> preds,
|
||||||
std::vector<xgboost::bst_float> labels,
|
std::vector<xgboost::bst_float> labels,
|
||||||
std::vector<xgboost::bst_float> weights,
|
std::vector<xgboost::bst_float> weights,
|
||||||
|
|||||||
@ -7,7 +7,9 @@
|
|||||||
|
|
||||||
TEST(Objective, DeclareUnifiedTest(HingeObj)) {
|
TEST(Objective, DeclareUnifiedTest(HingeObj)) {
|
||||||
xgboost::GenericParameter tparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
xgboost::GenericParameter tparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
||||||
xgboost::ObjFunction * obj = xgboost::ObjFunction::Create("binary:hinge", &tparam);
|
std::unique_ptr<xgboost::ObjFunction> obj {
|
||||||
|
xgboost::ObjFunction::Create("binary:hinge", &tparam)
|
||||||
|
};
|
||||||
|
|
||||||
xgboost::bst_float eps = std::numeric_limits<xgboost::bst_float>::min();
|
xgboost::bst_float eps = std::numeric_limits<xgboost::bst_float>::min();
|
||||||
CheckObjFunction(obj,
|
CheckObjFunction(obj,
|
||||||
@ -24,6 +26,4 @@ TEST(Objective, DeclareUnifiedTest(HingeObj)) {
|
|||||||
{ eps, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, eps });
|
{ eps, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, eps });
|
||||||
|
|
||||||
ASSERT_NO_THROW(obj->DefaultEvalMetric());
|
ASSERT_NO_THROW(obj->DefaultEvalMetric());
|
||||||
|
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,12 +6,18 @@
|
|||||||
#include "../../src/common/common.h"
|
#include "../../src/common/common.h"
|
||||||
#include "../helpers.h"
|
#include "../helpers.h"
|
||||||
|
|
||||||
|
namespace xgboost {
|
||||||
|
|
||||||
TEST(Objective, DeclareUnifiedTest(SoftmaxMultiClassObjGPair)) {
|
TEST(Objective, DeclareUnifiedTest(SoftmaxMultiClassObjGPair)) {
|
||||||
xgboost::GenericParameter lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
GenericParameter lparam = CreateEmptyGenericParam(GPUIDX);
|
||||||
std::vector<std::pair<std::string, std::string>> args {{"num_class", "3"}};
|
std::vector<std::pair<std::string, std::string>> args {{"num_class", "3"}};
|
||||||
xgboost::ObjFunction * obj = xgboost::ObjFunction::Create("multi:softmax", &lparam);
|
std::unique_ptr<ObjFunction> obj {
|
||||||
|
ObjFunction::Create("multi:softmax", &lparam)
|
||||||
|
};
|
||||||
|
|
||||||
obj->Configure(args);
|
obj->Configure(args);
|
||||||
|
CheckConfigReload(obj, "multi:softmax");
|
||||||
|
|
||||||
CheckObjFunction(obj,
|
CheckObjFunction(obj,
|
||||||
{1.0f, 0.0f, 2.0f, 2.0f, 0.0f, 1.0f}, // preds
|
{1.0f, 0.0f, 2.0f, 2.0f, 0.0f, 1.0f}, // preds
|
||||||
{1.0f, 0.0f}, // labels
|
{1.0f, 0.0f}, // labels
|
||||||
@ -20,21 +26,20 @@ TEST(Objective, DeclareUnifiedTest(SoftmaxMultiClassObjGPair)) {
|
|||||||
{0.36f, 0.16f, 0.44f, 0.45f, 0.16f, 0.37f}); // hess
|
{0.36f, 0.16f, 0.44f, 0.45f, 0.16f, 0.37f}); // hess
|
||||||
|
|
||||||
ASSERT_NO_THROW(obj->DefaultEvalMetric());
|
ASSERT_NO_THROW(obj->DefaultEvalMetric());
|
||||||
|
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Objective, DeclareUnifiedTest(SoftmaxMultiClassBasic)) {
|
TEST(Objective, DeclareUnifiedTest(SoftmaxMultiClassBasic)) {
|
||||||
auto lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
auto lparam = CreateEmptyGenericParam(GPUIDX);
|
||||||
std::vector<std::pair<std::string, std::string>> args{
|
std::vector<std::pair<std::string, std::string>> args{
|
||||||
std::pair<std::string, std::string>("num_class", "3")};
|
std::pair<std::string, std::string>("num_class", "3")};
|
||||||
|
|
||||||
xgboost::ObjFunction * obj = xgboost::ObjFunction::Create("multi:softmax", &lparam);
|
std::unique_ptr<ObjFunction> obj { ObjFunction::Create("multi:softmax", &lparam) };
|
||||||
obj->Configure(args);
|
obj->Configure(args);
|
||||||
|
CheckConfigReload(obj, "multi:softmax");
|
||||||
|
|
||||||
xgboost::HostDeviceVector<xgboost::bst_float> io_preds = {2.0f, 0.0f, 1.0f,
|
HostDeviceVector<bst_float> io_preds = {2.0f, 0.0f, 1.0f,
|
||||||
1.0f, 0.0f, 2.0f};
|
1.0f, 0.0f, 2.0f};
|
||||||
std::vector<xgboost::bst_float> out_preds = {0.0f, 2.0f};
|
std::vector<bst_float> out_preds = {0.0f, 2.0f};
|
||||||
obj->PredTransform(&io_preds);
|
obj->PredTransform(&io_preds);
|
||||||
|
|
||||||
auto& preds = io_preds.HostVector();
|
auto& preds = io_preds.HostVector();
|
||||||
@ -42,20 +47,21 @@ TEST(Objective, DeclareUnifiedTest(SoftmaxMultiClassBasic)) {
|
|||||||
for (int i = 0; i < static_cast<int>(io_preds.Size()); ++i) {
|
for (int i = 0; i < static_cast<int>(io_preds.Size()); ++i) {
|
||||||
EXPECT_NEAR(preds[i], out_preds[i], 0.01f);
|
EXPECT_NEAR(preds[i], out_preds[i], 0.01f);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Objective, DeclareUnifiedTest(SoftprobMultiClassBasic)) {
|
TEST(Objective, DeclareUnifiedTest(SoftprobMultiClassBasic)) {
|
||||||
xgboost::GenericParameter lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
GenericParameter lparam = CreateEmptyGenericParam(GPUIDX);
|
||||||
std::vector<std::pair<std::string, std::string>> args {
|
std::vector<std::pair<std::string, std::string>> args {
|
||||||
std::pair<std::string, std::string>("num_class", "3")};
|
std::pair<std::string, std::string>("num_class", "3")};
|
||||||
|
|
||||||
xgboost::ObjFunction * obj = xgboost::ObjFunction::Create("multi:softprob", &lparam);
|
std::unique_ptr<ObjFunction> obj {
|
||||||
|
ObjFunction::Create("multi:softprob", &lparam)
|
||||||
|
};
|
||||||
obj->Configure(args);
|
obj->Configure(args);
|
||||||
|
CheckConfigReload(obj, "multi:softprob");
|
||||||
|
|
||||||
xgboost::HostDeviceVector<xgboost::bst_float> io_preds = {2.0f, 0.0f, 1.0f};
|
HostDeviceVector<bst_float> io_preds = {2.0f, 0.0f, 1.0f};
|
||||||
std::vector<xgboost::bst_float> out_preds = {0.66524096f, 0.09003057f, 0.24472847f};
|
std::vector<bst_float> out_preds = {0.66524096f, 0.09003057f, 0.24472847f};
|
||||||
|
|
||||||
obj->PredTransform(&io_preds);
|
obj->PredTransform(&io_preds);
|
||||||
auto& preds = io_preds.HostVector();
|
auto& preds = io_preds.HostVector();
|
||||||
@ -63,5 +69,5 @@ TEST(Objective, DeclareUnifiedTest(SoftprobMultiClassBasic)) {
|
|||||||
for (int i = 0; i < static_cast<int>(io_preds.Size()); ++i) {
|
for (int i = 0; i < static_cast<int>(io_preds.Size()); ++i) {
|
||||||
EXPECT_NEAR(preds[i], out_preds[i], 0.01f);
|
EXPECT_NEAR(preds[i], out_preds[i], 0.01f);
|
||||||
}
|
}
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
|
} // namespace xgboost
|
||||||
|
|||||||
@ -2,15 +2,21 @@
|
|||||||
#include <xgboost/objective.h>
|
#include <xgboost/objective.h>
|
||||||
#include <xgboost/generic_parameters.h>
|
#include <xgboost/generic_parameters.h>
|
||||||
#include "../helpers.h"
|
#include "../helpers.h"
|
||||||
|
#include <xgboost/json.h>
|
||||||
|
|
||||||
|
namespace xgboost {
|
||||||
|
|
||||||
TEST(Objective, PairwiseRankingGPair) {
|
TEST(Objective, PairwiseRankingGPair) {
|
||||||
xgboost::GenericParameter tparam;
|
xgboost::GenericParameter tparam;
|
||||||
std::vector<std::pair<std::string, std::string>> args;
|
std::vector<std::pair<std::string, std::string>> args;
|
||||||
tparam.InitAllowUnknown(args);
|
tparam.InitAllowUnknown(args);
|
||||||
|
|
||||||
xgboost::ObjFunction * obj =
|
std::unique_ptr<xgboost::ObjFunction> obj {
|
||||||
xgboost::ObjFunction::Create("rank:pairwise", &tparam);
|
xgboost::ObjFunction::Create("rank:pairwise", &tparam)
|
||||||
|
};
|
||||||
obj->Configure(args);
|
obj->Configure(args);
|
||||||
|
CheckConfigReload(obj, "rank:pairwise");
|
||||||
|
|
||||||
// Test with setting sample weight to second query group
|
// Test with setting sample weight to second query group
|
||||||
CheckRankingObjFunction(obj,
|
CheckRankingObjFunction(obj,
|
||||||
{0, 0.1f, 0, 0.1f},
|
{0, 0.1f, 0, 0.1f},
|
||||||
@ -29,6 +35,26 @@ TEST(Objective, PairwiseRankingGPair) {
|
|||||||
{0.9975f, 0.9975f, 0.9975f, 0.9975f});
|
{0.9975f, 0.9975f, 0.9975f, 0.9975f});
|
||||||
|
|
||||||
ASSERT_NO_THROW(obj->DefaultEvalMetric());
|
ASSERT_NO_THROW(obj->DefaultEvalMetric());
|
||||||
|
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(Objective, NDCG_Json_IO) {
|
||||||
|
xgboost::GenericParameter tparam;
|
||||||
|
tparam.InitAllowUnknown(Args{});
|
||||||
|
|
||||||
|
std::unique_ptr<xgboost::ObjFunction> obj {
|
||||||
|
xgboost::ObjFunction::Create("rank:ndcg", &tparam)
|
||||||
|
};
|
||||||
|
|
||||||
|
obj->Configure(Args{});
|
||||||
|
Json j_obj {Object()};
|
||||||
|
obj->SaveConfig(&j_obj);
|
||||||
|
|
||||||
|
ASSERT_EQ(get<String>(j_obj["name"]), "rank:ndcg");;
|
||||||
|
|
||||||
|
auto const& j_param = j_obj["lambda_rank_param"];
|
||||||
|
|
||||||
|
ASSERT_EQ(get<String>(j_param["num_pairsample"]), "1");
|
||||||
|
ASSERT_EQ(get<String>(j_param["fix_list_weight"]), "0");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace xgboost
|
||||||
|
|||||||
@ -4,14 +4,17 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <xgboost/objective.h>
|
#include <xgboost/objective.h>
|
||||||
#include <xgboost/generic_parameters.h>
|
#include <xgboost/generic_parameters.h>
|
||||||
|
#include <xgboost/json.h>
|
||||||
#include "../helpers.h"
|
#include "../helpers.h"
|
||||||
|
namespace xgboost {
|
||||||
|
|
||||||
TEST(Objective, DeclareUnifiedTest(LinearRegressionGPair)) {
|
TEST(Objective, DeclareUnifiedTest(LinearRegressionGPair)) {
|
||||||
xgboost::GenericParameter tparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
GenericParameter tparam = CreateEmptyGenericParam(GPUIDX);
|
||||||
std::vector<std::pair<std::string, std::string>> args;
|
std::vector<std::pair<std::string, std::string>> args;
|
||||||
|
|
||||||
xgboost::ObjFunction * obj =
|
std::unique_ptr<ObjFunction> obj {
|
||||||
xgboost::ObjFunction::Create("reg:squarederror", &tparam);
|
ObjFunction::Create("reg:squarederror", &tparam)
|
||||||
|
};
|
||||||
|
|
||||||
obj->Configure(args);
|
obj->Configure(args);
|
||||||
CheckObjFunction(obj,
|
CheckObjFunction(obj,
|
||||||
@ -27,17 +30,15 @@ TEST(Objective, DeclareUnifiedTest(LinearRegressionGPair)) {
|
|||||||
{0, 0.1f, 0.9f, 1.0f, -1.0f, -0.9f, -0.1f, 0},
|
{0, 0.1f, 0.9f, 1.0f, -1.0f, -0.9f, -0.1f, 0},
|
||||||
{1, 1, 1, 1, 1, 1, 1, 1});
|
{1, 1, 1, 1, 1, 1, 1, 1});
|
||||||
ASSERT_NO_THROW(obj->DefaultEvalMetric());
|
ASSERT_NO_THROW(obj->DefaultEvalMetric());
|
||||||
|
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Objective, DeclareUnifiedTest(SquaredLog)) {
|
TEST(Objective, DeclareUnifiedTest(SquaredLog)) {
|
||||||
xgboost::GenericParameter tparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
GenericParameter tparam = CreateEmptyGenericParam(GPUIDX);
|
||||||
std::vector<std::pair<std::string, std::string>> args;
|
std::vector<std::pair<std::string, std::string>> args;
|
||||||
|
|
||||||
xgboost::ObjFunction * obj =
|
std::unique_ptr<ObjFunction> obj { ObjFunction::Create("reg:squaredlogerror", &tparam) };
|
||||||
xgboost::ObjFunction::Create("reg:squaredlogerror", &tparam);
|
|
||||||
obj->Configure(args);
|
obj->Configure(args);
|
||||||
|
CheckConfigReload(obj, "reg:squaredlogerror");
|
||||||
|
|
||||||
CheckObjFunction(obj,
|
CheckObjFunction(obj,
|
||||||
{0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, // pred
|
{0.1f, 0.2f, 0.4f, 0.8f, 1.6f}, // pred
|
||||||
@ -52,31 +53,33 @@ TEST(Objective, DeclareUnifiedTest(SquaredLog)) {
|
|||||||
{-0.5435f, -0.4257f, -0.25475f, -0.05855f, 0.1009f},
|
{-0.5435f, -0.4257f, -0.25475f, -0.05855f, 0.1009f},
|
||||||
{ 1.3205f, 1.0492f, 0.69215f, 0.34115f, 0.1091f});
|
{ 1.3205f, 1.0492f, 0.69215f, 0.34115f, 0.1091f});
|
||||||
ASSERT_EQ(obj->DefaultEvalMetric(), std::string{"rmsle"});
|
ASSERT_EQ(obj->DefaultEvalMetric(), std::string{"rmsle"});
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Objective, DeclareUnifiedTest(LogisticRegressionGPair)) {
|
TEST(Objective, DeclareUnifiedTest(LogisticRegressionGPair)) {
|
||||||
xgboost::GenericParameter tparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
GenericParameter tparam = CreateEmptyGenericParam(GPUIDX);
|
||||||
std::vector<std::pair<std::string, std::string>> args;
|
std::vector<std::pair<std::string, std::string>> args;
|
||||||
xgboost::ObjFunction * obj = xgboost::ObjFunction::Create("reg:logistic", &tparam);
|
std::unique_ptr<ObjFunction> obj { ObjFunction::Create("reg:logistic", &tparam) };
|
||||||
|
|
||||||
obj->Configure(args);
|
obj->Configure(args);
|
||||||
|
CheckConfigReload(obj, "reg:logistic");
|
||||||
|
|
||||||
CheckObjFunction(obj,
|
CheckObjFunction(obj,
|
||||||
{ 0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, // preds
|
{ 0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1}, // preds
|
||||||
{ 0, 0, 0, 0, 1, 1, 1, 1}, // labels
|
{ 0, 0, 0, 0, 1, 1, 1, 1}, // labels
|
||||||
{ 1, 1, 1, 1, 1, 1, 1, 1}, // weights
|
{ 1, 1, 1, 1, 1, 1, 1, 1}, // weights
|
||||||
{ 0.5f, 0.52f, 0.71f, 0.73f, -0.5f, -0.47f, -0.28f, -0.26f}, // out_grad
|
{ 0.5f, 0.52f, 0.71f, 0.73f, -0.5f, -0.47f, -0.28f, -0.26f}, // out_grad
|
||||||
{0.25f, 0.24f, 0.20f, 0.19f, 0.25f, 0.24f, 0.20f, 0.19f}); // out_hess
|
{0.25f, 0.24f, 0.20f, 0.19f, 0.25f, 0.24f, 0.20f, 0.19f}); // out_hess
|
||||||
|
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Objective, DeclareUnifiedTest(LogisticRegressionBasic)) {
|
TEST(Objective, DeclareUnifiedTest(LogisticRegressionBasic)) {
|
||||||
xgboost::GenericParameter lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
GenericParameter lparam = CreateEmptyGenericParam(GPUIDX);
|
||||||
std::vector<std::pair<std::string, std::string>> args;
|
std::vector<std::pair<std::string, std::string>> args;
|
||||||
xgboost::ObjFunction * obj = xgboost::ObjFunction::Create("reg:logistic", &lparam);
|
std::unique_ptr<ObjFunction> obj {
|
||||||
|
ObjFunction::Create("reg:logistic", &lparam)
|
||||||
|
};
|
||||||
|
|
||||||
obj->Configure(args);
|
obj->Configure(args);
|
||||||
|
CheckConfigReload(obj, "reg:logistic");
|
||||||
|
|
||||||
// test label validation
|
// test label validation
|
||||||
EXPECT_ANY_THROW(CheckObjFunction(obj, {0}, {10}, {1}, {0}, {0}))
|
EXPECT_ANY_THROW(CheckObjFunction(obj, {0}, {10}, {1}, {0}, {0}))
|
||||||
@ -90,40 +93,42 @@ TEST(Objective, DeclareUnifiedTest(LogisticRegressionBasic)) {
|
|||||||
<< "Expected error when base_score not in range [0,1f] for LogisticRegression";
|
<< "Expected error when base_score not in range [0,1f] for LogisticRegression";
|
||||||
|
|
||||||
// test PredTransform
|
// test PredTransform
|
||||||
xgboost::HostDeviceVector<xgboost::bst_float> io_preds = {0, 0.1f, 0.5f, 0.9f, 1};
|
HostDeviceVector<bst_float> io_preds = {0, 0.1f, 0.5f, 0.9f, 1};
|
||||||
std::vector<xgboost::bst_float> out_preds = {0.5f, 0.524f, 0.622f, 0.710f, 0.731f};
|
std::vector<bst_float> out_preds = {0.5f, 0.524f, 0.622f, 0.710f, 0.731f};
|
||||||
obj->PredTransform(&io_preds);
|
obj->PredTransform(&io_preds);
|
||||||
auto& preds = io_preds.HostVector();
|
auto& preds = io_preds.HostVector();
|
||||||
for (int i = 0; i < static_cast<int>(io_preds.Size()); ++i) {
|
for (int i = 0; i < static_cast<int>(io_preds.Size()); ++i) {
|
||||||
EXPECT_NEAR(preds[i], out_preds[i], 0.01f);
|
EXPECT_NEAR(preds[i], out_preds[i], 0.01f);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Objective, DeclareUnifiedTest(LogisticRawGPair)) {
|
TEST(Objective, DeclareUnifiedTest(LogisticRawGPair)) {
|
||||||
xgboost::GenericParameter lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
GenericParameter lparam = CreateEmptyGenericParam(GPUIDX);
|
||||||
std::vector<std::pair<std::string, std::string>> args;
|
std::vector<std::pair<std::string, std::string>> args;
|
||||||
xgboost::ObjFunction * obj = xgboost::ObjFunction::Create("binary:logitraw", &lparam);
|
std::unique_ptr<ObjFunction> obj {
|
||||||
|
ObjFunction::Create("binary:logitraw", &lparam)
|
||||||
|
};
|
||||||
|
|
||||||
obj->Configure(args);
|
obj->Configure(args);
|
||||||
|
|
||||||
CheckObjFunction(obj,
|
CheckObjFunction(obj,
|
||||||
{ 0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1},
|
{ 0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1},
|
||||||
{ 0, 0, 0, 0, 1, 1, 1, 1},
|
{ 0, 0, 0, 0, 1, 1, 1, 1},
|
||||||
{ 1, 1, 1, 1, 1, 1, 1, 1},
|
{ 1, 1, 1, 1, 1, 1, 1, 1},
|
||||||
{ 0.5f, 0.52f, 0.71f, 0.73f, -0.5f, -0.47f, -0.28f, -0.26f},
|
{ 0.5f, 0.52f, 0.71f, 0.73f, -0.5f, -0.47f, -0.28f, -0.26f},
|
||||||
{0.25f, 0.24f, 0.20f, 0.19f, 0.25f, 0.24f, 0.20f, 0.19f});
|
{0.25f, 0.24f, 0.20f, 0.19f, 0.25f, 0.24f, 0.20f, 0.19f});
|
||||||
|
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Objective, DeclareUnifiedTest(PoissonRegressionGPair)) {
|
TEST(Objective, DeclareUnifiedTest(PoissonRegressionGPair)) {
|
||||||
xgboost::GenericParameter lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
GenericParameter lparam = CreateEmptyGenericParam(GPUIDX);
|
||||||
std::vector<std::pair<std::string, std::string>> args;
|
std::vector<std::pair<std::string, std::string>> args;
|
||||||
xgboost::ObjFunction * obj = xgboost::ObjFunction::Create("count:poisson", &lparam);
|
std::unique_ptr<ObjFunction> obj {
|
||||||
|
ObjFunction::Create("count:poisson", &lparam)
|
||||||
|
};
|
||||||
|
|
||||||
args.emplace_back(std::make_pair("max_delta_step", "0.1f"));
|
args.emplace_back(std::make_pair("max_delta_step", "0.1f"));
|
||||||
obj->Configure(args);
|
obj->Configure(args);
|
||||||
|
|
||||||
CheckObjFunction(obj,
|
CheckObjFunction(obj,
|
||||||
{ 0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1},
|
{ 0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1},
|
||||||
{ 0, 0, 0, 0, 1, 1, 1, 1},
|
{ 0, 0, 0, 0, 1, 1, 1, 1},
|
||||||
@ -136,15 +141,17 @@ TEST(Objective, DeclareUnifiedTest(PoissonRegressionGPair)) {
|
|||||||
{}, // Empty weight
|
{}, // Empty weight
|
||||||
{ 1, 1.10f, 2.45f, 2.71f, 0, 0.10f, 1.45f, 1.71f},
|
{ 1, 1.10f, 2.45f, 2.71f, 0, 0.10f, 1.45f, 1.71f},
|
||||||
{1.10f, 1.22f, 2.71f, 3.00f, 1.10f, 1.22f, 2.71f, 3.00f});
|
{1.10f, 1.22f, 2.71f, 3.00f, 1.10f, 1.22f, 2.71f, 3.00f});
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Objective, DeclareUnifiedTest(PoissonRegressionBasic)) {
|
TEST(Objective, DeclareUnifiedTest(PoissonRegressionBasic)) {
|
||||||
xgboost::GenericParameter lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
GenericParameter lparam = CreateEmptyGenericParam(GPUIDX);
|
||||||
std::vector<std::pair<std::string, std::string>> args;
|
std::vector<std::pair<std::string, std::string>> args;
|
||||||
xgboost::ObjFunction * obj = xgboost::ObjFunction::Create("count:poisson", &lparam);
|
std::unique_ptr<ObjFunction> obj {
|
||||||
|
ObjFunction::Create("count:poisson", &lparam)
|
||||||
|
};
|
||||||
|
|
||||||
obj->Configure(args);
|
obj->Configure(args);
|
||||||
|
CheckConfigReload(obj, "count:poisson");
|
||||||
|
|
||||||
// test label validation
|
// test label validation
|
||||||
EXPECT_ANY_THROW(CheckObjFunction(obj, {0}, {-1}, {1}, {0}, {0}))
|
EXPECT_ANY_THROW(CheckObjFunction(obj, {0}, {-1}, {1}, {0}, {0}))
|
||||||
@ -156,21 +163,21 @@ TEST(Objective, DeclareUnifiedTest(PoissonRegressionBasic)) {
|
|||||||
EXPECT_NEAR(obj->ProbToMargin(0.9f), -0.10f, 0.01f);
|
EXPECT_NEAR(obj->ProbToMargin(0.9f), -0.10f, 0.01f);
|
||||||
|
|
||||||
// test PredTransform
|
// test PredTransform
|
||||||
xgboost::HostDeviceVector<xgboost::bst_float> io_preds = {0, 0.1f, 0.5f, 0.9f, 1};
|
HostDeviceVector<bst_float> io_preds = {0, 0.1f, 0.5f, 0.9f, 1};
|
||||||
std::vector<xgboost::bst_float> out_preds = {1, 1.10f, 1.64f, 2.45f, 2.71f};
|
std::vector<bst_float> out_preds = {1, 1.10f, 1.64f, 2.45f, 2.71f};
|
||||||
obj->PredTransform(&io_preds);
|
obj->PredTransform(&io_preds);
|
||||||
auto& preds = io_preds.HostVector();
|
auto& preds = io_preds.HostVector();
|
||||||
for (int i = 0; i < static_cast<int>(io_preds.Size()); ++i) {
|
for (int i = 0; i < static_cast<int>(io_preds.Size()); ++i) {
|
||||||
EXPECT_NEAR(preds[i], out_preds[i], 0.01f);
|
EXPECT_NEAR(preds[i], out_preds[i], 0.01f);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Objective, DeclareUnifiedTest(GammaRegressionGPair)) {
|
TEST(Objective, DeclareUnifiedTest(GammaRegressionGPair)) {
|
||||||
xgboost::GenericParameter lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
GenericParameter lparam = CreateEmptyGenericParam(GPUIDX);
|
||||||
std::vector<std::pair<std::string, std::string>> args;
|
std::vector<std::pair<std::string, std::string>> args;
|
||||||
xgboost::ObjFunction * obj = xgboost::ObjFunction::Create("reg:gamma", &lparam);
|
std::unique_ptr<ObjFunction> obj {
|
||||||
|
ObjFunction::Create("reg:gamma", &lparam)
|
||||||
|
};
|
||||||
|
|
||||||
obj->Configure(args);
|
obj->Configure(args);
|
||||||
CheckObjFunction(obj,
|
CheckObjFunction(obj,
|
||||||
@ -185,15 +192,17 @@ TEST(Objective, DeclareUnifiedTest(GammaRegressionGPair)) {
|
|||||||
{}, // Empty weight
|
{}, // Empty weight
|
||||||
{1, 1, 1, 1, 0, 0.09f, 0.59f, 0.63f},
|
{1, 1, 1, 1, 0, 0.09f, 0.59f, 0.63f},
|
||||||
{0, 0, 0, 0, 1, 0.90f, 0.40f, 0.36f});
|
{0, 0, 0, 0, 1, 0.90f, 0.40f, 0.36f});
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Objective, DeclareUnifiedTest(GammaRegressionBasic)) {
|
TEST(Objective, DeclareUnifiedTest(GammaRegressionBasic)) {
|
||||||
xgboost::GenericParameter lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
GenericParameter lparam = CreateEmptyGenericParam(GPUIDX);
|
||||||
std::vector<std::pair<std::string, std::string>> args;
|
std::vector<std::pair<std::string, std::string>> args;
|
||||||
xgboost::ObjFunction * obj = xgboost::ObjFunction::Create("reg:gamma", &lparam);
|
std::unique_ptr<ObjFunction> obj {
|
||||||
|
ObjFunction::Create("reg:gamma", &lparam)
|
||||||
|
};
|
||||||
|
|
||||||
obj->Configure(args);
|
obj->Configure(args);
|
||||||
|
CheckConfigReload(obj, "reg:gamma");
|
||||||
|
|
||||||
// test label validation
|
// test label validation
|
||||||
EXPECT_ANY_THROW(CheckObjFunction(obj, {0}, {-1}, {1}, {0}, {0}))
|
EXPECT_ANY_THROW(CheckObjFunction(obj, {0}, {-1}, {1}, {0}, {0}))
|
||||||
@ -205,24 +214,25 @@ TEST(Objective, DeclareUnifiedTest(GammaRegressionBasic)) {
|
|||||||
EXPECT_NEAR(obj->ProbToMargin(0.9f), -0.10f, 0.01f);
|
EXPECT_NEAR(obj->ProbToMargin(0.9f), -0.10f, 0.01f);
|
||||||
|
|
||||||
// test PredTransform
|
// test PredTransform
|
||||||
xgboost::HostDeviceVector<xgboost::bst_float> io_preds = {0, 0.1f, 0.5f, 0.9f, 1};
|
HostDeviceVector<bst_float> io_preds = {0, 0.1f, 0.5f, 0.9f, 1};
|
||||||
std::vector<xgboost::bst_float> out_preds = {1, 1.10f, 1.64f, 2.45f, 2.71f};
|
std::vector<bst_float> out_preds = {1, 1.10f, 1.64f, 2.45f, 2.71f};
|
||||||
obj->PredTransform(&io_preds);
|
obj->PredTransform(&io_preds);
|
||||||
auto& preds = io_preds.HostVector();
|
auto& preds = io_preds.HostVector();
|
||||||
for (int i = 0; i < static_cast<int>(io_preds.Size()); ++i) {
|
for (int i = 0; i < static_cast<int>(io_preds.Size()); ++i) {
|
||||||
EXPECT_NEAR(preds[i], out_preds[i], 0.01f);
|
EXPECT_NEAR(preds[i], out_preds[i], 0.01f);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Objective, DeclareUnifiedTest(TweedieRegressionGPair)) {
|
TEST(Objective, DeclareUnifiedTest(TweedieRegressionGPair)) {
|
||||||
xgboost::GenericParameter lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
GenericParameter lparam = CreateEmptyGenericParam(GPUIDX);
|
||||||
std::vector<std::pair<std::string, std::string>> args;
|
std::vector<std::pair<std::string, std::string>> args;
|
||||||
xgboost::ObjFunction * obj = xgboost::ObjFunction::Create("reg:tweedie", &lparam);
|
std::unique_ptr<ObjFunction> obj {
|
||||||
|
ObjFunction::Create("reg:tweedie", &lparam)
|
||||||
|
};
|
||||||
|
|
||||||
args.emplace_back(std::make_pair("tweedie_variance_power", "1.1f"));
|
args.emplace_back(std::make_pair("tweedie_variance_power", "1.1f"));
|
||||||
obj->Configure(args);
|
obj->Configure(args);
|
||||||
|
|
||||||
CheckObjFunction(obj,
|
CheckObjFunction(obj,
|
||||||
{ 0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1},
|
{ 0, 0.1f, 0.9f, 1, 0, 0.1f, 0.9f, 1},
|
||||||
{ 0, 0, 0, 0, 1, 1, 1, 1},
|
{ 0, 0, 0, 0, 1, 1, 1, 1},
|
||||||
@ -236,22 +246,21 @@ TEST(Objective, DeclareUnifiedTest(TweedieRegressionGPair)) {
|
|||||||
{ 1, 1.09f, 2.24f, 2.45f, 0, 0.10f, 1.33f, 1.55f},
|
{ 1, 1.09f, 2.24f, 2.45f, 0, 0.10f, 1.33f, 1.55f},
|
||||||
{0.89f, 0.98f, 2.02f, 2.21f, 1, 1.08f, 2.11f, 2.30f});
|
{0.89f, 0.98f, 2.02f, 2.21f, 1, 1.08f, 2.11f, 2.30f});
|
||||||
ASSERT_EQ(obj->DefaultEvalMetric(), std::string{"tweedie-nloglik@1.1"});
|
ASSERT_EQ(obj->DefaultEvalMetric(), std::string{"tweedie-nloglik@1.1"});
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(__CUDACC__)
|
#if defined(__CUDACC__)
|
||||||
TEST(Objective, CPU_vs_CUDA) {
|
TEST(Objective, CPU_vs_CUDA) {
|
||||||
xgboost::GenericParameter lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
GenericParameter lparam = CreateEmptyGenericParam(GPUIDX);
|
||||||
|
|
||||||
xgboost::ObjFunction * obj =
|
ObjFunction * obj =
|
||||||
xgboost::ObjFunction::Create("reg:squarederror", &lparam);
|
ObjFunction::Create("reg:squarederror", &lparam);
|
||||||
xgboost::HostDeviceVector<xgboost::GradientPair> cpu_out_preds;
|
HostDeviceVector<GradientPair> cpu_out_preds;
|
||||||
xgboost::HostDeviceVector<xgboost::GradientPair> cuda_out_preds;
|
HostDeviceVector<GradientPair> cuda_out_preds;
|
||||||
|
|
||||||
constexpr size_t kRows = 400;
|
constexpr size_t kRows = 400;
|
||||||
constexpr size_t kCols = 100;
|
constexpr size_t kCols = 100;
|
||||||
auto ppdmat = xgboost::CreateDMatrix(kRows, kCols, 0, 0);
|
auto ppdmat = CreateDMatrix(kRows, kCols, 0, 0);
|
||||||
xgboost::HostDeviceVector<float> preds;
|
HostDeviceVector<float> preds;
|
||||||
preds.Resize(kRows);
|
preds.Resize(kRows);
|
||||||
auto& h_preds = preds.HostVector();
|
auto& h_preds = preds.HostVector();
|
||||||
for (size_t i = 0; i < h_preds.size(); ++i) {
|
for (size_t i = 0; i < h_preds.size(); ++i) {
|
||||||
@ -285,8 +294,8 @@ TEST(Objective, CPU_vs_CUDA) {
|
|||||||
sgrad += std::pow(h_cpu_out[i].GetGrad() - h_cuda_out[i].GetGrad(), 2);
|
sgrad += std::pow(h_cpu_out[i].GetGrad() - h_cuda_out[i].GetGrad(), 2);
|
||||||
shess += std::pow(h_cpu_out[i].GetHess() - h_cuda_out[i].GetHess(), 2);
|
shess += std::pow(h_cpu_out[i].GetHess() - h_cuda_out[i].GetHess(), 2);
|
||||||
}
|
}
|
||||||
ASSERT_NEAR(sgrad, 0.0f, xgboost::kRtEps);
|
ASSERT_NEAR(sgrad, 0.0f, kRtEps);
|
||||||
ASSERT_NEAR(shess, 0.0f, xgboost::kRtEps);
|
ASSERT_NEAR(shess, 0.0f, kRtEps);
|
||||||
|
|
||||||
delete ppdmat;
|
delete ppdmat;
|
||||||
delete obj;
|
delete obj;
|
||||||
@ -294,11 +303,14 @@ TEST(Objective, CPU_vs_CUDA) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
TEST(Objective, DeclareUnifiedTest(TweedieRegressionBasic)) {
|
TEST(Objective, DeclareUnifiedTest(TweedieRegressionBasic)) {
|
||||||
xgboost::GenericParameter lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
GenericParameter lparam = CreateEmptyGenericParam(GPUIDX);
|
||||||
std::vector<std::pair<std::string, std::string>> args;
|
std::vector<std::pair<std::string, std::string>> args;
|
||||||
xgboost::ObjFunction * obj = xgboost::ObjFunction::Create("reg:tweedie", &lparam);
|
std::unique_ptr<ObjFunction> obj {
|
||||||
|
ObjFunction::Create("reg:tweedie", &lparam)
|
||||||
|
};
|
||||||
|
|
||||||
obj->Configure(args);
|
obj->Configure(args);
|
||||||
|
CheckConfigReload(obj, "reg:tweedie");
|
||||||
|
|
||||||
// test label validation
|
// test label validation
|
||||||
EXPECT_ANY_THROW(CheckObjFunction(obj, {0}, {-1}, {1}, {0}, {0}))
|
EXPECT_ANY_THROW(CheckObjFunction(obj, {0}, {-1}, {1}, {0}, {0}))
|
||||||
@ -310,25 +322,23 @@ TEST(Objective, DeclareUnifiedTest(TweedieRegressionBasic)) {
|
|||||||
EXPECT_NEAR(obj->ProbToMargin(0.9f), -0.10f, 0.01f);
|
EXPECT_NEAR(obj->ProbToMargin(0.9f), -0.10f, 0.01f);
|
||||||
|
|
||||||
// test PredTransform
|
// test PredTransform
|
||||||
xgboost::HostDeviceVector<xgboost::bst_float> io_preds = {0, 0.1f, 0.5f, 0.9f, 1};
|
HostDeviceVector<bst_float> io_preds = {0, 0.1f, 0.5f, 0.9f, 1};
|
||||||
std::vector<xgboost::bst_float> out_preds = {1, 1.10f, 1.64f, 2.45f, 2.71f};
|
std::vector<bst_float> out_preds = {1, 1.10f, 1.64f, 2.45f, 2.71f};
|
||||||
obj->PredTransform(&io_preds);
|
obj->PredTransform(&io_preds);
|
||||||
auto& preds = io_preds.HostVector();
|
auto& preds = io_preds.HostVector();
|
||||||
for (int i = 0; i < static_cast<int>(io_preds.Size()); ++i) {
|
for (int i = 0; i < static_cast<int>(io_preds.Size()); ++i) {
|
||||||
EXPECT_NEAR(preds[i], out_preds[i], 0.01f);
|
EXPECT_NEAR(preds[i], out_preds[i], 0.01f);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// CoxRegression not implemented in GPU code, no need for testing.
|
// CoxRegression not implemented in GPU code, no need for testing.
|
||||||
#if !defined(__CUDACC__)
|
#if !defined(__CUDACC__)
|
||||||
TEST(Objective, CoxRegressionGPair) {
|
TEST(Objective, CoxRegressionGPair) {
|
||||||
xgboost::GenericParameter lparam = xgboost::CreateEmptyGenericParam(GPUIDX);
|
GenericParameter lparam = CreateEmptyGenericParam(GPUIDX);
|
||||||
std::vector<std::pair<std::string, std::string>> args;
|
std::vector<std::pair<std::string, std::string>> args;
|
||||||
xgboost::ObjFunction * obj =
|
std::unique_ptr<ObjFunction> obj {
|
||||||
xgboost::ObjFunction::Create("survival:cox", &lparam);
|
ObjFunction::Create("survival:cox", &lparam)
|
||||||
|
};
|
||||||
|
|
||||||
obj->Configure(args);
|
obj->Configure(args);
|
||||||
CheckObjFunction(obj,
|
CheckObjFunction(obj,
|
||||||
@ -337,7 +347,7 @@ TEST(Objective, CoxRegressionGPair) {
|
|||||||
{ 1, 1, 1, 1, 1, 1, 1, 1},
|
{ 1, 1, 1, 1, 1, 1, 1, 1},
|
||||||
{ 0, 0, 0, -0.799f, -0.788f, -0.590f, 0.910f, 1.006f},
|
{ 0, 0, 0, -0.799f, -0.788f, -0.590f, 0.910f, 1.006f},
|
||||||
{ 0, 0, 0, 0.160f, 0.186f, 0.348f, 0.610f, 0.639f});
|
{ 0, 0, 0, 0.160f, 0.186f, 0.348f, 0.610f, 0.639f});
|
||||||
|
|
||||||
delete obj;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
} // namespace xgboost
|
||||||
|
|||||||
@ -77,7 +77,7 @@ TEST(Tree, Load) {
|
|||||||
std::unique_ptr<dmlc::Stream> fi(dmlc::Stream::Create(tmp_file.c_str(), "r"));
|
std::unique_ptr<dmlc::Stream> fi(dmlc::Stream::Create(tmp_file.c_str(), "r"));
|
||||||
|
|
||||||
xgboost::RegTree tree;
|
xgboost::RegTree tree;
|
||||||
tree.Load(fi.get());
|
tree.LoadModel(fi.get());
|
||||||
EXPECT_EQ(tree.GetDepth(1), 1);
|
EXPECT_EQ(tree.GetDepth(1), 1);
|
||||||
EXPECT_EQ(tree[0].SplitCond(), 0.5f);
|
EXPECT_EQ(tree[0].SplitCond(), 0.5f);
|
||||||
EXPECT_EQ(tree[0].SplitIndex(), 5);
|
EXPECT_EQ(tree[0].SplitIndex(), 5);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user