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