.. _lmlra-chap: ############################################################################### Multilinear singular value decomposition and low multilinear rank approximation ############################################################################### .. _lmlra: .. figure:: img/lmlra.* :alt: lmlra :align: center :width: 60% A (truncated) multilinear singular value decomposition or a low multilinear rank approximation of a third-order tensor. In both the multilinear singular value decomposition (MLSVD) :cite:`r619` and the low multilinear rank approximation (LMLRA) :cite:`r618,r677,cichocki2015tensor` a tensor :math:`\ten{T}` of size :math:`I_1 \times I_2 \times \cdots \times I_N` is written as the multilinear tensor-matrix product of a core tensor :math:`\ten{S}` of size :math:`R_1 \times R_2 \times \cdots \times R_N` with :math:`N` factors :math:`\mat{U}^{(n)}` of size :math:`I_n \times R_n`: .. math:: \ten{T} \approx \ten{S} \tmprod_1 \mat{U}^{(1)} \tmprod_2 \mat{U}^{(2)} \tmprod_3 \cdots \tmprod_{N} \mat{U}^{(N)}, in which :math:`\tmprod_n` is the tensor-matrix product in mode :math:`n` (see :ref:`data-op-sec`). For all :math:`n`, :math:`R_n \leq I_n`. A visual representation of these decompositions in the third-order case is shown in :num:`Figure #lmlra`. The relations between the two decompositions are explained in the demo on "`Multilinear Singular Value Decomposition and Low Multilinear Rank Approximation`_". The differences between the MLSVD and the LMLRA lie in the computation and the optimality of the decomposition. As the decompositions are closely related and have the same format, both are discussed together here. ***************************** Problem and tensor generation ***************************** ------------------------------------------------------- Generating pseudorandom factor matrices and core tensor ------------------------------------------------------- Tensorlab stores the core tensor :math:`\ten{S} \in \C^{R_1\times R_2\times\cdots\times R_N}` and the factor matrices :math:`\mat{U}^{(n)} \in \C^{I_n\times R_n}`, :math:`n=1,\ldots,N` corresponding to an MLSVD or LMLRA of a tensor of dimensions :math:`I_1 \times I_2 \times \cdots \times I_N` as a Matlab array ``S`` and a cell array ``U = {U{1}, U{2}, ..., U{N}}``. The ``lmlra_rnd`` method can be used to generate a pseudorandom core tensor :math:`\ten{S}` and pseudorandom factor matrices :math:`\mat{U}^{(n)}` corresponding to an MLSVD or LMLRA. For example, to construct a tensor of dimensions ``size_tens = [I1, I2, ..., IN]`` and with core size ``size_core = [R1, R2, ..., RN]`` we can use: .. code-block:: tensorlab size_tens = [17 19 21]; size_core = [3 5 7]; [U,S] = lmlra_rnd(size_tens,size_core); By default, ``lmlra_rnd`` generates ``U{n}`` and ``S`` using ``randn``, after which the result is normalized such that the factors :math:`\mat{U}^{(n)}` have orthonormal columns and such that :math:`\ten{S}` is all-orthogonal and has ordered multilinear singular values. Other generators can also be specified by providing option as a structure or as key-value pairs, e.g.: .. code-block:: tensorlab % using a structure options = struct; options.Real = @rand; options.Imag = @rand; [U,S] = lmlra_rnd(size_tens,size_core,options); % using key-value pairs [U,S] = lmlra_rnd(size_tens, size_core, 'Real', @rand, 'Imag', @rand); The core tensor ``S`` is generated as ``rand(size_core) + rand(size_core)*1i``, and is then normalized. Before the normalization, the factor matrices ``U{n}`` are generated as .. code-block:: tensorlab rand(size_tens(n),size_core(n)) + rand(size_tens(n),size_core(n))*1i``. The normalization of the factors matrices and the core tensor can be disabled by setting the ``Unitary`` and ``Normalize`` options to ``false``. ------------------------------------- Generating the associated full tensor ------------------------------------- Given a cell array of factor matrices ``U = {U{1},U{2},...}`` and core tensor ``S``, its associated full tensor ``T`` can be computed with .. code-block:: tensorlab T = lmlragen(U,S); % or T = ful({U,S}); Note that additional braces ``{}`` are necessary for the ``ful`` command. The tensor ``T = lmlragen(U,S)`` can be computed using elementary tensor operations as .. code-block:: tensorlab T = tmprod(S,U,1:length(U)); Both the ``ful`` and the ``lmlragen`` method also allow the generation of subtensors: .. code-block:: tensorlab [U, S] = lmlra_rnd([10 11 12], [3 4 2]); T = lmlragen(U,S); t1 = lmlragen(U, S, [1 1 2 3 5 8]); % using linear indices frob(t1 - T([1 1 2 3 5 8])) % is zero up to machine precision t2 = ful({U,S}, ':', ':', 9); % using subscripts (':' is a string!) frob(t2 - T(:,:,9)) % is zero up to machine precision ****************** Computing an MLSVD ****************** To compute an MLSVD of a tensor :math:`\ten{T}`, three algorithms are available: ``mlsvd`` can be used for dense tensors, ``mlsvds`` can be used for sparse tensors and ``mlsvd_rsi`` can be used for large tensors, which can be both dense and sparse. Currently, there exist no MLSVD algorithms for incomplete or efficiently represented tensors in Tensorlab. In the latter case ``ful(T)`` can be used, in the former case LMLRA algorithms can be used. For a dense tensor :math:`\ten{T}`, the full MLSVD and the multilinear singular values can be computed using .. code-block:: tensorlab [U, S] = lmlra_rnd([10 11 12], [3 4 5]); T = lmlragen(U,S); Tn = noisy(T, 40); % add Gaussian noise with an SNR of 40dB [U, S, sv] = mlsvd(T); It is often useful to inspect the multilinear singular values ``sv`` as they provide useful information on the multilinear rank of the tensor. As an example, the singular values can be plotted using: .. code-block:: tensorlab for n = 1:3 subplot(1,3,n); semilogy(sv{n}); end For a sparse tensor :math:`\ten{T}`, the core size needs to be provided; hence, to compute the full MLSVD the following command can be used: .. code-block:: tensorlab % Create sparse tensor with 4% non-zeros T = randn(10, 11, 12); T(randperm(numel(T), ceil(0.96*numel(T)))) = 0; T = fmt(T); % Compute mlsvd [U, S, sv] = mlsvds(T, [10 11 12]); Often, the MLSVD is truncated, i.e., the size of the core is smaller than the size of the tensor. An MLSVD can be truncated manually: for the tensor above, we can use .. code-block:: tensorlab new_core = [5 4 7]; Ut = arrayfun(@(n) U(:,1:new_core(n)), U, new_core, 'UniformOutput', false); St = S(1:new_core(1),1:new_core(2),1:new_core(3)); Alternatively, the core size can be passed to the ``mlsvd`` and ``mlsvds`` algorithms as an additional parameter. For example, the following code can be used to generate a multilinear rank-:math:`(2, 3, 4)` approximation: .. code-block:: tensorlab Tfull = randn(10, 11, 12); [Ufull, Sfull] = mlsvd(Tfull, [2 3 4]); Tsparse = randn(10, 11, 12); Tsparse(randperm(numel(Tsparse), ceil(0.96*numel(Tsparse)))) = 0; Tsparse = fmt(Tsparse); [Usparse, Ssparse] = mlsvds(Tsparse, [2 3 4]); The sizes of the core tensor and the factor matrices are now .. code-block:: tensorlab Ufull size(Sfull) .. code-block:: none :caption: 546165d Ufull = [10x2 double] [11x3 double] [12x4 double] ans = 2 3 4 .. Note that the size of the resulting core ``S`` can sometimes be smaller than the requested core size ``size_core``. For example, consider .. code-block:: tensorlab T = randn(10, 11, 12); [U, S] = mlsvd(T, [2 2 5]); U size(S) :: U = [10x2 double] [11x2 double] [12x4 double] ans = 2 2 4 The reason is that a tensor cannot have multilinear rank :math:`(2,2,5)`. If a core of size :math:`2\times 2 \times 5` is needed, the ``FullSVD`` options can be set to ``true``. .. code-block:: tensorlab T = randn(10, 11, 12); [U, S] = mlsvd(T, [2 2 5], [], 'FullSVD', true); % perms = [] uses default The MLSVD relies on singular value decompositions (SVD) of matrix representations of the given tensor. These SVDs can be expensive for larger tensors. The ``mlsvd_rsi`` routine uses randomized SVDs to improve the computation speed for dense and sparse tensors: .. code-block:: tensorlab size_core = [5 6 7]; [U, S] = lmlra_rnd([30,40,50], size_core); T = lmlragen(U,S); tic, [U1, S1] = mlsvd(T, size_core); toc tic, [U2, S2] = mlsvd_rsi(T, size_core); toc .. code-block:: none :caption: wersza1221 Elapsed time is 0.073221 seconds. Elapsed time is 0.027600 seconds. Major improvements can be obtained for larger tensors. The accuracy of the results computed by ``mlsvd_rsi`` is most often similar to the accuracy of the results computed by ``mlsvd`` or ``mlsvds``: .. code-block:: tensorlab froblmlrares(T, U1, S1)/frob(T) froblmlrares(T, U2, S2)/frob(T) .. code-block:: none :caption: fdgergs434 ans = 2.4422e-15 ans = 1.4491e-15 ****************** Computing an LMLRA ****************** Several algorithms are available to compute the LMLRA of a dense, sparse, incomplete or structured tensor :math:`\ten{T}` with a (fixed) core size :math:`R_1 \times R_2 \times \cdots \times R_N`. The commands ``lmlra_nls`` and ``lmlra_minf`` are optimization-based algorithms using nonlinear unconstrained optimization and nonlinear least squares, respectively. ``lmlra_aca`` uses adaptive cross approximation. This method often provides a good initialization. Finally, the principal method is ``lmlra``, which automatically computes an initialization and refines this initial guess using one of the optimization-based methods. All optimization-based algorithms accept dense, sparse, incomplete and efficient representations of structured tensors (see :ref:`data-chap`). If efficient representations are used, and a solution accurate up to machine precision is needed, make sure to read the note at the end of this chapter. -------------------- The principal method -------------------- The ``lmlra`` method is the easiest way to compute an LMLRA of a tensor :math:`\ten{T}`. For example .. code-block:: tensorlab % Generate pseudorandom LMLRA (U,S) and associated full tensor T. size_tens = [17 19 21]; size_core = [3 5 7]; [U,S] = lmlra_rnd(size_tens,size_core); T = lmlragen(U,S); % Compute an LMLRA of a noisy version of T with 20dB SNR. [Uhat,Shat] = lmlra(noisy(T,20), size_core); generates a real rank-(3,5,7) tensor and computes its low multilinear rank approximation. Internally, ``lmlra`` chooses a method to *generate an initialization* ``U0`` and ``S0``, e.g., ``mlsvd_rsi`` or ``lmlra_aca``. Next it *executes an algorithm* to compute the LMLRA given the initialization, e.g., ``lmlra_nls``. The command ``lmlra(T, size_core)`` computes an initial solution with the desired multilinear rank. Alternatively an initial solution can be provided using .. code-block:: tensorlab [Uinit, Sinit] = lmlra_rnd([10 11 12], size_core); [Uhat, Shat] = lmlra(T, Uinit, Sinit); The default initialization methods ``mlsvd_rsi`` or ``lmlra_aca`` return nearly the same initialization for every run. Therefore, if multiple runs are performed for a specific tensor :math:`\ten{T}` and a specific multilinear rank :math:`(R_1,R_2,\ldots,R_N)` (for the same options), the initialization method should be changed, e.g., to ``lmlra_rnd``. The following example uses the ``mlsvd_rsi`` algorithm as initialization method for the first run, and uses a random initialization (``lmlra_rnd``) for the other runs: .. code-block:: tensorlab init = @mlsvd_rsi; resultU = cell(1, 5); resultS = cell(1, 5); for n = 1:5 [resultU{n}, resultS{n}]= lmlra(T, size_core, 'Initialization', init); init = @lmlra_rnd; end The next subsection explains different options for the ``lmlra`` routine. ------------------- Setting the options ------------------- Different options can be used to customize the two steps in ``lmlra`` (see ``help lmlra`` for more information). Options can be provided using an ``options`` structure, or using key-value pairs, e.g., .. code-block:: tensorlab options.Display = true; % Show progress on the command line. options.Initialization = @lmlra_rnd; % Select pseudorandom initialization. options.Algorithm = @lmlra_minf; % Select NLS as the main algorithm. options.AlgorithmOptions.TolFun = 1e-12; % Set stop criteria. options.AlgorithmOptions.TolX = 1e-12; [Uhat,Shat] = lmlra(T,size_core,options); The structures ``InitializationOptions`` and ``AlgorithmOptions`` are passed as options structures to the algorithms corresponding to the initialization and algorithm steps, respectively. See ``help lmlra`` for a full overview of the possible options. The following is worthwhile to be highlighted: - ``ExpandLimit``: allows a structured tensor ``T`` to be expanded for the compression and/or initialization steps if ``prod(getsize(T)) <= ExpandLimit``. Set ``ExpandLimit = 1`` to make sure tensors are never expanded. The default is :math:`10^6`. ---------------------------- Viewing the algorithm output ---------------------------- Each step may output additional information specific to that step. For instance, most LMLRA algorithms such as ``lmlra_hooi`` will keep track of the number of iterations and the difference in subspace angle of every two successive iterates ``sangle``. To obtain this information, capture the third output: .. code-block:: tensorlab [Uhat,Shat,output] = lmlra(T,size_core,options); and inspect its fields, for example by plotting the objective function value: .. code-block:: tensorlab semilogy(0:output.Algorithm.iterations,sqrt(2*output.Algorithm.fval)); xlabel('iteration'); ylabel('frob(lmlrares(T,U))'); grid on; Setting the ``Display`` option to ``true`` or to a number greater than zero, prints additional information to the command window. For example: .. code-block:: tensorlab size_core = [5 6 3]; [U,S] = lmlra_rnd([10 11 12], size_core); T = lmlragen(U,S); Tn = noisy(T, 20); [Uhat, Shat, output] = lmlra(Tn, size_core, 'Display', 1); .. code-block:: none :caption: xfg5g5 Step 1: Initialization is mlsvd_rsi...relative error = 0.0930664. Step 2: Algorithm is lmlra_nls... iterations = 5, relative error = 0.0930472. Two steps are performed by ``lmlra``. In step 1, the ``mlsvd_rsi`` algorithm is used to compute an initial guess, which is refined in step 2 using the ``lmlra_nls`` algorithm. In the case the tensor ``T`` is given as the efficient representation of a structured tensor, step 1 can also contain an expansion step: .. code-block:: tensorlab % Create a tensor with CPD structure T = cpd_rnd([10 11 12], 5); T{1} = bsxfun(@times, T{1}, [1000 100 10 1 0.01]); options = struct; options.Algorithm = @lmlra_minf; % use minf algorithm [Uhat, Shat, output] = lmlra(T, [4 4 4], 'Display', 1, options); .. code-block:: none :caption: 66f66 Step 1: Initialization is mlsvd_rsi: | Step 1a: Structured tensor expanded to full tensor (prod(getsize(T) <= 1e+06) | Step 1b: mlsvd_rsi on full tensor... relative error = 5.32044e-06. Step 2: Algorithm is lmlra_minf... iterations = 1, relative error = 5.32044e-06. In step 1a, the tensor is expanded such that the ``mlsvd_rsi`` algorithm can be used as initialization. Without expansion, ``lmlra_aca`` is the default initialization algorithm for structured tensors. Note that structure is not yet detected automatically in the ``lmlra`` command. ------------------- Computing the error ------------------- The residual between a tensor :math:`\ten{T}` and its computed LMLRA defined by the core tensor :math:`\ten{\hat{S}}` and factor matrices :math:`\mat{\hat{U}}^{(n)}` can be computed with .. code-block:: tensorlab res = lmlrares(T,Uhat,Shat); If the tensor is dense, then the result is equivalent to ``lmlragen(Uhat,Shat)-T``. The relative error between the tensor and its LMLRA can then be computed as .. code-block:: tensorlab relerr = frob(lmlrares(T,Uhat,Shat))/frob(T); If the original factor matrices :math:`\mat{U}^{(n)}` that generated :math:`\ten{T}` are known, the subspace angle between each of them and their corresponding approximations :math:`\mat{\hat{U}}^{(n)}` from ``lmlra`` can also be used as a measure of the approximation error. The function ``lmlraerr`` computes the subspace angle between the given two sets of factor matrices. In other words, .. code-block:: tensorlab sangle = lmlraerr(U,Uhat); returns a vector in which the :math:`n`\th entry is ``subspace(U{n},Uhat{n})``. ************************************ Choosing the size of the core tensor ************************************ For dense tensors, one can use the ``mlrankest`` tool to help determine the size of the core tensor ``size_core``. Running ``mlrankest(T)`` plots an L-curve which represents the balance between an upper bound :cite:`r619` on the relative error of an LMLRA of ``T`` for different core tensor sizes, and the LMLRA's compression ratio. The following example applies ``mlrankest`` on the amino acids dataset :cite:`d060`: .. code-block:: tensorlab url = 'http://www.models.life.ku.dk/go-engine?filename=amino.mat'; urlwrite(url,'amino.mat'); % Download amino.mat in this directory. load amino X; mlrankest(X); The resulting figure is shown in :num:`Figure #mlrankest`. The corner of this L-curve is often a good estimate of the optimal trade-off between accuracy and compression. The core tensor size ``size_core`` corresponding to the L-curve corner is marked on the plot with a square and is also ``mlrankest``\'s first output. By capturing it as in ``size_core = mlrankest(X)``, the L-curve is no longer plotted. All together, an LMLRA of the amino acids dataset can be computed in only a few lines: .. code-block:: tensorlab url = 'http://www.models.life.ku.dk/go-engine?filename=amino.mat'; urlwrite(url,'amino.mat'); % Download amino.mat in this directory. load amino X; size_core = mlrankest(X); % Optimal core tensor size at L-curve corner. [U,S] = lmlra(X,size_core); Additionally, the compression and relative error of other choices of ``size_core`` can be viewed by using the figure's Data Cursor tool. .. _mlrankest: .. figure:: img/mlrankest.png :alt: Rank estimation for LMLRA :width: 80% :align: center The ``mlrankest`` tool for choosing the core tensor size ``size_core`` in an LMLRA. ********************** Overview of algorithms ********************** The ``mlsvd`` and ``lmlra`` methods are the principal methods for the MLSVD and the LMLRA. Below we give an overview of the available algorithms for the computation of the MLSVD and the LMLRA, as well as an overview of other convenient routines. The following algorithms can be used to compute the low multilinear rank approximation or the multilinear singular value decomposition of a dense, sparse, incomplete or efficiently represented tensor, unless otherwise specified. - ``lmlra(T, size_core)`` computes the LMLRA of a tensor ``T`` using a multistep approach :cite:`delathauwer2000best`. - ``lmlra_nls(T, U, S)`` computes the LMRLA of a tensor ``T`` using an NLS type algorithm with as initial guess ``U`` for the factor matrices and ``S`` for the core tensor :cite:`delathauwer2000best`. - ``lmlra_minf(T, U, S)`` computes the LMLRA of a tensor ``T`` using nonlinear unconstrained optimization with as initial guess ``U`` for the factor matrices and ``S`` for the core tensor :cite:`delathauwer2000best`. - ``lmlra_aca(T,size_core)`` computes the LMLRA of a tensor ``T`` using adaptive cross approximation :cite:`caiafa2010generalizing`. - ``mlsvd(T)`` computes the full MLSVD of a dense tensor ``T``, including the multilinear singular values. ``mlsvd(T, size_core)`` truncates the MLSVD to the given core size and ``mlsvd(T, tol)`` truncates the MLSVD if the multilinear singular values are below a given tolerance ``tol`` :cite:`r619,vannieuwenhoven2012new`. - ``mlsvds(T,size_core)`` computes the truncated MLSVD of a sparse tensor ``T``. The same options as for ``mlsvd`` apply. - ``mlsvd_rsi(T, size_core)`` computes the truncated MLSVD of a full or sparse tensor ``T`` using a randomized SVD in combination with randomized subspace iteration. This algorithm is useful for larger tensors that have a low multilinear rank :cite:`halko2011finding`. - ``lmlra_hooi(T, U)`` uses higher-order orthogonal iteration to compute the LMLRA of a dense tensor ``T`` :cite:`kroonenberg2008`. - ``lmlra3_dgn(T, U)`` uses a differential-geometric Newton method to compute the LMLRA of a dense third-order tensor ``T`` :cite:`ishteva2009differential`. - ``lmlra3_rtr(T, U)`` uses a Riemannian trust region method to compute the LMLRA of a dense third-order tensor ``T`` :cite:`ishteva2011best`. The following routines can be used for problem generation, error analysis and rank estimation. - ``lmlra_rnd(size_tens, size_core)`` generates pseudorandom factors for an LMLRA. - ``lmlragen(U, S)`` generates the full tensor from given factors ``U`` and core tensor ``S``. - ``lmlrares(T, U, S)`` computes the residual ``lmlragen(U,S) - T``. - ``froblmlrares(T, U)`` computes the Frobenius norm of the residual ``lmlragen(U,S) - T``. This also works for structured tensors ``T``. - ``lmlraerr(U, Uhat)`` computes the angle between the subspaces of the original tensor and the approximation. The subspaces are given as the factor matrices ``U`` of the original tensor and the factor matrices ``Uhat`` of the approximation. - ``mlrank(T,tol)`` computes the multilinear rank of a tensor ``T`` using a given tolerance ``tol``. - ``mlrankest(T)`` estimates the multilinear rank of a tensor ``T`` from an L-curve. .. note:: When using structured tensors, the expected numerical precision is only half the machine precision. This is due to the mathematical formulation implemented to exploit the structure. In most cases, this is not a problem as the maximal attainable precision is already limited due to noise and model errors. Only in the very specific case when a solution accurate up to machine precision can be found and is required, this can be an issue. (This requires ``TolFun`` and ``TolX`` to be ``eps^2`` and ``eps``, respectively.) A solution is to use the structured tensor ``T`` to compute a solution ``Ures``, ``Sres`` accurate up to half the machine precision, and then refine this solution using ``lmlra_nls(ful(T), Ures, Sres, 'TolFun', eps^2, 'TolX', eps)`` for a few iterations. (It can be useful to set ``MaxIter`` to ``2`` or ``3``.) This technique requires only very few expensive iterations involving the full tensor. *************** Further reading *************** The algorithms outlined in this chapter can be used to compute an unconstrained MLSVD or LMLRA. In the structured data fusion (SDF) framework constraints can be imposed as well, and coupled decompositions can be defined. See :ref:`sdf-basic-chap` for more details. The demo on "`Multilinear Singular Value Decomposition and Low Multilinear Rank Approximation`_" gives some intuition behind the MLSVD and the LMLRA.