From 2cfab82acf9fbfecf99cbbebe899f72863072b77 Mon Sep 17 00:00:00 2001 From: Kirill Mishchenko Date: Fri, 4 Aug 2017 11:47:57 +0500 Subject: [PATCH] Refactor CVBase constructors --- src/mlpack/core/cv/cv_base.hpp | 153 ++++++-------------------- src/mlpack/core/cv/cv_base_impl.hpp | 85 ++------------ src/mlpack/core/cv/simple_cv_impl.hpp | 10 +- 3 files changed, 49 insertions(+), 199 deletions(-) diff --git a/src/mlpack/core/cv/cv_base.hpp b/src/mlpack/core/cv/cv_base.hpp index ac24b3c190..b29aa762e6 100644 --- a/src/mlpack/core/cv/cv_base.hpp +++ b/src/mlpack/core/cv/cv_base.hpp @@ -23,9 +23,8 @@ namespace cv { * numClasses) and to assert that the machine learning algorithm and data * satisfy certain conditions. * - * This class is not meant to be used directly by users. Rather use the CVBase - * constructors as a reference for what additional arguments are accepted by - * cross-validation strategies like SimpleCV or KFoldCV. + * This class is not meant to be used directly by users. To cross-validate + * rather use end-user classes like SimpleCV or KFoldCV. * * @tparam MLAlgorithm A machine learning algorithm. * @tparam MatType The type of data. @@ -39,119 +38,6 @@ template class CVBase { - public: - /** - * This constructor can be used for regression algorithms and for binary - * classification algorithms. - * - * @param xs Dataset to cross-validate on. - * @param ys Predictions (labels for classification algorithms and responses - * for regression algorithms) for each point from the dataset. - * - * @tparam MatInType A type that can be converted to MatType. - * @tparam PredictionsInType A type that can be converted to PredictionsType. - */ - template - CVBase(const MatInType& xs, - const PredictionsInType& ys); - - /** - * This constructor can be used for multiclass classification algorithms. - * - * @param xs Dataset to cross-validate on. - * @param ys Labels for each point from the dataset. - * @param numClasses Number of classes in the dataset. - * - * @tparam MatInType A type that can be converted to MatType. - * @tparam PredictionsInType A type that can be converted to PredictionsType. - */ - template - CVBase(const MatInType& xs, - const PredictionsInType& ys, - const size_t numClasses); - - /** - * This constructor can be used for multiclass classification algorithms that - * can take a data::DatasetInfo parameter. - * - * @param xs Dataset to cross-validate on. - * @param datasetInfo Type information for each dimension of the dataset. - * @param ys Labels for each point from the dataset. - * @param numClasses Number of classes in the dataset. - * - * @tparam MatInType A type that can be converted to MatType. - * @tparam PredictionsInType A type that can be converted to PredictionsType. - */ - template - CVBase(const MatInType& xs, - const data::DatasetInfo& datasetInfo, - const PredictionsInType& ys, - const size_t numClasses); - - /** - * This constructor can be used for regression and binary classification - * algorithms that support weighted learning. - * - * @param xs Dataset to cross-validate on. - * @param ys Predictions (labels for classification algorithms and responses - * for regression algorithms) for each point from the dataset. - * @param weights Observation weights (for boosting). - * - * @tparam MatInType A type that can be converted to MatType. - * @tparam PredictionsInType A type that can be converted to PredictionsType. - * @tparam WeightsInType A type that can be converted to WeightsType. - */ - template - CVBase(const MatInType& xs, - const PredictionsInType& ys, - const WeightsInType& weights); - - /** - * This constructor can be used for multiclass classification algorithms that - * support weighted learning. - * - * @param xs Dataset to cross-validate on. - * @param ys Labels for each point from the dataset. - * @param numClasses Number of classes in the dataset. - * @param weights Observation weights (for boosting). - * - * @tparam MatInType A type that can be converted to MatType. - * @tparam PredictionsInType A type that can be converted to PredictionsType. - * @tparam WeightsInType A type that can be converted to WeightsType. - */ - template - CVBase(const MatInType& xs, - const PredictionsInType& ys, - const size_t numClasses, - const WeightsInType& weights); - - /** - * This constructor can be used for multiclass classification algorithms that - * can take a data::DatasetInfo parameter and support weighted learning. - * - * @param xs Dataset to cross-validate on. - * @param datasetInfo Type information for each dimension of the dataset. - * @param ys Labels for each point from the dataset. - * @param numClasses Number of classes in the dataset. - * @param weights Observation weights (for boosting). - * - * @tparam MatInType A type that can be converted to MatType. - * @tparam PredictionsInType A type that can be converted to PredictionsType. - * @tparam WeightsInType A type that can be converted to WeightsType. - */ - template - CVBase(const MatInType& xs, - const data::DatasetInfo& datasetInfo, - const PredictionsInType& ys, - const size_t numClasses, - const WeightsInType& weights); - protected: //! A short alias for MetaInfoExtractor. using MIE = @@ -160,6 +46,38 @@ class CVBase static_assert(MIE::IsSupported, "The given MLAlgorithm is not supported by MetaInfoExtractor"); + /** + * Assert that MLAlgorithm doesn't take any additional basic parameters like + * numClasses. + * + * The constructor is templated to avoid instantiation before being called. + */ + template + CVBase(); + + /** + * Assert that MLAlgorithm takes the numClasses parameter and store it. + * + * The constructor is templated to avoid instantiation before being called. + * + * @param numClasses Number of classes in the dataset. + */ + template + CVBase(const size_t numClasses); + + /** + * Assert that MLAlgorithm takes the numClasses parameter and a + * data::DatasetInfo parameter and store them. + * + * The constructor is templated to avoid instantiation before being called. + * + * @param datasetInfo Type information for each dimension of the dataset. + * @param numClasses Number of classes in the dataset. + */ + template + CVBase(const data::DatasetInfo& datasetInfo, + const size_t numClasses); + /** * Assert there is an equal number of data points and predictions. */ @@ -167,7 +85,8 @@ class CVBase const PredictionsType& ys); /** - * Assert there is an equal number of data points, predictions, and weights. + * Assert weighted learning is supported and there is an equal number of data + * points, predictions, and weights. */ static void AssertDataConsistency(const MatType& xs, const PredictionsType& ys, diff --git a/src/mlpack/core/cv/cv_base_impl.hpp b/src/mlpack/core/cv/cv_base_impl.hpp index 7d6220c7b8..5c13eb690f 100644 --- a/src/mlpack/core/cv/cv_base_impl.hpp +++ b/src/mlpack/core/cv/cv_base_impl.hpp @@ -19,12 +19,11 @@ template -template +template CVBase::CVBase(const MatInType&, - const PredictionsInType&) : + WeightsType>::CVBase() : isDatasetInfoPassed(false) { static_assert(!MIE::TakesNumClasses, @@ -35,13 +34,11 @@ template -template +template CVBase::CVBase(const MatInType&, - const PredictionsInType&, - const size_t numClasses) : + WeightsType>::CVBase(const size_t numClasses) : isDatasetInfoPassed(false), numClasses(numClasses) { @@ -53,13 +50,11 @@ template -template +template CVBase::CVBase(const MatInType&, - const data::DatasetInfo& datasetInfo, - const PredictionsInType&, + WeightsType>::CVBase(const data::DatasetInfo& datasetInfo, const size_t numClasses) : datasetInfo(datasetInfo), isDatasetInfoPassed(true), @@ -71,71 +66,6 @@ CVBase -template -CVBase::CVBase(const MatInType&, - const PredictionsInType&, - const WeightsInType&) : - isDatasetInfoPassed(false) -{ - static_assert(!MIE::TakesNumClasses, - "The given MLAlgorithm requires the numClasses parameter"); - static_assert(MIE::SupportsWeights, - "The given MLAlgorithm does not support weighted learning"); -} - -template -template -CVBase::CVBase(const MatInType&, - const PredictionsInType&, - const size_t numClasses, - const WeightsInType&) : - isDatasetInfoPassed(false), - numClasses(numClasses) -{ - static_assert(MIE::TakesNumClasses, - "The given MLAlgorithm does not take the numClasses parameter"); - static_assert(MIE::SupportsWeights, - "The given MLAlgorithm does not support weighted learning"); -} - -template -template -CVBase::CVBase(const MatInType&, - const data::DatasetInfo& datasetInfo, - const PredictionsInType&, - const size_t numClasses, - const WeightsInType&) : - datasetInfo(datasetInfo), - isDatasetInfoPassed(true), - numClasses(numClasses) -{ - static_assert(MIE::TakesNumClasses, - "The given MLAlgorithm does not take the numClasses parameter"); - static_assert(MIE::TakesDatasetInfo, - "The given MLAlgorithm does not accept a data::DatasetInfo parameter"); - static_assert(MIE::SupportsWeights, - "The given MLAlgorithm does not support weighted learning"); -} - template::SimpleCV(const double validationSize, const MatInType& xs, const PredictionsInType& ys) : - Base(xs, ys), xs(xs), ys(ys) { @@ -52,7 +51,7 @@ SimpleCV