Perceptron
An implementation of a perceptron -a single level neural network- for classification.
:- use_module('path/to/.../src/methods/perceptron/perceptron.pl').
%% usage example
TrainData = [5.1,3.5,1.4, 4.9,3.0,1.4, 4.7,3.2,1.3, 4.6,3.1,1.5],
TestData = [3,2,0, 5,1,4, 0,0,4, 3,3,5, 0,5,5, 2,5,5],
perceptron_initModelWithTrain(TrainData, 3, [0,1,0,1], 2, 1000),
perceptron_classify(TestData, 3, PredictList).
Available Predicates
- perceptron_initModelNoTrain/3
- perceptron_initModelWithTrain/5
- perceptron_biases/1
- perceptron_classify/3
- perceptron_train/5
- perceptron_weights/2
perceptron_initModelNoTrain/3
Initilizes the perceptron model and its weight matrix but doesn't train it.
%% predicate definition
perceptron_initModelNoTrain(NumClasses, Dimensionality, MaxIterations) :-
NumClasses >= 0,
Dimensionality > 0,
MaxIterations >= 0,
initModelNoTrainI(NumClasses, Dimensionality, MaxIterations).
%% foreign c++ predicate definition
foreign(initModelNoTrain, c, initModelNoTrainI(+integer, +integer, +integer)).
Parameters
Name | Type | Description | Default |
---|---|---|---|
numClasses | +integer | 0 | |
dimensionality | +integer | Number of classes in the data. | 0 |
maxIterations | +integer | The maximum number of iterations the perceptron is to be run. | 1000 |
perceptron_initModelWithTrain/5
Initilizes the perceptron model and its weight matrix and trains it with the given data.
%% predicate definition
perceptron_initModelWithTrain(DataList, DataRows, LabelsList, NumClasses, MaxIterations) :-
NumClasses >= 0,
MaxIterations >= 0,
convert_list_to_float_array(DataList, DataRows, array(Xsize, Xrownum, X)),
convert_list_to_float_array(LabelsList, array(Ysize, Y)),
initModelWithTrainI(X, Xsize, Xrownum, Y, Ysize, NumClasses, MaxIterations).
%% foreign c++ predicate definition
foreign(initModelWithTrain, c, initModelWithTrainI( +pointer(float_array), +integer, +integer,
+pointer(float_array), +integer,
+integer, +integer)).
Parameters
Name | Type | Description | Default |
---|---|---|---|
data | +matrix | A matrix containing the training set. | - |
labels | +vector | A vector containing labels for the training set. | - |
numClasses | +integer | Number of classes in the data. | 0 |
maxIterations | +integer | The maximum number of iterations the perceptron is to be run. | 1000 |
perceptron_biases/1
Get the biases.
%% predicate definition
perceptron_biases(BiasesList) :-
biasesI(Y, Ysize),
convert_float_array_to_list(Y, Ysize, BiasesList).
%% foreign c++ predicate definition
foreign(biases, c, biasesI(-pointer(float_array), -integer)).
Parameters
Name | Type | Description | Default |
---|---|---|---|
biases | -vector | Get the biases. | - |
perceptron_classify/3
After training, use the weights matrix to classify test, and put the predicted classes in predictedLabels.
%% predicate definition
perceptron_classify(TestList, TestRows, PredictLabelList) :-
convert_list_to_float_array(TestList, TestRows, array(Xsize, Xrows, X)),
classifyI(X, Xsize, Xrows, Y, Ysize),
convert_float_array_to_list(Y, Ysize, PredictLabelList).
%% foreign c++ predicate definition
foreign(classify, c, classifyI( +pointer(float_array), +integer, +integer,
-pointer(float_array), -integer)).
Parameters
Name | Type | Description | Default |
---|---|---|---|
test | +matrix | - | |
predicLabels | -vector | The predicted labels for the test data. | - |
perceptron_train/5
Train the perceptron on the given data for up to the maximum number of iterations. This training does not reset the model weights, so you can call train/8 on multiple datasets sequentially.
%% predicate definition
perceptron_train(DataList, DataRows, LabelsList, NumClasses, WeightsList) :-
NumClasses >= 0,
convert_list_to_float_array(DataList, DataRows, array(Xsize, Xrownum, X)),
convert_list_to_float_array(LabelsList, array(Ysize, Y)),
convert_list_to_float_array(WeightsList, array(Zsize, Z)),
trainI(X, Xsize, Xrownum, Y, Ysize, NumClasses, Z, Zsize).
%% foreign c++ predicate definition
foreign(train, c, trainI( +pointer(float_array), +integer, +integer,
+pointer(float_array), +integer,
+integer,
+pointer(float_array), +integer)).
Parameters
Name | Type | Description | Default |
---|---|---|---|
data | +matrix | A matrix containing the training set. | - |
labels | +vector | A vector containing labels for the training set. | - |
numClasses | +integer | Number of classes in the data. | 0 |
instanceWeights | +vector | Cost matrix. Stores the cost of mispredicting instances. This is useful for boosting. | - |
perceptron_weights/2
Get the weight matrix.
%% predicate definition
perceptron_weights(WeightsList, XCols) :-
weightsI(X, XCols, XRows),
convert_float_array_to_2d_list(X, XCols, XRows, WeightsList).
%% foreign c++ predicate definition
foreign(weights, c, weightsI(-pointer(float_array), -integer, -integer)).
Parameters
Name | Type | Description | Default |
---|---|---|---|
weights | -matrix | Get the weight matrix. | - |
Connected Links/Resources
If you want a more detailed explanation, then go to the python documentation. There is most of the time a good explanation on how the methods work and what the parameters do.
added some of the links from the python documentation