planc
Parallel Lowrank Approximation with Non-negativity Constraints
mu.hpp
Go to the documentation of this file.
1 /* Copyright 2016 Ramakrishnan Kannan */
2 
3 #ifndef NMF_MU_HPP_
4 #define NMF_MU_HPP_
5 
6 #include "common/nmf.hpp"
7 
8 namespace planc {
9 
10 template <class T>
11 class MUNMF : public NMF<T> {
12  private:
13  // Not happy with this design. However to avoid computing At again and again
14  // making this as private variable.
15  T At;
16  MAT WtW;
17  MAT HtH;
18  MAT AtW;
19  MAT AH;
20 
21  /*
22  * Collected statistics are
23  * iteration Htime Wtime totaltime normH normW densityH densityW relError
24  */
25  void allocateMatrices() {
26  WtW = arma::zeros<MAT>(this->k, this->k);
27  HtH = arma::zeros<MAT>(this->k, this->k);
28  AtW = arma::zeros<MAT>(this->n, this->k);
29  AH = arma::zeros<MAT>(this->m, this->k);
30  }
31  void freeMatrices() {
32  this->At.clear();
33  WtW.clear();
34  HtH.clear();
35  AtW.clear();
36  AH.clear();
37  }
38 
39  public:
40  MUNMF(const T &A, int lowrank) : NMF<T>(A, lowrank) {
41  allocateMatrices();
42  this->At = this->A.t();
43  }
44  MUNMF(const T &A, const MAT &llf, const MAT &rlf) : NMF<T>(A, llf, rlf) {
45  allocateMatrices();
46  this->At = this->A.t();
47  }
48  void computeNMF() {
49  unsigned int currentIteration = 0;
50  INFO << "computed transpose At=" << PRINTMATINFO(this->At) << std::endl;
51  while (currentIteration < this->num_iterations()) {
52  tic();
53  // update H
54  tic();
55  AtW = this->At * this->W;
56  WtW = this->W.t() * this->W;
57  INFO << "starting H Prereq for "
58  << " took=" << toc();
59  INFO << PRINTMATINFO(WtW) << PRINTMATINFO(AtW) << std::endl;
60  // to avoid divide by zero error.
61  tic();
62  // H = H.*AtW./(WtW_reg*H + epsilon);
63  this->H = (this->H % AtW) / (this->H * WtW + EPSILON_1EMINUS16);
64  INFO << "Completed H (" << currentIteration << "/"
65  << this->num_iterations() << ")"
66  << " time =" << toc() << std::endl;
67 
68  // update W;
69  tic();
70  AH = this->A * this->H;
71  HtH = this->H.t() * this->H;
72  INFO << "starting W Prereq for "
73  << " took=" << toc() << PRINTMATINFO(HtH) << PRINTMATINFO(AH)
74  << std::endl;
75  tic();
76  // W = W.*AH./(W*HtH_reg + epsilon);
77  this->W = (this->W % AH) / ((this->W * HtH) + EPSILON_1EMINUS16);
78  INFO << "Completed W (" << currentIteration << "/"
79  << this->num_iterations() << ")"
80  << " time =" << toc() << std::endl;
81  INFO << "Completed It (" << currentIteration << "/"
82  << this->num_iterations() << ")"
83  << " time =" << toc() << std::endl;
84  this->computeObjectiveError();
85  INFO << "Completed it = " << currentIteration
86  << " MUERR=" << sqrt(this->objective_err) / this->normA << std::endl;
87  currentIteration++;
88  }
89  this->normalize_by_W();
90  }
91  ~MUNMF() { freeMatrices(); }
92 };
93 
94 } // namespace planc
95 
96 #endif // NMF_MU_HPP_
MUNMF(const T &A, int lowrank)
Definition: mu.hpp:40
void tic()
start the timer. easy to call as tic(); some code; double t=toc();
Definition: utils.hpp:42
#define EPSILON_1EMINUS16
Definition: utils.h:43
const unsigned int num_iterations() const
Returns the number of iterations.
Definition: nmf.hpp:350
double toc()
Definition: utils.hpp:48
#define INFO
Definition: utils.h:36
MUNMF(const T &A, const MAT &llf, const MAT &rlf)
Definition: mu.hpp:44
void computeNMF()
Definition: mu.hpp:48
#define MAT
Definition: utils.h:52
~MUNMF()
Definition: mu.hpp:91
ncp_factors contains the factors of the ncp every ith factor is of size n_i * k number of factors is ...
Definition: ncpfactors.hpp:20
#define PRINTMATINFO(A)
Definition: utils.h:63
void computeObjectiveError()
Definition: nmf.hpp:238