All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
nmf.cpp
Go to the documentation of this file.
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the PDF manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
#include <ctime>
#include <cmath>
#include "viennacl/linalg/prod.hpp"
#include "viennacl/linalg/nmf.hpp"
typedef float ScalarType;
const ScalarType EPS = ScalarType(0.03);
template<typename MATRIX>
float matrix_compare(MATRIX & res, viennacl::matrix_base<ScalarType>& ref)
{
float diff = 0.0;
float mx = 0.0;
for (std::size_t i = 0; i < ref.size1(); i++)
{
for (std::size_t j = 0; j < ref.size2(); ++j)
{
diff = std::max(diff, std::abs(res(i, j) - ref(i, j)));
ScalarType valRes = (ScalarType) res(i, j);
mx = std::max(mx, valRes);
}
}
return diff / mx;
}
void fill_random(viennacl::matrix_base<ScalarType>& v);
void fill_random(viennacl::matrix_base<ScalarType>& v)
{
for (std::size_t i = 0; i < v.size1(); i++)
{
for (std::size_t j = 0; j < v.size2(); ++j)
v(i, j) = static_cast<ScalarType>(rand()) / ScalarType(RAND_MAX);
}
}
void test_nmf(std::size_t m, std::size_t k, std::size_t n);
void test_nmf(std::size_t m, std::size_t k, std::size_t n)
{
viennacl::matrix<ScalarType> v_ref(m, n);
viennacl::matrix<ScalarType> w_ref(m, k);
viennacl::matrix<ScalarType> h_ref(k, n);
fill_random(w_ref);
fill_random(h_ref);
v_ref = viennacl::linalg::prod(w_ref, h_ref); //reference result
viennacl::matrix<ScalarType> w_nmf(m, k);
viennacl::matrix<ScalarType> h_nmf(k, n);
fill_random(w_nmf);
fill_random(h_nmf);
viennacl::linalg::nmf_config conf;
conf.print_relative_error(true);
conf.max_iterations(3000); //3000 iterations are enough for the test
viennacl::linalg::nmf(v_ref, w_nmf, h_nmf, conf);
viennacl::matrix<ScalarType> v_nmf = viennacl::linalg::prod(w_nmf, h_nmf);
float diff = matrix_compare(v_ref, v_nmf);
bool diff_ok = fabs(diff) < EPS;
long iterations = static_cast<long>(conf.iters());
printf("%6s [%lux%lux%lu] diff = %.6f (%ld iterations)\n", diff_ok ? "[[OK]]" : "[FAIL]", m, k, n,
diff, iterations);
if (!diff_ok)
exit(EXIT_FAILURE);
}
int main()
{
//srand(time(NULL)); //let's use deterministic tests, so keep the default srand() initialization
std::cout << std::endl;
std::cout << "------- Test NMF --------" << std::endl;
std::cout << std::endl;
test_nmf(3, 3, 3);
test_nmf(5, 4, 5);
test_nmf(16, 7, 12);
test_nmf(140, 86, 113);
std::cout << std::endl;
std::cout << "------- Test completed --------" << std::endl;
std::cout << std::endl;
return EXIT_SUCCESS;
}
main
int main()
Definition: nmf.cpp:100
prod.hpp
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
viennacl::matrix_base
Definition: matrix_def.hpp:103
viennacl::linalg::nmf_config
Configuration class for the nonnegative-matrix-factorization algorithm. Specify tolerances, maximum iteration counts, etc., here.
Definition: nmf_operations.hpp:39
EPS
const ScalarType EPS
Definition: nmf.cpp:31
viennacl::matrix
A dense matrix class.
Definition: forwards.h:375
viennacl::linalg::detail::max
T max(const T &lhs, const T &rhs)
Maximum.
Definition: util.hpp:59
matrix_compare
float matrix_compare(MATRIX &res, viennacl::matrix_base< ScalarType > &ref)
Definition: nmf.cpp:34
viennacl::linalg::prod
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
viennacl::linalg::nmf_config::max_iterations
vcl_size_t max_iterations() const
Returns the maximum number of iterations for the NMF algorithm.
Definition: nmf_operations.hpp:74
test_nmf
void test_nmf(std::size_t m, std::size_t k, std::size_t n)
Definition: nmf.cpp:64
viennacl::matrix_base::size2
size_type size2() const
Returns the number of columns.
Definition: matrix_def.hpp:226
viennacl::matrix_base::size1
size_type size1() const
Returns the number of rows.
Definition: matrix_def.hpp:224
viennacl::linalg::nmf_config::iters
vcl_size_t iters() const
Returns the number of iterations of the last NMF run using this configuration object.
Definition: nmf_operations.hpp:85
viennacl::linalg::nmf
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
ScalarType
float ScalarType
Definition: fft_1d.cpp:42
fill_random
void fill_random(viennacl::matrix_base< ScalarType > &v)
Definition: nmf.cpp:53
ScalarType
float ScalarType
Definition: nmf.cpp:29
diff
ScalarType diff(ScalarType &s1, viennacl::scalar< ScalarType > &s2)
Definition: blas3_solve.cpp:50
viennacl::linalg::nmf_config::print_relative_error
bool print_relative_error() const
Returns the flag specifying whether the relative tolerance should be printed in each iteration...
Definition: nmf_operations.hpp:103
nmf.hpp
Provides a nonnegative matrix factorization implementation. Experimental.