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
1e937e41
Commit
1e937e41
authored
Jul 17, 2022
by
Jakhes
Browse files
Options
Downloads
Patches
Plain Diff
adding main file version that can send vecs and mats from prolog to c
parent
4d3709b3
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
+109
-6
109 additions, 6 deletions
main.cpp
main.pl
+66
-5
66 additions, 5 deletions
main.pl
with
175 additions
and
11 deletions
main.cpp
+
109
−
6
View file @
1e937e41
...
...
@@ -3,32 +3,35 @@
Always include the glue header in your foreign resource code.
*/
#include
"main_glue.h"
#include
<mlpack-3.4.2/src/mlpack/methods/randomized_svd/randomized_svd.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 <vector>
using
namespace
arma
;
using
namespace
mlpack
;
using
namespace
std
;
using
namespace
mlpack
::
regression
;
using
namespace
svd
;
BayesianLinearRegression
regressor
;
void
my_function
(
SP_integer
num
)
void
initModel
(
SP_integer
b
)
{
// 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
,
dataset
.
n_cols
-
2
);
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
...
...
@@ -43,6 +46,106 @@ void my_function(SP_integer num)
// compare test target and prediction
cout
<<
"Test Target: "
<<
testTarget
<<
endl
;
cout
<<
"Prediction: "
<<
prediction
<<
endl
;
cout
<<
"Train Data: "
<<
trainData
<<
endl
;
cout
<<
"Train Target: "
<<
trainTarget
<<
endl
;
cout
<<
"Alpha"
<<
alpha
()
<<
endl
;
cout
<<
"Beta"
<<
beta
()
<<
endl
;
}
// input:
SP_integer
alpha
()
{
return
regressor
.
Alpha
();
}
// input:
SP_integer
beta
()
{
return
regressor
.
Beta
();
}
// input:
// output: const arma::colvec &
void
dataOffset
()
{
}
// input:
// output: const arma::colvec &
void
dataScale
()
{
}
// input:
// output: const arma::colvec &
void
omega
()
{
}
// input: const arma::mat &points, arma::rowvec &predictions
void
predict
(
int
a
)
{
}
// input: const arma::mat &points, arma::rowvec &predictions, arma::rowvec &std
void
predict
()
{
}
// input:
SP_integer
responsesOffset
()
{
return
regressor
.
ResponsesOffset
();
}
// input: const arma::mat &data, const arma::rowvec &responses
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
vector
<
float
>
dataVec
;
dataVec
.
assign
(
matrix
,
matrix
+
matSize
);
vector
<
float
>
responsesVec
;
responsesVec
.
assign
(
arr
,
arr
+
vecSize
);
// converting the std::vec to arma::mat
rowvec
responses
=
conv_to
<
rowvec
>::
from
(
responsesVec
);
mat
data
=
conv_to
<
mat
>::
from
(
dataVec
);
data
=
reshape
(
data
,
matRowNum
,
(
matSize
/
matRowNum
));
double
error
=
regressor
.
RMSE
(
data
,
responses
);
return
error
;
}
// input: const arma::mat &data, const arma::rowvec &responses
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
vector
<
float
>
dataVec
;
dataVec
.
assign
(
matrix
,
matrix
+
matSize
);
vector
<
float
>
responsesVec
;
responsesVec
.
assign
(
arr
,
arr
+
vecSize
);
// converting the std::vec to arma::mat
rowvec
responses
=
conv_to
<
rowvec
>::
from
(
responsesVec
);
mat
data
=
conv_to
<
mat
>::
from
(
dataVec
);
data
=
reshape
(
data
,
matRowNum
,
(
matSize
/
matRowNum
));
regressor
.
Train
(
data
,
responses
);
}
// input:
SP_integer
variance
()
{
return
regressor
.
Variance
();
}
This diff is collapsed.
Click to expand it.
main.pl
+
66
−
5
View file @
1e937e41
:-
load_files
(
library
(
str_decl
),
[
when
(
compile_time
),
if
(
changed
)]).
:-
use_module
(
library
(
structs
)).
:-
use_module
(
library
(
csv
)).
foreign
(
my_function
,
c
,
function
(
+
integer
)).
:-
foreign_type
float32
=
float_32
,
float_array
=
array
(
float32
).
%%float_array_array = array(pointer(float_array)).
foreign_resource
(
main
,
[
my_function
]).
foreign
(
initModel
,
c
,
init
(
+
integer
)).
foreign
(
alpha
,
c
,
alpha
([
-
integer
])).
foreign
(
beta
,
c
,
beta
([
-
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
(
variance
,
c
,
variance
([
-
integer
])).
foreign_resource
(
main
,
[
initModel
,
alpha
,
beta
,
rmse
,
train
,
variance
]).
:-
load_foreign_resource
(
main
).
convert_list_to_float_array
(
Arr
,
Row_num
,
array
(
Size
,
Row_num
,
Mem
))
:-
len
(
Arr
,
Size
),
new
(
float_array
,
Size
,
Mem
),
fill_float_array
(
Arr
,
0
,
Mem
).
convert_list_to_float_array
(
Arr
,
array
(
Size
,
Mem
,
10
))
:-
len
(
Arr
,
Size
),
new
(
float_array
,
Size
,
Mem
),
fill_float_array
(
Arr
,
0
,
Mem
).
fill_float_array
([],
_
,
_
).
fill_float_array
([
H
|
Tail
],
Index
,
Mem
)
:-
put_contents
(
Mem
,
Index
,
H
),
New_index
is
Index
+
1
,
fill_float_array
(
Tail
,
New_index
,
Mem
).
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
%% returns the length of a list
len
([],
0
).
len
([
_
|
Tail
],
List_L
)
:-
len
(
Tail
,
Tail_L
),
List_L
is
Tail_L
+
1
.
%% take the elements in a csv record and put them in a list
convert_record_to_arr
([],
[]).
convert_record_to_arr
([
float
(
Num
,
_
)|
Tail
],
[
Num
|
Rest
])
:-
convert_record_to_arr
(
Tail
,
Rest
).
convert_record_to_arr
([
string
(
_
)|
Tail
],
Rest
)
:-
convert_record_to_arr
(
Tail
,
Rest
).
%%user:runtime_entry(start) :-
%% You may consider putting some other code here...
%%write('hello world'),nl.
\ No newline at end of file
take_csv_row
(
Out
)
:-
open
(
'/home/afkjakhes/eclipse-workspace/Prolog mlpack Libary/iris.csv'
,
read
,
A
),
read_record
(
A
,
_
),
read_record
(
A
,
Rec
),
convert_record_to_arr
(
Rec
,
Out
).
\ No newline at end of file
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