NAG CPP Interface
nagcpp::matop::real_nmf_rcomm (f01sb)
1
Purpose
real_nmf_rcomm computes a non-negative matrix factorization for a real non-negative matrix . It uses reverse communication for evaluating matrix products, so that the matrix is not accessed explicitly.
2
Specification
#include "f01/nagcpp_f01sb.hpp"
template <typename W, typename H, typename HT>
void function real_nmf_rcomm(types::f77_integer &irevcm, W &&w, H &&h, HT &&ht, CopyableComm &comm, OptionalF01SB opt)
template <typename W, typename H, typename HT>
void function real_nmf_rcomm(types::f77_integer &irevcm, W &&w, H &&h, HT &&ht, CopyableComm &comm)
3
Description
The matrix
is factorized into the product of an
matrix
and a
matrix
, both with non-negative elements. The factorization is approximate,
, with
and
chosen to minimize the functional
You are free to choose any value for , provided . The product will then be a low-rank approximation to , with rank at most .
real_nmf_rcomm finds and using an iterative method known as the Hierarchical Alternating Least Squares algorithm. You may specify initial values for and , or you may provide a seed value for real_nmf_rcomm to generate the initial values using a random number generator.
real_nmf_rcomm does not explicitly need to access the elements of ; it only requires the result of matrix multiplications of the form or . A reverse communication interface is used, in which control is returned to the calling program whenever a matrix product is required.
4
References
Cichocki A and Phan A–H (2009) Fast local algorithms for large scale nonnegative matrix and tensor factorizations IEICE Transactions on Fundamentals of Electronics, Communications and Computer Sciences E92–A 708–721
Cichocki A, Zdunek R and Amari S–I (2007) Hierarchical ALS algorithms for nonnegative matrix and 3D tensor factorization Lecture Notes in Computer Science 4666 Springer 169–176
Ho N–D (2008) Nonnegative matrix factorization algorithms and applications PhD Thesis Univ. Catholique de Louvain
5
Arguments
Note: this function uses
reverse communication. Its use involves an initial entry, intermediate exits and re-entries, and a final exit, as indicated by the argument
irevcm. Between intermediate exits and re-entries,
all arguments other than w and
ht must remain unchanged.
-
1:
– types::f77_integer
Input/Output
-
On initial entry: must be set to .
On intermediate exit:
specifies what action you must take before re-entering
real_nmf_rcomm with
irevcm unchanged. The value of
irevcm should be interpreted as follows:
- Indicates the start of a new iteration. No action is required by you, but w and h are available for printing, and a limit on the number of iterations can be applied.
- Indicates that before re-entry to real_nmf_rcomm, the product must be computed and stored in ht.
- Indicates that before re-entry to real_nmf_rcomm, the product must be computed and stored in w.
On final exit: .
-
2:
– double array
Input/Output
-
On initial entry:
- if , w should be set to an initial iterate for the non-negative matrix factor, .
- If , w need not be set. real_nmf_rcomm will generate a random initial iterate.
On intermediate exit:
if
or
,
w contains the current iterate of the
non-negative matrix
.
On intermediate re-entry:
- if , w must contain , where is stored in .
- If , or , w must not be changed.
On final exit:
w contains the
non-negative matrix
.
-
3:
– double array
Input/Output
-
On initial entry:
- if , h should be set to an initial iterate for the non-negative matrix factor, .
- If , h need not be set. real_nmf_rcomm will generate a random initial iterate.
On intermediate exit:
if
,
h contains the current iterate of the
non-negative matrix
.
On intermediate re-entry:
h must not be changed.
On final exit:
h contains the
non-negative matrix
.
-
4:
– double array
Input/Output
-
On initial entry:
ht need not be set.
On intermediate exit:
if
,
ht contains the
non-negative matrix
, which is required in order to form
.
On intermediate re-entry: if
,
ht must contain
.
If
,
or
,
ht must not be changed.
On final exit:
ht is undefined.
-
5:
– CopyableComm
Input/Output
-
Communication structure.
On initial entry: need not be set.
Container for:
- comm – double array
This optional parameter
may be set using the method
CopyableComm::comm
and accessed via
CopyableComm::get_comm.
- icomm – types::f77_integer array
This optional parameter
may be set using the method
CopyableComm::icomm
and accessed via
CopyableComm::get_icomm.
-
6:
– OptionalF01SB
Input/Output
-
Optional parameter container, derived from
Optional.
Container for:
- seed – types::f77_integer
This optional parameter
may be set using the method
OptionalF01SB::seed
and accessed via
OptionalF01SB::get_seed.
Default:
On initial entry:
- if , the supplied values of and are used for the initial iterate.
- If , the value of seed is used to seed a random number generator for the initial iterates and . See Section 9.3 for further details.
- errtol – double
This optional parameter
may be set using the method
OptionalF01SB::errtol
and accessed via
OptionalF01SB::get_errtol.
Default:
On entry: the convergence tolerance for when the Hierarchical Alternating Least Squares iteration has reached a stationary point. If , is used.
5.1Additional Quantities
- 1:
- , the number of rows of the matrix
- 2:
- , the number of columns of the matrix
- 3:
- , the number of columns of the matrix
6
Exceptions and Warnings
Errors or warnings detected by the function:
All errors and warnings have an associated numeric error code field,
errorid, stored either as a member of the thrown exception object (see
errorid), or as a member of
opt.
ifail, depending on how errors
and warnings are being handled (see
Error Handling for more details).
- Raises: ErrorException
-
- On intermediate re-entry, .
Constraint: .
- On initial entry, .
Constraint: .
- On entry, .
Constraint: .
- On entry, .
Constraint: .
- On entry, , and
.
Constraint: .
- An internal error occurred when generating initial values for w and h.
Please contact NAG.
- On entry, one of more of the elements of w or h were negative.
- On entry, argument must be a x array.
Supplied argument has dimensions.
- On entry, argument must be a x array.
Supplied argument was a x array.
- On entry, argument must be a x array.
Not all of the sizes for the supplied array could be ascertained.
- On entry, the raw data component of is null.
- On entry, unable to ascertain a value for .
- On entry, the data in is stored in Major Order.
The data was expected to be in Major Order.
- On entry, the communication class has not been initialized correctly.
- An unexpected error has been triggered by this routine.
- Your licence key may have expired or may not have been installed correctly.
- Dynamic memory allocation failed.
7
Accuracy
The Hierarchical Alternating Least Squares algorithm used by
real_nmf_rcomm is locally convergent; it is guaranteed to converge to a stationary point of
, but this may not be the global minimum. The iteration is deemed to have converged if the gradient of
is less than
errtol times the gradient at the initial values of
and
.
Due to the local convergence property, you may wish to run real_nmf_rcomm multiple times with different starting iterates. This can be done by explicitly providing the starting values of and each time, or by choosing a different random seed each time.
Note that even if real_nmf_rcomm exits with , the factorization given by and may still be a good enough approximation to be useful.
8
Parallelism and Performance
Please see the description for the underlying computational routine in this section of the
FL Interface documentation.
real_nmf_rcomm is designed to be used when is large and sparse. Whenever a matrix multiplication is required, the function will return control to the calling program so that the multiplication can be done in the most efficient way possible. Note that and will not, in general, be sparse even if is sparse.
If
is small and dense, then
f01saf (no CPP interface) can be used to compute
and
without the use of a reverse communication interface.
9.1
Uniqueness
Note that non-negative matrix factorization is not unique. For a factorization given by the matrices and , an equally good solution is given by and , where is any real non-negative matrix whose inverse is also non-negative. In real_nmf_rcomm, and are normalized so that the columns of have unit length.
9.2
Choice of
The most appropriate choice of the factorization rank, , is often problem dependent. Details of your particular application may help in guiding your choice of , for example, it may be known a priori that the data in naturally falls into a certain number of categories.
Alternatively, trial and error can be used. Compute non-negative matrix factorizations for several different values of (typically with ) and select the one that performs the best.
Finally, it is also possible to use a singular value decomposition of
to guide your choice of
, by looking for an abrupt decay in the size of the singular values of
. The singular value decomposition can be computed using
f12fbf (no CPP interface).
9.3
Generating Random Initial Iterates
If
on entry, then
real_nmf_rcomm uses the functions
g05kff (no CPP interface) and
g05saf (no CPP interface), with the
NAG basic generator, to populate
w and
h. For further information on this random number generator see
Section 2.1.1 in the
G05 Chapter Introduction.
Note that this generator gives a repeatable sequence of random numbers, so if the value of
seed is not changed between function calls, then the same initial iterates will be generated.
9.4
Use in Conjunction with NAG Library Functions
To compute the non-negative matrix factorization, the following
logic can normally be used:
- set
-
start looping
- call real_nmf_rcomm
-
if then
-
else if then
- print the and matrices if required and check the number of iterations
-
else if then
-
else if then
The code used to compute the matrix products will vary depending on the way
is stored. If all the elements of
are stored explicitly, then
dgemm) can be used. If
is triangular, then
f06yff (no CPP interface) should be used. If
is symmetric, then
f06ycf (no CPP interface) should be used. For sparse
stored in coordinate storage format
real_gen_matvec and
f11xef (no CPP interface) can be used. Alternatively, if
is stored in compressed column format
direct_real_gen_matmul can be used.
10
Example
This example finds a non-negative matrix factorization for the matrix
10.1
Example Program