diff --git a/src/methods/lmnn/lmnn.cpp b/src/methods/lmnn/lmnn.cpp index b8a8066a8a4134b51b97d7dc9093d39e8bb17eca..b18b332fd429ba6f9f899c5e12ecbabf8dcf67cf 100644 --- a/src/methods/lmnn/lmnn.cpp +++ b/src/methods/lmnn/lmnn.cpp @@ -131,6 +131,7 @@ void lmnn(char const *optimizer, catch(const std::exception& e) { raisePrologSystemExeption(e.what()); + return; } diff --git a/src/methods/lmnn/lmnn.pl b/src/methods/lmnn/lmnn.pl index ed4db78c7465798f8d32bf27cd342461d54809e1..6865305298e8945c31a56a49082815d3932e3de6 100644 --- a/src/methods/lmnn/lmnn.pl +++ b/src/methods/lmnn/lmnn.pl @@ -40,6 +40,15 @@ %% Is a single predicate that initiates the lmnn model with all the given params and then performs Large Margin Nearest Neighbors metric learning on the reference data. %% lmnn(Optimizer, DataList, DataRows, LabelsList, K, Regularization, StepSize, Passes, MaxIterations, Tolerance, Center, Shuffle, BatchSize, Range, Rank, DistanceList, ZCols) :- + K > 0, + Regularization >= 0.0, + StepSize >= 0.0, + Passes >= 0, + MaxIterations >= 0, + Tolerance >= 0.0, + BatchSize > 0, + Range > 0, + Rank >= 0, convert_list_to_float_array(DataList, DataRows, array(Xsize, Xrownum, X)), convert_list_to_float_array(LabelsList, array(Ysize, Y)), lmnnI(Optimizer, X, Xsize, Xrownum, Y, Ysize, K, Regularization, StepSize, Passes, MaxIterations, Tolerance, Center, Shuffle, BatchSize, Range, Rank, Z, ZCols, ZRows), diff --git a/src/methods/lmnn/lmnn_test.pl b/src/methods/lmnn/lmnn_test.pl index cfa4b4c100e5d27e87fccffd086ff38d659ebeb8..d36c0dcad2240ca2c89fd396c5612c40ba13e4cd 100644 --- a/src/methods/lmnn/lmnn_test.pl +++ b/src/methods/lmnn/lmnn_test.pl @@ -6,38 +6,89 @@ :- use_module(lmnn). :- use_module('../../helper_files/helper.pl'). -reset_Model :- - initModel(1,0,50,0.0001). %% -%% TESTING predicate predicate/10 +%% TESTING predicate lmnn/10 %% -:- begin_tests(predicate). +:- begin_tests(lmnn). %% Failure Tests -test(testDescription, [error(domain_error('expectation' , culprit), _)]) :- - reset_Model_No_Train(perceptron), - train([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, [0,0,0,0], 2, culprit, 50, 0.0001, _). +test(lmnn_Wrong_Optimizer_Input, [error(domain_error('expectation' , wrongInput), _)]) :- + lmnn(wrongInput, [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, [0,1,0,1], 2, 0.5, 0.01, 50, 100000, 0.000001, 0, 0, 50, 1, 0, _, _). + +test(lmnn_Negative_K, fail) :- + lmnn(lbfgs, [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, [0,1,0,1], -2, 0.5, 0.01, 50, 100000, 0.000001, 0, 0, 50, 1, 0, _, _). + +test(lmnn_Too_Many_K, fail) :- + lmnn(lbfgs, [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, [0,1,0,1], 10, 0.5, 0.01, 50, 100000, 0.000001, 0, 0, 50, 1, 0, _, _). + +test(lmnn_Negative_Regularization, fail) :- + lmnn(lbfgs, [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, [0,1,0,1], 2, -0.5, 0.01, 50, 100000, 0.000001, 0, 0, 50, 1, 0, _, _). + +test(lmnn_Negative_StepSize, fail) :- + lmnn(lbfgs, [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, [0,1,0,1], 2, 0.5, -0.01, 50, 100000, 0.000001, 0, 0, 50, 1, 0, _, _). + +test(lmnn_Negative_Passes, fail) :- + lmnn(lbfgs, [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, [0,1,0,1], 2, 0.5, 0.01, -50, 100000, 0.000001, 0, 0, 50, 1, 0, _, _). + +test(lmnn_Negative_MaxIterations, fail) :- + lmnn(lbfgs, [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, [0,1,0,1], 2, 0.5, 0.01, 50, -100000, 0.000001, 0, 0, 50, 1, 0, _, _). + +test(lmnn_Negative_Tolerance, fail) :- + lmnn(lbfgs, [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, [0,1,0,1], 2, 0.5, 0.01, 50, 100000, -0.000001, 0, 0, 50, 1, 0, _, _). + +test(lmnn_Negative_BatchSize, fail) :- + lmnn(lbfgs, [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, [0,1,0,1], 2, 0.5, 0.01, 50, 100000, 0.000001, 0, 0, -50, 1, 0, _, _). + +test(lmnn_Negative_Range, fail) :- + lmnn(lbfgs, [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, [0,1,0,1], 2, 0.5, 0.01, 50, 100000, 0.000001, 0, 0, 50, -1, 0, _, _). + +test(lmnn_Negative_Rank, fail) :- + lmnn(lbfgs, [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, [0,1,0,1], 2, 0.5, 0.01, 50, 100000, 0.000001, 0, 0, 50, 1, -1, _, _). + + +test(lmnn_Negative_Too_Few_Labels, [error(_, system_error('Error'))]) :- + lmnn(lbfgs, [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, [0,1], 2, 0.5, 0.01, 50, 100000, 0.000001, 0, 0, 50, 1, 0, _, _). + +test(lmnn_Negative_Too_Many_Labels, [error(_, system_error('Error'))]) :- + lmnn(lbfgs, [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, [0,1,0,1,0,1], 2, 0.5, 0.01, 50, 100000, 0.000001, 0, 0, 50, 1, 0, _, _). + +test(lmnn_Negative_Too_Many_LabelClasses, [error(_, system_error('Error'))]) :- + lmnn(lbfgs, [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, [0,1,2,3], 2, 0.5, 0.01, 50, 100000, 0.000001, 0, 0, 50, 1, 0, _, _). -test(testDescription2, [error(_,system_error('The values of the Label have to start at 0 and be >= 0 and < the given numClass!'))]) :- - reset_Model_No_Train(perceptron), - train([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, [0,1,0,2], 2, perceptron, 50, 0.0001, _). %% Successful Tests -test(testDescription3, [true(Error =:= 1)]) :- - reset_Model_No_Train(perceptron), - train([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, [0,0,0,0], 2, perceptron, 50, 0.0001, Error). +test(lmnn_Normal_Use_Amsgrad) :- + lmnn(amsgrad, [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, [0,1,0,1], 2, 0.5, 0.01, 50, 100000, 0.000001, 0, 0, 50, 1, 0, DistanceList, _), + print('\nDistance: '), + print(DistanceList). + +test(lmnn_Normal_Use_BBSGD) :- + lmnn(bbsgd, [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, [0,1,0,1], 2, 0.5, 0.01, 50, 100000, 0.000001, 0, 0, 50, 1, 0, DistanceList, _), + print('\nDistance: '), + print(DistanceList). + +test(lmnn_Normal_Use_SGD) :- + lmnn(sgd, [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, [0,1,0,1], 2, 0.5, 0.01, 50, 100000, 0.000001, 0, 0, 50, 1, 0, DistanceList, _), + print('\nDistance: '), + print(DistanceList). + +test(lmnn_Normal_Use_LBFGS) :- + lmnn(lbfgs, [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, [0,1,0,1], 2, 0.5, 0.01, 50, 100000, 0.000001, 0, 0, 50, 1, 0, DistanceList, _), + print('\nDistance: '), + print(DistanceList). -test(testDescription4, [true(Error =:= 0.9797958971132711)]) :- - reset_Model_No_Train(perceptron), +test(lmnn_CSV_Input) :- open('src/data_csv/iris2.csv', read, File), - take_csv_row(File, skipFirstRow,10, Data), - train(Data, 4, [0,1,0,1,1,0,1,1,1,0], 2, perceptron, 50, 0.0001, Error). + take_csv_row(File, skipFirstRow, 10, Data), + lmnn(lbfgs, Data, 4, [0,1,0,1,1,0,1,1,1,0], 2, 0.7, 0.00021, 30, 1000, 0.00031, 1, 1, 30, 2, 2, DistanceList, _), + print('\nDistance: '), + print(DistanceList). -:- end_tests(predicate). +:- end_tests(lmnn). run_lmnn_tests :- run_tests.