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 template<typename PT, typename WT, typename... SignatureParams>
43 {
44  using PredictionsType = PT;
45  using WeightsType = WT;
46 
47  /* A minimum number of parameters that should be inferred */
48  static const size_t MinNumberOfAdditionalArgs = 1;
49 
50  template<typename Class, typename RT, typename... Ts>
51  using Type = RT(Class::*)(SignatureParams..., Ts...);
52 };
53 
54 template<typename MT, typename PT>
55 struct TrainForm<MT, PT, void, false, false> : public TrainFormBase<PT, void,
56  const MT&, const PT&> {};
57 
58 template<typename MT, typename PT>
59 struct TrainForm<MT, PT, void, true, false> : public TrainFormBase<PT, void,
60  const MT&, const data::DatasetInfo&, const PT&> {};
61 
62 template<typename MT, typename PT, typename WT>
63 struct TrainForm<MT, PT, WT, false, false> : public TrainFormBase<PT, WT,
64  const MT&, const PT&, const WT&> {};
65 
66 template<typename MT, typename PT, typename WT>
67 struct TrainForm<MT, PT, WT, true, false> : public TrainFormBase<PT, WT,
68  const MT&, const data::DatasetInfo&, const PT&, const WT&> {};
69 
70 template<typename MT, typename PT>
71 struct TrainForm<MT, PT, void, false, true> : public TrainFormBase<PT, void,
72  const MT&, const PT&, const size_t> {};
73 
74 template<typename MT, typename PT>
75 struct TrainForm<MT, PT, void, true, true> : public TrainFormBase<PT, void,
76  const MT&, const data::DatasetInfo&, const PT&, const size_t> {};
77 
78 template<typename MT, typename PT, typename WT>
79 struct TrainForm<MT, PT, WT, false, true> : public TrainFormBase<PT, WT,
80  const MT&, const PT&, const size_t, const WT&> {};
81 
82 template<typename MT, typename PT, typename WT>
83 struct TrainForm<MT, PT, WT, true, true> : public TrainFormBase<PT, WT,
84  const MT&, const data::DatasetInfo&, const PT&, const size_t, const WT&> {};
85 
86 /* A struct for indication that a right method form can't be found */
88 {
89  using PredictionsType = void*;
90  using WeightsType = void*;
91 };
92 
103 template<typename MLAlgorithm,
104  template<class, template<class...> class, size_t> class... HMFs>
106 
107 template<typename MLAlgorithm,
108  template<class, template<class...> class, size_t> class HasMethodForm,
109  template<class, template<class...> class, size_t> class... HMFs>
110 struct SelectMethodForm<MLAlgorithm, HasMethodForm, HMFs...>
111 {
112  template<typename... Forms>
113  class From
114  {
115  /* Declaration and definition of Implementation for the case when
116  * RemainingForms are empty */
117  template<typename... RemainingForms>
118  struct Implementation
119  {
120  using NextSMF = SelectMethodForm<MLAlgorithm, HMFs...>;
121  using Type = typename NextSMF::template From<Forms...>::Type;
122  };
123 
124  /* The case when there is at least one remaining form */
125  template<typename Form, typename... RemainingForms>
126  struct Implementation<Form, RemainingForms...>
127  {
128  using Type = typename std::conditional<
129  HasMethodForm<MLAlgorithm, Form::template Type,
130  Form::MinNumberOfAdditionalArgs>::value,
131  Form,
132  typename Implementation<RemainingForms...>::Type>::type;
133  };
134 
135  public:
136  using Type = typename Implementation<Forms...>::Type;
137  };
138 };
139 
140 template<typename MLAlgorithm>
141 struct SelectMethodForm<MLAlgorithm>
142 {
143  template<typename... Forms>
144  struct From
145  {
147  };
148 };
149 
175 template<typename MLAlgorithm,
176  typename MT = arma::mat,
177  typename PT = arma::Row<size_t>,
178  typename WT = arma::rowvec>
180 {
181  /* Defining type functions that check presence of Train methods of a given
182  * form. Defining such functions for templated and non-templated Train
183  * methods. */
184  HAS_METHOD_FORM(Train, HasTrain);
185  HAS_METHOD_FORM(template Train<>, HasTTrain);
186  HAS_METHOD_FORM(template Train<const MT&>, HasMTrain);
187  HAS_METHOD_FORM(SINGLE_ARG(template Train<const MT&, const PT&>), HasMPTrain);
188  HAS_METHOD_FORM(SINGLE_ARG(template Train<const MT&, const PT&, const WT&>),
189  HasMPWTrain);
190 
191  /* Forms of Train for regression algorithms */
195 
196  /* Forms of Train for classification algorithms */
199 
200  /* Forms of Train with weights for regression algorithms */
204 
205  /* Forms of Train with weights for classification algorithms */
208 
209  /* A short alias for a type function that selects a right method form */
210  template<typename... MethodForms>
211  using Select = typename SelectMethodForm<MLAlgorithm, HasTrain, HasTTrain,
212  HasMTrain, HasMPTrain, HasMPWTrain>::template From<MethodForms...>;
213 
214  /* An indication whether a method form is selected */
215  template<typename... MFs /* MethodForms */>
216  using Selects = typename std::conditional<
217  std::is_same<typename Select<MFs...>::Type, NotFoundMethodForm>::value,
218  std::false_type, std::true_type>::type;
219 
220  public:
225  using PredictionsType =
226  typename Select<TF1, TF2, TF3, TF4, TF5>::Type::PredictionsType;
227 
232  using WeightsType =
233  typename Select<WTF1, WTF2, WTF3, WTF4, WTF5>::Type::WeightsType;
234 
239  static const bool IsSupported = !std::is_same<PredictionsType, void*>::value;
240 
244  static const bool SupportsWeights = !std::is_same<WeightsType, void*>::value;
245 
249  static const bool TakesDatasetInfo = Selects<TF5>::value;
250 
254  static const bool TakesNumClasses = Selects<TF4, TF5>::value;
255 };
256 
257 } // namespace cv
258 } // namespace mlpack
259 
260 #endif
#define HAS_METHOD_FORM(METHOD, NAME)
HAS_METHOD_FORM generates a template that allows to check at compile time 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.
#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...
static const size_t MinNumberOfAdditionalArgs