... | ... | @@ -2,26 +2,43 @@ |
|
|
|
|
|
An implementation of a perceptron -a single level neural network- for classification.
|
|
|
|
|
|
```prolog
|
|
|
:- 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
|
|
|
|
|
|
* [initModelNoTrain/3](/PrologMethods/Classification/perceptron#initmodelnotrain3)
|
|
|
* [initModelWithTrain/7](/PrologMethods/Classification/perceptron#initmodelwithtrain7)
|
|
|
* [biases/2](/PrologMethods/Classification/perceptron#biases2)
|
|
|
* [classify/5](/PrologMethods/Classification/perceptron#classify5)
|
|
|
* [train/8](/PrologMethods/Classification/perceptron#train8)
|
|
|
* [weights/3](/PrologMethods/Classification/perceptron#weights3)
|
|
|
* [perceptron_initModelNoTrain/3](/PrologMethods/Classification/perceptron#perceptron_initmodelnotrain3)
|
|
|
* [perceptron_initModelWithTrain/5](/PrologMethods/Classification/perceptron#perceptron_initmodelwithtrain5)
|
|
|
* [perceptron_biases/1](/PrologMethods/Classification/perceptron#perceptron_biases1)
|
|
|
* [perceptron_classify/3](/PrologMethods/Classification/perceptron#perceptron_classify3)
|
|
|
* [perceptron_train/5](/PrologMethods/Classification/perceptron#perceptron_train5)
|
|
|
* [perceptron_weights/2](/PrologMethods/Classification/perceptron#perceptron_weights2)
|
|
|
|
|
|
---
|
|
|
|
|
|
[links/resources](/PrologMethods/Classification/perceptron#connected-linksresources)
|
|
|
|
|
|
## **_initModelNoTrain/3_**
|
|
|
## **_perceptron_initModelNoTrain/3_**
|
|
|
|
|
|
Initilizes the perceptron model and its weight matrix but doesn't train it.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
initModelNoTrain(+integer, +integer, +integer).
|
|
|
%% 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
|
... | ... | @@ -33,15 +50,24 @@ initModelNoTrain(+integer, +integer, +integer). |
|
|
|
|
|
---
|
|
|
|
|
|
## **_initModelWithTrain/7_**
|
|
|
## **_perceptron_initModelWithTrain/5_**
|
|
|
|
|
|
Initilizes the perceptron model and its weight matrix and trains it with the given data.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
initModelWithTrain( +pointer(float_array), +integer, +integer,
|
|
|
%% 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).
|
|
|
+integer, +integer)).
|
|
|
|
|
|
```
|
|
|
|
|
|
### Parameters
|
... | ... | @@ -54,13 +80,18 @@ initModelWithTrain( +pointer(float_array), +integer, +integer, |
|
|
|
|
|
---
|
|
|
|
|
|
## **_biases/2_**
|
|
|
## **_perceptron_biases/1_**
|
|
|
|
|
|
Get the biases.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
biases(-pointer(float_array), -integer).
|
|
|
%% 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
|
... | ... | @@ -70,14 +101,20 @@ biases(-pointer(float_array), -integer). |
|
|
|
|
|
---
|
|
|
|
|
|
## **_classify/5_**
|
|
|
## **_perceptron_classify/3_**
|
|
|
|
|
|
After training, use the weights matrix to classify test, and put the predicted classes in predictedLabels.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
classify( +pointer(float_array), +integer, +integer,
|
|
|
-pointer(float_array), -integer).
|
|
|
%% 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
|
... | ... | @@ -88,16 +125,24 @@ classify( +pointer(float_array), +integer, +integer, |
|
|
|
|
|
---
|
|
|
|
|
|
## **_train/8_**
|
|
|
## **_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.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
train( +pointer(float_array), +integer, +integer,
|
|
|
%% 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).
|
|
|
+pointer(float_array), +integer)).
|
|
|
```
|
|
|
|
|
|
### Parameters
|
... | ... | @@ -110,13 +155,18 @@ train( +pointer(float_array), +integer, +integer, |
|
|
|
|
|
---
|
|
|
|
|
|
## **_weights/3_**
|
|
|
## **_perceptron_weights/2_**
|
|
|
|
|
|
Get the weight matrix.
|
|
|
|
|
|
```prolog
|
|
|
%% part of the predicate definition
|
|
|
weights(-pointer(float_array), -integer, -integer).
|
|
|
%% 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
|
... | ... | @@ -130,8 +180,8 @@ weights(-pointer(float_array), -integer, -integer). |
|
|
|
|
|
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.
|
|
|
|
|
|
* [**MLpack::perceptron_C++\_documentation**](https://www.mlpack.org/doc/stable/doxygen/classmlpack_1_1perceptron_1_1Perceptron.html)
|
|
|
* [**MLpack::perceptron_Python_documentation**](https://www.mlpack.org/doc/stable/python_documentation.html#perceptron)
|
|
|
* [**MLpack::perceptron_C++\_documentation**](https://www.mlpack.org/doc/mlpack-3.4.2/doxygen/classmlpack_1_1perceptron_1_1Perceptron.html)
|
|
|
* [**MLpack::perceptron_Python_documentation**](https://www.mlpack.org/doc/mlpack-3.4.2/python_documentation.html#perceptron)
|
|
|
|
|
|
added some of the links from the python documentation
|
|
|
|
... | ... | |