MLPACK  1.0.4
random_acol_init.hpp
Go to the documentation of this file.
00001 
00026 #ifndef __MLPACK_METHODS_NMF_RANDOM_ACOL_INIT_HPP
00027 #define __MLPACK_METHODS_NMF_RANDOM_ACOL_INIT_HPP
00028 
00029 #include <mlpack/core.hpp>
00030 
00031 namespace mlpack {
00032 namespace nmf {
00033 
00041 template<int p = 5>
00042 class RandomAcolInitialization
00043 {
00044  public:
00045   // Empty constructor required for the InitializeRule template
00046   RandomAcolInitialization()
00047   { }
00048 
00049   inline static void Initialize(const arma::mat& V,
00050                                 const size_t r,
00051                                 arma::mat& W,
00052                                 arma::mat& H)
00053   {
00054     const size_t n = V.n_rows;
00055     const size_t m = V.n_cols;
00056 
00057     if (p > m)
00058     {
00059       Log::Warn << "Number of random columns is more than the number of columns"
00060           << "available in the V matrix; weird results may ensue!" << std::endl;
00061     }
00062 
00063     W.zeros(n, r);
00064 
00065     // Initialize W matrix with random columns.
00066     for (size_t col = 0; col < r; col++)
00067     {
00068       for (size_t randCol = 0; randCol < p; randCol++)
00069       {
00070         W.col(col) += V.col(math::RandInt(0, m));
00071       }
00072     }
00073 
00074     // Now divide by p.
00075     W /= p;
00076 
00077     // Initialize H to random values.
00078     H.randu(r, m);
00079   }
00080 }; // Class RandomAcolInitialization
00081 
00082 }; // namespace nmf
00083 }; // namespace mlpack
00084 
00085 #endif