Refactor CVBase constructors

This commit is contained in:
Kirill Mishchenko
2017-08-04 12:12:25 +05:00
parent 6c9d9b0401
commit 2cfab82acf
3 changed files with 49 additions and 199 deletions
+36 -117
View File
@@ -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<typename MLAlgorithm,
typename WeightsType>
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<typename MatInType, typename PredictionsInType>
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<typename MatInType, typename PredictionsInType>
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<typename MatInType, typename PredictionsInType>
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<typename MatInType,
typename PredictionsInType,
typename WeightsInType>
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<typename MatInType,
typename PredictionsInType,
typename WeightsInType>
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<typename MatInType,
typename PredictionsInType,
typename WeightsInType>
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<typename = void>
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<typename = void>
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<typename = void>
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,
+9 -76
View File
@@ -19,12 +19,11 @@ template<typename MLAlgorithm,
typename MatType,
typename PredictionsType,
typename WeightsType>
template<typename MatInType, typename PredictionsInType>
template<typename>
CVBase<MLAlgorithm,
MatType,
PredictionsType,
WeightsType>::CVBase(const MatInType&,
const PredictionsInType&) :
WeightsType>::CVBase() :
isDatasetInfoPassed(false)
{
static_assert(!MIE::TakesNumClasses,
@@ -35,13 +34,11 @@ template<typename MLAlgorithm,
typename MatType,
typename PredictionsType,
typename WeightsType>
template<typename MatInType, typename PredictionsInType>
template<typename>
CVBase<MLAlgorithm,
MatType,
PredictionsType,
WeightsType>::CVBase(const MatInType&,
const PredictionsInType&,
const size_t numClasses) :
WeightsType>::CVBase(const size_t numClasses) :
isDatasetInfoPassed(false),
numClasses(numClasses)
{
@@ -53,13 +50,11 @@ template<typename MLAlgorithm,
typename MatType,
typename PredictionsType,
typename WeightsType>
template<typename MatInType, typename PredictionsInType>
template<typename>
CVBase<MLAlgorithm,
MatType,
PredictionsType,
WeightsType>::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<MLAlgorithm,
"The given MLAlgorithm does not accept a data::DatasetInfo parameter");
}
template<typename MLAlgorithm,
typename MatType,
typename PredictionsType,
typename WeightsType>
template<typename MatInType, typename PredictionsInType, typename WeightsInType>
CVBase<MLAlgorithm,
MatType,
PredictionsType,
WeightsType>::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<typename MLAlgorithm,
typename MatType,
typename PredictionsType,
typename WeightsType>
template<typename MatInType, typename PredictionsInType, typename WeightsInType>
CVBase<MLAlgorithm,
MatType,
PredictionsType,
WeightsType>::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<typename MLAlgorithm,
typename MatType,
typename PredictionsType,
typename WeightsType>
template<typename MatInType, typename PredictionsInType, typename WeightsInType>
CVBase<MLAlgorithm,
MatType,
PredictionsType,
WeightsType>::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<typename MLAlgorithm,
typename MatType,
typename PredictionsType,
@@ -193,6 +123,9 @@ void CVBase<MLAlgorithm,
const PredictionsType& ys,
const WeightsType& weights)
{
static_assert(MIE::SupportsWeights,
"The given MLAlgorithm does not support weighted learning");
AssertSizeEquality(xs, ys);
AssertWeightsSize(xs, weights);
}
+4 -6
View File
@@ -30,7 +30,6 @@ SimpleCV<MLAlgorithm,
WeightsType>::SimpleCV(const double validationSize,
const MatInType& xs,
const PredictionsInType& ys) :
Base(xs, ys),
xs(xs),
ys(ys)
{
@@ -52,7 +51,7 @@ SimpleCV<MLAlgorithm,
const MatInType& xs,
const PredictionsInType& ys,
const size_t numClasses) :
Base(xs, ys, numClasses),
Base(numClasses),
xs(xs),
ys(ys)
{
@@ -75,7 +74,7 @@ SimpleCV<MLAlgorithm,
const data::DatasetInfo& datasetInfo,
const PredictionsInType& ys,
const size_t numClasses) :
Base(xs, datasetInfo, ys, numClasses),
Base(datasetInfo, numClasses),
xs(xs),
ys(ys)
{
@@ -97,7 +96,6 @@ SimpleCV<MLAlgorithm,
const MatInType& xs,
const PredictionsInType& ys,
const WeightsInType& weights) :
Base(xs, ys, weights),
xs(xs),
ys(ys),
weights(weights)
@@ -121,7 +119,7 @@ SimpleCV<MLAlgorithm,
const PredictionsInType& ys,
const size_t numClasses,
const WeightsInType& weights) :
Base(xs, ys, numClasses, weights),
Base(numClasses),
xs(xs),
ys(ys),
weights(weights)
@@ -146,7 +144,7 @@ SimpleCV<MLAlgorithm,
const PredictionsInType& ys,
const size_t numClasses,
const WeightsInType& weights) :
Base(xs, datasetInfo, ys, numClasses, weights),
Base(datasetInfo, numClasses),
xs(xs),
ys(ys),
weights(weights)