Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Prolog mlpack Library
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
general
stups
Prolog mlpack Library
Commits
5e3ef98f
Commit
5e3ef98f
authored
Jul 18, 2022
by
Jakhes
Browse files
Options
Downloads
Patches
Plain Diff
adding main files that fully implement the BayesianLinReg Method
parent
1e937e41
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
main.cpp
+126
-39
126 additions, 39 deletions
main.cpp
main.pl
+36
-17
36 additions, 17 deletions
main.pl
with
162 additions
and
56 deletions
main.cpp
+
126
−
39
View file @
5e3ef98f
...
@@ -5,7 +5,6 @@
...
@@ -5,7 +5,6 @@
#include
"main_glue.h"
#include
"main_glue.h"
#include
<mlpack-3.4.2/src/mlpack/methods/bayesian_linear_regression/bayesian_linear_regression.hpp>
#include
<mlpack-3.4.2/src/mlpack/methods/bayesian_linear_regression/bayesian_linear_regression.hpp>
#include
<mlpack-3.4.2/src/mlpack/core.hpp>
#include
<mlpack-3.4.2/src/mlpack/core.hpp>
//#include <vector>
using
namespace
arma
;
using
namespace
arma
;
using
namespace
mlpack
;
using
namespace
mlpack
;
...
@@ -14,98 +13,147 @@ using namespace mlpack::regression;
...
@@ -14,98 +13,147 @@ using namespace mlpack::regression;
BayesianLinearRegression
regressor
;
BayesianLinearRegression
regressor
;
// Extra function to reduce some code
void
initModel
(
SP_integer
b
)
float
*
convertArmaToArray
(
colvec
vec
)
{
{
vector
<
float
>
vectorData
=
conv_to
<
vector
<
float
>>::
from
(
vec
);
int
vectorSize
=
vectorData
.
size
();
// load the iris data
// using malloc so the memory is still valid outside of the function
mat
dataset
;
float
*
arr
=
(
float
*
)
malloc
(
sizeof
(
float
)
*
vectorSize
);
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
);
// save the data in a normal array so it can be send to prolog
testData
.
shed_row
(
testData
.
n_rows
-
1
);
for
(
int
i
=
0
;
i
<
vectorSize
;
i
++
)
rowvec
testTarget
=
testData
.
row
(
testData
.
n_rows
-
1
);
{
testData
.
shed_row
(
testData
.
n_rows
-
1
);
arr
[
i
]
=
vectorData
[
i
];
}
return
arr
;
// init the bayesian linear regressor model
}
// train the model
regressor
.
Train
(
trainData
,
trainTarget
);
// predict with the test data
float
*
convertArmaToArray
(
rowvec
vec
)
rowvec
prediction
;
{
regressor
.
Predict
(
testData
,
prediction
);
colvec
newVec
=
conv_to
<
colvec
>::
from
(
vec
);
return
convertArmaToArray
(
newVec
);
}
// compare test target and prediction
// input: const bool , const bool , const size_t , const double
cout
<<
"Train Data: "
<<
trainData
<<
endl
;
// output: double
cout
<<
"Train Target: "
<<
trainTarget
<<
endl
;
void
initModel
(
SP_integer
centerData
,
SP_integer
scaleData
,
SP_integer
nIterMax
,
double
tol
)
cout
<<
"Alpha"
<<
alpha
()
<<
endl
;
{
cout
<<
"Beta"
<<
beta
()
<<
endl
;
bool
centerDataBool
=
(
centerData
==
1
);
bool
scaleDataBool
=
(
scaleData
==
1
);
regressor
=
new
BayesianLinearRegression
(
centerDataBool
,
scaleDataBool
,
nIterMax
,
tol
);
}
}
// input:
// input:
// output: double
SP_integer
alpha
()
SP_integer
alpha
()
{
{
return
regressor
.
Alpha
();
return
regressor
.
Alpha
();
}
}
// input:
// input:
// output: double
SP_integer
beta
()
SP_integer
beta
()
{
{
return
regressor
.
Beta
();
return
regressor
.
Beta
();
}
}
// input:
// input:
// output: const arma::colvec &
// output: const arma::colvec &
void
dataOffset
()
float
*
dataOffset
(
SP_integer
*
arraySize
)
{
{
// save the DataOffset output in colvec
colvec
armaDataOffset
=
regressor
.
DataOffset
();
*
arraySize
=
armaDataOffset
.
n_elem
;
return
convertArmaToArray
(
armaDataOffset
);
}
}
// input:
// input:
// output: const arma::colvec &
// output: const arma::colvec &
void
dataScale
()
float
*
dataScale
(
SP_integer
*
arraySize
)
{
{
// save the DataScale output in colvec
colvec
armaDataScale
=
regressor
.
DataScale
();
*
arraySize
=
armaDataScale
.
n_elem
;
return
convertArmaToArray
(
armaDataScale
);
}
}
// input:
// input:
// output: const arma::colvec &
// output: const arma::colvec &
void
omega
(
)
float
*
omega
(
SP_integer
*
arraySize
)
{
{
// save the Omega output in colvec
colvec
armaOmega
=
regressor
.
Omega
();
*
arraySize
=
armaOmega
.
n_elem
;
return
convertArmaToArray
(
armaOmega
);
}
}
// input: const arma::mat &points, arma::rowvec &predictions
// input: const arma::mat &points, arma::rowvec &predictions
void
predict
(
int
a
)
// output:
void
predict
(
float
*
pointsArr
,
SP_integer
pointsSize
,
SP_integer
pointsRowNum
,
float
**
predicArr
,
SP_integer
*
predicSize
)
{
{
// convert the Prolog arrays to std::vec for easy conversion to arma::mat
vector
<
float
>
dataVec
;
dataVec
.
assign
(
pointsArr
,
pointsArr
+
pointsSize
);
mat
data
=
conv_to
<
mat
>::
from
(
dataVec
);
data
=
reshape
(
data
,
pointsRowNum
,
(
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
=
convertArmaToArray
(
predictions
);
}
}
// input: const arma::mat &points, arma::rowvec &predictions, arma::rowvec &std
// input: const arma::mat &points, arma::rowvec &predictions, arma::rowvec &std
void
predict
()
// output:
void
predictWithStd
(
float
*
pointsArr
,
SP_integer
pointsSize
,
SP_integer
pointsRowNum
,
float
**
predicArr
,
SP_integer
*
predicSize
,
float
**
stdArr
,
SP_integer
*
stdSize
)
{
{
// convert the Prolog arrays to std::vec for easy conversion to arma::mat
vector
<
float
>
dataVec
;
dataVec
.
assign
(
pointsArr
,
pointsArr
+
pointsSize
);
// converting the std::vec to arma::mat
mat
data
=
conv_to
<
mat
>::
from
(
dataVec
);
data
=
reshape
(
data
,
pointsRowNum
,
(
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
=
convertArmaToArray
(
predictions
);
*
stdArr
=
convertArmaToArray
(
std
);
}
}
// input:
// input:
// output: double
SP_integer
responsesOffset
()
SP_integer
responsesOffset
()
{
{
return
regressor
.
ResponsesOffset
();
return
regressor
.
ResponsesOffset
();
}
}
// input: const arma::mat &data, const arma::rowvec &responses
// 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
)
SP_integer
rmse
(
float
*
matrix
,
SP_integer
matSize
,
SP_integer
matRowNum
,
float
*
arr
,
SP_integer
vecSize
)
{
{
// convert the Prolog arrays to std::vec for easy conversion to arma::mat
// convert the Prolog arrays to std::vec for easy conversion to arma::mat
...
@@ -125,6 +173,7 @@ SP_integer rmse(float *matrix, SP_integer matSize, SP_integer matRowNum, float *
...
@@ -125,6 +173,7 @@ SP_integer rmse(float *matrix, SP_integer matSize, SP_integer matRowNum, float *
}
}
// input: const arma::mat &data, const arma::rowvec &responses
// 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
)
void
train
(
float
*
matrix
,
SP_integer
matSize
,
SP_integer
matRowNum
,
float
*
arr
,
SP_integer
vecSize
)
{
{
// convert the Prolog arrays to std::vec for easy conversion to arma::mat
// convert the Prolog arrays to std::vec for easy conversion to arma::mat
...
@@ -144,8 +193,46 @@ void train(float *matrix, SP_integer matSize, SP_integer matRowNum, float *arr,
...
@@ -144,8 +193,46 @@ void train(float *matrix, SP_integer matSize, SP_integer matRowNum, float *arr,
}
}
// input:
// input:
// output: double
SP_integer
variance
()
SP_integer
variance
()
{
{
return
regressor
.
Variance
();
return
regressor
.
Variance
();
}
}
/*
void testing()
{
// 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);
// 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;
}
*/
This diff is collapsed.
Click to expand it.
main.pl
+
36
−
17
View file @
5e3ef98f
...
@@ -7,26 +7,33 @@
...
@@ -7,26 +7,33 @@
:-
foreign_type
:-
foreign_type
float32
=
float_32
,
float32
=
float_32
,
float_array
=
array
(
float32
).
float_array
=
array
(
float32
).
%%float_array_array = array(pointer(float_array)).
foreign
(
initModel
,
c
,
init
(
+
integer
)).
foreign
(
initModel
,
c
,
init
Model
(
+
integer
,
+
integer
,
+
integer
,
+
float32
)).
foreign
(
alpha
,
c
,
alpha
([
-
integer
])).
foreign
(
alpha
,
c
,
alpha
([
-
integer
])).
foreign
(
beta
,
c
,
beta
([
-
integer
])).
foreign
(
beta
,
c
,
beta
([
-
integer
])).
foreign
(
dataOffset
,
c
,
dataOffset
(
-
integer
,
[
-
pointer
(
float_array
)])).
foreign
(
dataScale
,
c
,
dataScale
(
-
integer
,
[
-
pointer
(
float_array
)])).
foreign
(
omega
,
c
,
omega
(
-
integer
,
[
-
pointer
(
float_array
)])).
foreign
(
predict
,
c
,
predict
(
+
pointer
(
float_array
),
+
integer
,
+
integer
,
-
pointer
(
float_array
),
-
integer
)).
foreign
(
predictWithStd
,
c
,
predictWithStd
(
+
pointer
(
float_array
),
+
integer
,
+
integer
,
-
pointer
(
float_array
),
-
integer
,
-
pointer
(
float_array
),
-
integer
)).
foreign
(
rmse
,
c
,
rmse
(
+
pointer
(
float_array
),
+
integer
,
+
integer
,
+
pointer
(
float_array
),
+
integer
,
[
-
integer
])).
foreign
(
rmse
,
c
,
rmse
(
+
pointer
(
float_array
),
+
integer
,
+
integer
,
+
pointer
(
float_array
),
+
integer
,
[
-
integer
])).
foreign
(
train
,
c
,
train
(
+
pointer
(
float_array
),
+
integer
,
+
integer
,
+
pointer
(
float_array
),
+
integer
)).
foreign
(
train
,
c
,
train
(
+
pointer
(
float_array
),
+
integer
,
+
integer
,
+
pointer
(
float_array
),
+
integer
)).
foreign
(
variance
,
c
,
variance
([
-
integer
])).
foreign
(
variance
,
c
,
variance
([
-
integer
])).
foreign_resource
(
main
,
[
initModel
,
alpha
,
beta
,
rmse
,
train
,
variance
]).
foreign_resource
(
main
,
[
initModel
,
alpha
,
beta
,
dataOffset
,
dataScale
,
omega
,
predict
,
predictWithStd
,
rmse
,
train
,
variance
]).
:-
load_foreign_resource
(
main
).
:-
load_foreign_resource
(
main
).
%% Functions for editing float arrays
convert_list_to_float_array
(
Arr
,
Row_num
,
array
(
Size
,
Row_num
,
Mem
))
:-
convert_list_to_float_array
(
Arr
,
Row_num
,
array
(
Size
,
Row_num
,
Mem
))
:-
len
(
Arr
,
Size
),
len
(
Arr
,
Size
),
new
(
float_array
,
Size
,
Mem
),
new
(
float_array
,
Size
,
Mem
),
fill_float_array
(
Arr
,
0
,
Mem
).
fill_float_array
(
Arr
,
0
,
Mem
).
convert_list_to_float_array
(
Arr
,
array
(
Size
,
Mem
,
10
))
:-
convert_list_to_float_array
(
Arr
,
array
(
Size
,
Mem
))
:-
len
(
Arr
,
Size
),
len
(
Arr
,
Size
),
new
(
float_array
,
Size
,
Mem
),
new
(
float_array
,
Size
,
Mem
),
fill_float_array
(
Arr
,
0
,
Mem
).
fill_float_array
(
Arr
,
0
,
Mem
).
...
@@ -37,17 +44,15 @@ fill_float_array([H|Tail], Index, Mem) :-
...
@@ -37,17 +44,15 @@ fill_float_array([H|Tail], Index, Mem) :-
New_index
is
Index
+
1
,
New_index
is
Index
+
1
,
fill_float_array
(
Tail
,
New_index
,
Mem
).
fill_float_array
(
Tail
,
New_index
,
Mem
).
convert_float_array_to_list
(
Mem
,
Count
,
Out
)
:-
convert_float_array_to_list
(
Mem
,
0
,
Count
,
Out
).
convert_float_array_to_list
(
_
,
Count
,
Count
,
[]).
convert_float_array_to_list
(
Mem
,
Index
,
Count
,
[
Val
|
Rest
])
:-
NewIndex
is
Index
+
1
,
convert_float_array_to_list
(
Mem
,
NewIndex
,
Count
,
Rest
),
get_contents
(
Mem
,
Index
,
Val
).
fire
:-
convert_list_to_float_array
([
5.1
,
3.5
,
1.4
,
4.9
,
3.0
,
1.4
,
4.7
,
3.2
,
1.3
,
4.6
,
3.1
,
1.5
],
3
,
array
(
Xsize
,
Xrownum
,
X
)),
convert_list_to_float_array
([
0.2
,
0.2
,
0.2
,
0.2
],
array
(
Ysize
,
Y
,
_
)),
train
(
X
,
Xsize
,
Xrownum
,
Y
,
Ysize
).
%% Funktions for reading the csv Files
%% Funktions for reading the csv Files
...
@@ -66,9 +71,23 @@ convert_record_to_arr([string(_)|Tail], Rest) :-
...
@@ -66,9 +71,23 @@ convert_record_to_arr([string(_)|Tail], Rest) :-
convert_record_to_arr
(
Tail
,
Rest
).
convert_record_to_arr
(
Tail
,
Rest
).
take_csv_row
(
Out
)
:-
take_csv_row
(
Out
)
:-
open
(
'/home/afkjakhes/eclipse-workspace/Prolog mlpack Libary/iris.csv'
,
read
,
A
),
open
(
'/home/afkjakhes/eclipse-workspace/Prolog mlpack Libary/iris.csv'
,
read
,
A
),
read_record
(
A
,
_
),
read_record
(
A
,
_
),
read_record
(
A
,
Rec
),
read_record
(
A
,
Rec
),
convert_record_to_arr
(
Rec
,
Out
).
convert_record_to_arr
(
Rec
,
Out
).
%% Some funktions that use the BayesianLinearRegression
predict
(
PredictList
,
StdList
)
:-
train
,
convert_list_to_float_array
([
5.1
,
3.5
,
1.4
,
4.9
,
3.0
,
1.4
,
4.7
,
3.2
,
1.3
,
4.6
,
3.1
,
1.5
],
3
,
array
(
Xsize
,
Xrownum
,
X
)),
predictWithStd
(
X
,
Xsize
,
Xrownum
,
Predic
,
PredicSize
,
Std
,
StdSize
),
convert_float_array_to_list
(
Predic
,
PredicSize
,
PredictList
),
convert_float_array_to_list
(
Std
,
StdSize
,
StdList
).
train
:-
convert_list_to_float_array
([
5.1
,
3.5
,
1.4
,
4.9
,
3.0
,
1.4
,
4.7
,
3.2
,
1.3
,
4.6
,
3.1
,
1.5
],
3
,
array
(
Xsize
,
Xrownum
,
X
)),
convert_list_to_float_array
([
0.2
,
0.2
,
0.2
,
0.2
],
array
(
Ysize
,
Y
)),
train
(
X
,
Xsize
,
Xrownum
,
Y
,
Ysize
).
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment