ViennaCL - The Vienna Computing Library: examples/tutorial/nmf.cpp Source File

ViennaCL - The Vienna Computing Library 1.7.1 Free open-source GPU-accelerated linear algebra and solver library. |

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
nmf.cpp
Go to the documentation of this file.
1 /* =========================================================================
2 Copyright (c) 2010-2016, 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
26 #include "viennacl/matrix.hpp"
27 #include "viennacl/linalg/nmf.hpp"
28
29 // feel free to change this to 'double' if supported by your hardware.
30 typedef float ScalarType;
31
36 template<typename MajorT>
37 void fill_random(viennacl::matrix<ScalarType, MajorT> & A)
38 {
39 for (std::size_t i = 0; i < A.size1(); i++)
40 for (std::size_t j = 0; j < A.size2(); ++j)
41 A(i, j) = static_cast<ScalarType>(rand()) / ScalarType(RAND_MAX);
42 }
43
48 int main()
49 {
50 std::cout << std::endl;
51 std::cout << "------- Tutorial NMF --------" << std::endl;
52 std::cout << std::endl;
53
57 unsigned int m = 7; //size1 of W and size1 of V
58 unsigned int n = 6; //size2 of V and size2 of H
59 unsigned int k = 3; //size2 of W and size1 of H
60
61 viennacl::matrix<ScalarType, viennacl::column_major> V(m, n);
62 viennacl::matrix<ScalarType, viennacl::column_major> W(m, k);
63 viennacl::matrix<ScalarType, viennacl::column_major> H(k, n);
64
68 fill_random(V);
69 fill_random(W);
70 fill_random(H);
71
72 std::cout << "Input matrices:" << std::endl;
73 std::cout << "V" << V << std::endl;
74 std::cout << "W" << W << std::endl;
75 std::cout << "H" << H << "\n" << std::endl;
76
80 viennacl::linalg::nmf_config conf;
81 conf.print_relative_error(false);
82 conf.max_iterations(50); // 50 iterations are enough here
83
87 std::cout << "Computing NMF" << std::endl;
88 viennacl::linalg::nmf(V, W, H, conf);
89
90 std::cout << "RESULT:" << std::endl;
91 std::cout << "V" << V << std::endl;
92 std::cout << "W" << W << std::endl;
93 std::cout << "H" << H << "\n" << std::endl;
94
98 std::cout << "W*H:" << std::endl;
99 viennacl::matrix<ScalarType> resultCorrect = viennacl::linalg::prod(W, H);
100 std::cout << resultCorrect << std::endl;
101
102 std::cout << std::endl;
103 std::cout << "------- Tutorial completed --------" << std::endl;
104 std::cout << std::endl;
105
106 }
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.