diff --git a/src/mlpack/methods/perceptron/perceptron.hpp b/src/mlpack/methods/perceptron/perceptron.hpp index 4994588c40..3fdfce6e14 100644 --- a/src/mlpack/methods/perceptron/perceptron.hpp +++ b/src/mlpack/methods/perceptron/perceptron.hpp @@ -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& labels, + const size_t numClasses, + const arma::rowvec& instanceWeights, + const size_t maxIterations = 1000); + /** * Alternate constructor which copies parameters from an already initiated * perceptron. diff --git a/src/mlpack/methods/perceptron/perceptron_impl.hpp b/src/mlpack/methods/perceptron/perceptron_impl.hpp index e45dfbb95b..7373a3843b 100644 --- a/src/mlpack/methods/perceptron/perceptron_impl.hpp +++ b/src/mlpack/methods/perceptron/perceptron_impl.hpp @@ -62,6 +62,32 @@ Perceptron::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::Perceptron( + const MatType& data, + const arma::Row& 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. diff --git a/src/mlpack/tests/perceptron_test.cpp b/src/mlpack/tests/perceptron_test.cpp index 97f2686a26..57422961fc 100644 --- a/src/mlpack/tests/perceptron_test.cpp +++ b/src/mlpack/tests/perceptron_test.cpp @@ -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 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); }