mlpack
|
This class implements a simple perceptron (i.e., a single layer neural network). More...
#include <perceptron.hpp>
Public Member Functions | |
Perceptron (const size_t numClasses=0, const size_t dimensionality=0, const size_t maxIterations=1000) | |
Constructor: create the perceptron with the given number of classes and initialize the weight matrix, but do not perform any training. More... | |
Perceptron (const MatType &data, const arma::Row< size_t > &labels, const size_t numClasses, const size_t maxIterations=1000) | |
Constructor: constructs the perceptron by building the weights matrix, which is later used in classification. More... | |
Perceptron (const Perceptron &other, const MatType &data, const arma::Row< size_t > &labels, const size_t numClasses, const arma::rowvec &instanceWeights) | |
Alternate constructor which copies parameters from an already initiated perceptron. More... | |
void | Train (const MatType &data, const arma::Row< size_t > &labels, const size_t numClasses, const arma::rowvec &instanceWeights=arma::rowvec()) |
Train the perceptron on the given data for up to the maximum number of iterations (specified in the constructor or through MaxIterations()). More... | |
void | Classify (const MatType &test, arma::Row< size_t > &predictedLabels) |
Classification function. More... | |
template<typename Archive > | |
void | serialize (Archive &ar, const uint32_t) |
Serialize the perceptron. | |
size_t | MaxIterations () const |
Get the maximum number of iterations. | |
size_t & | MaxIterations () |
Modify the maximum number of iterations. | |
size_t | NumClasses () const |
Get the number of classes this perceptron has been trained for. | |
const arma::mat & | Weights () const |
Get the weight matrix. | |
arma::mat & | Weights () |
Modify the weight matrix. You had better know what you are doing! | |
const arma::vec & | Biases () const |
Get the biases. | |
arma::vec & | Biases () |
Modify the biases. You had better know what you are doing! | |
This class implements a simple perceptron (i.e., a single layer neural network).
It converges if the supplied training dataset is linearly separable.
LearnPolicy | Options of SimpleWeightUpdate and GradientDescent. |
WeightInitializationPolicy | Option of ZeroInitialization and RandomInitialization. |
mlpack::perceptron::Perceptron< LearnPolicy, WeightInitializationPolicy, MatType >::Perceptron | ( | const size_t | numClasses = 0 , |
const size_t | dimensionality = 0 , |
||
const size_t | maxIterations = 1000 |
||
) |
Constructor: create the perceptron with the given number of classes and initialize the weight matrix, but do not perform any training.
Construct the perceptron with the given number of classes and maximum number of iterations.
(Call the Train() function to perform training.)
numClasses | Number of classes in the dataset. |
dimensionality | Dimensionality of the dataset. |
maxIterations | Maximum number of iterations for the perceptron learning algorithm. |
mlpack::perceptron::Perceptron< LearnPolicy, WeightInitializationPolicy, MatType >::Perceptron | ( | const MatType & | data, |
const arma::Row< size_t > & | labels, | ||
const size_t | numClasses, | ||
const size_t | maxIterations = 1000 |
||
) |
Constructor: constructs the perceptron by building the weights matrix, which is later used in classification.
Constructor - constructs the perceptron.
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.
data | Input, training data. |
labels | Labels of dataset. |
numClasses | Number of classes in the dataset. |
maxIterations | Maximum number of iterations for the perceptron learning algorithm. |
Or rather, builds the weights matrix, which is later used in classification. It adds a bias input vector of 1 to the input data to take care of the bias weights.
data | Input, training data. |
labels | Labels of dataset. |
maxIterations | Maximum number of iterations for the perceptron learning algorithm. |
mlpack::perceptron::Perceptron< LearnPolicy, WeightInitializationPolicy, MatType >::Perceptron | ( | const Perceptron< LearnPolicy, WeightInitializationPolicy, MatType > & | other, |
const MatType & | data, | ||
const arma::Row< size_t > & | labels, | ||
const size_t | numClasses, | ||
const arma::rowvec & | instanceWeights | ||
) |
Alternate constructor which copies parameters from an already initiated perceptron.
other | The other initiated Perceptron object from which we copy the values from. |
data | The data on which to train this Perceptron object on. |
labels | The labels of data. |
numClasses | Number of classes in the data. |
instanceWeights | Weight vector to use while training. For boosting purposes. |
other | The other initiated Perceptron object from which we copy the values from. |
data | The data on which to train this Perceptron object on. |
instanceWeights | Weight vector to use while training. For boosting purposes. |
labels | The labels of data. |
void mlpack::perceptron::Perceptron< LearnPolicy, WeightInitializationPolicy, MatType >::Classify | ( | const MatType & | test, |
arma::Row< size_t > & | predictedLabels | ||
) |
Classification function.
After training, use the weights matrix to classify test, and put the predicted classes in predictedLabels.
test | Testing data or data to classify. |
predictedLabels | Vector to store the predicted classes after classifying test. |
void mlpack::perceptron::Perceptron< LearnPolicy, WeightInitializationPolicy, MatType >::Train | ( | const MatType & | data, |
const arma::Row< size_t > & | labels, | ||
const size_t | numClasses, | ||
const arma::rowvec & | instanceWeights = arma::rowvec() |
||
) |
Train the perceptron on the given data for up to the maximum number of iterations (specified in the constructor or through MaxIterations()).
Training function.
A single iteration corresponds to a single pass through the data, so if you want to pass through the dataset only once, set MaxIterations() to 1.
This training does not reset the model weights, so you can call Train() on multiple datasets sequentially.
data | Dataset on which training should be performed. |
labels | Labels of the dataset. |
numClasses | Number of classes in the data. |
instanceWeights | Cost matrix. Stores the cost of mispredicting instances. This is useful for boosting. |
It trains on trainData using the cost matrix instanceWeights.
data | Data to train on. |
labels | Labels of data. |
instanceWeights | Cost matrix. Stores the cost of mispredicting instances. This is useful for boosting. |