12 #ifndef MLPACK_CORE_HPT_CV_FUNCTION_IMPL_HPP 13 #define MLPACK_CORE_HPT_CV_FUNCTION_IMPL_HPP 18 template<
typename CVType,
21 typename... BoundArgs>
22 template<
size_t BoundArgIndex,
size_t ParamIndex>
23 struct CVFunction<CVType, MLAlgorithm, TotalArgs, BoundArgs...>::UseBoundArg<
24 BoundArgIndex, ParamIndex, true>
26 using BoundArgType =
typename 27 std::tuple_element<BoundArgIndex, std::tuple<BoundArgs...>>::type;
29 static const bool value = BoundArgType::index == BoundArgIndex + ParamIndex;
32 template<
typename CVType,
35 typename... BoundArgs>
36 template<
size_t BoundArgIndex,
size_t ParamIndex>
37 struct CVFunction<CVType, MLAlgorithm, TotalArgs, BoundArgs...>::UseBoundArg<
38 BoundArgIndex, ParamIndex, false>
40 static const bool value =
false;
43 template<
typename CVType,
46 typename... BoundArgs>
50 const double relativeDelta,
51 const double minDelta,
52 const BoundArgs&... args) :
54 datasetInfo(datasetInfo),
56 bestObjective(
std::numeric_limits<double>::max()),
57 relativeDelta(relativeDelta),
61 template<
typename CVType,
64 typename... BoundArgs>
66 const arma::mat& parameters)
68 return Evaluate<0, 0>(parameters);
71 template<
typename CVType,
74 typename... BoundArgs>
76 const arma::mat& parameters,
79 gradient = arma::mat(arma::size(parameters));
80 arma::mat increasedParameters = parameters;
81 double originalParametersEvaluation =
Evaluate(parameters);
82 for (
size_t i = 0; i < parameters.n_rows; ++i)
84 double delta = std::max(std::abs(parameters(i)) * relativeDelta, minDelta);
85 increasedParameters(i) += delta;
87 (
Evaluate(increasedParameters) - originalParametersEvaluation) / delta;
88 increasedParameters(i) = parameters(i);
92 template<
typename CVType,
95 typename... BoundArgs>
96 template<
size_t BoundArgIndex,
101 const arma::mat& parameters,
104 return PutNextArg<BoundArgIndex, ParamIndex>(parameters, args...);
107 template<
typename CVType,
108 typename MLAlgorithm,
110 typename... BoundArgs>
111 template<
size_t BoundArgIndex,
120 double objective = cv.Evaluate(args...);
124 if (bestObjective > objective ||
125 bestObjective == std::numeric_limits<double>::max())
127 bestObjective = objective;
128 bestModel = std::move(cv.Model());
134 template<
typename CVType,
135 typename MLAlgorithm,
137 typename... BoundArgs>
138 template<
size_t BoundArgIndex,
143 const arma::mat& parameters,
146 return Evaluate<BoundArgIndex + 1, ParamIndex>(
147 parameters, args..., std::get<BoundArgIndex>(boundArgs).value);
150 template<
typename CVType,
151 typename MLAlgorithm,
153 typename... BoundArgs>
154 template<
size_t BoundArgIndex,
160 const arma::mat& parameters,
163 if (datasetInfo.Type(ParamIndex) == data::Datatype::categorical)
165 return Evaluate<BoundArgIndex, ParamIndex + 1>(parameters, args...,
166 datasetInfo.UnmapString(
size_t(parameters(ParamIndex, 0)), ParamIndex));
170 return Evaluate<BoundArgIndex, ParamIndex + 1>(parameters, args...,
171 parameters(ParamIndex, 0));
Auxiliary information for a dataset, including mappings to/from strings (or other types) and the data...
Definition: dataset_mapper.hpp:41
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
Definition: pointer_wrapper.hpp:23
CVFunction(CVType &cv, data::DatasetMapper< data::IncrementPolicy, double > &datasetInfo, const double relativeDelta, const double minDelta, const BoundArgs &... args)
Initialize a CVFunction object.
Definition: cv_function_impl.hpp:47
double Evaluate(const arma::mat ¶meters)
Run cross-validation with the bound and passed parameters.
Definition: cv_function_impl.hpp:65
void Gradient(const arma::mat ¶meters, arma::mat &gradient)
Evaluate numerically the gradient of the CVFunction with the given parameters.
Definition: cv_function_impl.hpp:75
This wrapper serves for adapting the interface of the cross-validation classes to the one that can be...
Definition: cv_function.hpp:39