From 758e18f8fde286e72f9febe62ca93dcce83d90d9 Mon Sep 17 00:00:00 2001 From: akhandait Date: Wed, 18 Jul 2018 02:46:41 +0530 Subject: [PATCH] Overload Forward() function. --- src/mlpack/methods/ann/ffn.hpp | 16 ++++++++++++++++ src/mlpack/methods/ann/ffn_impl.hpp | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/mlpack/methods/ann/ffn.hpp b/src/mlpack/methods/ann/ffn.hpp index 26dd862aff..7c826ae65f 100644 --- a/src/mlpack/methods/ann/ffn.hpp +++ b/src/mlpack/methods/ann/ffn.hpp @@ -284,6 +284,22 @@ class FFN */ void Forward(arma::mat inputs, arma::mat& results); + /** + * Perform a partial forward pass of the data. + * + * This function is meant for the cases when users require a forward pass only + * through certain layers and not the entire network. + * + * @param inputs The input data for the specified first layer. + * @param results The predicted results from the specified last layer. + * @param begin The index of the first layer. + * @param end The index of the last layer. + */ + void Forward(arma::mat inputs, + arma::mat& results, + const size_t begin, + const size_t end); + /** * Perform the backward pass of the data in real batch mode. * diff --git a/src/mlpack/methods/ann/ffn_impl.hpp b/src/mlpack/methods/ann/ffn_impl.hpp index 8a3c2c57f0..b180b9b0ec 100644 --- a/src/mlpack/methods/ann/ffn_impl.hpp +++ b/src/mlpack/methods/ann/ffn_impl.hpp @@ -124,6 +124,26 @@ void FFN::Forward( results = boost::apply_visitor(outputParameterVisitor, network.back()); } +template +void FFN::Forward( + arma::mat inputs, arma::mat& results, const size_t begin, const size_t end) +{ + boost::apply_visitor(ForwardVisitor(std::move(inputs), std::move( + boost::apply_visitor(outputParameterVisitor, network[begin]))), + network[begin]); + + for (size_t i = 1; i < end - begin + 1; ++i) + { + boost::apply_visitor(ForwardVisitor(std::move(boost::apply_visitor( + outputParameterVisitor, network[begin + i - 1])), std::move( + boost::apply_visitor(outputParameterVisitor, network[begin + i]))), + network[begin + i]); + } + + results = boost::apply_visitor(outputParameterVisitor, network[end]); +} + template double FFN::Backward(