Skip to content
Snippets Groups Projects
Commit 3726a83c authored by Jakhes's avatar Jakhes
Browse files

Update the template Method

parent 6b479247
No related branches found
No related tags found
No related merge requests found
......@@ -9,188 +9,67 @@
// including helper functions for converting between arma structures and arrays
#include "../../helper_files/helper.hpp"
// some of the most used namespaces
using namespace arma;
using namespace mlpack;
using namespace std;
using namespace mlpack::regression;
// Global Variable of the BayesianLinearRegression object so it can be accessed from all functions
BayesianLinearRegression regressor;
// input: const bool , const bool , const size_t , const double
// output: double
void initModel(SP_integer centerData, SP_integer scaleData, SP_integer nIterMax, double tol)
{
regressor = new BayesianLinearRegression((centerData == 1), (scaleData == 1), nIterMax, tol);
}
// Global Variable of the GlobalMethodObject object so it can be accessed from all functions
GlobalMethodObject globalMethodObject;
// TODO:
// input:
// output: double
SP_integer alpha()
{
return regressor.Alpha();
}
// input:
// output: double
SP_integer beta()
{
return regressor.Beta();
}
// input:
// output: const arma::colvec &
float * dataOffset(SP_integer *arraySize)
{
// save the DataOffset output in colvec
colvec armaDataOffset = regressor.DataOffset();
*arraySize = armaDataOffset.n_elem;
return convertToArray(armaDataOffset);
}
// input:
// output: const arma::colvec &
float * dataScale(SP_integer *arraySize)
// output:
// description:
void sampleFunction()
{
// save the DataScale output in colvec
colvec armaDataScale = regressor.DataScale();
*arraySize = armaDataScale.n_elem;
return convertToArray(armaDataScale);
}
// input:
// output: const arma::colvec &
float * omega(SP_integer *arraySize)
{
// save the Omega output in colvec
colvec armaOmega = regressor.Omega();
*arraySize = armaOmega.n_elem;
return convertToArray(armaOmega);
}
// input: const arma::mat &points, arma::rowvec &predictions
// output:
void predict(float *pointsArr, SP_integer pointsSize, SP_integer pointsRowNum, float **predicArr, SP_integer *predicSize)
void takeNumberFunction(SP_integer integerNumber, double doubleNumber)
{
// convert the Prolog arrays to std::vec for easy conversion to arma::mat
mat data = convertArrayToMat(pointsArr, pointsSize, pointsRowNum);
// run the prediction and save the result in arma::rowvec
rowvec predictions;
regressor.Predict(data, predictions);
// give back the sizes and the converted results as arrays
*predicSize = predictions.n_elem;
*predicArr = convertToArray(predictions);
}
// input: const arma::mat &points, arma::rowvec &predictions, arma::rowvec &std
// output:
void predictWithStd(float *pointsArr, SP_integer pointsSize, SP_integer pointsRowNum, float **predicArr, SP_integer *predicSize, float **stdArr, SP_integer *stdSize)
SP_integer returnNumberFunction()
{
// convert the Prolog arrays to std::vec for easy conversion to arma::mat
mat data = convertArrayToMat(pointsArr, pointsSize, pointsRowNum);
// run the prediction and save the results in arma::rowvecs
rowvec predictions;
rowvec std;
regressor.Predict(data, predictions, std);
// give back the sizes and the converted results as arrays
*predicSize = predictions.n_elem;
*stdSize = std.n_elem;
*predicArr = convertToArray(predictions);
*stdArr = convertToArray(std);
return 7;
}
// input:
// output: double
SP_integer responsesOffset()
void takeMatrixFunction(float *givenMatArr, SP_integer givenMatSize, SP_integer givenMatRowNum)
{
return regressor.ResponsesOffset();
// convert the Prolog arrays to arma::mat
mat data = convertArrayToMat(givenMatArr, givenMatSize, givenMatRowNum);
}
// input: const arma::mat &data, const arma::rowvec &responses
// output: double
SP_integer rmse(float *matrix, SP_integer matSize, SP_integer matRowNum, float *arr, SP_integer vecSize)
void takeArrayFunction(float *givenArr, SP_integer givenArrSize)
{
// convert the Prolog arrays to arma::rowvec and arma::mat
rowvec responses = convertArrayToRowvec(arr, vecSize);
mat data = convertArrayToMat(matrix, matSize, matRowNum);
// run the model function and return the error
return regressor.RMSE(data, responses);
// convert the Prolog arrays to arma::rowvec
rowvec givenVector = convertArrayToRowvec(givenArr, givenArrSize);
}
// input: const arma::mat &data, const arma::rowvec &responses
// output:
void train(float *matrix, SP_integer matSize, SP_integer matRowNum, float *arr, SP_integer vecSize)
{
if(matSize / matRowNum != vecSize)
void returnMatrixFunction(float **returnMatArr, SP_integer *returnMatColNum, SP_integer *returnMatRowNum)
{
cout << "Target dim doesnt fit to the Data dim" << endl;
return;
}
// convert the Prolog arrays to arma::rowvec and arma::mat
rowvec responses = convertArrayToRowvec(arr, vecSize);
mat data = convertArrayToMat(matrix, matSize, matRowNum);
// get the ReturnMat
mat toReturnMat = rowvec(3, 3, fill::ones);
// run the model function
regressor.Train(data, responses);
}
// return the Matrix dimensions
*returnMatColNum = toReturnMat.n_cols;
*returnMatRowNum = toReturnMat.n_rows;
// input:
// output: double
SP_integer variance()
{
return regressor.Variance();
// return the Matrix as one long Array
*returnMatArr = convertToArray(toReturnMat);
}
/*
void testing()
void returnArrayFunction(float **returnArr, SP_integer *returnArrSize)
{
// load the iris data
mat dataset;
bool loaded = mlpack::data::Load("iris.csv", dataset);
// split the data into train and test
mat trainData = dataset.cols(1, 4);
trainData.shed_row(trainData.n_rows - 1);
rowvec trainTarget = trainData.row(trainData.n_rows - 1);
trainData.shed_row(trainData.n_rows - 1);
mat testData = dataset.col(dataset.n_cols - 1);
testData.shed_row(testData.n_rows - 1);
rowvec testTarget = testData.row(testData.n_rows - 1);
testData.shed_row(testData.n_rows - 1);
// get the ReturnVector
rowvec toReturnVector = rowvec(3, fill::ones);
// return the Vector lenght
*returnArrSize = toReturnVector.n_elem;
// init the bayesian linear regressor model
// train the model
regressor.Train(trainData, trainTarget);
// predict with the test data
rowvec prediction;
regressor.Predict(testData, prediction);
// compare test target and prediction
cout << "Train Data: " << trainData << endl;
cout << "Train Target: " << trainTarget << endl;
cout << "Alpha" << alpha() << endl;
cout << "Beta" << beta() << endl;
// return the Vector as Array
*returnArr = convertToArray(toReturnVector);
}
\ No newline at end of file
*/
:- module(new_method, [function/1]).
%% requirements of library(struct)
:- load_files(library(str_decl),
[when(compile_time), if(changed)]).
%% needed for using the array type and for reading from csv
%% needed for using the array type
:- use_module(library(structs)).
:- use_module('../../helper_files/helper.pl').
......@@ -12,13 +13,16 @@
float32 = float_32,
float_array = array(float32).
%% definitions for the connected function and what there inputs and output arguments are
%% definitions for the connected function
%% Funktion that takes data from Prolog
%% input
%% output
%% TODO:
%% input:
%% output:
%% description:
foreign(function, c, function(arguments)).
%% +integer , +float32
%% [-integer] , [-float32]
......@@ -34,7 +38,7 @@ foreign(function, c, function(arguments)).
%% array return
%% -pointer(float_array), -integer
%% Defines what functions should be connected from main.cpp
%% Defines the functions that get connected from main.cpp
foreign_resource(new_method, [function]).
:- load_foreign_resource(new_method).
\ 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