ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
nmf.cpp
Go to the documentation of this file.
1 /* =========================================================================
2  Copyright (c) 2010-2015, Institute for Microelectronics,
3  Institute for Analysis and Scientific Computing,
4  TU Wien.
5  Portions of this software are copyright by UChicago Argonne, LLC.
6 
7  -----------------
8  ViennaCL - The Vienna Computing Library
9  -----------------
10 
11  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
12 
13  (A list of authors and contributors can be found in the PDF manual)
14 
15  License: MIT (X11), see file LICENSE in the base directory
16  ============================================================================= */
17 
18 
23 #include <ctime>
24 #include <cmath>
25 
26 #include "viennacl/linalg/prod.hpp"
27 #include "viennacl/linalg/nmf.hpp"
28 
29 typedef float ScalarType;
30 
31 const ScalarType EPS = ScalarType(0.03);
32 
33 template<typename MATRIX>
35 {
36  float diff = 0.0;
37  float mx = 0.0;
38 
39  for (std::size_t i = 0; i < ref.size1(); i++)
40  {
41  for (std::size_t j = 0; j < ref.size2(); ++j)
42  {
43  diff = std::max(diff, std::abs(res(i, j) - ref(i, j)));
44  ScalarType valRes = (ScalarType) res(i, j);
45  mx = std::max(mx, valRes);
46  }
47  }
48  return diff / mx;
49 }
50 
52 
54 {
55  for (std::size_t i = 0; i < v.size1(); i++)
56  {
57  for (std::size_t j = 0; j < v.size2(); ++j)
58  v(i, j) = static_cast<ScalarType>(rand()) / ScalarType(RAND_MAX);
59  }
60 }
61 
62 void test_nmf(std::size_t m, std::size_t k, std::size_t n);
63 
64 void test_nmf(std::size_t m, std::size_t k, std::size_t n)
65 {
66  viennacl::matrix<ScalarType> v_ref(m, n);
67  viennacl::matrix<ScalarType> w_ref(m, k);
68  viennacl::matrix<ScalarType> h_ref(k, n);
69 
70  fill_random(w_ref);
71  fill_random(h_ref);
72 
73  v_ref = viennacl::linalg::prod(w_ref, h_ref); //reference result
74 
75  viennacl::matrix<ScalarType> w_nmf(m, k);
76  viennacl::matrix<ScalarType> h_nmf(k, n);
77 
78  fill_random(w_nmf);
79  fill_random(h_nmf);
80 
82  conf.print_relative_error(true);
83  conf.max_iterations(3000); //3000 iterations are enough for the test
84 
85  viennacl::linalg::nmf(v_ref, w_nmf, h_nmf, conf);
86 
88 
89  float diff = matrix_compare(v_ref, v_nmf);
90  bool diff_ok = fabs(diff) < EPS;
91 
92  long iterations = static_cast<long>(conf.iters());
93  printf("%6s [%lux%lux%lu] diff = %.6f (%ld iterations)\n", diff_ok ? "[[OK]]" : "[FAIL]", m, k, n,
94  diff, iterations);
95 
96  if (!diff_ok)
97  exit(EXIT_FAILURE);
98 }
99 
100 int main()
101 {
102  //srand(time(NULL)); //let's use deterministic tests, so keep the default srand() initialization
103  std::cout << std::endl;
104  std::cout << "------- Test NMF --------" << std::endl;
105  std::cout << std::endl;
106 
107  test_nmf(3, 3, 3);
108  test_nmf(5, 4, 5);
109  test_nmf(16, 7, 12);
110  test_nmf(140, 86, 113);
111 
112  std::cout << std::endl;
113  std::cout << "------- Test completed --------" << std::endl;
114  std::cout << std::endl;
115 
116  return EXIT_SUCCESS;
117 }
int main()
Definition: nmf.cpp:100
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
Configuration class for the nonnegative-matrix-factorization algorithm. Specify tolerances, maximum iteration counts, etc., here.
const ScalarType EPS
Definition: nmf.cpp:31
A dense matrix class.
Definition: forwards.h:375
T max(const T &lhs, const T &rhs)
Maximum.
Definition: util.hpp:59
float matrix_compare(MATRIX &res, viennacl::matrix_base< ScalarType > &ref)
Definition: nmf.cpp:34
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
vcl_size_t max_iterations() const
Returns the maximum number of iterations for the NMF algorithm.
void test_nmf(std::size_t m, std::size_t k, std::size_t n)
Definition: nmf.cpp:64
size_type size2() const
Returns the number of columns.
Definition: matrix_def.hpp:226
size_type size1() const
Returns the number of rows.
Definition: matrix_def.hpp:224
vcl_size_t iters() const
Returns the number of iterations of the last NMF run using this configuration object.
void nmf(viennacl::matrix_base< ScalarType > const &V, viennacl::matrix_base< ScalarType > &W, viennacl::matrix_base< ScalarType > &H, viennacl::linalg::nmf_config const &conf)
The nonnegative matrix factorization (approximation) algorithm as suggested by Lee and Seung...
Definition: nmf.hpp:57
float ScalarType
Definition: fft_1d.cpp:42
void fill_random(viennacl::matrix_base< ScalarType > &v)
Definition: nmf.cpp:53
float ScalarType
Definition: nmf.cpp:29
ScalarType diff(ScalarType &s1, viennacl::scalar< ScalarType > &s2)
Definition: blas3_solve.cpp:69
bool print_relative_error() const
Returns the flag specifying whether the relative tolerance should be printed in each iteration...
Provides a nonnegative matrix factorization implementation. Experimental.