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 "viennacl/matrix.hpp"
#include "viennacl/linalg/nmf.hpp"
// feel free to change this to 'double' if supported by your hardware.
typedef float ScalarType;
template<typename MajorT>
void fill_random(viennacl::matrix<ScalarType, MajorT> & A)
{
for (std::size_t i = 0; i < A.size1(); i++)
for (std::size_t j = 0; j < A.size2(); ++j)
A(i, j) = static_cast<ScalarType>(rand()) / ScalarType(RAND_MAX);
}
int main()
{
std::cout << std::endl;
std::cout << "------- Tutorial NMF --------" << std::endl;
std::cout << std::endl;
unsigned int m = 7; //size1 of W and size1 of V
unsigned int n = 6; //size2 of V and size2 of H
unsigned int k = 3; //size2 of W and size1 of H
viennacl::matrix<ScalarType, viennacl::column_major> V(m, n);
viennacl::matrix<ScalarType, viennacl::column_major> W(m, k);
viennacl::matrix<ScalarType, viennacl::column_major> H(k, n);
fill_random(V);
fill_random(W);
fill_random(H);
std::cout << "Input matrices:" << std::endl;
std::cout << "V" << V << std::endl;
std::cout << "W" << W << std::endl;
std::cout << "H" << H << "\n" << std::endl;
viennacl::linalg::nmf_config conf;
conf.print_relative_error(false);
conf.max_iterations(50); // 50 iterations are enough here
std::cout << "Computing NMF" << std::endl;
viennacl::linalg::nmf(V, W, H, conf);
std::cout << "RESULT:" << std::endl;
std::cout << "V" << V << std::endl;
std::cout << "W" << W << std::endl;
std::cout << "H" << H << "\n" << std::endl;
std::cout << "W*H:" << std::endl;
viennacl::matrix<ScalarType> resultCorrect = viennacl::linalg::prod(W, H);
std::cout << resultCorrect << std::endl;
std::cout << std::endl;
std::cout << "------- Tutorial completed --------" << std::endl;
std::cout << std::endl;
}
main
int main()
Definition: nmf.cpp:100
matrix.hpp
Implementation of the dense matrix class.
viennacl::linalg::nmf_config
Configuration class for the nonnegative-matrix-factorization algorithm. Specify tolerances, maximum iteration counts, etc., here.
Definition: nmf_operations.hpp:39
viennacl::matrix
A dense matrix class.
Definition: forwards.h:375
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
viennacl::matrix_base< NumericT >::size2
size_type size2() const
Returns the number of columns.
Definition: matrix_def.hpp:226
viennacl::matrix_base< NumericT >::size1
size_type size1() const
Returns the number of rows.
Definition: matrix_def.hpp:224
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
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.