mlpack
amf_impl.hpp
Go to the documentation of this file.
1 
14 namespace mlpack {
15 namespace amf {
16 
20 template<typename TerminationPolicyType,
21  typename InitializationRuleType,
22  typename UpdateRuleType>
24  const TerminationPolicyType& terminationPolicy,
25  const InitializationRuleType& initializationRule,
26  const UpdateRuleType& update) :
27  terminationPolicy(terminationPolicy),
28  initializationRule(initializationRule),
29  update(update)
30 { }
31 
40 template<typename TerminationPolicyType,
41  typename InitializationRuleType,
42  typename UpdateRuleType>
43 template<typename MatType>
45 Apply(const MatType& V,
46  const size_t r,
47  arma::mat& W,
48  arma::mat& H)
49 {
50  // Initialize W and H.
51  initializationRule.Initialize(V, r, W, H);
52 
53  Log::Info << "Initialized W and H." << std::endl;
54 
55  // initialize the update rule
56  update.Initialize(V, r);
57  // initialize the termination policy
58  terminationPolicy.Initialize(V);
59 
60  // check if termination conditions are met
61  while (!terminationPolicy.IsConverged(W, H))
62  {
63  // Update the values of W and H based on the update rules provided.
64  update.WUpdate(V, W, H);
65  update.HUpdate(V, W, H);
66  }
67 
68  // get final residue and iteration count from termination policy
69  const double residue = terminationPolicy.Index();
70  const size_t iteration = terminationPolicy.Iteration();
71 
72  Log::Info << "AMF converged to residue of " << residue << " in "
73  << iteration << " iterations." << std::endl;
74 
75  return residue;
76 }
77 
78 } // namespace amf
79 } // namespace mlpack
double Apply(const MatType &V, const size_t r, arma::mat &W, arma::mat &H)
Apply Alternating Matrix Factorization to the provided matrix.
Definition: amf_impl.hpp:45
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
static MLPACK_EXPORT util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
Definition: log.hpp:84
AMF(const TerminationPolicyType &terminationPolicy=TerminationPolicyType(), const InitializationRuleType &initializeRule=InitializationRuleType(), const UpdateRuleType &update=UpdateRuleType())
Create the AMF object and (optionally) set the parameters which AMF will run with.
Definition: amf_impl.hpp:23