diff --git a/src/mlpack/core/cv/cv_base.hpp b/src/mlpack/core/cv/cv_base.hpp index e528059a4c..ac24b3c190 100644 --- a/src/mlpack/core/cv/cv_base.hpp +++ b/src/mlpack/core/cv/cv_base.hpp @@ -160,76 +160,6 @@ class CVBase static_assert(MIE::IsSupported, "The given MLAlgorithm is not supported by MetaInfoExtractor"); - /** - * A set of methods for extracting input data arguments. It is supposed to be - * called with variadic template arguments like ExtractDataArgs(args...). - */ - template - static std::tuple ExtractDataArgs( - const MatInType& xs, - const PredictionsInType& ys) - { return std::tuple(xs, ys); } - - template - static std::tuple ExtractDataArgs( - const MatInType& xs, - const PredictionsInType& ys, - const size_t /* numClasses */) - { return std::tuple(xs, ys); } - - template - static std::tuple ExtractDataArgs( - const MatInType& xs, - const data::DatasetInfo& /* datasetInfo */, - const PredictionsInType& ys, - const size_t /* numClasses */) - { return std::tuple(xs, ys); } - - template - static std::tuple ExtractDataArgs( - const MatInType& xs, - const PredictionsInType& ys, - const WeightsInType& weights) - { - return std::tuple(xs, ys, weights); - } - - template - static std::tuple ExtractDataArgs( - const MatInType& xs, - const PredictionsInType& ys, - const size_t /* numClasses */, - const WeightsInType& weights) - { - return std::tuple(xs, ys, weights); - } - - template - static std::tuple ExtractDataArgs( - const MatInType& xs, - const data::DatasetInfo& /* datasetInfo */, - const PredictionsInType& ys, - const size_t /* numClasses */, - const WeightsInType& weights) - { - return std::tuple(xs, ys, weights); - } - /** * Assert there is an equal number of data points and predictions. */ diff --git a/src/mlpack/core/cv/simple_cv.hpp b/src/mlpack/core/cv/simple_cv.hpp index d6bc393e3b..1a34f35b37 100644 --- a/src/mlpack/core/cv/simple_cv.hpp +++ b/src/mlpack/core/cv/simple_cv.hpp @@ -24,12 +24,8 @@ namespace cv { * set. * * To construct a SimpleCV object you need to pass the validationSize parameter - * and arguments that specify data. For the latter see the CVBase constructors - * as a reference - the CVBase constructors take exactly the same arguments as - * ones that are supposed to be passed after the validationSize parameter in the - * SimpleCV constructor. - * - * For example, SoftmaxRegression can be validated in the following way. + * and arguments that specify data. For example, SoftmaxRegression can be + * validated in the following way. * * @code * // 100-point 5-dimensional random dataset. @@ -74,15 +70,134 @@ class SimpleCV : { public: /** - * This constructor splits data into training and validation sets. + * This constructor can be used for regression algorithms and for binary + * classification algorithms. * - * @param validationSize A proportion (between 0 and 1) of the data used as a + * @param validationSize A proportion (between 0 and 1) of data used as a * validation set. - * @param args Basic constructor arguments for MLAlgortithm (see the CVBase - * constructors for reference). + * @param xs Data points to cross-validate on. + * @param ys Predictions (labels for classification algorithms and responses + * for regression algorithms) for each data point. + * + * @tparam MatInType A type that can be converted to MatType. + * @tparam PredictionsInType A type that can be converted to PredictionsType. */ - template - SimpleCV(const double validationSize, const CVBaseArgs&... args); + template + SimpleCV(const double validationSize, + const MatInType& xs, + const PredictionsInType& ys); + + /** + * This constructor can be used for multiclass classification algorithms. + * + * @param validationSize A proportion (between 0 and 1) of data used as a + * validation set. + * @param xs Data points to cross-validate on. + * @param ys Labels for each data point. + * @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 + SimpleCV(const double validationSize, + 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 validationSize A proportion (between 0 and 1) of data used as a + * validation set. + * @param xs Data points to cross-validate on. + * @param datasetInfo Type information for each dimension of the dataset. + * @param ys Labels for each data point. + * @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 + SimpleCV(const double validationSize, + 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 validationSize A proportion (between 0 and 1) of data used as a + * validation set. + * @param xs Data points to cross-validate on. + * @param ys Predictions (labels for classification algorithms and responses + * for regression algorithms) for each data point. + * @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 + SimpleCV(const double validationSize, + const MatInType& xs, + const PredictionsInType& ys, + const WeightsInType& weights); + + /** + * This constructor can be used for multiclass classification algorithms that + * support weighted learning. + * + * @param validationSize A proportion (between 0 and 1) of data used as a + * validation set. + * @param xs Data points to cross-validate on. + * @param ys Labels for each data point. + * @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 + SimpleCV(const double validationSize, + 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 validationSize A proportion (between 0 and 1) of data used as a + * validation set. + * @param xs Data points to cross-validate on. + * @param datasetInfo Type information for each dimension of the dataset. + * @param ys Labels for each data point. + * @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 + SimpleCV(const double validationSize, + const MatInType& xs, + const data::DatasetInfo& datasetInfo, + const PredictionsInType& ys, + const size_t numClasses, + const WeightsInType& weights); /** * Train on the training set and assess performance on the validation set by @@ -123,23 +238,6 @@ class SimpleCV : //! A pointer to the last trained model. std::unique_ptr modelPtr; - /** - * Initialize without weights. - */ - template::value == 2>::type> - void Init(const double validationSize, const DataArgsTupleT& dataArgsTuple); - - /** - * Initialize with weights. - */ - template::value == 3>::type, - typename = void> - void Init(const double validationSize, const DataArgsTupleT& dataArgsTuple); - /** * Initialize training and validation sets. */ diff --git a/src/mlpack/core/cv/simple_cv_impl.hpp b/src/mlpack/core/cv/simple_cv_impl.hpp index 1e72f1e7ad..b188216556 100644 --- a/src/mlpack/core/cv/simple_cv_impl.hpp +++ b/src/mlpack/core/cv/simple_cv_impl.hpp @@ -22,16 +22,140 @@ template -template +template SimpleCV::SimpleCV(const double validationSize, - const CVBaseArgs&... args) : - Base(args...) + const MatInType& xs, + const PredictionsInType& ys) : + Base(xs, ys), + xs(xs), + ys(ys) { - Init(validationSize, Base::ExtractDataArgs(args...)); + Base::AssertDataConsistency(this->xs, this->ys); + InitTrainingAndValidationSets(validationSize); +} + +template +template +SimpleCV::SimpleCV(const double validationSize, + const MatInType& xs, + const PredictionsInType& ys, + const size_t numClasses) : + Base(xs, ys, numClasses), + xs(xs), + ys(ys) +{ + Base::AssertDataConsistency(this->xs, this->ys); + InitTrainingAndValidationSets(validationSize); +} + +template +template +SimpleCV::SimpleCV(const double validationSize, + const MatInType& xs, + const data::DatasetInfo& datasetInfo, + const PredictionsInType& ys, + const size_t numClasses) : + Base(xs, datasetInfo, ys, numClasses), + xs(xs), + ys(ys) +{ + Base::AssertDataConsistency(this->xs, this->ys); + InitTrainingAndValidationSets(validationSize); +} + +template +template +SimpleCV::SimpleCV(const double validationSize, + const MatInType& xs, + const PredictionsInType& ys, + const WeightsInType& weights) : + Base(xs, ys, weights), + xs(xs), + ys(ys), + weights(weights) +{ + Base::AssertDataConsistency(this->xs, this->ys, this->weights); + InitTrainingAndValidationSets(validationSize); + trainingWeights = GetSubset(this->weights, 0, trainingXs.n_cols - 1); +} + +template +template +SimpleCV::SimpleCV(const double validationSize, + const MatInType& xs, + const PredictionsInType& ys, + const size_t numClasses, + const WeightsInType& weights) : + Base(xs, ys, numClasses, weights), + xs(xs), + ys(ys), + weights(weights) +{ + Base::AssertDataConsistency(this->xs, this->ys, this->weights); + InitTrainingAndValidationSets(validationSize); + trainingWeights = GetSubset(this->weights, 0, trainingXs.n_cols - 1); +} + +template +template +SimpleCV::SimpleCV(const double validationSize, + const MatInType& xs, + const data::DatasetInfo& datasetInfo, + const PredictionsInType& ys, + const size_t numClasses, + const WeightsInType& weights) : + Base(xs, datasetInfo, ys, numClasses, weights), + xs(xs), + ys(ys), + weights(weights) +{ + Base::AssertDataConsistency(this->xs, this->ys, this->weights); + InitTrainingAndValidationSets(validationSize); + trainingWeights = GetSubset(this->weights, 0, trainingXs.n_cols - 1); } template -template -void SimpleCV::Init(const double validationSize, - const DataArgsTupleT& dataArgsTuple) -{ - xs = std::get<0>(dataArgsTuple); - ys = std::get<1>(dataArgsTuple); - - Base::AssertDataConsistency(xs, ys); - - InitTrainingAndValidationSets(validationSize); -} - -template -template -void SimpleCV::Init(const double validationSize, - const DataArgsTupleT& dataArgsTuple) -{ - xs = std::get<0>(dataArgsTuple); - ys = std::get<1>(dataArgsTuple); - weights = std::get<2>(dataArgsTuple); - - Base::AssertDataConsistency(xs, ys, weights); - - InitTrainingAndValidationSets(validationSize); - - trainingWeights = GetSubset(weights, 0, trainingXs.n_cols - 1); -} - template