planc
Parallel Lowrank Approximation with Non-negativity Constraints
nmf.cpp
Go to the documentation of this file.
1 /* Copyright 2016 Ramakrishnan Kannan */
2 
3 #include "common/nmf.hpp"
4 #include <stdio.h>
5 #include <string>
7 #include "common/utils.hpp"
8 #include "nmf/aoadmm.hpp"
9 #include "nmf/bppnmf.hpp"
10 #include "nmf/hals.hpp"
11 #include "nmf/mu.hpp"
12 
13 template <class NMFTYPE>
14 void NMFDriver(int k, UWORD m, UWORD n, std::string AfileName,
15  std::string WfileName, std::string HfileName, int numIt) {
16 #ifdef BUILD_SPARSE
17  SP_MAT A;
18 #else
19  MAT A;
20 #endif
21  double t2;
22  if (!AfileName.empty() &&
23  !AfileName.compare(AfileName.size() - 4, 4, "rand")) {
24 #ifdef BUILD_SPARSE
25  A.load(AfileName, arma::coord_ascii);
26  INFO << "Successfully loaded the input matrix" << std::endl;
27 #else
28  tic();
29  A.load(AfileName);
30  t2 = toc();
31  INFO << "Successfully loaded dense input matrix. A=" << PRINTMATINFO(A)
32  << " took=" << t2 << std::endl;
33  m = A.n_rows;
34  n = A.n_cols;
35 #endif
36  } else {
37 #ifdef BUILD_SPARSE
38  A = arma::sprandu<SP_MAT>(m, n, 0.001);
39 #else
40  A = arma::randu<MAT>(m, n);
41 #endif
42  INFO << "generated random matrix A=" << PRINTMATINFO(A) << std::endl;
43  }
44  MAT W, H;
45  NMFTYPE nmfAlgorithm(A, k);
46  nmfAlgorithm.num_iterations(numIt);
47  INFO << "completed constructor" << PRINTMATINFO(A) << std::endl;
48  tic();
49  nmfAlgorithm.computeNMF();
50  t2 = toc();
51  INFO << "time taken:" << t2 << std::endl;
52  if (WfileName.compare("_w") != 0) {
53  nmfAlgorithm.getLeftLowRankFactor().save(WfileName, arma::raw_ascii);
54  }
55  if (HfileName.compare("_h") != 0) {
56  nmfAlgorithm.getRightLowRankFactor().save(HfileName, arma::raw_ascii);
57  }
58 }
59 #ifdef BUILD_SPARSE
60 void incrementalGraph(std::string AfileName, std::string WfileName) {
61  SP_MAT A;
62  A.load(AfileName);
63  INFO << "Loaded input matrix A=" << PRINTMATINFO(A) << std::endl;
64  MAT W, H;
65  W.load(WfileName);
66  INFO << "Loaded input matrix W=" << PRINTMATINFO(W) << std::endl;
67  H.ones(A.n_cols, W.n_cols);
68  planc::BPPNMF<SP_MAT> bppnmf(A, W, H);
69  H = bppnmf.solveScalableNNLS();
70  OUTPUT << H << std::endl;
71 }
72 #endif
73 void parseCommandLineandCallNMF(int argc, char* argv[]) {
74  planc::ParseCommandLine pc(argc, argv);
75  pc.parseplancopts();
76  pc.printConfig();
77  switch (pc.lucalgo()) {
78  case MU:
79 #ifdef BUILD_SPARSE
80  NMFDriver<planc::MUNMF<SP_MAT> >(
81  pc.lowrankk(), pc.globalm(), pc.globaln(), pc.input_file_name(),
82  pc.output_file_name() + "_w", pc.output_file_name() + "_h",
83  pc.iterations());
84 #else
85  NMFDriver<planc::MUNMF<MAT> >(
86  pc.lowrankk(), pc.globalm(), pc.globaln(), pc.input_file_name(),
87  pc.output_file_name() + "_w", pc.output_file_name() + "_h",
88  pc.iterations());
89 #endif
90  break;
91  case HALS:
92 #ifdef BUILD_SPARSE
93  NMFDriver<planc::HALSNMF<SP_MAT> >(
94  pc.lowrankk(), pc.globalm(), pc.globaln(), pc.input_file_name(),
95  pc.output_file_name() + "_w", pc.output_file_name() + "_h",
96  pc.iterations());
97 #else
98  NMFDriver<planc::HALSNMF<MAT> >(
99  pc.lowrankk(), pc.globalm(), pc.globaln(), pc.input_file_name(),
100  pc.output_file_name() + "_w", pc.output_file_name() + "_h",
101  pc.iterations());
102 #endif
103  break;
104  case ANLSBPP:
105 #ifdef BUILD_SPARSE
106  NMFDriver<planc::BPPNMF<SP_MAT> >(
107  pc.lowrankk(), pc.globalm(), pc.globaln(), pc.input_file_name(),
108  pc.output_file_name() + "_w", pc.output_file_name() + "_h",
109  pc.iterations());
110 #else
111  NMFDriver<planc::BPPNMF<MAT> >(
112  pc.lowrankk(), pc.globalm(), pc.globaln(), pc.input_file_name(),
113  pc.output_file_name() + "_w", pc.output_file_name() + "_h",
114  pc.iterations());
115 #endif
116  break;
117  case AOADMM:
118 #ifdef BUILD_SPARSE
119  // NMFDriver<AOADMMNMF<SP_MAT > >(lowRank, m, n, AfileName, WInitfileName,
120  // HInitfileName, WfileName, HfileName,
121  // numIt);
122  NMFDriver<planc::AOADMMNMF<SP_MAT> >(
123  pc.lowrankk(), pc.globalm(), pc.globaln(), pc.input_file_name(),
124  pc.output_file_name() + "_w", pc.output_file_name() + "_h",
125  pc.iterations());
126 
127 #else
128  NMFDriver<planc::AOADMMNMF<MAT> >(
129  pc.lowrankk(), pc.globalm(), pc.globaln(), pc.input_file_name(),
130  pc.output_file_name() + "_w", pc.output_file_name() + "_h",
131  pc.iterations());
132 #endif
133  break;
134  default:
135  ERR << "Not a valid algorithm" << std::endl;
136  }
137 }
138 
139 int main(int argc, char* argv[]) { parseCommandLineandCallNMF(argc, argv); }
algotype lucalgo()
Returns the NMF algorithm to run. Passed as parameter –algo or -a.
void NMFDriver(int k, UWORD m, UWORD n, std::string AfileName, std::string WfileName, std::string HfileName, int numIt)
Definition: nmf.cpp:14
int main(int argc, char *argv[])
Definition: nmf.cpp:139
std::string output_file_name()
Returns output file name.
void tic()
start the timer. easy to call as tic(); some code; double t=toc();
Definition: utils.hpp:42
int iterations()
Returns number of iterations. passed as -t or –iter.
double toc()
Definition: utils.hpp:48
#define ERR
Definition: utils.h:28
void parseplancopts()
parses the command line parameters
#define SP_MAT
Definition: utils.h:57
#define INFO
Definition: utils.h:36
Definition: utils.h:10
#define UWORD
Definition: utils.h:60
UWORD lowrankk()
returns the low rank. Passed as parameter –lowrank or -k
void parseCommandLineandCallNMF(int argc, char *argv[])
Definition: nmf.cpp:73
#define OUTPUT
Definition: utils.h:40
#define MAT
Definition: utils.h:52
UWORD globalm()
return global rows. Passed as parameter -d
std::string input_file_name()
Returns input file name. Passed as -i or –input.
void printConfig()
print the configuration received through the command line paramters
#define PRINTMATINFO(A)
Definition: utils.h:63
Definition: utils.h:10
Definition: utils.h:10
Definition: utils.h:10