From 154b0ed963d75b7bf9f02be024191fcd04ff693d Mon Sep 17 00:00:00 2001 From: jeffin sam Date: Fri, 17 Apr 2020 00:18:09 +0530 Subject: [PATCH] Exposing callbacks to sparseautoencoder (#2198) Exposing callbacks to sparseautoencoder. --- HISTORY.md | 2 ++ .../sparse_autoencoder/sparse_autoencoder.hpp | 28 +++++++++++++++++ .../sparse_autoencoder_impl.hpp | 30 +++++++++++++++++++ src/mlpack/tests/callback_test.cpp | 21 +++++++++++++ 4 files changed, 81 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 104f23218b..32670d12bd 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -9,6 +9,8 @@ * Pass CMAKE_CXX_FLAGS (compilation options) correctly to Python build (#2367). + * Expose ensmallen Callbacks for sparseautoencoder (#2198). + ### mlpack 3.3.0 ###### 2020-04-07 * Templated return type of `Forward function` of loss functions (#2339). diff --git a/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder.hpp b/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder.hpp index d55b68b844..a9d2d1b846 100644 --- a/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder.hpp +++ b/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder.hpp @@ -86,6 +86,34 @@ class SparseAutoencoder const double rho = 0.01, OptimizerType optimizer = OptimizerType()); + /** + * Construct the sparse autoencoder model with the given training data. This + * will train the model. The parameters 'lambda', 'beta' and 'rho' can be set + * optionally. Changing these parameters will have an effect on regularization + * and sparsity of the model. + * + * @tparam OptimizerType The optimizer to use. + * @tparam CallbackTypes Types of Callback Functions. + * @param data Input data with each column as one example. + * @param visibleSize Size of input vector expected at the visible layer. + * @param hiddenSize Size of input vector expected at the hidden layer. + * @param lambda L2-regularization parameter. + * @param beta KL divergence parameter. + * @param rho Sparsity parameter. + * @param optimizer Desired optimizer. + * @param callbacks Callback function for ensmallen optimizer `OptimizerType`. + * See https://www.ensmallen.org/docs.html#callback-documentation. + */ + template + SparseAutoencoder(const arma::mat& data, + const size_t visibleSize, + const size_t hiddenSize, + const double lambda, + const double beta, + const double rho , + OptimizerType optimizer, + CallbackTypes&&... callbacks); + /** * Transforms the provided data into the representation learned by the sparse * autoencoder. The function basically performs a feedforward computation diff --git a/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder_impl.hpp b/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder_impl.hpp index ac43162832..e59d7494a9 100644 --- a/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder_impl.hpp +++ b/src/mlpack/methods/sparse_autoencoder/sparse_autoencoder_impl.hpp @@ -46,6 +46,36 @@ SparseAutoencoder::SparseAutoencoder(const arma::mat& data, << "trained model is " << out << "." << std::endl; } +template +SparseAutoencoder::SparseAutoencoder(const arma::mat& data, + const size_t visibleSize, + const size_t hiddenSize, + double lambda, + double beta, + double rho, + OptimizerType optimizer, + CallbackTypes&&... callbacks) : + visibleSize(visibleSize), + hiddenSize(hiddenSize), + lambda(lambda), + beta(beta), + rho(rho) +{ + SparseAutoencoderFunction encoderFunction(data, visibleSize, hiddenSize, + lambda, beta, rho); + + parameters = encoderFunction.GetInitialPoint(); + + // Train the model. + Timer::Start("sparse_autoencoder_optimization"); + const double out = optimizer.Optimize(encoderFunction, parameters, + callbacks...); + Timer::Stop("sparse_autoencoder_optimization"); + + Log::Info << "SparseAutoencoder::SparseAutoencoder(): final objective of " + << "trained model is " << out << "." << std::endl; +} + } // namespace nn } // namespace mlpack diff --git a/src/mlpack/tests/callback_test.cpp b/src/mlpack/tests/callback_test.cpp index a75b647b40..2ec9d6a585 100644 --- a/src/mlpack/tests/callback_test.cpp +++ b/src/mlpack/tests/callback_test.cpp @@ -22,6 +22,8 @@ #include #include #include +#include + #include using namespace mlpack; @@ -257,4 +259,23 @@ BOOST_AUTO_TEST_CASE(RBMCallbackTest) BOOST_REQUIRE_GT(stream.str().length(), 0); } +/** + * Tests the SparseAutoencoder implementation with + * StoreBestCoordinates callback. + */ +BOOST_AUTO_TEST_CASE(SparseAutoencodeCallbackTest) +{ + // Simple fake dataset. + arma::mat data1("0.1 0.2 0.3 0.4 0.5;" + "0.1 0.2 0.3 0.4 0.5;" + "0.1 0.2 0.3 0.4 0.5;" + "0.1 0.2 0.3 0.4 0.5;" + "0.1 0.2 0.3 0.4 0.5"); + + ens::L_BFGS optimizer(5, 100); + ens::StoreBestCoordinates cb; + mlpack::nn::SparseAutoencoder encoder2(data1, 5, 1, 0, 0, 0 , optimizer, cb); + BOOST_REQUIRE_GT(cb.BestObjective(), 0); +} + BOOST_AUTO_TEST_SUITE_END();