Select Git revision
softmax_regression.pl
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
softmax_regression.pl 6.69 KiB
:- module(softmax_regression, [ initModelNoTrain/3,
initModelWithTrain/6,
classifyPoint/2,
classifyMatrix/5,
computeAccuracy/4,
featureSize/1,
parameters/2,
train/5]).
%% requirements of library(struct)
:- load_files(library(str_decl),
[when(compile_time), if(changed)]).
%% needed for using the array type
:- use_module(library(structs)).
:- use_module('../../helper_files/helper.pl').
%% type definitions for the float array
:- foreign_type
float32 = float_32,
float_array = array(float32).
%% definitions for the connected function
%% --Input--
%% int inputSize => 1,
%% int numClasses => 0,
%% bool fitIntercept => (1)true / (0)false => false
%%
%% --Output--
%%
%% --Description--
%% Initializes the softmax_regression model without training.
%% Be sure to use Train before calling Classif or ComputeAccuracy, otherwise the results may be meaningless.
%%
initModelNoTrain(InputSize, NumClasses, FitIntercept) :-
InputSize > 0,
NumClasses >= 0,
initModelNoTrainI(InputSize, NumClasses, FitIntercept).
foreign(initModelNoTrain, c, initModelNoTrainI( +integer, +integer,
+integer)).
%% --Input--
%% mat data,
%% vec labels,
%% int numClasses => 0,
%% float32 lambda => 0.0001,
%% bool fitIntercept => (1)true / (0)false => false
%%
%% --Output--
%%
%% --Description--
%% Initializes the softmax_regression model and trains it.
%%
initModelWithTrain(DataList, DataRows, LabelsList, NumClasses, Lambda, FitIntercept) :-
NumClasses >= 0,
Lambda >= 0,
convert_list_to_float_array(DataList, DataRows, array(Xsize, Xrownum, X)),
convert_list_to_float_array(LabelsList, array(Ysize, Y)),
initModelWithTrainI(X, Xsize, Xrownum, Y, Ysize, NumClasses, Lambda, FitIntercept).
foreign(initModelWithTrain, c, initModelWithTrainI( +pointer(float_array), +integer, +integer,
+pointer(float_array), +integer,
+integer, +float32,
+integer)).
%% --Input--
%% vec point
%%
%% --Output--
%% int predicted label
%%
%% --Description--
%% Classify the given point.
%%
classifyPoint(DataList, Prediction) :-
convert_list_to_float_array(DataList, array(Xsize, X)),
classifyPointI(X, Xsize, Prediction).
foreign(classifyPoint, c, classifyPointI(+pointer(float_array), +integer,
[-integer])).
%% --Input--
%% mat data
%%
%% --Output--
%% vec predicted labels,
%% mat probabilities
%%
%% --Description--
%% Classify the given points, returning class probabilities and predicted class label for each point.
%%
classifyMatrix(DataList, DataRows, PredictionList, ProbsList, ZCols) :-
convert_list_to_float_array(DataList, DataRows, array(Xsize, Xrows, X)),
classifyMatrixI(X, Xsize, Xrows, Y, Ysize, Z, ZCols, ZRows),
convert_float_array_to_list(Y, Ysize, PredictionList),
convert_float_array_to_2d_list(Z, ZCols, ZRows, ProbsList).
foreign(classifyMatrix, c, classifyMatrixI( +pointer(float_array), +integer, +integer,
-pointer(float_array), -integer,
-pointer(float_array), -integer, -integer)).
%% --Input--
%% mat data,
%% vec labels
%%
%% --Output--
%% float32 accuracy
%%
%% --Description--
%% Computes accuracy of the learned model given the feature data and the labels associated with each data point.
%% Predictions are made using the provided data and are compared with the actual labels.
%%
computeAccuracy(DataList, DataRows, LabelsList, Accuracy) :-
convert_list_to_float_array(DataList, DataRows, array(Xsize, Xrownum, X)),
convert_list_to_float_array(LabelsList, array(Ysize, Y)),
computeAccuracyI(X, Xsize, Xrownum, Y, Ysize, Accuracy).
foreign(computeAccuracy, c, computeAccuracyI( +pointer(float_array), +integer, +integer,
+pointer(float_array), +integer,
[-float32])).
%% --Input--
%%
%% --Output--
%% int size of the features
%%
%% --Description--
%% Gets the features size of the training data.
%%
featureSize(FeatureSize) :-
featureSizeI(FeatureSize).
foreign(featureSize, c, featureSizeI([-integer])).
%% --Input--
%%
%% --Output--
%% mat parameters
%%
%% --Description--
%% Get the model parameters.
%%
parameters(PrametersList, XCols) :-
parametersI(X, XCols, XRows),
convert_float_array_to_2d_list(X, XCols, XRows, PrametersList).
foreign(parameters, c, parametersI(-pointer(float_array), -integer, -integer)).
%% --Input--
%% mat data,
%% vec labels,
%% int numClasses => 0
%%
%% --Output--
%% float32 objective value of final point
%%
%% --Description--
%% Trains the softmax regression model with the given training data.
%%
train(DataList, DataRows, LabelsList, NumClasses, FinalValue) :-
NumClasses >= 0,
convert_list_to_float_array(DataList, DataRows, array(Xsize, Xrownum, X)),
convert_list_to_float_array(LabelsList, array(Ysize, Y)),
trainI(X, Xsize, Xrownum, Y, Ysize, NumClasses, FinalValue).
foreign(train, c, trainI( +pointer(float_array), +integer, +integer,
+pointer(float_array), +integer,
+integer,
[-float32])).
%% Defines the functions that get connected from main.cpp
foreign_resource(softmax_regression, [ initModelNoTrain,
initModelWithTrain,
classifyPoint,
classifyMatrix,
computeAccuracy,
featureSize,
parameters,
train]).
:- load_foreign_resource(softmax_regression).