From 5d26413a1361b6e048c9d7ff6f177abd147e7c97 Mon Sep 17 00:00:00 2001 From: Jakhes <dean.schmitz@schmitzbauer.de> Date: Wed, 23 Nov 2022 14:12:03 +0100 Subject: [PATCH] Updating Method Label tests --- src/methods/adaboost/adaboost.cpp | 57 +++++++++++++------ src/methods/adaboost/adaboost_test.pl | 28 +++------ .../bayesian_linear_regression_test.pl | 4 -- src/methods/decision_tree/decision_tree.cpp | 28 ++++++--- .../decision_tree/decision_tree_test.pl | 23 ++------ .../hoeffding_tree/hoeffding_tree_test.pl | 17 ------ src/methods/lars/lars_test.pl | 9 --- src/methods/linear_SVM/linear_SVM_test.pl | 14 ----- .../linear_regression_test.pl | 20 ------- src/methods/lmnn/lmnn_test.pl | 3 - .../logistic_regression_test.pl | 4 -- .../naive_bayes_classifier_test.pl | 7 --- src/methods/nca/nca_test.pl | 4 -- src/methods/perceptron/perceptron_test.pl | 7 --- .../random_forest/random_forest_test.pl | 14 ----- .../softmax_regression/softmax_regression.cpp | 26 +++++++-- .../softmax_regression_test.pl | 40 ++----------- 17 files changed, 101 insertions(+), 204 deletions(-) diff --git a/src/methods/adaboost/adaboost.cpp b/src/methods/adaboost/adaboost.cpp index 11b16c2..303c5cb 100644 --- a/src/methods/adaboost/adaboost.cpp +++ b/src/methods/adaboost/adaboost.cpp @@ -27,6 +27,9 @@ bool usingPerceptron = true; bool isPerceptronTrained = false; bool isDecisionStumpTrained = false; +int trainedDataDimensionPerceptron = 0; +int trainedDataDimensionDecisionStump = 0; + // input: const MatType & data, // const arma::Row< size_t > & labels, @@ -47,6 +50,13 @@ void initModelWithTraining(float *dataMatArr, SP_integer dataMatSize, SP_integer { // convert the Prolog array to arma::mat mat data = convertArrayToMat(dataMatArr, dataMatSize, dataMatRowNum); + // check if labels fit the data + if (data.n_cols != labelsArrSize) + { + raisePrologSystemExeption("The number of data points does not match the number of labels!"); + return; + } + // convert the Prolog array to arma::rowvec Row< size_t > labelsVector = convertArrayToVec(labelsArr, labelsArrSize); @@ -55,12 +65,14 @@ void initModelWithTraining(float *dataMatArr, SP_integer dataMatSize, SP_integer usingPerceptron = true; adaBoostPerceptron = AdaBoost<Perceptron<>>(data, labelsVector, numClasses, perceptron::Perceptron<>(numClasses, 0UL, iterations), iterations, tolerance); isPerceptronTrained = true; + trainedDataDimensionPerceptron = data.n_rows; } else if (strcmp(learner, "decision_stump") == 0) { usingPerceptron = false; adaBoostDecisionStump = AdaBoost<DecisionTree<>>(data, labelsVector, numClasses, tree::DecisionTree<>(data,labelsVector, numClasses), iterations, tolerance); isDecisionStumpTrained = true; + trainedDataDimensionDecisionStump = data.n_rows; } else { @@ -83,12 +95,14 @@ void initModelNoTraining(double tol = 1e-6, usingPerceptron = true; adaBoostPerceptron = AdaBoost<Perceptron<>>(tol); isPerceptronTrained = false; + trainedDataDimensionPerceptron = 0; } else if (strcmp(learner, "decision_stump") == 0) { usingPerceptron = false; adaBoostDecisionStump = AdaBoost<DecisionStump<>>(tol); isDecisionStumpTrained = false; + trainedDataDimensionDecisionStump = 0; } else { @@ -98,8 +112,8 @@ void initModelNoTraining(double tol = 1e-6, // input: const MatType & test, -// arma::Row< size_t > & predictedLabels, -// arma::mat & probabilities +// arma::Row< size_t > & predictedLabels <-, +// arma::mat & probabilities <- // output: // description: // Classifies the given data into the number of classes the model was trained for. @@ -120,14 +134,18 @@ void classify(float *testMatArr, SP_integer testMatSize, SP_integer testMatRowNu // run the classify function on the model depending on if perceptron was selected if(usingPerceptron && isPerceptronTrained) { + if (trainedDataDimensionPerceptron != test.n_rows) + { + raisePrologSystemExeption("The given Datapoints Dimension is diffrent than the trained Dimension!"); + return; + } try { adaBoostPerceptron.Classify(test, predicLabelsReturnVector, probsReturnMat); } catch(const std::exception& e) { - std::cerr << e.what() << '\n'; - raisePrologSystemExeption("The given data matrix has incorrect dimensions compared to the training data!"); + raisePrologSystemExeption(e.what()); return; } @@ -135,14 +153,18 @@ void classify(float *testMatArr, SP_integer testMatSize, SP_integer testMatRowNu } else if(!usingPerceptron && isDecisionStumpTrained) { + if (trainedDataDimensionDecisionStump != test.n_rows) + { + raisePrologSystemExeption("The given Datapoints Dimension is diffrent than the trained Dimension!"); + return; + } try { adaBoostDecisionStump.Classify(test, predicLabelsReturnVector, probsReturnMat); } catch(const std::exception& e) { - std::cerr << e.what() << '\n'; - raisePrologSystemExeption("The given data matrix has incorrect dimensions compared to the training data!"); + raisePrologSystemExeption(e.what()); return; } } @@ -242,6 +264,13 @@ double train(float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRowNum { // convert the Prolog array to arma::mat mat data = convertArrayToMat(dataMatArr, dataMatSize, dataMatRowNum); + // check if labels fit the data + if (data.n_cols != labelsArrSize) + { + raisePrologSystemExeption("The number of data points does not match the number of labels!"); + return 0.0; + } + // convert the Prolog array to arma::rowvec Row< size_t > labelsVector = convertArrayToVec(labelsArr, labelsArrSize); @@ -252,7 +281,9 @@ double train(float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRowNum try { isPerceptronTrained = true; - return adaBoostPerceptron.Train(data, labelsVector, numClasses, Perceptron(), iterations, tolerance); + double error = adaBoostPerceptron.Train(data, labelsVector, numClasses, Perceptron(), iterations, tolerance); + trainedDataDimensionPerceptron = data.n_rows; + return error; } catch(const std::exception& e) { @@ -260,10 +291,6 @@ double train(float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRowNum { raisePrologSystemExeption("The values of the Label have to start at 0 and be >= 0 and < the given numClass!"); } - else if (strcmp(e.what(),"Row::subvec(): indices out of bounds or incorrectly used") == 0) - { - raisePrologSystemExeption("The given Labels Vector is too short!"); - } else { raisePrologSystemExeption(e.what()); @@ -277,7 +304,9 @@ double train(float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRowNum try { isDecisionStumpTrained = true; - return adaBoostDecisionStump.Train(data, labelsVector, numClasses, DecisionStump<>(), iterations, tolerance); + double error = adaBoostDecisionStump.Train(data, labelsVector, numClasses, DecisionStump<>(), iterations, tolerance); + trainedDataDimensionDecisionStump = data.n_rows; + return error; } catch(const std::exception& e) { @@ -285,10 +314,6 @@ double train(float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRowNum { raisePrologSystemExeption("The values of the Label have to start at 0 and be >= 0 and < the given numClass!"); } - else if (strcmp(e.what(),"Row::subvec(): indices out of bounds or incorrectly used") == 0) - { - raisePrologSystemExeption("The given Labels Vector is too short!"); - } else { raisePrologSystemExeption(e.what()); diff --git a/src/methods/adaboost/adaboost_test.pl b/src/methods/adaboost/adaboost_test.pl index fe9a54f..c526564 100644 --- a/src/methods/adaboost/adaboost_test.pl +++ b/src/methods/adaboost/adaboost_test.pl @@ -40,8 +40,11 @@ test(initModelWithTraining_WrongIterations, fail) :- test(initModelWithTraining_WrongTol, fail) :- adaboost_initModelWithTraining([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, 1, 0], 2, perceptron, 50, -10.0). -test(initModelWithTraining_MissmatchingLabels) :- - adaboost_initModelWithTraining([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, 1, 0, 1 ,1], 2, perceptron, 50, 0.0001). +test(initModelWithTraining_Too_Few_Labels, [error(_, system_error('The number of data points does not match the number of labels!'))]) :- + adaboost_initModelWithTraining([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, perceptron, 50, 0.0001). + +test(initModelWithTraining_Too_Many_Labels, [error(_, system_error('The number of data points does not match the number of labels!'))]) :- + adaboost_initModelWithTraining([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, perceptron, 50, 0.0001). %% Successful Tests @@ -90,12 +93,11 @@ test(classify_on_untrained_model, [error(_,system_error('The model is not traine reset_Model_No_Train(perceptron), adaboost_classify([3, 2, 0, 5, 1, 4, 0, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, _, _, _). -test(classify_with_bad_data_input_perceptron, [error(_,system_error('The given data matrix has incorrect dimensions compared to the training data!'))]) :- +test(classify_with_bad_data_input_perceptron, [error(_,system_error('The given Datapoints Dimension is diffrent than the trained Dimension!'))]) :- reset_Model_With_Train(perceptron), adaboost_classify([3, 2, 0, 5, 1, 4, 0, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 2, _, _, _). -%% should cause an exeption but doesnt TODO: -test(classify_with_bad_data_input_decision_stump) :- +test(classify_with_bad_data_input_decision_stump, [error(_,system_error('The given Datapoints Dimension is diffrent than the trained Dimension!'))]) :- reset_Model_With_Train(decision_stump), adaboost_classify([3, 2, 0, 5, 1, 4, 0, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 5, _, _, _). @@ -258,25 +260,13 @@ test(train_With_Bad_Tol_Input, fail) :- reset_Model_No_Train(perceptron), adaboost_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, _). -test(train_With_Bad_Labels_Too_Many_Classes, [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), - adaboost_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, _). - -test(train_With_Bad_Labels_Negative_Perceptron, [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), - adaboost_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,-1], 2, perceptron, 50, 0.0001, _). - -%% should cause an exeption but doesnt TODO: -test(train_With_Bad_Labels_Negative_Decision_Stump) :- - reset_Model_No_Train(decision_stump), - adaboost_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,-1], 2, decision_stump, 50, 0.0001, _). %% seems to be allowed -test(train_With_Too_Many_Labels) :- +test(train_With_Too_Many_Labels, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- reset_Model_No_Train(perceptron), adaboost_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,0,0,1], 2, perceptron, 50, 0.0001, _). -test(train_With_Too_Little_Labels, [error(_,system_error('The given Labels Vector is too short!'))]) :- +test(train_With_Too_Little_Labels, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- reset_Model_No_Train(decision_stump), adaboost_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], 2, decision_stump, 50, 0.0001, _). diff --git a/src/methods/bayesian_linear_regression/bayesian_linear_regression_test.pl b/src/methods/bayesian_linear_regression/bayesian_linear_regression_test.pl index 6f7fa10..4db1610 100644 --- a/src/methods/bayesian_linear_regression/bayesian_linear_regression_test.pl +++ b/src/methods/bayesian_linear_regression/bayesian_linear_regression_test.pl @@ -350,10 +350,6 @@ test(bay_lin_reg_Train_Too_Large_Label_Dims, [error(_,system_error('Target dim d reset_Model, blr_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,1,0,0,0]). -%% doesnt cause an Exception -test(bay_lin_reg_Train_Wrong_Label_Value) :- - reset_Model, - blr_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,5,0,-1]). %% Successful Tests diff --git a/src/methods/decision_tree/decision_tree.cpp b/src/methods/decision_tree/decision_tree.cpp index 8e6e7e7..23f6717 100644 --- a/src/methods/decision_tree/decision_tree.cpp +++ b/src/methods/decision_tree/decision_tree.cpp @@ -18,7 +18,7 @@ using namespace mlpack::tree; // Global Variable of the DecisionTree object so it can be accessed from all functions DecisionTree decisionTreeObj; -// TODO: + // input: const arma::mat & dataset, // const arma::vec & labels, // const size_t numClasses, @@ -33,6 +33,12 @@ void initModel( float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRow { // convert the Prolog array to arma::mat mat data = convertArrayToMat(dataMatArr, dataMatSize, dataMatRowNum); + // check if labels fit the data + if (data.n_cols != labelsArrSize) + { + raisePrologSystemExeption("The number of data points does not match the number of labels!"); + return; + } // convert the Prolog array to arma::rowvec Row<size_t> labelsVector = convertArrayToVec(labelsArr, labelsArrSize); @@ -41,16 +47,16 @@ void initModel( float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRow { decisionTreeObj = DecisionTree(data, labelsVector, numClasses, minimumLeafSize, minimumGainSplit, maximumDepth); } - catch(const std::out_of_range& e) + catch(const std::exception& e) { - raisePrologSystemExeption("Labels Vector is too short or its values are incorrect: should fit into [0,numClasses)!"); + raisePrologSystemExeption(e.what()); return; } decisionTreeObj = DecisionTree(data, labelsVector, numClasses, minimumLeafSize, minimumGainSplit, maximumDepth); } -// TODO: + // input: const arma::mat & dataset, // arma::Row< size_t > & predictions <-, // arma::rowvec & probabilities <- @@ -88,7 +94,7 @@ void classifyPoint( float *pointArr, SP_integer pointArrSize, returnVectorInformation(probsReturnVector, probsArr, probsArrSize); } -// TODO: + // input: const arma::mat & dataset, // arma::Row< size_t > & predictions <-, // arma::rowvec & probabilities <- @@ -125,7 +131,7 @@ void classifyMatrix(float *dataMatArr, SP_integer dataMatSize, SP_integer dataMa } -// TODO: + // input: MatType dataset, // LabelsType labels, // const size_t numClasses, @@ -142,7 +148,13 @@ double train( float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRow { // convert the Prolog array to arma::mat mat data = convertArrayToMat(dataMatArr, dataMatSize, dataMatRowNum); - + // check if labels fit the data + if (data.n_cols != labelsArrSize) + { + raisePrologSystemExeption("The number of data points does not match the number of labels!"); + return 0.0; + } + // convert the Prolog array to arma::rowvec Row<size_t> labelsVector = convertArrayToVec(labelsArr, labelsArrSize); @@ -150,7 +162,7 @@ double train( float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRow { return decisionTreeObj.Train(data, labelsVector, numClasses, minimumLeafSize, minimumGainSplit, maximumDepth); } - catch(const std::invalid_argument& e) + catch(const std::exception& e) { raisePrologSystemExeption(e.what()); return 0.0; diff --git a/src/methods/decision_tree/decision_tree_test.pl b/src/methods/decision_tree/decision_tree_test.pl index c5ed2ac..fa13df2 100644 --- a/src/methods/decision_tree/decision_tree_test.pl +++ b/src/methods/decision_tree/decision_tree_test.pl @@ -31,16 +31,12 @@ test(decision_tree_Too_High_GainSplit, fail) :- test(decision_tree_Negative_MaxDepth, fail) :- decision_tree_initModel([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], 1, 1, 0.5, -1). -test(decision_tree_Init_With_Wrong_Label_Dims1, [error(_,system_error('Labels Vector is too short or its values are incorrect: should fit into [0,numClasses)!'))]) :- +test(decision_tree_Init_With_Wrong_Label_Dims1, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- decision_tree_initModel([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], 1, 1, 0.5, 1). -%% If the label vector is to long it seems to cause no problems -test(decision_tree_Init_With_Wrong_Label_Dims2) :- +test(decision_tree_Init_With_Wrong_Label_Dims2, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- decision_tree_initModel([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,0,0], 1, 1, 0.5, 1). -%% The same when the label values are out of range -test(decision_tree_Init_With_Wrong_Label_Value) :- - decision_tree_initModel([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,5,0,-1], 1, 1, 0.5, 1). %% Successful Tests @@ -120,13 +116,6 @@ test(classify_Matrix_With_Wrong_Dims2) :- %% Successful Tests -test(classify_Matrix_Wierd_Trained_Labels) :- - decision_tree_initModel([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,5,0,-1], 1, 1, 0.5, 1), - decision_tree_classifyMatrix([5.1,3.5,1.4, 5.1,3.5,1.4, 5.1,3.5,1.4, 5.1,3.5,1.4, 5.1,3.5,1.4], 3, PredictionList, ProbsList, _), - print(PredictionList), - print('\n'), - print(ProbsList). - test(classify_Matrix_Direkt_Input1) :- reset_Model_With_Train, decision_tree_classifyMatrix([5.1,3.5,1.4, 5.1,3.5,1.4, 5.1,3.5,1.4, 5.1,3.5,1.4, 5.1,3.5,1.4], 3, PredictionList, ProbsList, _), @@ -175,15 +164,11 @@ test(decision_tree_Train_Too_High_GainSplit, fail) :- test(decision_tree_Train_Negative_MaxDepth, fail) :- decision_tree_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], 1, 1, 0.5, -1, _). -test(decision_tree_Train_Wrong_Label_Dims1, [error(_,system_error('DecisionTree::Train(): number of points (4) does not match number of labels (2)!\n'))]) :- +test(decision_tree_Train_Wrong_Label_Dims1, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- decision_tree_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], 1, 1, 0.5, 1, _). -test(decision_tree_Train_Wrong_Label_Dims2, [error(_,system_error('DecisionTree::Train(): number of points (4) does not match number of labels (6)!\n'))]) :- +test(decision_tree_Train_Wrong_Label_Dims2, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- decision_tree_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,0,0], 1, 1, 0.5, 1, _). - -%% there seems to be no check for the label values -test(decision_tree_Train_Wrong_Labels) :- - decision_tree_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, [-1,0,0,5], 1, 1, 0.5, 1, _). %% Successful Tests diff --git a/src/methods/hoeffding_tree/hoeffding_tree_test.pl b/src/methods/hoeffding_tree/hoeffding_tree_test.pl index 280b7cb..b273566 100644 --- a/src/methods/hoeffding_tree/hoeffding_tree_test.pl +++ b/src/methods/hoeffding_tree/hoeffding_tree_test.pl @@ -52,14 +52,6 @@ test(hoeffding_Init_With_Wrong_Label_Dims1, [error(_,system_error('The number of test(hoeffding_Init_With_Wrong_Label_Dims2, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- hoeffding_tree_initAndBuildModel(gini_binary, [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,0,1], 2, 0, 0.95, 5000, 100, 100, 10, 100). - -%% doesnt cause an error -test(hoeffding_Init_With_Wrong_Label_Value) :- - hoeffding_tree_initAndBuildModel(gini_binary, [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, 0.95, 5000, 100, 100, 10, 100). - -%% doesnt cause an error -test(hoeffding_Init_With_Too_Many_Label_Value) :- - hoeffding_tree_initAndBuildModel(gini_binary, [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, [1,1,0,2], 2, 0, 0.95, 5000, 100, 100, 10, 100). %% Successful Tests @@ -176,15 +168,6 @@ test(hoeffding_Train_With_Wrong_Label_Dims2, [error(_,system_error('The number o reset_Model, hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [0,1,0,1,0,0,1], 0). - -%% all 3 test dont cause an error -test(hoeffding_Train_With_Wrong_Label_Value) :- - reset_Model, - hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [0,-1,0,-1,0], 0). - -test(hoeffding_Train_With_Too_Many_Label_Value) :- - reset_Model, - hoeffding_tree_train([3, 2, 0, 5, 1, 4, 1, 0, 4, 3, 3, 5, 0, 5, 5, 2, 5, 5, 0, 2], 4, [1,1,0,2,3], 0). test(hoeffding_Train_Bad_Data_Dims) :- reset_Model, diff --git a/src/methods/lars/lars_test.pl b/src/methods/lars/lars_test.pl index 7332dbd..2cabcf0 100644 --- a/src/methods/lars/lars_test.pl +++ b/src/methods/lars/lars_test.pl @@ -30,10 +30,6 @@ test(lars_initAndTrainModel_Too_Few_Labels, [error(_,system_error('The number of test(lars_initAndTrainModel_Too_Many_Labels, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- lars_initAndTrainModel(0, 0.0, 0.0, 1.0e-16, [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], 0, _). -%% doesnt cause an error -test(lars_initAndTrainModel_Too_Many_Labelclasses) :- - lars_initAndTrainModel(0, 0.0, 0.0, 1.0e-16, [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], 0, _). - test(lars_initAndTrainModel_Wrongly_Transposed, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- lars_initAndTrainModel(0, 0.0, 0.0, 1.0e-16, [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], 1, _). @@ -126,11 +122,6 @@ test(lars_ComputeError_Too_Many_Labels, [error(_,system_error('The number of dat reset_Model_WithTrain, lars_computeError([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], 1, _). -%% doenst cause an error -test(lars_ComputeError_Too_Many_Labelclasses) :- - reset_Model_WithTrain, - lars_computeError([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], 1, _). - %% Successful Tests diff --git a/src/methods/linear_SVM/linear_SVM_test.pl b/src/methods/linear_SVM/linear_SVM_test.pl index b61f720..155be25 100644 --- a/src/methods/linear_SVM/linear_SVM_test.pl +++ b/src/methods/linear_SVM/linear_SVM_test.pl @@ -39,10 +39,6 @@ test(linear_SVM_InitModelWithTrain_Too_Few_Labels, [error(_,system_error('The nu test(linear_SVM_InitModelWithTrain_Too_Many_Labels, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- linear_SVM_initModelWithTrain([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.0001, 1.0, 0, lbfgs). - -%% doesnt cause error -test(linear_SVM_InitModelWithTrain_Too_Many_LabelClasses) :- - linear_SVM_initModelWithTrain([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.0001, 1.0, 0, lbfgs). %% Successful Tests @@ -200,11 +196,6 @@ test(linear_SVM_ComputeAccuracy_Too_Many_Labels, [error(_,system_error('The numb reset_Model_WithTrain, linear_SVM_computeAccuracy([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], _). -%% doesnt cause error -test(linear_SVM_ComputeAccuracy_Too_Many_LabelClasses) :- - reset_Model_WithTrain, - linear_SVM_computeAccuracy([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], _). - test(linear_SVM_ComputeAccuracy_Wrong_Data_Dims, [error(_,system_error('The given Datapoints Dimension is != the trained Datapoints Dimension!'))]) :- reset_Model_WithTrain, linear_SVM_computeAccuracy([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 4, [0,1,0], _). @@ -258,11 +249,6 @@ test(linear_SVM_Train_Too_Few_Labels, [error(_,system_error('The number of data test(linear_SVM_Train_Too_Many_Labels, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- reset_Model_NoTrain, linear_SVM_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,1,0,1], 2, lbfgs, _). - -%% doesnt cause error -test(linear_SVM_Train_Too_Many_LabelClasses) :- - reset_Model_NoTrain, - linear_SVM_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,2,3], 2, lbfgs, _). %% Successful Tests diff --git a/src/methods/linear_regression/linear_regression_test.pl b/src/methods/linear_regression/linear_regression_test.pl index 507c254..45af09e 100644 --- a/src/methods/linear_regression/linear_regression_test.pl +++ b/src/methods/linear_regression/linear_regression_test.pl @@ -27,9 +27,6 @@ test(linear_regression_InitModel_Too_Few_Labels, [error(_, system_error('The num test(linear_regression_InitModel_Too_Many_Labels, [error(_, system_error('The number of data points does not match the number of labels!'))]) :- linear_regression_initModel([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], 0.0, 1). -%% doesnt cause error -test(linear_regression_InitModel_Too_Many_Labelclasses) :- - linear_regression_initModel([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], 0.0, 1). %% Successful Tests @@ -59,10 +56,6 @@ test(linear_regression_InitModelWithWeights_Too_Few_Labels, [error(_, system_err test(linear_regression_InitModelWithWeights_Too_Many_Labels, [error(_, system_error('The number of data points does not match the number of labels!'))]) :- linear_regression_initModelWithWeights([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], [0.2,0.1,0.2,0.5], 0.0, 1). -%% doesnt cause error -test(linear_regression_InitModelWithWeights_Too_Many_Labelclasses) :- - linear_regression_initModelWithWeights([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], [0.2,0.1,0.2,0.5], 0.0, 1). - test(linear_regression_InitModelWithWeights_Too_Few_Weights, [error(_, system_error('The number of data points does not match the number of weights!'))]) :- linear_regression_initModelWithWeights([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.2,0.5], 0.0, 1). @@ -100,11 +93,6 @@ test(linear_regression_ComputeError_Too_Many_Labels, [error(_, system_error('The reset_Model, linear_regression_computeError([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], _). -%% doesnt cause error -test(linear_regression_ComputeError_Too_Many_Labelclasses) :- - reset_Model, - linear_regression_computeError([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], _). - %% Successful Tests @@ -224,10 +212,6 @@ test(linear_regression_Train_Too_Few_Labels, [error(_, system_error('The number test(linear_regression_Train_Too_Many_Labels, [error(_, system_error('The number of data points does not match the number of labels!'))]) :- linear_regression_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,1,0,1], 1, _). -%% doesnt cause error -test(linear_regression_Train_Too_Many_Labelclasses) :- - linear_regression_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,2,3], 1, _). - %% Successful Tests @@ -260,10 +244,6 @@ test(linear_regression_TrainWithWeights_Too_Few_Labels, [error(_, system_error(' test(linear_regression_TrainWithWeights_Too_Many_Labels, [error(_, system_error('The number of data points does not match the number of labels!'))]) :- linear_regression_trainWithWeights([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], [0.2,0.1,0.2,0.5], 1 ,_). -%% doesnt cause error -test(linear_regression_TrainWithWeights_Too_Many_Labelclasses) :- - linear_regression_trainWithWeights([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], [0.2,0.1,0.2,0.5], 1 ,_). - test(linear_regression_TrainWithWeights_Too_Few_Weights, [error(_, system_error('The number of data points does not match the number of weights!'))]) :- linear_regression_trainWithWeights([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.2,0.5], 1 ,_). diff --git a/src/methods/lmnn/lmnn_test.pl b/src/methods/lmnn/lmnn_test.pl index e3d5937..4f47f86 100644 --- a/src/methods/lmnn/lmnn_test.pl +++ b/src/methods/lmnn/lmnn_test.pl @@ -54,9 +54,6 @@ test(lmnn_Negative_Too_Few_Labels, [error(_, system_error('The number of data po test(lmnn_Negative_Too_Many_Labels, [error(_, system_error('The number of data points does not match the number of labels!'))]) :- 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('fatal error; see Log::Fatal output'))]) :- - 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, _, _). - %% Successful Tests diff --git a/src/methods/logistic_regression/logistic_regression_test.pl b/src/methods/logistic_regression/logistic_regression_test.pl index e1371f8..91407c5 100644 --- a/src/methods/logistic_regression/logistic_regression_test.pl +++ b/src/methods/logistic_regression/logistic_regression_test.pl @@ -204,10 +204,6 @@ test(logistic_regression_ComputeAccuracy_Too_Few_Labels, [error(_, system_error( test(logistic_regression_ComputeAccuracy_Too_Many_Labels, [error(_, system_error('The number of data points does not match the number of labels!'))]) :- logistic_regression_computeAccuracy([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], 0.5, _). -%% doesnt cause error -test(logistic_regression_computeAccuracy_Too_Many_Labelclasses) :- - logistic_regression_computeAccuracy([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], 0.5, _). - %% Successful Tests test(logistic_regression_ComputeAccuracy_Normal_Use) :- diff --git a/src/methods/naive_bayes_classifier/naive_bayes_classifier_test.pl b/src/methods/naive_bayes_classifier/naive_bayes_classifier_test.pl index 7db8158..427a548 100644 --- a/src/methods/naive_bayes_classifier/naive_bayes_classifier_test.pl +++ b/src/methods/naive_bayes_classifier/naive_bayes_classifier_test.pl @@ -57,9 +57,6 @@ test(nbc_InitModelWithTrain_Too_Short_Label, [error(_,system_error('The number o test(nbc_InitModelWithTrain_Too_Long_Label, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- nbc_initModelWithTrain([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, 0.000001). - -test(nbc_InitModelWithTrain_Too_Many_Label_Classes, [error(_,system_error('The given Labels dont fit the format [0,Numclasses-1]!'))]) :- - nbc_initModelWithTrain([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, 0.000001). %% Successful Tests @@ -184,10 +181,6 @@ test(nbc_TrainMatrix_Too_Short_Label, [error(_,system_error('The number of data test(nbc_TrainMatrix_Too_Long_Label, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- reset_Model_NoTrain, nbc_trainMatrix([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). - -test(nbc_TrainMatrix_Too_Many_Label_Classes, [error(_,system_error('The given Labels dont fit the format [0,Numclasses-1]!'))]) :- - reset_Model_NoTrain, - nbc_trainMatrix([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). test(nbc_TrainMatrix_After_InitTrain, [error(_,system_error('The Datapoints Dimensionality doesnt fit the trained Dimensionality!'))]) :- reset_Model_WithTrain, diff --git a/src/methods/nca/nca_test.pl b/src/methods/nca/nca_test.pl index a6aff52..a59dd90 100644 --- a/src/methods/nca/nca_test.pl +++ b/src/methods/nca/nca_test.pl @@ -53,10 +53,6 @@ test(nca_Too_Short_Label, [error(_,system_error('The number of data points does test(nca_Too_Long_Label, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- nca(lbfgs, 0.01, 500000, 0.00001, 1, 5, 0.0001, 0.9, 50, 0.000000001, 100000000, 50, [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], _, _). - -%% Doesnt cause an error -test(nca_Too_Many_Label_Classes) :- - nca(lbfgs, 0.01, 500000, 0.00001, 1, 5, 0.0001, 0.9, 50, 0.000000001, 100000000, 50, [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], _, _). %% Successful Tests diff --git a/src/methods/perceptron/perceptron_test.pl b/src/methods/perceptron/perceptron_test.pl index bd77ad9..680ea3d 100644 --- a/src/methods/perceptron/perceptron_test.pl +++ b/src/methods/perceptron/perceptron_test.pl @@ -63,9 +63,6 @@ test(random_forest_InitModelWithTrainNoWeights_Too_Short_Label, [error(_,system_ test(random_forest_InitModelWithTrainNoWeights_Too_Long_Label, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- perceptron_initModelWithTrain([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, 1000). - -test(random_forest_InitModelWithTrainNoWeights_Too_Many_Label_Classes, [error(_,system_error('The given Labels dont fit the format [0,Numclasses-1]!'))]) :- - perceptron_initModelWithTrain([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, 1000). %% Successful Tests @@ -166,10 +163,6 @@ test(perceptron_Train_Too_Long_Label, [error(_,system_error('The number of data reset_Model_NoTrain, 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,1,0,1], 2, [0.1,0.2,0.3,0.4]). -test(perceptron_Train_Too_Many_Label_Classes, [error(_,system_error('The given Labels dont fit the format [0,Numclasses-1]!'))]) :- - reset_Model_NoTrain, - 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,2,3], 2, [0.1,0.2,0.3,0.4]). - test(perceptron_Train_Too_Short_Weights, [error(_,system_error('The number of data points does not match the number of weights!'))]) :- reset_Model_NoTrain, diff --git a/src/methods/random_forest/random_forest_test.pl b/src/methods/random_forest/random_forest_test.pl index 249a498..0320daf 100644 --- a/src/methods/random_forest/random_forest_test.pl +++ b/src/methods/random_forest/random_forest_test.pl @@ -57,9 +57,6 @@ test(random_forest_InitModelWithTrainNoWeights_Too_Short_Label, [error(_,system_ test(random_forest_InitModelWithTrainNoWeights_Too_Long_Label, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- random_forest_initModelWithTrainNoWeights([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, 20, 1, 0.0000001, 0). - -test(random_forest_InitModelWithTrainNoWeights_Too_Many_Label_Classes, [error(_,system_error('The given Labels dont fit the format [0,Numclasses-1]!'))]) :- - random_forest_initModelWithTrainNoWeights([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, 20, 1, 0.0000001, 0). %% Successful Tests @@ -105,9 +102,6 @@ test(random_forest_InitModelWithTrainWithWeights_Too_Short_Label, [error(_,syste test(random_forest_InitModelWithTrainWithWeights_Too_Long_Label, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- random_forest_initModelWithTrainWithWeights([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.1,0.7,1.2], 20, 1, 0.0000001, 0). -test(random_forest_InitModelWithTrainWithWeights_Too_Many_Label_Classes, [error(_,system_error('The given Labels dont fit the format [0,Numclasses-1]!'))]) :- - random_forest_initModelWithTrainWithWeights([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.1,0.7,1.2], 20, 1, 0.0000001, 0). - test(random_forest_InitModelWithTrainWithWeights_Too_Short_Weights, [error(_,system_error('The number of data points does not match the number of weights!'))]) :- random_forest_initModelWithTrainWithWeights([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.1], 20, 1, 0.0000001, 0). @@ -280,10 +274,6 @@ test(random_forest_TrainNoWeights_Too_Short_Label, [error(_,system_error('The nu test(random_forest_TrainNoWeights_Too_Long_Label, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- reset_Model_NoTrain, random_forest_trainNoWeights([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, 20, 1, 0.0000001, 0, _). - -test(random_forest_TrainNoWeights_Too_Many_Label_Classes, [error(_,system_error('The given Labels dont fit the format [0,Numclasses-1]!'))]) :- - reset_Model_NoTrain, - random_forest_trainNoWeights([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, 20, 1, 0.0000001, 0, _). %% Successful Tests @@ -342,10 +332,6 @@ test(random_forest_TrainWithWeights_Too_Long_Label, [error(_,system_error('The n reset_Model_NoTrain, random_forest_trainWithWeights([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.1,0.7,1.2], 20, 1, 0.0000001, 0, _). -test(random_forest_TrainWithWeights_Too_Many_Label_Classes, [error(_,system_error('The given Labels dont fit the format [0,Numclasses-1]!'))]) :- - reset_Model_NoTrain, - random_forest_trainWithWeights([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.1,0.7,1.2], 20, 1, 0.0000001, 0, _). - test(random_forest_TrainWithWeights_Too_Short_Weights, [error(_,system_error('The number of data points does not match the number of weights!'))]) :- reset_Model_NoTrain, diff --git a/src/methods/softmax_regression/softmax_regression.cpp b/src/methods/softmax_regression/softmax_regression.cpp index a4505f2..97a8f94 100644 --- a/src/methods/softmax_regression/softmax_regression.cpp +++ b/src/methods/softmax_regression/softmax_regression.cpp @@ -51,6 +51,13 @@ void initModelWithTrain(float *dataMatArr, SP_integer dataMatSize, SP_integer da { // convert the Prolog array to arma::mat mat data = convertArrayToMat(dataMatArr, dataMatSize, dataMatRowNum); + // check if labels fit the data + if (data.n_cols != labelsArrSize) + { + raisePrologSystemExeption("The number of data points does not match the number of labels!"); + return; + } + // convert the Prolog array to arma::rowvec Row< size_t > labelsVector = convertArrayToVec(labelsArr, labelsArrSize); @@ -148,6 +155,13 @@ double computeAccuracy(float *dataMatArr, SP_integer dataMatSize, SP_integer dat { // convert the Prolog array to arma::mat mat data = convertArrayToMat(dataMatArr, dataMatSize, dataMatRowNum); + // check if labels fit the data + if (data.n_cols != labelsArrSize) + { + raisePrologSystemExeption("The number of data points does not match the number of labels!"); + return 0.0; + } + // convert the Prolog array to arma::rowvec Row< size_t > labelsVector = convertArrayToVec(labelsArr, labelsArrSize); @@ -156,11 +170,6 @@ double computeAccuracy(float *dataMatArr, SP_integer dataMatSize, SP_integer dat { return softmaxRegression.ComputeAccuracy(data, labelsVector); } - catch(const std::out_of_range& e) - { - raisePrologSystemExeption("The Labels Vector has the wrong Dimension!"); - return 0.0; - } catch(const std::exception& e) { raisePrologSystemExeption(e.what()); @@ -210,6 +219,13 @@ double train(float *dataMatArr, SP_integer dataMatSize, SP_integer dataMatRowNum { // convert the Prolog array to arma::mat mat data = convertArrayToMat(dataMatArr, dataMatSize, dataMatRowNum); + // check if labels fit the data + if (data.n_cols != labelsArrSize) + { + raisePrologSystemExeption("The number of data points does not match the number of labels!"); + return 0.0; + } + // convert the Prolog array to arma::rowvec Row< size_t > labelsVector = convertArrayToVec(labelsArr, labelsArrSize); diff --git a/src/methods/softmax_regression/softmax_regression_test.pl b/src/methods/softmax_regression/softmax_regression_test.pl index 99dd60b..19275ff 100644 --- a/src/methods/softmax_regression/softmax_regression_test.pl +++ b/src/methods/softmax_regression/softmax_regression_test.pl @@ -51,20 +51,12 @@ test(softmax_InitWithTrain_Negative_NumClass, fail) :- test(softmax_InitWithTrain_Negative_Lambda, fail) :- softmax_regression_initModelWithTrain([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.0001, 0). -test(softmax_InitWithTrain_Wrong_Label_Dims1, [error(_,system_error('element-wise multiplication: incompatible matrix dimensions: 2x4 and 2x2'))]) :- +test(softmax_InitWithTrain_Wrong_Label_Dims1, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- softmax_regression_initModelWithTrain([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.0001, 0). -test(softmax_InitWithTrain_Wrong_Label_Dims2, [error(_,system_error('element-wise multiplication: incompatible matrix dimensions: 2x4 and 2x7'))]) :- +test(softmax_InitWithTrain_Wrong_Label_Dims2, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- softmax_regression_initModelWithTrain([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,0,1], 2, 0.0001, 0). - -%% Doesnt cause exception -test(softmax_InitWithTrain_Wrong_Label_Value) :- - softmax_regression_initModelWithTrain([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.0001, 0). - -%% Doesnt cause exception -test(softmax_InitWithTrain_Too_Many_Label_Value) :- - softmax_regression_initModelWithTrain([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, [1,1,0,2], 2, 0.0001, 0). %% Successful Tests @@ -168,25 +160,15 @@ test(softmax_ComputeAccuracy_On_Untrained_Model) :- reset_Model_NoTrain, softmax_regression_computeAccuracy([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], _). -test(softmax_ComputeAccuracy_Wrong_Label_Dims1, [error(_,system_error('The Labels Vector has the wrong Dimension!'))]) :- +test(softmax_ComputeAccuracy_Wrong_Label_Dims1, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- reset_Model_WithTrain, softmax_regression_computeAccuracy([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], _). %% Doesnt cause exception -test(softmax_ComputeAccuracy_Wrong_Label_Dims2) :- +test(softmax_ComputeAccuracy_Wrong_Label_Dims2, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- reset_Model_WithTrain, softmax_regression_computeAccuracy([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,0,1], _). -%% The same when the label values are out of range -test(softmax_ComputeAccuracy_Wrong_Label_Value) :- - reset_Model_WithTrain, - softmax_regression_computeAccuracy([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], _). - -%% Doesnt cause an exception -test(softmax_ComputeAccuracy_Too_Many_Label_Value) :- - reset_Model_WithTrain, - softmax_regression_computeAccuracy([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, [1,1,0,2], _). - test(softmax_ComputeAccuracy_Wrong_Data_Dims, [error(_,system_error('SoftmaxRegression::Classify(): dataset has 4 dimensions, but model has 3 dimensions!'))]) :- reset_Model_WithTrain, softmax_regression_computeAccuracy([5.1,3.5,1.4,4.9,3.0,1.4,4.7,3.2,1.3,4.6,3.1,1.5], 4, [0,1,0], Accuracy), @@ -292,25 +274,15 @@ test(softmax_Train_Negative_NumClass, fail) :- reset_Model_NoTrain, softmax_regression_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,1], -1, _). -test(softmax_Train_Wrong_Label_Dims1, [error(_,system_error('element-wise multiplication: incompatible matrix dimensions: 2x4 and 2x2'))]) :- +test(softmax_Train_Wrong_Label_Dims1, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- reset_Model_NoTrain, softmax_regression_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], 2, _). %% If the label vector is to long it seems to cause no problems -test(softmax_Train_Wrong_Label_Dims2, [error(_,system_error('element-wise multiplication: incompatible matrix dimensions: 2x4 and 2x7'))]) :- +test(softmax_Train_Wrong_Label_Dims2, [error(_,system_error('The number of data points does not match the number of labels!'))]) :- reset_Model_NoTrain, softmax_regression_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,1,0,0,1], 2, _). -%% The same when the label values are out of range -test(softmax_Train_Wrong_Label_Value) :- - reset_Model_NoTrain, - softmax_regression_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,-1], 2, _). - -%% doesnt cause a exeption -test(softmax_Train_Too_Many_Label_Value) :- - reset_Model_NoTrain, - softmax_regression_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, [1,1,0,2], 2, _). - %% doesnt cause a exeption test(softmax_Train_Wrong_Data_Dims) :- reset_Model_NoTrain, -- GitLab