Add constructor to Perceptron for weighted data for KFoldCV.

This commit is contained in:
Ryan Curtin
2022-04-15 21:26:28 -04:00
parent 80e65d8a5d
commit 3133a4abbd
3 changed files with 73 additions and 0 deletions
@@ -68,6 +68,29 @@ class Perceptron
const size_t numClasses,
const size_t maxIterations = 1000);
/**
* Constructor: construct the perceptron by building the weights matrix, which
* is later used in classification. The number of classes should be specified
* separately, and the labels vector should contain values in the range [0,
* numClasses - 1]. The data::NormalizeLabels() function can be used if the
* labels vector does not contain values in the required range.
*
* This constructor supports weights for each data point.
*
* @param data Input, training data.
* @param labels Labels of dataset.
* @param numClasses Number of classes in the dataset.
* @param instanceWeights Weight vector to use for each training point while
* training.
* @param maxIterations Maximum number of iterations for the perceptron
* learning algorithm.
*/
Perceptron(const MatType& data,
const arma::Row<size_t>& labels,
const size_t numClasses,
const arma::rowvec& instanceWeights,
const size_t maxIterations = 1000);
/**
* Alternate constructor which copies parameters from an already initiated
* perceptron.
@@ -62,6 +62,32 @@ Perceptron<LearnPolicy, WeightInitializationPolicy, MatType>::Perceptron(
Train(data, labels, numClasses);
}
/**
* Constructor: construct the perceptron by building the weights matrix, which
* is later used in classification. The number of classes should be specified
* separately, and the labels vector should contain values in the range [0,
* numClasses - 1]. The data::NormalizeLabels() function can be used if the
* labels vector does not contain values in the required range.
*
* This constructor supports weights for each data point.
*/
template<
typename LearnPolicy,
typename WeightInitializationPolicy,
typename MatType
>
Perceptron<LearnPolicy, WeightInitializationPolicy, MatType>::Perceptron(
const MatType& data,
const arma::Row<size_t>& labels,
const size_t numClasses,
const arma::rowvec& instanceWeights,
const size_t maxIterations) :
maxIterations(maxIterations)
{
// Start training.
Train(data, labels, numClasses, instanceWeights);
}
/**
* Alternate constructor which copies parameters from an already initiated
* perceptron.
+24
View File
@@ -244,4 +244,28 @@ TEST_CASE("SecondaryConstructor", "[PerceptronTest]")
Perceptron<> p1(trainData, labels.row(0), 2, 1000);
Perceptron<> p2(p1);
REQUIRE(p1.Weights().n_elem > 0);
REQUIRE(p2.Weights().n_elem > 0);
}
/**
* This tests that we can build the Perceptron when specifying instance weights.
*/
TEST_CASE("InstanceWeightsConstructor", "[PerceptronTest]")
{
mat trainData;
trainData = { { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2 } };
Mat<size_t> labels;
labels = { 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1 };
rowvec instanceWeights;
instanceWeights = { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 0.9,
0.8, 0.7, 0.6, 0.5, 0.4 };
Perceptron<> p(trainData, labels.row(0), 2, instanceWeights, 1000);
REQUIRE(p.Weights().n_elem > 0);
}