| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#ifndef XGBOOST_LEARNER_H_ |
| 9 |
#define XGBOOST_LEARNER_H_ |
| 10 |
|
| 11 |
#include <dmlc/io.h> |
| 12 |
#include <xgboost/base.h> |
| 13 |
#include <xgboost/context.h> |
| 14 |
#include <xgboost/gradient.h> |
| 15 |
#include <xgboost/linalg.h> |
| 16 |
#include <xgboost/metric.h> |
| 17 |
#include <xgboost/model.h> |
| 18 |
#include <xgboost/span.h> |
| 19 |
#include <xgboost/task.h> |
| 20 |
|
| 21 |
#include <algorithm> |
| 22 |
#include <cstdint> |
| 23 |
#include <map> |
| 24 |
#include <memory> |
| 25 |
#include <string> |
| 26 |
#include <utility> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
namespace xgboost { |
| 30 |
class FeatureMap; |
| 31 |
class Metric; |
| 32 |
class GradientBooster; |
| 33 |
class ObjFunction; |
| 34 |
class DMatrix; |
| 35 |
class Json; |
| 36 |
struct XGBAPIThreadLocalEntry; |
| 37 |
template <typename T> |
| 38 |
class HostDeviceVector; |
| 39 |
class CatContainer; |
| 40 |
|
| 41 |
enum class PredictionType : std::uint8_t { |
| 42 |
kValue = 0, |
| 43 |
kMargin = 1, |
| 44 |
kContribution = 2, |
| 45 |
kApproxContribution = 3, |
| 46 |
kInteraction = 4, |
| 47 |
kApproxInteraction = 5, |
| 48 |
kLeaf = 6 |
| 49 |
}; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
class Learner : public Model, public Configurable, public dmlc::Serializable { |
| 68 |
public: |
| 69 |
~Learner() override; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
virtual void Configure() = 0; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
virtual void UpdateOneIter(std::int32_t iter, std::shared_ptr<DMatrix> train) = 0; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
virtual void BoostOneIter(std::int32_t iter, std::shared_ptr<DMatrix> train, |
| 91 |
GradientContainer* in_gpair) = 0; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
virtual std::string EvalOneIter(int iter, |
| 100 |
const std::vector<std::shared_ptr<DMatrix>>& data_sets, |
| 101 |
const std::vector<std::string>& data_names) = 0; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
virtual void Predict(std::shared_ptr<DMatrix> data, bool output_margin, |
| 116 |
HostDeviceVector<bst_float>* out_preds, bst_layer_t layer_begin, |
| 117 |
bst_layer_t layer_end, bool training = false, bool pred_leaf = false, |
| 118 |
bool pred_contribs = false, bool approx_contribs = false, |
| 119 |
bool pred_interactions = false) = 0; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
virtual void InplacePredict(std::shared_ptr<DMatrix> p_m, PredictionType type, float missing, |
| 132 |
HostDeviceVector<float>** out_preds, bst_layer_t layer_begin, |
| 133 |
bst_layer_t layer_end) = 0; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
virtual void CalcFeatureScore(std::string const& importance_type, |
| 139 |
common::Span<int32_t const> trees, |
| 140 |
std::vector<bst_feature_t>* features, |
| 141 |
std::vector<float>* scores) = 0; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
virtual int32_t BoostedRounds() const = 0; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
virtual std::uint32_t Groups() const = 0; |
| 151 |
|
| 152 |
void LoadModel(Json const& in) override = 0; |
| 153 |
void SaveModel(Json* out) const override = 0; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
virtual void SetParams(Args const& args) = 0; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
virtual void SetParam(const std::string& key, const std::string& value) = 0; |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
virtual bst_feature_t GetNumFeature() const = 0; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
virtual void SetAttr(const std::string& key, const std::string& value) = 0; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
virtual bool GetAttr(const std::string& key, std::string* out) const = 0; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
virtual bool DelAttr(const std::string& key) = 0; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
virtual std::vector<std::string> GetAttrNames() const = 0; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
virtual void SetFeatureNames(std::vector<std::string> const& fn) = 0; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
virtual void GetFeatureNames(std::vector<std::string>* fn) const = 0; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
virtual void SetFeatureTypes(std::vector<std::string> const& ft) = 0; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
virtual void GetFeatureTypes(std::vector<std::string>* ft) const = 0; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
[[nodiscard]] virtual CatContainer const* Cats() const = 0; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
virtual Learner* Slice(bst_layer_t begin, bst_layer_t end, bst_layer_t step, |
| 240 |
bool* out_of_bound) = 0; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
virtual std::vector<std::string> DumpModel(const FeatureMap& fmap, |
| 249 |
bool with_stats, |
| 250 |
std::string format) = 0; |
| 251 |
|
| 252 |
virtual XGBAPIThreadLocalEntry& GetThreadLocal() const = 0; |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
virtual void Reset() = 0; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
static Learner* Create(const std::vector<std::shared_ptr<DMatrix> >& cache_data); |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
virtual Context const* Ctx() const = 0; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
virtual const std::map<std::string, std::string>& GetConfigurationArguments() const = 0; |
| 272 |
|
| 273 |
protected: |
| 274 |
|
| 275 |
std::unique_ptr<ObjFunction> obj_; |
| 276 |
|
| 277 |
std::unique_ptr<GradientBooster> gbm_; |
| 278 |
|
| 279 |
std::vector<std::unique_ptr<Metric> > metrics_; |
| 280 |
|
| 281 |
Context ctx_; |
| 282 |
}; |
| 283 |
|
| 284 |
struct LearnerModelParamLegacy; |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
enum class MultiStrategy : std::int32_t { |
| 290 |
kOneOutputPerTree = 0, |
| 291 |
kMultiOutputTree = 1, |
| 292 |
}; |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
struct LearnerModelParam { |
| 298 |
private: |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
linalg::Vector<float> base_score_; |
| 307 |
|
| 308 |
LearnerModelParam(LearnerModelParamLegacy const& user_param, ObjInfo t, |
| 309 |
MultiStrategy multi_strategy); |
| 310 |
|
| 311 |
public: |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
bst_feature_t num_feature{0}; |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
std::uint32_t num_output_group{0}; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
ObjInfo task{ObjInfo::kRegression}; |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
MultiStrategy multi_strategy{MultiStrategy::kOneOutputPerTree}; |
| 328 |
|
| 329 |
LearnerModelParam() = default; |
| 330 |
LearnerModelParam(Context const* ctx, LearnerModelParamLegacy const& user_param, |
| 331 |
linalg::Vector<float> base_score, ObjInfo t, MultiStrategy multi_strategy); |
| 332 |
|
| 333 |
LearnerModelParam(bst_feature_t n_features, linalg::Vector<float> base_score, |
| 334 |
std::uint32_t n_groups, bst_target_t n_targets, MultiStrategy multi_strategy) |
| 335 |
: base_score_{std::move(base_score)}, |
| 336 |
num_feature{n_features}, |
| 337 |
num_output_group{std::max(n_groups, n_targets)}, |
| 338 |
multi_strategy{multi_strategy} {} |
| 339 |
|
| 340 |
linalg::VectorView<float const> BaseScore(Context const* ctx) const; |
| 341 |
[[nodiscard]] linalg::VectorView<float const> BaseScore(DeviceOrd device) const; |
| 342 |
|
| 343 |
void Copy(LearnerModelParam const& that); |
| 344 |
[[nodiscard]] bool IsVectorLeaf() const noexcept { |
| 345 |
return multi_strategy == MultiStrategy::kMultiOutputTree; |
| 346 |
} |
| 347 |
[[nodiscard]] bst_target_t OutputLength() const noexcept { return this->num_output_group; } |
| 348 |
[[nodiscard]] bst_target_t LeafLength() const noexcept { |
| 349 |
return this->IsVectorLeaf() ? this->OutputLength() : 1; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
[[nodiscard]] bool Initialized() const { return num_feature != 0 && num_output_group != 0; } |
| 354 |
}; |
| 355 |
|
| 356 |
} |
| 357 |
#endif |