meta_info_extractor.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_CV_META_INFO_EXTRACTOR_HPP
13 #define MLPACK_CORE_CV_META_INFO_EXTRACTOR_HPP
14 
15 #include <type_traits>
16 
17 #include <mlpack/core.hpp>
19 
20 namespace mlpack {
21 namespace cv {
22 
34 template<typename MatType,
35  typename PredictionsType,
36  typename WeightsType,
37  bool DatasetInfo,
38  bool NumClasses>
39 struct TrainForm;
40 
41 #if _MSC_VER <= 1916
42 // Visual Studio 2017 version 15.9 or older.
43 // Due to an internal MSVC compiler bug (MSVC ) we can't use two parameter
44 // packs. So we have to write multiple TrainFormBase forms.
45 template<typename PT, typename WT, typename T1, typename T2>
47 {
48  using PredictionsType = PT;
49  using WeightsType = WT;
50 
51  /* A minimum number of parameters that should be inferred */
52  static const size_t MinNumberOfAdditionalArgs = 1;
53 
54  template<typename Class, typename RT, typename... Ts>
55  using Type = RT(Class::*)(T1, T2, Ts...);
56 };
57 
58 template<typename PT, typename WT, typename T1, typename T2, typename T3>
60 {
61  using PredictionsType = PT;
62  using WeightsType = WT;
63 
64  /* A minimum number of parameters that should be inferred */
65  static const size_t MinNumberOfAdditionalArgs = 1;
66 
67  template<typename Class, typename RT, typename... Ts>
68  using Type = RT(Class::*)(T1, T2, T3, Ts...);
69 };
70 
71 template<typename PT, typename WT, typename T1, typename T2, typename T3,
72  typename T4>
74 {
75  using PredictionsType = PT;
76  using WeightsType = WT;
77 
78  /* A minimum number of parameters that should be inferred */
79  static const size_t MinNumberOfAdditionalArgs = 1;
80 
81  template<typename Class, typename RT, typename... Ts>
82  using Type = RT(Class::*)(T1, T2, T3, T4, Ts...);
83 };
84 
85 template<typename PT, typename WT, typename T1, typename T2, typename T3,
86  typename T4, typename T5>
88 {
89  using PredictionsType = PT;
90  using WeightsType = WT;
91 
92  /* A minimum number of parameters that should be inferred */
93  static const size_t MinNumberOfAdditionalArgs = 1;
94 
95  template<typename Class, typename RT, typename... Ts>
96  using Type = RT(Class::*)(T1, T2, T3, T4, T5, Ts...);
97 };
98 
99 template<typename MT, typename PT>
100 struct TrainForm<MT, PT, void, false, false> : public TrainFormBase4<PT, void,
101  const MT&, const PT&> {};
102 
103 template<typename MT, typename PT>
104 struct TrainForm<MT, PT, void, true, false> : public TrainFormBase5<PT, void,
105  const MT&, const data::DatasetInfo&, const PT&> {};
106 
107 template<typename MT, typename PT, typename WT>
108 struct TrainForm<MT, PT, WT, false, false> : public TrainFormBase5<PT, WT,
109  const MT&, const PT&, const WT&> {};
110 
111 template<typename MT, typename PT, typename WT>
112 struct TrainForm<MT, PT, WT, true, false> : public TrainFormBase6<PT, WT,
113  const MT&, const data::DatasetInfo&, const PT&, const WT&> {};
114 
115 template<typename MT, typename PT>
116 struct TrainForm<MT, PT, void, false, true> : public TrainFormBase5<PT, void,
117  const MT&, const PT&, const size_t> {};
118 
119 template<typename MT, typename PT>
120 struct TrainForm<MT, PT, void, true, true> : public TrainFormBase6<PT, void,
121  const MT&, const data::DatasetInfo&, const PT&, const size_t> {};
122 
123 template<typename MT, typename PT, typename WT>
124 struct TrainForm<MT, PT, WT, false, true> : public TrainFormBase6<PT, WT,
125  const MT&, const PT&, const size_t, const WT&> {};
126 
127 template<typename MT, typename PT, typename WT>
128 struct TrainForm<MT, PT, WT, true, true> : public TrainFormBase7<PT, WT,
129  const MT&, const data::DatasetInfo&, const PT&,
130  const size_t, const WT&> {};
131 #else
132 template<typename PT, typename WT, typename... SignatureParams>
133 struct TrainFormBase
134 {
135  using PredictionsType = PT;
136  using WeightsType = WT;
137 
138  /* A minimum number of parameters that should be inferred */
139  static const size_t MinNumberOfAdditionalArgs = 1;
140 
141  template<typename Class, typename RT, typename... Ts>
142  using Type = RT(Class::*)(SignatureParams..., Ts...);
143 };
144 
145 template<typename MT, typename PT>
146 struct TrainForm<MT, PT, void, false, false> : public TrainFormBase<PT, void,
147  const MT&, const PT&> {};
148 
149 template<typename MT, typename PT>
150 struct TrainForm<MT, PT, void, true, false> : public TrainFormBase<PT, void,
151  const MT&, const data::DatasetInfo&, const PT&> {};
152 
153 template<typename MT, typename PT, typename WT>
154 struct TrainForm<MT, PT, WT, false, false> : public TrainFormBase<PT, WT,
155  const MT&, const PT&, const WT&> {};
156 
157 template<typename MT, typename PT, typename WT>
158 struct TrainForm<MT, PT, WT, true, false> : public TrainFormBase<PT, WT,
159  const MT&, const data::DatasetInfo&, const PT&, const WT&> {};
160 
161 template<typename MT, typename PT>
162 struct TrainForm<MT, PT, void, false, true> : public TrainFormBase<PT, void,
163  const MT&, const PT&, const size_t> {};
164 
165 template<typename MT, typename PT>
166 struct TrainForm<MT, PT, void, true, true> : public TrainFormBase<PT, void,
167  const MT&, const data::DatasetInfo&, const PT&, const size_t> {};
168 
169 template<typename MT, typename PT, typename WT>
170 struct TrainForm<MT, PT, WT, false, true> : public TrainFormBase<PT, WT,
171  const MT&, const PT&, const size_t, const WT&> {};
172 
173 template<typename MT, typename PT, typename WT>
174 struct TrainForm<MT, PT, WT, true, true> : public TrainFormBase<PT, WT,
175  const MT&, const data::DatasetInfo&, const PT&,
176  const size_t, const WT&> {};
177 #endif
178 
179 /* A struct for indication that a right method form can't be found */
181 {
182  using PredictionsType = void*;
183  using WeightsType = void*;
184 };
185 
196 template<typename MLAlgorithm,
197  template<class, template<class...> class, size_t> class... HMFs>
199 
200 template<typename MLAlgorithm,
201  template<class, template<class...> class, size_t> class HasMethodForm,
202  template<class, template<class...> class, size_t> class... HMFs>
203 struct SelectMethodForm<MLAlgorithm, HasMethodForm, HMFs...>
204 {
205  template<typename... Forms>
206  class From
207  {
208  /* Declaration and definition of Implementation for the case when
209  * RemainingForms are empty */
210  template<typename... RemainingForms>
211  struct Implementation
212  {
213  using NextSMF = SelectMethodForm<MLAlgorithm, HMFs...>;
214  using Type = typename NextSMF::template From<Forms...>::Type;
215  };
216 
217  /* The case when there is at least one remaining form */
218  template<typename Form, typename... RemainingForms>
219  struct Implementation<Form, RemainingForms...>
220  {
221  using Type = typename std::conditional<
222  HasMethodForm<MLAlgorithm, Form::template Type,
223  Form::MinNumberOfAdditionalArgs>::value,
224  Form,
225  typename Implementation<RemainingForms...>::Type>::type;
226  };
227 
228  public:
229  using Type = typename Implementation<Forms...>::Type;
230  };
231 };
232 
233 template<typename MLAlgorithm>
234 struct SelectMethodForm<MLAlgorithm>
235 {
236  template<typename... Forms>
237  struct From
238  {
240  };
241 };
242 
268 template<typename MLAlgorithm,
269  typename MT = arma::mat,
270  typename PT = arma::Row<size_t>,
271  typename WT = arma::rowvec>
273 {
274  /* Defining type functions that check presence of Train methods of a given
275  * form. Defining such functions for templated and non-templated Train
276  * methods. */
277  HAS_METHOD_FORM(Train, HasTrain);
278  HAS_METHOD_FORM(template Train<>, HasTTrain);
279  HAS_METHOD_FORM(template Train<const MT&>, HasMTrain);
280  HAS_METHOD_FORM(SINGLE_ARG(template Train<const MT&, const PT&>), HasMPTrain);
281  HAS_METHOD_FORM(SINGLE_ARG(template Train<const MT&, const PT&, const WT&>),
282  HasMPWTrain);
283 
284  /* Forms of Train for regression algorithms */
288 
289  /* Forms of Train for classification algorithms */
292 
293  /* Forms of Train with weights for regression algorithms */
297 
298  /* Forms of Train with weights for classification algorithms */
301 
302  /* A short alias for a type function that selects a right method form */
303  template<typename... MethodForms>
304  using Select = typename SelectMethodForm<MLAlgorithm, HasTrain, HasTTrain,
305  HasMTrain, HasMPTrain, HasMPWTrain>::template From<MethodForms...>;
306 
307  /* An indication whether a method form is selected */
308  template<typename... MFs /* MethodForms */>
309  using Selects = typename std::conditional<
310  std::is_same<typename Select<MFs...>::Type, NotFoundMethodForm>::value,
311  std::false_type, std::true_type>::type;
312 
313  public:
318  using PredictionsType =
319  typename Select<TF1, TF2, TF3, TF4, TF5>::Type::PredictionsType;
320 
325  using WeightsType =
326  typename Select<WTF1, WTF2, WTF3, WTF4, WTF5>::Type::WeightsType;
327 
332  static const bool IsSupported = !std::is_same<PredictionsType, void*>::value;
333 
337  static const bool SupportsWeights = !std::is_same<WeightsType, void*>::value;
338 
342  static const bool TakesDatasetInfo = Selects<TF5>::value;
343 
347  static const bool TakesNumClasses = Selects<TF4, TF5>::value;
348 };
349 
350 } // namespace cv
351 } // namespace mlpack
352 
353 #endif
#define HAS_METHOD_FORM(METHOD, NAME)
HAS_METHOD_FORM generates a template that allows a compile time check for whether a given class has a...
.hpp
Definition: add_to_po.hpp:21
MetaInfoExtractor is a tool for extracting meta information about a given machine learning algorithm...
A type function that selects a right method form.
RT(Class::*)(const MT &, const data::DatasetInfo &, const PT &, const size_t, const WT &, Ts...) Type
#define SINGLE_ARG(...)
DatasetMapper< data::IncrementPolicy > DatasetInfo
A wrapper struct for holding a Train form.
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
RT(Class::*)(const MT &, const PT &, const size_t, Ts...) Type
static const size_t MinNumberOfAdditionalArgs
RT(Class::*)(const MT &, const data::DatasetInfo &, const PT &, const size_t, Ts...) Type