Sparse Coding
An implementation of Sparse Coding with Dictionary Learning. Given a dataset, this will decompose the dataset into a sparse combination of a few dictionary elements, where the dictionary is learned during computation.
:- use_module('path/to/.../src/methods/sparse_coding/sparse_coding.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],
sparse_coding_initModelWithTrain(TrainData, 3, 15, 0.1, 0.0, 100, 0.01, 0.000001),
sparse_coding_encode(TestData, 3, CodesList, CodesRows).
Available Predicates
- sparse_coding_initModelWithTrain/8
- sparse_coding_initModelNoTrain/6
- sparse_coding_encode/4
- sparse_coding_train/3
sparse_coding_initModelWithTrain/8
Initializes sparse_coding model and trains it.
%% predicate definition
sparse_coding_initModelWithTrain(MatList, MatRows, Atoms, Lambda1, Lambda2, MaxIterations, ObjTolerance, NewtonTolerance) :-
Atoms >= 0,
Lambda1 > 0,
Lambda2 >= 0,
MaxIterations >= 0,
ObjTolerance > 0,
NewtonTolerance > 0,
convert_list_to_float_array(MatList, MatRows, array(Xsize, Xrownum, X)),
initModelWithTrainI(X, Xsize, Xrownum, Atoms, Lambda1, Lambda2, MaxIterations, ObjTolerance, NewtonTolerance).
%% foreign c++ predicate definition
foreign(initModelWithTrain, c, initModelWithTrainI( +pointer(float_array), +integer, +integer,
+integer, +float32, +float32, +integer, +float32, +float32)).
Parameters
Name | Type | Description | Default |
---|---|---|---|
data | +matrix | Matrix of training data. | - |
atoms | +integer | Number of atoms in the dictionary. | 15 |
lambda1 | +float | Sparse coding l1-norm regularization parameter. | 0.0 |
lambda2 | +float | Sparse coding l2-norm regularization parameter. | 0.0 |
maxIterations | +integer | Maximum number of iterations for sparse coding (0 indicates no limit). | 0 |
objTolerance | +float | Tolerance for convergence of the objective function. | 0.01 |
newtonTolerance | +float | Tolerance for convergence of Newton method. | 1e-6 |
sparse_coding_initModelNoTrain/6
Initializes sparse_coding model but will not train the model, and a subsequent call to Train will be required before the model can encode points with Encode.
%% predicate definition
sparse_coding_initModelNoTrain(Atoms, Lambda1, Lambda2, MaxIterations, ObjTolerance, NewtonTolerance) :-
Atoms >= 0,
Lambda1 > 0,
Lambda2 >= 0,
MaxIterations >= 0,
ObjTolerance > 0,
NewtonTolerance > 0,
initModelNoTrainI(Atoms, Lambda1, Lambda2, MaxIterations, ObjTolerance, NewtonTolerance).
%% foreign c++ predicate definition
foreign(initModelNoTrain, c, initModelNoTrainI(+integer, +float32, +float32, +integer, +float32, +float32)).
Parameters
Name | Type | Description | Default |
---|---|---|---|
atoms | +integer | Number of atoms in the dictionary. | 15 |
lambda1 | +float | Sparse coding l1-norm regularization parameter. | 0.0 |
lambda2 | +float | Sparse coding l2-norm regularization parameter. | 0.0 |
maxIterations | +integer | Maximum number of iterations for sparse coding (0 indicates no limit). | 0 |
objTolerance | +float | Tolerance for convergence of the objective function. | 0.01 |
newtonTolerance | +float | Tolerance for convergence of Newton method. | 1e-6 |
sparse_coding_encode/4
Sparse code each point in the given dataset via LARS, using the current dictionary and store the encoded data in the codes matrix.
%% predicate definition
sparse_coding_encode(DataList, DataRows, CodesList, YCols) :-
convert_list_to_float_array(DataList, DataRows, array(Xsize, Xrownum, X)),
encodeI(X, Xsize, Xrownum, Y, YCols, YRows),
convert_float_array_to_2d_list(Y, YCols, YRows, CodesList).
%% foreign c++ predicate definition
foreign(encode, c, encodeI( +pointer(float_array), +integer, +integer,
-pointer(float_array), -integer, -integer)).
Parameters
Name | Type | Description | Default |
---|---|---|---|
data | +matrix | Input data matrix to be encoded. | - |
codes | -matrix | Output codes matrix. | - |
sparse_coding_train/3
Train the sparse coding model on the given dataset.
%% predicate definition
sparse_coding_train(DataList, DataRows, ReturnValue) :-
convert_list_to_float_array(DataList, DataRows, array(Xsize, Xrownum, X)),
trainI(X, Xsize, Xrownum, ReturnValue).
%% foreign c++ predicate definition
foreign(train, c, trainI( +pointer(float_array), +integer, +integer,
[-float32])).
Parameters
Name | Type | Description | Default |
---|---|---|---|
data | +matrix | Matrix of training data. | - |
objValue | -float | The final objective value. | - |
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