[mlpack-svn] r13331 - mlpack/trunk/src/mlpack/methods/nmf
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Fri Aug 3 17:21:31 EDT 2012
Author: rmohan
Date: 2012-08-03 17:21:31 -0400 (Fri, 03 Aug 2012)
New Revision: 13331
Added:
mlpack/trunk/src/mlpack/methods/nmf/random_acol_init.hpp
Modified:
mlpack/trunk/src/mlpack/methods/nmf/CMakeLists.txt
mlpack/trunk/src/mlpack/methods/nmf/als_update_rules.hpp
mlpack/trunk/src/mlpack/methods/nmf/mult_div_update_rules.hpp
mlpack/trunk/src/mlpack/methods/nmf/nmf.hpp
mlpack/trunk/src/mlpack/methods/nmf/nmf_impl.hpp
Log:
Wrote all the NMF test case and updated Alternating Least Squares method with pseudo-inverse function'
Modified: mlpack/trunk/src/mlpack/methods/nmf/CMakeLists.txt
===================================================================
--- mlpack/trunk/src/mlpack/methods/nmf/CMakeLists.txt 2012-08-03 19:59:12 UTC (rev 13330)
+++ mlpack/trunk/src/mlpack/methods/nmf/CMakeLists.txt 2012-08-03 21:21:31 UTC (rev 13331)
@@ -7,7 +7,7 @@
mult_div_update_rules.hpp
als_update_rules.hpp
random_init.hpp
-# randomacolinit.hpp
+ random_acol_init.hpp
nmf.hpp
nmf_impl.hpp
)
Modified: mlpack/trunk/src/mlpack/methods/nmf/als_update_rules.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/nmf/als_update_rules.hpp 2012-08-03 19:59:12 UTC (rev 13330)
+++ mlpack/trunk/src/mlpack/methods/nmf/als_update_rules.hpp 2012-08-03 21:21:31 UTC (rev 13331)
@@ -38,13 +38,14 @@
* @param W Basis matrix to be updated.
* @param H Encoding matrix.
*/
- inline static void Update(const arma::mat& /* V */,
+ inline static void Update(const arma::mat& V,
arma::mat& W,
const arma::mat& H)
{
- W = (inv(H * H.t()) * H * H.t()).t();
-
- // Set all negative numbers to 0.
+ //W = (inv(H * H.t()) * H * V.t()).t();
+ W = V * H.t() * pinv(H * H.t());
+
+ // Set all negative numbers to machine epsilon
for (size_t i = 0; i < W.n_elem; i++)
{
if (W(i) < 0.0)
@@ -79,7 +80,7 @@
const arma::mat& W,
arma::mat& H)
{
- H = inv(W.t() * W) * W.t() * V;
+ H = pinv(W.t() * W) * W.t() * V;
// Set all negative numbers to 0.
for (size_t i = 0; i < H.n_elem; i++)
Modified: mlpack/trunk/src/mlpack/methods/nmf/mult_div_update_rules.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/nmf/mult_div_update_rules.hpp 2012-08-03 19:59:12 UTC (rev 13330)
+++ mlpack/trunk/src/mlpack/methods/nmf/mult_div_update_rules.hpp 2012-08-03 21:21:31 UTC (rev 13331)
@@ -5,7 +5,7 @@
* Update rules for the Non-negative Matrix Factorization. This follows a method
* described in the paper 'Algorithms for Non-negative Matrix Factorization'
* by D. D. Lee and H. S. Seung. This is a multiplicative rule that ensures
- * that the the 'divergence'
+ * that the Kullback–Leibler divergence
* \f$ \sum_i \sum_j (V_{ij} log\frac{V_{ij}}{(WH)_{ij}}-V_{ij}+(WH)_{ij}) \f$is
* non-increasing between subsequent iterations. Both of the update rules
* for W and H are defined in this file.
Modified: mlpack/trunk/src/mlpack/methods/nmf/nmf.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/nmf/nmf.hpp 2012-08-03 19:59:12 UTC (rev 13330)
+++ mlpack/trunk/src/mlpack/methods/nmf/nmf.hpp 2012-08-03 21:21:31 UTC (rev 13331)
@@ -89,7 +89,7 @@
* the H vector has states that it needs to store.
*/
NMF(const size_t maxIterations = 10000,
- const double minResidue = 1e-5,
+ const double minResidue = 1e-10,
const InitializationRule initializateRule = InitializationRule(),
const WUpdateRule wUpdate = WUpdateRule(),
const HUpdateRule hUpdate = HUpdateRule());
Modified: mlpack/trunk/src/mlpack/methods/nmf/nmf_impl.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/nmf/nmf_impl.hpp 2012-08-03 19:59:12 UTC (rev 13330)
+++ mlpack/trunk/src/mlpack/methods/nmf/nmf_impl.hpp 2012-08-03 21:21:31 UTC (rev 13331)
@@ -81,21 +81,15 @@
if (iteration != 0)
{
residue = fabs(normOld - norm);
- if (normOld > 1.0)
- {
- residue /= normOld;
- }
+ residue /= normOld;
}
normOld = norm;
- Log::Debug << "NMF iteration " << iteration << ": residue "
- << sqrt(residue) << std::endl;
-
iteration++;
}
- Log::Info << "NMF converged to residue of " << sqrt(residue) << " in "
+ Log::Debug << "NMF converged to residue of " << sqrt(residue) << " in "
<< iteration << " iterations." << std::endl;
}
Added: mlpack/trunk/src/mlpack/methods/nmf/random_acol_init.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/nmf/random_acol_init.hpp (rev 0)
+++ mlpack/trunk/src/mlpack/methods/nmf/random_acol_init.hpp 2012-08-03 21:21:31 UTC (rev 13331)
@@ -0,0 +1,125 @@
+/**
+ * @file randomacolinit.hpp
+ * @author Mohan Rajendran
+ *
+ * Intialization rule for the Non-negative Matrix Factorization. This simple
+ * initialization is performed by the rendom Acol initialization introduced in
+ * the paper 'Algorithms, Initializations and Convergence' by Langville et al.
+ * This method sets each of the column of W by averaging p randomly chosen
+ * columns of A.
+ */
+
+#ifndef __MLPACK_METHODS_NMF_RANDOMACOLINIT_HPP
+#define __MLPACK_METHODS_NMF_RANDOMACOLINIT_HPP
+
+#include <mlpack/core.hpp>
+
+namespace mlpack {
+namespace nmf {
+
+class RandomAcolInitialization
+{
+ public:
+ // Empty constructor required for the InitializeRule template
+ RandomAcolInitialization()
+ { }
+
+ inline static void Initialize(const arma::mat& V,
+ const size_t r,
+ arma::mat& W,
+ arma::mat& H)
+ {
+ // Simple implementation. This can be left here.
+
+ size_t n = V.n_rows;
+ size_t m = V.n_cols;
+
+ size_t p = 5;
+
+ if(p > m)
+ {
+ Log::Info << "No. of random columns is more than the number of columns "
+ << "available in the V matrix. Setting the no. of random columns "
+ << "to " << m << "." << std::endl;
+ p = m;
+ }
+
+ W.reset();
+
+ // Initialize W matrix
+ arma::vec temp;
+ for(size_t col=0;col<r;col++)
+ {
+ temp.zeros(n);
+ for(size_t randcol=0;randcol<p;randcol++)
+ {
+ size_t rnd = math::RandInt(0,m);
+ temp += V.col(rnd);
+ }
+ W.insert_cols(col,temp/p);
+ }
+
+ // Intialize H to random values
+ H.randu(r,m);
+ }
+
+}; // Class RandomAcolInitialization
+
+}; // namespace nmf
+}; // namespace mlpack
+
+#endif
+
+/*namespace mlpack {
+namespace nmf {
+
+class RandomAcolInitialization
+{
+ private:
+ size_t p;
+ public:
+ // Constructor required for the InitializeRule template
+ RandomAcolInitialization()
+ { }
+
+ inline void Init(const arma::mat& V,
+ arma::mat& W,
+ arma::mat& H,
+ const size_t& r)
+ {
+ // Simple inplementation. This can be left here.
+ size_t n = V.n_rows;
+ size_t m = V.n_cols;
+ p = 5;
+ if(p > m)
+ {
+ Log::Info << "No. of random columns is more than the number of columns "
+ << "available in the V matrix. Setting the no. of random columns "
+ << "to " << m << "." << std::endl;
+ p = m;
+ }
+
+ // Initialize W matrix
+ W.zeros(n,r);
+ arma::colvec temp;
+ for(size_t col=0;col<r;col++)
+ {
+ temp.zeros();
+ for(size_t randcol=0;randcol<p;randcol++)
+ {
+ size_t rnd = math::RandInt(0,m);
+ temp += V.col(rnd);
+ }
+ W.insert_cols(col,temp/p);
+ }
+
+ // Intialize H to random values
+ H.randu(r,m);
+ }
+
+}; // Class RandomInitialization
+
+}; // namespace nmf
+}; // namespace mlpack
+
+#endif*/
More information about the mlpack-svn
mailing list