mlpack
lcc_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_LOCAL_COORDINATE_CODING_LCC_IMPL_HPP
13 #define MLPACK_METHODS_LOCAL_COORDINATE_CODING_LCC_IMPL_HPP
14 
15 // In case it hasn't been included yet.
16 #include "lcc.hpp"
17 
18 namespace mlpack {
19 namespace lcc {
20 
21 template<typename DictionaryInitializer>
23  const arma::mat& data,
24  const size_t atoms,
25  const double lambda,
26  const size_t maxIterations,
27  const double tolerance,
28  const DictionaryInitializer& initializer) :
29  atoms(atoms),
30  lambda(lambda),
31  maxIterations(maxIterations),
32  tolerance(tolerance)
33 {
34  // Train the model.
35  Train(data, initializer);
36 }
37 
38 template<typename DictionaryInitializer>
40  const arma::mat& data,
41  const DictionaryInitializer& initializer)
42 {
43  Timer::Start("local_coordinate_coding");
44 
45  // Initialize the dictionary.
46  initializer.Initialize(data, atoms, dictionary);
47 
48  double lastObjVal = DBL_MAX;
49 
50  // Take the initial coding step, which has to happen before entering the main
51  // loop.
52  Log::Info << "Initial Coding Step." << std::endl;
53 
54  arma::mat codes;
55  Encode(data, codes);
56  arma::uvec adjacencies = find(codes);
57 
58  Log::Info << " Sparsity level: " << 100.0 * ((double)(adjacencies.n_elem)) /
59  ((double)(atoms * data.n_cols)) << "%.\n";
60  Log::Info << " Objective value: " << Objective(data, codes, adjacencies)
61  << "." << std::endl;
62 
63  for (size_t t = 1; t != maxIterations; t++)
64  {
65  Log::Info << "Iteration " << t << " of " << maxIterations << "."
66  << std::endl;
67 
68  // First step: optimize the dictionary.
69  Log::Info << "Performing dictionary step..." << std::endl;
70  OptimizeDictionary(data, codes, adjacencies);
71  double dsObjVal = Objective(data, codes, adjacencies);
72  Log::Info << " Objective value: " << dsObjVal << "." << std::endl;
73 
74  // Second step: perform the coding.
75  Log::Info << "Performing coding step..." << std::endl;
76  Encode(data, codes);
77  adjacencies = find(codes);
78  Log::Info << " Sparsity level: " << 100.0 * ((double) (adjacencies.n_elem))
79  / ((double)(atoms * data.n_cols)) << "%.\n";
80 
81  // Terminate if the objective increased in the coding step.
82  double curObjVal = Objective(data, codes, adjacencies);
83  if (curObjVal > dsObjVal)
84  {
85  Log::Warn << "Objective increased in coding step! Terminating."
86  << std::endl;
87  break;
88  }
89 
90  // Find the new objective value and improvement so we can check for
91  // convergence.
92  double improvement = lastObjVal - curObjVal;
93  Log::Info << "Objective value: " << curObjVal << " (improvement "
94  << std::scientific << improvement << ")." << std::endl;
95 
96  if (improvement < tolerance)
97  {
98  Log::Info << "Converged within tolerance " << tolerance << ".\n";
99  break;
100  }
101 
102  lastObjVal = curObjVal;
103  }
104 
105  Timer::Stop("local_coordinate_coding");
106  return lastObjVal;
107 }
108 
109 template<typename Archive>
111  const uint32_t /* version */)
112 {
113  ar(CEREAL_NVP(atoms));
114  ar(CEREAL_NVP(dictionary));
115  ar(CEREAL_NVP(lambda));
116  ar(CEREAL_NVP(maxIterations));
117  ar(CEREAL_NVP(tolerance));
118 }
119 
120 } // namespace lcc
121 } // namespace mlpack
122 
123 #endif
static void Start(const std::string &name)
Start the given timer.
Definition: timers.cpp:28
double Objective(const arma::mat &data, const arma::mat &codes, const arma::uvec &adjacencies) const
Compute objective function given the list of adjacencies.
Definition: lcc.cpp:226
void OptimizeDictionary(const arma::mat &data, const arma::mat &codes, const arma::uvec &adjacencies)
Learn dictionary by solving linear system.
Definition: lcc.cpp:66
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
void serialize(Archive &ar, const uint32_t)
Serialize the model.
Definition: lcc_impl.hpp:110
static MLPACK_EXPORT util::PrefixedOutStream Warn
Prints warning messages prefixed with [WARN ].
Definition: log.hpp:87
static void Stop(const std::string &name)
Stop the given timer.
Definition: timers.cpp:36
LocalCoordinateCoding(const arma::mat &data, const size_t atoms, const double lambda, const size_t maxIterations=0, const double tolerance=0.01, const DictionaryInitializer &initializer=DictionaryInitializer())
Set the parameters to LocalCoordinateCoding, and train the dictionary.
Definition: lcc_impl.hpp:22
static MLPACK_EXPORT util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
Definition: log.hpp:84
void Encode(const arma::mat &data, arma::mat &codes)
Code each point via distance-weighted LARS.
Definition: lcc.cpp:31
double Train(const arma::mat &data, const DictionaryInitializer &initializer=DictionaryInitializer())
Run local coordinate coding.
Definition: lcc_impl.hpp:39