12 #ifndef MLPACK_CORE_HPT_HPT_IMPL_HPP 13 #define MLPACK_CORE_HPT_HPT_IMPL_HPP 20 template<
typename MLAlgorithm,
22 template<
typename,
typename,
typename,
typename,
typename>
class CV,
25 typename PredictionsType,
27 template<
typename... CVArgs>
28 HyperParameterTuner<MLAlgorithm,
35 cv(args...), relativeDelta(0.01), minDelta(1e-10) {}
37 template<
typename MLAlgorithm,
39 template<
typename,
typename,
typename,
typename,
typename>
class CV,
42 typename PredictionsType,
44 template<
typename... Args>
54 static const size_t numberOfParametersToOptimize =
58 numberOfParametersToOptimize);
60 arma::mat bestParameters(numberOfParametersToOptimize, 1);
61 const auto argsTuple = std::tie(args...);
63 InitAndOptimize<0>(argsTuple, bestParameters, datasetInfo);
67 if (datasetInfo.
Type(i) == data::Datatype::categorical)
68 bestParameters[i] = datasetInfo.
UnmapString(bestParameters[i], i);
74 template<
typename MLAlgorithm,
76 template<
typename,
typename,
typename,
typename,
typename>
class CV,
79 typename PredictionsType,
81 template<
size_t I,
typename ArgsTuple,
typename... FixedArgs,
typename>
88 WeightsType>::InitAndOptimize(
90 arma::mat& bestParams,
92 FixedArgs... fixedArgs)
94 static const size_t totalArgs = std::tuple_size<ArgsTuple>::value;
96 std::vector<bool> categoricalDimensions(datasetInfo.
Dimensionality());
101 categoricalDimensions[d] = datasetInfo.
Type(d) ==
102 mlpack::data::Datatype::categorical;
105 CVFunction<CVType, MLAlgorithm, totalArgs, FixedArgs...>
106 cvFunction(cv, datasetInfo, relativeDelta, minDelta, fixedArgs...);
107 bestObjective = Metric::NeedsMinimization ? optimizer.Optimize(cvFunction,
108 bestParams, categoricalDimensions, numCategories) :
109 -optimizer.Optimize(cvFunction, bestParams, categoricalDimensions,
111 bestModel = std::move(cvFunction.BestModel());
114 template<
typename MLAlgorithm,
116 template<
typename,
typename,
typename,
typename,
typename>
class CV,
119 typename PredictionsType,
120 typename WeightsType>
121 template<
size_t I,
class ArgsTuple,
class... FixedArgs,
class,
class>
128 WeightsType>::InitAndOptimize(
129 const ArgsTuple& args,
130 arma::mat& bestParams,
132 FixedArgs... fixedArgs)
134 using PreFixedArgT =
typename std::remove_reference<
135 typename std::tuple_element<I, ArgsTuple>::type>::type;
138 InitAndOptimize<I + 1>(args, bestParams, datasetInfo, fixedArgs...,
139 FixedArgT{std::get<I>(args).value});
142 template<
typename MLAlgorithm,
144 template<
typename,
typename,
typename,
typename,
typename>
class CV,
147 typename PredictionsType,
148 typename WeightsType>
149 template<
size_t I,
class ArgsTuple,
class... FixedArgs,
class,
class,
class>
156 WeightsType>::InitAndOptimize(
157 const ArgsTuple& args,
158 arma::mat& bestParams,
160 FixedArgs... fixedArgs)
162 static const size_t dimension =
163 I - std::tuple_size<std::tuple<FixedArgs...>>::value;
164 datasetInfo.
Type(dimension) = data::Datatype::numeric;
165 bestParams(dimension) = std::get<I>(args);
167 InitAndOptimize<I + 1>(args, bestParams, datasetInfo, fixedArgs...);
170 template<
typename MLAlgorithm,
172 template<
typename,
typename,
typename,
typename,
typename>
class CV,
175 typename PredictionsType,
176 typename WeightsType>
177 template<
size_t I,
class ArgsTuple,
class... FixedArgs,
class,
class,
class,
185 WeightsType>::InitAndOptimize(
186 const ArgsTuple& args,
187 arma::mat& bestParams,
189 FixedArgs... fixedArgs)
191 static const size_t dimension =
192 I - std::tuple_size<std::tuple<FixedArgs...>>::value;
193 for (
auto value : std::get<I>(args))
194 datasetInfo.
MapString<
size_t>(value, dimension);
198 std::ostringstream oss;
199 oss <<
"HyperParameterTuner::Optimize(): the collection passed as the " 200 <<
"argument " << I + 1 <<
" is empty" << std::endl;
201 throw std::invalid_argument(oss.str());
204 InitAndOptimize<I + 1>(args, bestParams, datasetInfo, fixedArgs...);
207 template<
typename MLAlgorithm,
209 template<
typename,
typename,
typename,
typename,
typename>
class CV,
212 typename PredictionsType,
213 typename WeightsType>
214 template<
typename TupleType,
size_t I,
typename... Args,
typename>
221 WeightsType>::VectorToTuple(
222 const arma::vec& vector,
const Args&... args)
224 return VectorToTuple<TupleType, I + 1>(vector, args..., vector(I));
227 template<
typename MLAlgorithm,
229 template<
typename,
typename,
typename,
typename,
typename>
class CV,
232 typename PredictionsType,
233 typename WeightsType>
234 template<
typename TupleType,
size_t I,
typename... Args,
typename,
typename>
241 WeightsType>::VectorToTuple(
242 const arma::vec& ,
const Args&... args)
244 return TupleType(args...);
Auxiliary information for a dataset, including mappings to/from strings (or other types) and the data...
Definition: dataset_mapper.hpp:41
IncrementPolicy is used as a helper class for DatasetMapper.
Definition: increment_policy.hpp:33
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The class HyperParameterTuner for the given MLAlgorithm utilizes the provided Optimizer to find the v...
Definition: hpt.hpp:96
size_t Dimensionality() const
Get the dimensionality of the DatasetMapper object (that is, how many dimensions it has information f...
Definition: dataset_mapper_impl.hpp:228
const InputType & UnmapString(const T value, const size_t dimension, const size_t unmappingIndex=0) const
Return the input that corresponds to a given value in a given dimension.
Definition: dataset_mapper_impl.hpp:125
T MapString(const InputType &input, const size_t dimension)
Given the input and the dimension to which it belongs, return its numeric mapping.
Definition: dataset_mapper_impl.hpp:85
Datatype Type(const size_t dimension) const
Return the type of a given dimension (numeric or categorical).
Definition: dataset_mapper_impl.hpp:196
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
OptimizerType & Optimizer()
Access and modify the optimizer.
Definition: hpt.hpp:110
size_t NumMappings(const size_t dimension) const
Get the number of mappings for a particular dimension.
Definition: dataset_mapper_impl.hpp:222
A struct for storing information about a fixed argument.
Definition: fixed.hpp:52
This wrapper serves for adapting the interface of the cross-validation classes to the one that can be...
Definition: cv_function.hpp:39
typename DeduceHyperParameterTypes< Args... >::TupleType TupleOfHyperParameters
A short alias for deducing types of hyper-parameters from types of arguments in the Optimize method i...
Definition: deduce_hp_types.hpp:127
TupleOfHyperParameters< Args... > Optimize(const Args &... args)
Find the best hyper-parameters by using the given Optimizer.
Definition: hpt_impl.hpp:51