forpy 2

types.h
Go to the documentation of this file.
1 /* Author: Christoph Lassner. */
2 #pragma once
3 #ifndef FORPY_TYPES_H_
4 #define FORPY_TYPES_H_
5
6 #include <Eigen/Dense>
7 #include <map>
8 #include <memory>
9 #include <numeric>
10 #include <utility>
11 #include <vector>
12
13 #include "./util/hash.h"
14 #include "./util/storage.h"
15
16 namespace forpy {
17
21 template <typename T>
22 struct Name {
23 static std::string value() { return "unknown"; };
24 };
25 template <>
26 struct Name<double> {
27 static std::string value() { return "d"; }
28 };
29 template <>
30 struct Name<float> {
31 static std::string value() { return "f"; }
32 };
33 template <>
34 struct Name<uint> {
35 static std::string value() { return "ui"; }
36 };
37 template <>
38 struct Name<uint8_t> {
39 static std::string value() { return "ui8"; }
40 };
41 template <>
42 struct Name<int16_t> {
43 static std::string value() { return "i16"; }
44 };
45 template <>
46 struct Name<int> {
47 static std::string value() { return "i"; }
48 };
49
51 template <typename DT>
52 using Mat = Eigen::Matrix<DT, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
53
55 template <typename DT>
56 using MatCM =
57 Eigen::Matrix<DT, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>;
58
60 template <typename DT>
61 using MatCRef = Eigen::Ref<const Mat<DT>>;
62
64 template <typename DT>
65 using MatCMCRef = Eigen::Ref<const MatCM<DT>>;
66
68 template <typename DT>
69 using MatRef = Eigen::Ref<Mat<DT>>;
70
72 template <typename DT>
73 using Vec = Eigen::Matrix<DT, Eigen::Dynamic, 1, Eigen::ColMajor>;
74
75 template <typename DT>
76 using VecRM = Eigen::Matrix<DT, 1, Eigen::Dynamic, Eigen::RowMajor>;
77
78 template <typename DT>
79 using VecRef = Eigen::Ref<Vec<DT>>;
80
81 template <typename DT>
82 using VecRMRef = Eigen::Ref<VecRM<DT>>;
83
84 template <typename DT>
85 using VecCRef = Eigen::Ref<const Vec<DT>>;
86
87 template <typename DT>
88 using VecCMap =
89 Eigen::Map<const Eigen::Matrix<DT, 1, Eigen::Dynamic, Eigen::RowMajor>,
90 Eigen::Unaligned, Eigen::InnerStride<>>;
91
95 enum class ECompletionLevel {
97 Node,
100 Level,
102 Complete
103 };
104
106 typedef size_t id_t;
107
108 typedef std::function<id_t(const Data<MatCRef> &, const id_t &,
109 const std::function<void(void *)> &)>
110 node_predf;
111
113 typedef unsigned int uint;
114
116 enum class EThresholdSelection { LessEqOnly, GreaterOnly, Both };
117
118 template <typename FT>
119 struct SplitOptRes {
120 id_t split_idx;
121 FT thresh;
122 float gain;
123 bool valid;
124
125 inline friend std::ostream &operator<<(std::ostream &stream,
126 const SplitOptRes<FT> &self) {
127 stream << "forpy::SplitOptRes_X[<=" << self.thresh
128 << "; gain: " << self.gain << ", valid: " << self.valid << "]";
129 return stream;
130 };
131
132 bool operator==(SplitOptRes<FT> const &rhs) const {
133 return split_idx == rhs.split_idx && thresh == rhs.thresh &&
134 gain == rhs.gain && valid == rhs.valid;
135 }
136 };
137 typedef mu::variant<SplitOptRes<float>, SplitOptRes<double>, SplitOptRes<uint>,
138 SplitOptRes<uint8_t>>
139 OptSplitV;
140
141 typedef std::pair<id_t, id_t> interv_t;
142
152 struct TodoMark {
153 inline TodoMark() : node_id(0), depth(0){};
154 inline TodoMark(std::shared_ptr<std::vector<id_t>> sample_ids,
155 const interv_t &interv, const id_t &node_id,
156 const uint &depth)
157 : sample_ids(sample_ids),
158 interv(interv),
159 node_id(node_id),
160 depth(depth){};
161 std::shared_ptr<std::vector<id_t>> sample_ids;
162 interv_t interv;
163 id_t node_id;
164 uint depth;
165 inline bool operator==(TodoMark const &rhs) const {
166 return node_id == rhs.node_id && depth == rhs.depth &&
167 interv == rhs.interv && *sample_ids == *(rhs.sample_ids);
168 }
169 inline friend std::ostream &operator<<(std::ostream &stream,
170 const TodoMark &self) {
171 stream << "forpy::TodoMark[node_id: " << self.node_id << ", depth "
172 << self.depth << "]";
173 return stream;
174 };
175 MOVE_ASSIGN(TodoMark);
176
177 private:
178 friend class cereal::access;
179 template <class Archive>
180 void serialize(Archive &ar, const uint &) {
181 ar(CEREAL_NVP(sample_ids), CEREAL_NVP(interv), CEREAL_NVP(node_id),
182 CEREAL_NVP(depth));
183 };
184 DISALLOW_COPY_AND_ASSIGN(TodoMark);
185 };
186
187 typedef std::pair<ptrdiff_t, ptrdiff_t> regint_t;
188
190 typedef std::vector<std::pair<std::shared_ptr<std::vector<size_t>>,
191 std::shared_ptr<std::vector<float> const>>>
192 usage_map_t;
193
202 typedef std::pair<std::shared_ptr<std::vector<id_t>>, id_t> include_pair_t;
203
205 enum class ESearchType { DFS, BFS };
206
208 typedef std::unordered_set<std::vector<size_t>, vector_hasher> proposal_set_t;
209
210 #pragma clang diagnostic push
211 #pragma clang diagnostic ignored "-Wunused-variable"
212 const double GAIN_EPS = 1E-7;
213 #pragma clang diagnostic pop
214 const MatCRef<float> FORPY_ZERO_MATR(Mat<float>::Zero(0, 1));
215 }; // namespace forpy
216 #endif // FORPY_TYPES_H_
forpy::include_pair_t
std::pair< std::shared_ptr< std::vector< id_t > >, id_t > include_pair_t
A pair containing information about newly included samples.
Definition: types.h:202
forpy::TodoMark::depth
uint depth
Definition: types.h:164
forpy::SplitOptRes::gain
float gain
Definition: types.h:122
forpy::SplitOptRes
Definition: types.h:119
forpy::TodoMark::access
friend class cereal::access
Definition: types.h:178
forpy::GAIN_EPS
const double GAIN_EPS
Definition: types.h:212
forpy::Name::value
static std::string value()
Definition: types.h:23
forpy::TodoMark::interv
interv_t interv
Definition: types.h:162
forpy::EThresholdSelection::LessEqOnly
forpy::TodoMark::operator==
bool operator==(TodoMark const &rhs) const
Definition: types.h:165
hash.h
forpy::SplitOptRes::thresh
FT thresh
Definition: types.h:121
forpy::id_t
size_t id_t
Element id type.
Definition: types.h:106
forpy::VecRMRef
Eigen::Ref< VecRM< DT > > VecRMRef
Definition: types.h:82
forpy::ECompletionLevel::Complete
forpy::ESearchType::BFS
forpy::FORPY_ZERO_MATR
const MatCRef< float > FORPY_ZERO_MATR(Mat< float >::Zero(0, 1))
forpy::ECompletionLevel::Level
forpy::TodoMark::MOVE_ASSIGN
MOVE_ASSIGN(TodoMark)
forpy::usage_map_t
std::vector< std::pair< std::shared_ptr< std::vector< size_t > >, std::shared_ptr< std::vector< float > const > > > usage_map_t
Describes how each sample is used for each tree.
Definition: types.h:192
forpy::SplitOptRes::valid
bool valid
Definition: types.h:123
forpy::MatCMCRef
Eigen::Ref< const MatCM< DT > > MatCMCRef
Parameterized const matrix column major matrix ref type.
Definition: types.h:65
forpy::interv_t
std::pair< id_t, id_t > interv_t
Definition: types.h:141
forpy::TodoMark::serialize
void serialize(Archive &ar, const uint &)
Definition: types.h:180
forpy::VecRef
Eigen::Ref< Vec< DT > > VecRef
Definition: types.h:79
forpy::TodoMark::node_id
id_t node_id
Definition: types.h:163
forpy::regint_t
std::pair< ptrdiff_t, ptrdiff_t > regint_t
Definition: types.h:187
forpy::TodoMark::TodoMark
TodoMark(std::shared_ptr< std::vector< id_t >> sample_ids, const interv_t &interv, const id_t &node_id, const uint &depth)
Definition: types.h:154
forpy::ESearchType
ESearchType
Definition: types.h:205
forpy::TodoMark
Stores the parameters for one marked tree node.
Definition: types.h:152
forpy::TodoMark::operator<<
friend std::ostream & operator<<(std::ostream &stream, const TodoMark &self)
Definition: types.h:169
forpy::proposal_set_t
std::unordered_set< std::vector< size_t >, vector_hasher > proposal_set_t
The type of a set of dimension selections.
Definition: types.h:208
forpy::Name< int >::value
static std::string value()
Definition: types.h:47
forpy::SplitOptRes::split_idx
id_t split_idx
Definition: types.h:120
storage.h
forpy::ECompletionLevel::Node
forpy::Name< float >::value
static std::string value()
Definition: types.h:31
forpy::MatCM
Eigen::Matrix< DT, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatCM
Parameterized column major matrix type.
Definition: types.h:57
forpy::MatRef
Eigen::Ref< Mat< DT > > MatRef
Parameterized standard non-const matrix ref type.
Definition: types.h:69
forpy::TodoMark::sample_ids
std::shared_ptr< std::vector< id_t > > sample_ids
Definition: types.h:160
forpy::TodoMark::TodoMark
TodoMark()
Definition: types.h:153
forpy::TodoMark::DISALLOW_COPY_AND_ASSIGN
DISALLOW_COPY_AND_ASSIGN(TodoMark)
forpy::OptSplitV
mu::variant< SplitOptRes< float >, SplitOptRes< double >, SplitOptRes< uint >, SplitOptRes< uint8_t > > OptSplitV
Definition: types.h:139
forpy::EThresholdSelection::GreaterOnly
forpy::EThresholdSelection
EThresholdSelection
Specifies which thresholds should be used for a decision.
Definition: types.h:116
forpy::SplitOptRes::operator==
bool operator==(SplitOptRes< FT > const &rhs) const
Definition: types.h:132
forpy::Name< uint8_t >::value
static std::string value()
Definition: types.h:39
forpy::Name
Struct for translating primitive types to a short name.
Definition: types.h:22
forpy::VecCRef
Eigen::Ref< const Vec< DT > > VecCRef
Definition: types.h:85
forpy::Vec
Eigen::Matrix< DT, Eigen::Dynamic, 1, Eigen::ColMajor > Vec
Definition: types.h:73
forpy::Mat
Eigen::Matrix< DT, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > Mat
Parameterized Matrix type (row major).
Definition: types.h:52
forpy
Definition: fastdprov.h:13
forpy::EThresholdSelection::Both
forpy::Name< uint >::value
static std::string value()
Definition: types.h:35
forpy::vector_hasher
A simple vector<size_t> hasher.
Definition: hash.h:34
forpy::SplitOptRes::operator<<
friend std::ostream & operator<<(std::ostream &stream, const SplitOptRes< FT > &self)
Definition: types.h:125
forpy::Name< int16_t >::value
static std::string value()
Definition: types.h:43
forpy::VecCMap
Eigen::Map< const Eigen::Matrix< DT, 1, Eigen::Dynamic, Eigen::RowMajor >, Eigen::Unaligned, Eigen::InnerStride<> > VecCMap
Definition: types.h:90
forpy::ECompletionLevel
ECompletionLevel
Specifies the completion level for one training step.
Definition: types.h:95
forpy::MatCRef
Eigen::Ref< const Mat< DT > > MatCRef
Parameterized const matrix ref type.
Definition: types.h:61
forpy::uint
unsigned int uint
Convenience typedef for unsigned int.
Definition: types.h:113
forpy::VecRM
Eigen::Matrix< DT, 1, Eigen::Dynamic, Eigen::RowMajor > VecRM
Definition: types.h:76
forpy::ESearchType::DFS
forpy::node_predf
std::function< id_t(const Data< MatCRef > &, const id_t &, const std::function< void(void *)> &)> node_predf
Definition: types.h:110
forpy::Name< double >::value
static std::string value()
Definition: types.h:27