xgboost

observer.h
Go to the documentation of this file.
1
5 #ifndef XGBOOST_COMMON_OBSERVER_H_
6 #define XGBOOST_COMMON_OBSERVER_H_
7
8 #include <iostream>
9 #include <limits>
10 #include <string>
11 #include <vector>
12
13 #include "xgboost/host_device_vector.h"
14 #include "xgboost/parameter.h"
15 #include "xgboost/json.h"
16 #include "xgboost/base.h"
17 #include "xgboost/tree_model.h"
18
19 #if defined(XGBOOST_STRICT_R_MODE) && XGBOOST_STRICT_R_MODE == 1
20 #define OBSERVER_PRINT LOG(INFO)
21 #define OBSERVER_ENDL ""
22 #define OBSERVER_NEWLINE ""
23 #else // defined(XGBOOST_STRICT_R_MODE) && XGBOOST_STRICT_R_MODE == 1
24 #define OBSERVER_PRINT std::cout << std::setprecision(17)
25 #define OBSERVER_ENDL std::endl
26 #define OBSERVER_NEWLINE "\n"
27 #endif // defined(XGBOOST_STRICT_R_MODE) && XGBOOST_STRICT_R_MODE == 1
28
29 namespace xgboost {
30 /*\brief An observer for logging internal data structures.
31 *
32 * This class is designed to be `diff` tool friendly, which means it uses plain
33 * `std::cout` for printing to avoid the time information emitted by `LOG(DEBUG)` or
34 * similiar facilities. Exception: use `LOG(INFO)` for the R package, to comply
35 * with CRAN policy.
36 */
37 class TrainingObserver {
38 #if defined(XGBOOST_USE_DEBUG_OUTPUT)
39 bool constexpr static kObserve {true};
40 #else
41 bool constexpr static kObserve {false};
42 #endif // defined(XGBOOST_USE_DEBUG_OUTPUT)
43
44 public:
45 void Update(int32_t iter) const {
46 if (XGBOOST_EXPECT(!kObserve, true)) { return; }
47 OBSERVER_PRINT << "Iter: " << iter << OBSERVER_ENDL;
48 }
49 /*\brief Observe tree. */
50 void Observe(RegTree const& tree) {
51 if (XGBOOST_EXPECT(!kObserve, true)) { return; }
52 OBSERVER_PRINT << "Tree:" << OBSERVER_ENDL;
53 Json j_tree {Object()};
54 tree.SaveModel(&j_tree);
55 std::string str;
56 Json::Dump(j_tree, &str, true);
57 OBSERVER_PRINT << str << OBSERVER_ENDL;
58 }
59 /*\brief Observe tree. */
60 void Observe(RegTree const* p_tree) {
61 if (XGBOOST_EXPECT(!kObserve, true)) { return; }
62 auto const& tree = *p_tree;
63 this->Observe(tree);
64 }
65 /*\brief Observe data hosted by `std::vector'. */
66 template <typename T>
67 void Observe(std::vector<T> const& h_vec, std::string name,
68 size_t n = std::numeric_limits<std::size_t>::max()) const {
69 if (XGBOOST_EXPECT(!kObserve, true)) { return; }
70 OBSERVER_PRINT << "Procedure: " << name << OBSERVER_ENDL;
71
72 for (size_t i = 0; i < h_vec.size(); ++i) {
73 OBSERVER_PRINT << h_vec[i] << ", ";
74 if (i % 8 == 0) {
75 OBSERVER_PRINT << OBSERVER_NEWLINE;
76 }
77 if ((i + 1) == n) {
78 break;
79 }
80 }
81 OBSERVER_PRINT << OBSERVER_ENDL;
82 }
83 /*\brief Observe data hosted by `HostDeviceVector'. */
84 template <typename T>
85 void Observe(HostDeviceVector<T> const& vec, std::string name,
86 size_t n = std::numeric_limits<std::size_t>::max()) const {
87 if (XGBOOST_EXPECT(!kObserve, true)) { return; }
88 auto const& h_vec = vec.HostVector();
89 this->Observe(h_vec, name, n);
90 }
91 template <typename T>
92 void Observe(HostDeviceVector<T>* vec, std::string name,
93 size_t n = std::numeric_limits<std::size_t>::max()) const {
94 if (XGBOOST_EXPECT(!kObserve, true)) { return; }
95 this->Observe(*vec, name, n);
96 }
97
98 /*\brief Observe objects with `XGBoostParamer' type. */
99 template <typename Parameter,
100 typename std::enable_if<
101 std::is_base_of<XGBoostParameter<Parameter>, Parameter>::value>::type* = nullptr>
102 void Observe(const Parameter &p, std::string name) const {
103 if (XGBOOST_EXPECT(!kObserve, true)) { return; }
104
105 Json obj {toJson(p)};
106 OBSERVER_PRINT << "Parameter: " << name << ":\n" << obj << OBSERVER_ENDL;
107 }
108 /*\brief Observe parameters provided by users. */
109 void Observe(Args const& args) const {
110 if (XGBOOST_EXPECT(!kObserve, true)) { return; }
111
112 for (auto kv : args) {
113 OBSERVER_PRINT << kv.first << ": " << kv.second << OBSERVER_NEWLINE;
114 }
115 OBSERVER_PRINT << OBSERVER_ENDL;
116 }
117
118 /*\brief Get a global instance. */
119 static TrainingObserver& Instance() {
120 static TrainingObserver observer;
121 return observer;
122 }
123 };
124 } // namespace xgboost
125 #endif // XGBOOST_COMMON_OBSERVER_H_
xgboost::TrainingObserver::Observe
void Observe(std::vector< T > const &h_vec, std::string name, size_t n=std::numeric_limits< std::size_t >::max()) const
Definition: observer.h:67
xgboost::TrainingObserver::Observe
void Observe(RegTree const *p_tree)
Definition: observer.h:60
OBSERVER_PRINT
#define OBSERVER_PRINT
Definition: observer.h:24
xgboost::HostDeviceVector
Definition: host_device_vector.h:85
xgboost::Args
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:245
xgboost::TrainingObserver::Update
void Update(int32_t iter) const
Definition: observer.h:45
host_device_vector.h
A device-and-host vector abstraction layer.
xgboost::TrainingObserver::Instance
static TrainingObserver & Instance()
Definition: observer.h:119
xgboost::RegTree
define regression tree to be the most common tree model. This is the data structure used in xgboost&#39;s...
Definition: tree_model.h:106
json.h
xgboost::TrainingObserver::Observe
void Observe(HostDeviceVector< T > *vec, std::string name, size_t n=std::numeric_limits< std::size_t >::max()) const
Definition: observer.h:92
xgboost::TrainingObserver::Observe
void Observe(Args const &args) const
Definition: observer.h:109
xgboost::HostDeviceVector::HostVector
std::vector< T > & HostVector()
xgboost::TrainingObserver::Observe
void Observe(RegTree const &tree)
Definition: observer.h:50
xgboost
namespace of xgboost
Definition: base.h:102
xgboost::TrainingObserver::Observe
void Observe(HostDeviceVector< T > const &vec, std::string name, size_t n=std::numeric_limits< std::size_t >::max()) const
Definition: observer.h:85
base.h
defines configuration macros of xgboost.
xgboost::Object
JsonObject Object
Definition: json.h:538
xgboost::RegTree::SaveModel
void SaveModel(Json *out) const override
saves the model config to a json object
xgboost::TrainingObserver
Definition: observer.h:37
OBSERVER_ENDL
#define OBSERVER_ENDL
Definition: observer.h:25
xgboost::TrainingObserver::Observe
void Observe(const Parameter &p, std::string name) const
Definition: observer.h:102
xgboost::Json
Data structure representing JSON format.
Definition: json.h:326
XGBOOST_EXPECT
#define XGBOOST_EXPECT(cond, ret)
Definition: base.h:75
xgboost::Json::Dump
static void Dump(Json json, std::ostream *stream, bool pretty=ConsoleLogger::ShouldLog(ConsoleLogger::LogVerbosity::kDebug))
Dump json into stream.
parameter.h
macro for using C++11 enum class as DMLC parameter
OBSERVER_NEWLINE
#define OBSERVER_NEWLINE
Definition: observer.h:26
tree_model.h
model structure for tree