diff --git a/src/mlpack/methods/approx_kfn/approx_kfn_main.cpp b/src/mlpack/methods/approx_kfn/approx_kfn_main.cpp index f362ae7c00..9adce050e6 100644 --- a/src/mlpack/methods/approx_kfn/approx_kfn_main.cpp +++ b/src/mlpack/methods/approx_kfn/approx_kfn_main.cpp @@ -189,6 +189,16 @@ static void mlpackMain() "the reference set must be passed"); } + if (CLI::HasParam("k") && CLI::HasParam("reference") && + ((size_t) CLI::GetParam("k")) > + CLI::GetParam("reference").n_cols) + { + Log::Fatal << "Number of neighbors to search for (" + << CLI::GetParam("k") << ") must be less than the number of " + << "reference points (" + << CLI::GetParam("reference").n_cols << ")." << std::endl; + } + // Do the building of a model, if necessary. ApproxKFNModel* m; arma::mat referenceSet; // This may be used at query time. @@ -264,16 +274,23 @@ static void mlpackMain() if (CLI::HasParam("exact_distances")) { // Check the exact distances matrix has the right dimensions. - RequireParamValue("exact_distances", - [&](const arma::mat& exactDists){ return (exactDists.n_rows == k); }, - true, "the number of rows in exact distances matrix must be equal to " - "k."); - RequireParamValue("exact_distances", - [&](const arma::mat& exactDists){ return (exactDists.n_cols == - referenceSet.n_cols); }, true, "the number of columns in exact " - "distances matrix must be equal to the number of columns in " - "reference set."); exactDistances = std::move(CLI::GetParam("exact_distances")); + + if (exactDistances.n_rows != k) + { + delete m; + Log::Fatal << "The number of rows in the exact distances matrix (" + << exactDistances.n_rows << " must be equal to k (" << k << ")." + << std::endl; + } + else if (exactDistances.n_cols != referenceSet.n_cols) + { + delete m; + Log::Fatal << "The number of columns in the exact distances matrix (" + << exactDistances.n_cols << ") must be equal to the number of " + << "columns in the reference set (" << referenceSet.n_cols << ")." + << std::endl; + } } else { diff --git a/src/mlpack/tests/main_tests/approx_kfn_test.cpp b/src/mlpack/tests/main_tests/approx_kfn_test.cpp index c60874c73c..3633df8c4e 100644 --- a/src/mlpack/tests/main_tests/approx_kfn_test.cpp +++ b/src/mlpack/tests/main_tests/approx_kfn_test.cpp @@ -73,7 +73,7 @@ BOOST_AUTO_TEST_CASE(ApproxKFNInvalidKTest) SetInputParam("k", (int) 81); // Invalid. Log::Fatal.ignoreInput = true; - BOOST_REQUIRE_THROW(mlpackMain(), std::invalid_argument); + BOOST_REQUIRE_THROW(mlpackMain(), std::runtime_error); Log::Fatal.ignoreInput = false; }