mlpack
sparse_autoencoder_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_SPARSE_AUTOENCODER_SPARSE_AUTOENCODER_IMPL_HPP
13 #define MLPACK_METHODS_SPARSE_AUTOENCODER_SPARSE_AUTOENCODER_IMPL_HPP
14 
15 // In case it hasn't been included yet.
16 #include "sparse_autoencoder.hpp"
17 
18 namespace mlpack {
19 namespace nn {
20 
21 template<typename OptimizerType>
23  const size_t visibleSize,
24  const size_t hiddenSize,
25  double lambda,
26  double beta,
27  double rho,
28  OptimizerType optimizer) :
29  visibleSize(visibleSize),
30  hiddenSize(hiddenSize),
31  lambda(lambda),
32  beta(beta),
33  rho(rho)
34 {
35  SparseAutoencoderFunction encoderFunction(data, visibleSize, hiddenSize,
36  lambda, beta, rho);
37 
38  parameters = encoderFunction.GetInitialPoint();
39 
40  // Train the model.
41  Timer::Start("sparse_autoencoder_optimization");
42  const double out = optimizer.Optimize(encoderFunction, parameters);
43  Timer::Stop("sparse_autoencoder_optimization");
44 
45  Log::Info << "SparseAutoencoder::SparseAutoencoder(): final objective of "
46  << "trained model is " << out << "." << std::endl;
47 }
48 
49 template<typename OptimizerType, typename... CallbackTypes>
51  const size_t visibleSize,
52  const size_t hiddenSize,
53  double lambda,
54  double beta,
55  double rho,
56  OptimizerType optimizer,
57  CallbackTypes&&... callbacks) :
58  visibleSize(visibleSize),
59  hiddenSize(hiddenSize),
60  lambda(lambda),
61  beta(beta),
62  rho(rho)
63 {
64  SparseAutoencoderFunction encoderFunction(data, visibleSize, hiddenSize,
65  lambda, beta, rho);
66 
67  parameters = encoderFunction.GetInitialPoint();
68 
69  // Train the model.
70  Timer::Start("sparse_autoencoder_optimization");
71  const double out = optimizer.Optimize(encoderFunction, parameters,
72  callbacks...);
73  Timer::Stop("sparse_autoencoder_optimization");
74 
75  Log::Info << "SparseAutoencoder::SparseAutoencoder(): final objective of "
76  << "trained model is " << out << "." << std::endl;
77 }
78 
79 } // namespace nn
80 } // namespace mlpack
81 
82 #endif
static void Start(const std::string &name)
Start the given timer.
Definition: timers.cpp:28
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
This is a class for the sparse autoencoder objective function.
Definition: sparse_autoencoder_function.hpp:26
const arma::mat & GetInitialPoint() const
Return the initial point for the optimization.
Definition: sparse_autoencoder_function.hpp:86
static void Stop(const std::string &name)
Stop the given timer.
Definition: timers.cpp:36
SparseAutoencoder(const arma::mat &data, const size_t visibleSize, const size_t hiddenSize, const double lambda=0.0001, const double beta=3, const double rho=0.01, OptimizerType optimizer=OptimizerType())
Construct the sparse autoencoder model with the given training data.
Definition: sparse_autoencoder_impl.hpp:22
static MLPACK_EXPORT util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
Definition: log.hpp:84