diff --git a/src/mlpack/methods/ann/ffn_impl.hpp b/src/mlpack/methods/ann/ffn_impl.hpp index 389c864546..43e2b2cf5c 100644 --- a/src/mlpack/methods/ann/ffn_impl.hpp +++ b/src/mlpack/methods/ann/ffn_impl.hpp @@ -683,8 +683,11 @@ void FFN< inputDimensions.end(), 0); // TODO: improve this error message... - Log::Assert(totalInputSize == inputDimensionality, functionName + ": input " - "size does not match expected size set with InputDimensions()!"); + if (totalInputSize != inputDimensionality) + { + throw std::logic_error(functionName + ": input size does not match expected" + " size set with InputDimensions()!"); + } // If the input dimensions have not changed from what has been computed // before, we can terminate early---the network already has its dimensions diff --git a/src/mlpack/tests/feedforward_network_test.cpp b/src/mlpack/tests/feedforward_network_test.cpp index a8cba65428..8ac3f9dda1 100644 --- a/src/mlpack/tests/feedforward_network_test.cpp +++ b/src/mlpack/tests/feedforward_network_test.cpp @@ -868,11 +868,10 @@ TEST_CASE("OptimizerTest", "[FeedForwardNetworkTest]") model.Train(trainData, trainLabels, opt); } -// TODO: this is no longer possible for the Linear layer, at least. /** * Test to see if an exception is thrown when input with * wrong shape is provided to a FFN. - * + */ TEST_CASE("FFNCheckInputShapeTest", "[FeedForwardNetworkTest]") { // Load the dataset. @@ -889,20 +888,14 @@ TEST_CASE("FFNCheckInputShapeTest", "[FeedForwardNetworkTest]") testData.shed_row(testData.n_rows - 1); FFN, RandomInitialization> model; - // Purposely putting wrong input shape so that error is thrown. - model.Add(trainData.n_rows - 3, 8); - model.Add(8, 3); + model.Add(8); + model.Add(3); model.Add(); - std::string expectedMsg = "FFN<>::Train(): "; - expectedMsg += "the first layer of the network expects "; - expectedMsg += std::to_string(trainData.n_rows - 3) + " elements, "; - expectedMsg += "but the input has " + std::to_string(trainData.n_rows) + - " dimensions! "; - ens::DE opt(200, 1000, 0.6, 0.8, 1e-5); + // Now set up the input incorrectly. + model.InputDimensions() = std::vector({ 1, 2, 3 }); + REQUIRE_THROWS_AS(model.Train(trainData, trainLabels, opt), std::logic_error); - // TODO: check expectedMsg ? } -*/