Skip to content
Snippets Groups Projects
Commit 7913021e authored by Jakhes's avatar Jakhes
Browse files

Finishing lcc

parent f26add63
No related branches found
No related tags found
No related merge requests found
......@@ -13,32 +13,170 @@
using namespace arma;
using namespace mlpack;
using namespace std;
using namespace mlpack::lcc;
// Global Variable of the GlobalMethodObject object so it can be accessed from all functions
GlobalMethodObject globalMethodObject;
// Global Variable of the LocalCoordinateCoding object so it can be accessed from all functions
LocalCoordinateCoding lccGlobObj;
bool normalizeData = false;
// TODO:
// input:
// input: const arma::mat & data,
// const size_t atoms,
// const double lambda,
// const size_t maxIterations = 0,
// const double tolerance = 0.01,
// const DictionaryInitializer & initializer = DictionaryInitializer()
// output:
// description:
void sampleFunction()
void initModelWithTrain(float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRowNum,
SP_integer normalize, SP_integer atoms, double lambda, SP_integer maxIterations, double tolerance)
{
normalizeData = normalize == 1;
// convert the Prolog arrays to arma::mat
mat data = convertArrayToMat(dataMatArr, dataMatSize, dataMatRowNum);
if (normalizeData)
{
Log::Info << "Normalizing data before coding..." << endl;
for (size_t i = 0; i < data.n_cols; ++i)
data.col(i) /= arma::norm(data.col(i), 2);
}
lccGlobObj = LocalCoordinateCoding(data, atoms, lambda, maxIterations, tolerance);
}
// TODO:
// input: const size_t atoms = 0,
// const double lambda = 0.0,
// const size_t maxIterations = 0,
// const double tolerance = 0.01
// output:
// description:
void initModelNoTrain(SP_integer normalize, SP_integer atoms, double lambda, SP_integer maxIterations, double tolerance)
{
normalizeData = normalize == 1;
lccGlobObj = LocalCoordinateCoding(atoms, lambda, maxIterations, tolerance);
}
// TODO:
// input: const arma::mat & data,
// arma::mat & codes <-
// output:
// description:
void encode(float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRowNum,
float **codesMatArr, SP_integer *codesMatColNum, SP_integer *codesMatRowNum)
{
// convert the Prolog arrays to arma::mat
mat data = convertArrayToMat(dataMatArr, dataMatSize, dataMatRowNum);
// create the ReturnMat
mat codesReturnMat;
// Normalize each point if the user asked for it.
if (IO::HasParam("normalize"))
{
Log::Info << "Normalizing test data before coding..." << endl;
for (size_t i = 0; i < data.n_cols; ++i)
data.col(i) /= arma::norm(data.col(i), 2);
}
lccGlobObj.Encode(data, codesReturnMat);
// return the Matrix
returnMatrixInformation(codesReturnMat, codesMatArr, codesMatColNum, codesMatRowNum);
}
// TODO:
// input: const arma::mat & data,
// const arma::mat & codes,
// const arma::uvec & adjacencies
// output:
// description:
void objective(float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRowNum,
float **codesMatArr, SP_integer *codesMatColNum, SP_integer *codesMatRowNum,
float **adjacenciesArr, SP_integer *adjacenciesArrSize)
{
// convert the Prolog arrays to arma::mat
mat data = convertArrayToMat(dataMatArr, dataMatSize, dataMatRowNum);
// create the ReturnMat
mat codesReturnMat;
// create the ReturnVector
uvec adjacenciesReturnVector;
// Normalize each point if the user asked for it.
if (IO::HasParam("normalize"))
{
Log::Info << "Normalizing test data before coding..." << endl;
for (size_t i = 0; i < data.n_cols; ++i)
data.col(i) /= arma::norm(data.col(i), 2);
}
lccGlobObj.Objective(data, codesReturnMat, adjacenciesReturnVector);
// return the Matrix
returnMatrixInformation(codesReturnMat, codesMatArr, codesMatColNum, codesMatRowNum);
// return the Vector
returnVectorInformation(arma::conv_to<vec>::from(adjacenciesReturnVector), adjacenciesArr, adjacenciesArrSize);
}
// TODO:
// input: const arma::mat & data,
// const arma::mat & codes,
// const arma::uvec & adjacencies
// output:
// description:
void optimizeDictionary(float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRowNum,
float **codesMatArr, SP_integer *codesMatColNum, SP_integer *codesMatRowNum,
float **adjacenciesArr, SP_integer *adjacenciesArrSize)
{
// convert the Prolog arrays to arma::mat
mat data = convertArrayToMat(dataMatArr, dataMatSize, dataMatRowNum);
// create the ReturnMat
mat codesReturnMat;
// create the ReturnVector
uvec adjacenciesReturnVector;
// Normalize each point if the user asked for it.
if (IO::HasParam("normalize"))
{
Log::Info << "Normalizing test data before coding..." << endl;
for (size_t i = 0; i < data.n_cols; ++i)
data.col(i) /= arma::norm(data.col(i), 2);
}
lccGlobObj.Objective(data, codesReturnMat, adjacenciesReturnVector);
// return the Matrix
returnMatrixInformation(codesReturnMat, codesMatArr, codesMatColNum, codesMatRowNum);
// return the Vector
returnVectorInformation(arma::conv_to<vec>::from(adjacenciesReturnVector), adjacenciesArr, adjacenciesArrSize);
}
// TODO:
// input: const arma::mat & data,
// const DictionaryInitializer & initializer = DictionaryInitializer()
// output: double final objective value
// description:
double train(float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRowNum)
{
// convert the Prolog arrays to arma::mat
mat data = convertArrayToMat(dataMatArr, dataMatSize, dataMatRowNum);
if (normalizeData)
{
Log::Info << "Normalizing data before coding..." << endl;
for (size_t i = 0; i < matX.n_cols; ++i)
matX.col(i) /= norm(matX.col(i), 2);
}
}
// Basic input parameters
//
// SP_integer integerNumber, double doubleNumber, char const *string
//
// Snippets
//
// Take Matrix: mat-take, take-mat
// Take Array: arr-take, take-arr
//
// Return Matrix: mat-return, return-mat
// Return Array: arr-return, return-arr
for (size_t i = 0; i < data.n_cols; ++i)
data.col(i) /= arma::norm(data.col(i), 2);
}
return lccGlobObj.Train(data);
}
\ No newline at end of file
:- module(local_coordinate_coding, [function/1]).
:- module(local_coordinate_coding, [ initModelWithTrain/8,
initModelNoTrain/5,
encode/6,
objective/8,
optimizeDictionary/8,
train/4]).
%% requirements of library(struct)
:- load_files(library(str_decl),
......@@ -17,32 +22,81 @@
%% TODO:
%% --Input--
%% mat data,
%% bool normalize => (1)true / (0)false,
%% int atoms,
%% float32 lambda,
%% int maxIterations => 0,
%% float32 tolerance => 0.01
%%
%% --Output--
%%
%% --Description--
foreign(function, c, function(arguments)).
foreign(initModelWithTrain, c, initModelWithTrain(+pointer(float_array), +integer, +integer,
+integer, +integer, +float32, +integer, +float32)).
%% TODO:
%% --Input--
%% bool normalize => (1)true / (0)false,
%% int atoms,
%% float32 lambda,
%% int maxIterations => 0,
%% float32 tolerance => 0.01
%%
%% --Output--
%%
%% --Description--
foreign(initModelNoTrain, c, initModelNoTrain(+integer, +integer, +float32, +integer, +float32)).
%% TODO:
%% --Input--
%% mat data
%%
%% --Output--
%% mat codes
%%
%% --Description--
foreign(encode, c, encode(+pointer(float_array), +integer, +integer, -pointer(float_array), -integer, -integer)).
%% +integer , +float32, +string
%% [-integer] , [-float32], [-string]
%% matrix input
%% +pointer(float_array), +integer, +integer
%% array input
%% +pointer(float_array), +integer
%% TODO:
%% --Input--
%% mat data
%%
%% --Output--
%% mat codes,
%% vec adjacencies
%%
%% --Description--
foreign(objective, c, objective(+pointer(float_array), +integer, +integer, -pointer(float_array), -integer, -integer, -pointer(float_array), -integer)).
%% matrix return
%% -pointer(float_array), -integer, -integer
%% TODO:
%% --Input--
%% mat data
%%
%% --Output--
%% mat codes,
%% vec adjacencies
%%
%% --Description--
foreign(optimizeDictionary, c, optimizeDictionary(+pointer(float_array), +integer, +integer, -pointer(float_array), -integer, -integer, -pointer(float_array), -integer)).
%% array return
%% -pointer(float_array), -integer
%% TODO:
%% --Input--
%% mat data
%%
%% --Output--
%% float32 final objective value
%%
%% --Description--
foreign(train, c, train(+pointer(float_array), +integer, +integer, [-float32])).
%% bool name => (1)true / (0)false
%% Defines the functions that get connected from main.cpp
foreign_resource(local_coordinate_coding, [function]).
foreign_resource(local_coordinate_coding, [ initModelWithTrain,
initModelNoTrain,
encode,
objective,
optimizeDictionary,
train]).
:- load_foreign_resource(local_coordinate_coding).
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment