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 
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  */
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);
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) {
76  }
77  if ((i + 1) == n) {
78  break;
79  }
80  }
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  }
116  }
117 
118  /*\brief Get a global instance. */
120  static TrainingObserver observer;
121  return observer;
122  }
123 };
124 } // namespace xgboost
125 #endif // XGBOOST_COMMON_OBSERVER_H_
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
void Observe(RegTree const *p_tree)
Definition: observer.h:60
#define OBSERVER_PRINT
Definition: observer.h:24
Definition: host_device_vector.h:86
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:253
void Update(int32_t iter) const
Definition: observer.h:45
A device-and-host vector abstraction layer.
static TrainingObserver & Instance()
Definition: observer.h:119
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
static void Dump(Json json, std::string *out)
void Observe(HostDeviceVector< T > *vec, std::string name, size_t n=std::numeric_limits< std::size_t >::max()) const
Definition: observer.h:92
void Observe(Args const &args) const
Definition: observer.h:109
std::vector< T > & HostVector()
void Observe(RegTree const &tree)
Definition: observer.h:50
namespace of xgboost
Definition: base.h:102
void Observe(HostDeviceVector< T > const &vec, std::string name, size_t n=std::numeric_limits< std::size_t >::max()) const
Definition: observer.h:85
defines configuration macros of xgboost.
JsonObject Object
Definition: json.h:532
void SaveModel(Json *out) const override
saves the model config to a json object
Definition: observer.h:37
#define OBSERVER_ENDL
Definition: observer.h:25
void Observe(const Parameter &p, std::string name) const
Definition: observer.h:102
Data structure representing JSON format.
Definition: json.h:326
#define XGBOOST_EXPECT(cond, ret)
Definition: base.h:75
macro for using C++11 enum class as DMLC parameter
#define OBSERVER_NEWLINE
Definition: observer.h:26
model structure for tree