mlpack  master
sparse_coding.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_SPARSE_CODING_SPARSE_CODING_HPP
14 #define MLPACK_METHODS_SPARSE_CODING_SPARSE_CODING_HPP
15 
16 #include <mlpack/prereqs.hpp>
18 
19 // Include our three simple dictionary initializers.
20 #include "nothing_initializer.hpp"
22 #include "random_initializer.hpp"
23 
24 namespace mlpack {
25 namespace sparse_coding {
26 
116 {
117  public:
144  template<typename DictionaryInitializer = DataDependentRandomInitializer>
145  SparseCoding(const arma::mat& data,
146  const size_t atoms,
147  const double lambda1,
148  const double lambda2 = 0,
149  const size_t maxIterations = 0,
150  const double objTolerance = 0.01,
151  const double newtonTolerance = 1e-6,
152  const DictionaryInitializer& initializer =
153  DictionaryInitializer());
154 
171  SparseCoding(const size_t atoms = 0,
172  const double lambda1 = 0,
173  const double lambda2 = 0,
174  const size_t maxIterations = 0,
175  const double objTolerance = 0.01,
176  const double newtonTolerance = 1e-6);
177 
181  template<typename DictionaryInitializer = DataDependentRandomInitializer>
182  void Train(const arma::mat& data,
183  const DictionaryInitializer& initializer =
184  DictionaryInitializer());
185 
193  void Encode(const arma::mat& data, arma::mat& codes);
194 
206  double OptimizeDictionary(const arma::mat& data,
207  const arma::mat& codes,
208  const arma::uvec& adjacencies);
209 
213  void ProjectDictionary();
214 
218  double Objective(const arma::mat& data, const arma::mat& codes) const;
219 
221  const arma::mat& Dictionary() const { return dictionary; }
223  arma::mat& Dictionary() { return dictionary; }
224 
226  size_t Atoms() const { return atoms; }
228  size_t& Atoms() { return atoms; }
229 
231  double Lambda1() const { return lambda1; }
233  double& Lambda1() { return lambda1; }
234 
236  double Lambda2() const { return lambda2; }
238  double& Lambda2() { return lambda2; }
239 
241  size_t MaxIterations() const { return maxIterations; }
243  size_t& MaxIterations() { return maxIterations; }
244 
246  double ObjTolerance() const { return objTolerance; }
248  double& ObjTolerance() { return objTolerance; }
249 
251  double NewtonTolerance() const { return newtonTolerance; }
253  double& NewtonTolerance() { return newtonTolerance; }
254 
256  template<typename Archive>
257  void Serialize(Archive& ar, const unsigned int /* version */);
258 
259  private:
261  size_t atoms;
262 
264  arma::mat dictionary;
265 
267  double lambda1;
269  double lambda2;
270 
274  double objTolerance;
277 };
278 
279 } // namespace sparse_coding
280 } // namespace mlpack
281 
282 // Include implementation.
283 #include "sparse_coding_impl.hpp"
284 
285 #endif
size_t Atoms() const
Access the number of atoms.
size_t MaxIterations() const
Get the maximum number of iterations.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
The core includes that mlpack expects; standard C++ includes and Armadillo.
void ProjectDictionary()
Project each atom of the dictionary back onto the unit ball, if necessary.
double Lambda1() const
Access the L1 regularization term.
double newtonTolerance
Tolerance for Newton&#39;s method (dictionary training).
const arma::mat & Dictionary() const
Access the dictionary.
double & Lambda1()
Modify the L1 regularization term.
arma::mat & Dictionary()
Modify the dictionary.
double Lambda2() const
Access the L2 regularization term.
SparseCoding(const arma::mat &data, const size_t atoms, const double lambda1, const double lambda2=0, const size_t maxIterations=0, const double objTolerance=0.01, const double newtonTolerance=1e-6, const DictionaryInitializer &initializer=DictionaryInitializer())
Set the parameters to SparseCoding.
double ObjTolerance() const
Get the objective tolerance.
double objTolerance
Tolerance for main objective.
void Train(const arma::mat &data, const DictionaryInitializer &initializer=DictionaryInitializer())
Train the sparse coding model on the given dataset.
double lambda2
l2 regularization term.
An implementation of Sparse Coding with Dictionary Learning that achieves sparsity via an l1-norm reg...
arma::mat dictionary
Dictionary (columns are atoms).
double & ObjTolerance()
Modify the objective tolerance.
void Serialize(Archive &ar, const unsigned int)
Serialize the sparse coding model.
size_t & Atoms()
Modify the number of atoms.
double lambda1
l1 regularization term.
double & NewtonTolerance()
Modify the tolerance for Newton&#39;s method (dictionary optimization step).
size_t & MaxIterations()
Modify the maximum number of iterations.
double NewtonTolerance() const
Get the tolerance for Newton&#39;s method (dictionary optimization step).
double Objective(const arma::mat &data, const arma::mat &codes) const
Compute the objective function.
double & Lambda2()
Modify the L2 regularization term.
double OptimizeDictionary(const arma::mat &data, const arma::mat &codes, const arma::uvec &adjacencies)
Learn dictionary via Newton method based on Lagrange dual.
size_t maxIterations
Maximum number of iterations during training.
void Encode(const arma::mat &data, arma::mat &codes)
Sparse code each point in the given dataset via LARS, using the current dictionary and store the enco...