From 305c7a86f65decb5871bf899a9e6da4e97fd98e5 Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Thu, 3 Oct 2019 21:29:25 +0200 Subject: [PATCH 1/4] Expose the instantiated network. --- src/mlpack/methods/ann/ffn.hpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/mlpack/methods/ann/ffn.hpp b/src/mlpack/methods/ann/ffn.hpp index 9e875a16f2..af9ec3ca80 100644 --- a/src/mlpack/methods/ann/ffn.hpp +++ b/src/mlpack/methods/ann/ffn.hpp @@ -257,6 +257,14 @@ class FFN */ void Add(LayerTypes layer) { network.push_back(layer); } + //! Get the network model. + const std::vector >& Model() const + { + return network; + } + //! Modify the network model. + std::vector >& Model() { return network; } + //! Return the number of separable functions (the number of predictor points). size_t NumFunctions() const { return numFunctions; } @@ -265,7 +273,7 @@ class FFN //! Modify the initial point for the optimization. arma::mat& Parameters() { return parameter; } - //! Get the matrix of responses to the input data points. + //! Get the matrix of resposnses to the input data points. const arma::mat& Responses() const { return responses; } //! Modify the matrix of responses to the input data points. arma::mat& Responses() { return responses; } From 57daceb0f1e476a393001260cbe9744e88510c82 Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Thu, 3 Oct 2019 21:30:04 +0200 Subject: [PATCH 2/4] Test the Model() ffn method. --- src/mlpack/tests/feedforward_network_test.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/mlpack/tests/feedforward_network_test.cpp b/src/mlpack/tests/feedforward_network_test.cpp index c47d633ace..b84f2a557b 100644 --- a/src/mlpack/tests/feedforward_network_test.cpp +++ b/src/mlpack/tests/feedforward_network_test.cpp @@ -575,4 +575,41 @@ BOOST_AUTO_TEST_CASE(FFNTrainReturnObjective) BOOST_REQUIRE_EQUAL(std::isfinite(objVal), true); } + +/** + * Test that FFN::Model() allows us to access the instantiated network. + */ +BOOST_AUTO_TEST_CASE(FFNReturnModel) +{ + // Create dummy network. + FFN > model; + Linear<>* linearA = new Linear<>(3, 3); + model.Add(linearA); + Linear<>* linearB = new Linear<>(3, 4); + model.Add(linearB); + + // Initialize network parameter. + model.ResetParameters(); + + // Set all network parameter to one. + model.Parameters().ones(); + + // Zero the second layer parameter. + linearB->Parameters().zeros(); + + // Get the layer parameter from layer A and layer B and store them in + // parameterA and parameterB. + arma::mat parameterA, parameterB; + boost::apply_visitor(ParametersVisitor(std::move(parameterA)), + model.Model()[0]); + boost::apply_visitor(ParametersVisitor(std::move(parameterB)), + model.Model()[1]); + + CheckMatrices(parameterA, arma::ones(3 * 3 + 3, 1)); + CheckMatrices(parameterB, arma::zeros(3 * 4 + 4, 1)); + + CheckMatrices(linearA->Parameters(), arma::ones(3 * 3 + 3, 1)); + CheckMatrices(linearB->Parameters(), arma::zeros(3 * 4 + 4, 1)); +} + BOOST_AUTO_TEST_SUITE_END(); From dae940f24866d5bab3be3c72cfd9ef85b7cd8806 Mon Sep 17 00:00:00 2001 From: Marcus Edel Date: Fri, 4 Oct 2019 20:32:56 +0200 Subject: [PATCH 3/4] Fix spelling issue. --- src/mlpack/methods/ann/ffn.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mlpack/methods/ann/ffn.hpp b/src/mlpack/methods/ann/ffn.hpp index af9ec3ca80..6f34381b23 100644 --- a/src/mlpack/methods/ann/ffn.hpp +++ b/src/mlpack/methods/ann/ffn.hpp @@ -273,7 +273,7 @@ class FFN //! Modify the initial point for the optimization. arma::mat& Parameters() { return parameter; } - //! Get the matrix of resposnses to the input data points. + //! Get the matrix of responses to the input data points. const arma::mat& Responses() const { return responses; } //! Modify the matrix of responses to the input data points. arma::mat& Responses() { return responses; } From 113ed59889ab1a238c635c3a87fc35f26b82c691 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Sat, 5 Oct 2019 09:47:29 -0400 Subject: [PATCH 4/4] Update history. --- HISTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/HISTORY.md b/HISTORY.md index ea75bbbf30..6c59d9507d 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,6 @@ ### mlpack ?.?.? ###### ????-??-?? + * Add Model() to the FFN class to access individual layers (#2043). ### mlpack 3.2.1 ###### 2019-10-01