Alternating least squares for CANDECOMP/PARAFAC (CP) Decomposition

The function cp_als computes an estimate of the best rank-R CP model of a tensor X using the well-known alternating least-squares algorithm (see, e.g., Kolda and Bader, SIAM Review, 2009, for more information). The input X can be almost any type of tensor inclusing a tensor, sptensor, ktensor, or ttensor. The output CP model is a ktensor.

Contents

Load some data

We use the well-known amino acids data set from Andersson and Bro. It contains fluorescence measurements of 5 samples containing 3 amino acids: Tryptophan, Tyrosine, and Phenylalanine.Each amino acid corresponds to a rank-one component. The tensor is of size 5 x 51 x 201 from 5 samples, 51 excitations, and 201 emissions. Further details can be found here: http://www.models.life.ku.dk/Amino_Acid_fluo. Please cite the following paper for this data: Rasmus Bro, PARAFAC: Tutorial and applications, Chemometrics and Intelligent Laboratory Systems, 1997, 38, 149-171. This dataset can be found in the doc directory.

load aminoacids

Basic call to the method, specifying the data tensor and its rank

This uses a random initial guess. At each iteration, it reports the 'fit' which is defined as 1-(norm(X-M)/norm(X)) and is loosely the proportion of the data described by the CP model, i.e., a fit of 1 is perfect.

rng(3) %<- Setting random seed for reproducibility of this script
M1 = cp_als(X,3); %<- Call the method
CP_ALS:
 Iter  1: f = 4.431681e-01 f-delta = 4.4e-01
 Iter  2: f = 6.811163e-01 f-delta = 2.4e-01
 Iter  3: f = 8.302664e-01 f-delta = 1.5e-01
 Iter  4: f = 9.053451e-01 f-delta = 7.5e-02
 Iter  5: f = 9.335960e-01 f-delta = 2.8e-02
 Iter  6: f = 9.410321e-01 f-delta = 7.4e-03
 Iter  7: f = 9.446529e-01 f-delta = 3.6e-03
 Iter  8: f = 9.474319e-01 f-delta = 2.8e-03
 Iter  9: f = 9.499205e-01 f-delta = 2.5e-03
 Iter 10: f = 9.522513e-01 f-delta = 2.3e-03
 Iter 11: f = 9.544558e-01 f-delta = 2.2e-03
 Iter 12: f = 9.565373e-01 f-delta = 2.1e-03
 Iter 13: f = 9.584909e-01 f-delta = 2.0e-03
 Iter 14: f = 9.603111e-01 f-delta = 1.8e-03
 Iter 15: f = 9.619935e-01 f-delta = 1.7e-03
 Iter 16: f = 9.635362e-01 f-delta = 1.5e-03
 Iter 17: f = 9.649398e-01 f-delta = 1.4e-03
 Iter 18: f = 9.662071e-01 f-delta = 1.3e-03
 Iter 19: f = 9.673433e-01 f-delta = 1.1e-03
 Iter 20: f = 9.683552e-01 f-delta = 1.0e-03
 Iter 21: f = 9.692506e-01 f-delta = 9.0e-04
 Iter 22: f = 9.700386e-01 f-delta = 7.9e-04
 Iter 23: f = 9.707283e-01 f-delta = 6.9e-04
 Iter 24: f = 9.713293e-01 f-delta = 6.0e-04
 Iter 25: f = 9.718507e-01 f-delta = 5.2e-04
 Iter 26: f = 9.723014e-01 f-delta = 4.5e-04
 Iter 27: f = 9.726898e-01 f-delta = 3.9e-04
 Iter 28: f = 9.730234e-01 f-delta = 3.3e-04
 Iter 29: f = 9.733095e-01 f-delta = 2.9e-04
 Iter 30: f = 9.735541e-01 f-delta = 2.4e-04
 Iter 31: f = 9.737631e-01 f-delta = 2.1e-04
 Iter 32: f = 9.739412e-01 f-delta = 1.8e-04
 Iter 33: f = 9.740930e-01 f-delta = 1.5e-04
 Iter 34: f = 9.742221e-01 f-delta = 1.3e-04
 Iter 35: f = 9.743319e-01 f-delta = 1.1e-04
 Iter 36: f = 9.744252e-01 f-delta = 9.3e-05
 Final f = 9.744252e-01 

We typically can achieve a final fit of f = 0.97. The method stops when the change in the fit becomes less than the specified tolerance, which defaults to 1-e4.

Visualize the results

Use the ktensor/viz function to visualize the results.

vizopts = {'PlotCommands',{'bar','line','line'},...
    'ModeTitles',{'Concentration','Emission','Excitation'},...
    'BottomSpace',0.10,'HorzSpace',0.04,'Normalize',0};
info1 = viz(M1,'Figure',1,vizopts{:});

Run again with a different initial guess, output the initial guess.

This time we have two outputs. The first output is the solution as a ktensor. The second output is a cell array containing the initial guess. Since the first mode is not needed, it is omitted from the cell array.

[M2bad,U2] = cp_als(X,3);
CP_ALS:
 Iter  1: f = 5.512552e-01 f-delta = 5.5e-01
 Iter  2: f = 6.377027e-01 f-delta = 8.6e-02
 Iter  3: f = 6.438691e-01 f-delta = 6.2e-03
 Iter  4: f = 6.728753e-01 f-delta = 2.9e-02
 Iter  5: f = 7.088155e-01 f-delta = 3.6e-02
 Iter  6: f = 7.144994e-01 f-delta = 5.7e-03
 Iter  7: f = 7.188603e-01 f-delta = 4.4e-03
 Iter  8: f = 7.245002e-01 f-delta = 5.6e-03
 Iter  9: f = 7.313569e-01 f-delta = 6.9e-03
 Iter 10: f = 7.398597e-01 f-delta = 8.5e-03
 Iter 11: f = 7.514761e-01 f-delta = 1.2e-02
 Iter 12: f = 7.690794e-01 f-delta = 1.8e-02
 Iter 13: f = 7.939227e-01 f-delta = 2.5e-02
 Iter 14: f = 8.166727e-01 f-delta = 2.3e-02
 Iter 15: f = 8.299350e-01 f-delta = 1.3e-02
 Iter 16: f = 8.374720e-01 f-delta = 7.5e-03
 Iter 17: f = 8.422694e-01 f-delta = 4.8e-03
 Iter 18: f = 8.458255e-01 f-delta = 3.6e-03
 Iter 19: f = 8.488925e-01 f-delta = 3.1e-03
 Iter 20: f = 8.518580e-01 f-delta = 3.0e-03
 Iter 21: f = 8.549459e-01 f-delta = 3.1e-03
 Iter 22: f = 8.583117e-01 f-delta = 3.4e-03
 Iter 23: f = 8.620862e-01 f-delta = 3.8e-03
 Iter 24: f = 8.663939e-01 f-delta = 4.3e-03
 Iter 25: f = 8.713540e-01 f-delta = 5.0e-03
 Iter 26: f = 8.770652e-01 f-delta = 5.7e-03
 Iter 27: f = 8.835659e-01 f-delta = 6.5e-03
 Iter 28: f = 8.907687e-01 f-delta = 7.2e-03
 Iter 29: f = 8.983839e-01 f-delta = 7.6e-03
 Iter 30: f = 9.058962e-01 f-delta = 7.5e-03
 Iter 31: f = 9.126883e-01 f-delta = 6.8e-03
 Iter 32: f = 9.183110e-01 f-delta = 5.6e-03
 Iter 33: f = 9.226911e-01 f-delta = 4.4e-03
 Iter 34: f = 9.260765e-01 f-delta = 3.4e-03
 Iter 35: f = 9.288145e-01 f-delta = 2.7e-03
 Iter 36: f = 9.311877e-01 f-delta = 2.4e-03
 Iter 37: f = 9.333745e-01 f-delta = 2.2e-03
 Iter 38: f = 9.354731e-01 f-delta = 2.1e-03
 Iter 39: f = 9.375329e-01 f-delta = 2.1e-03
 Iter 40: f = 9.395775e-01 f-delta = 2.0e-03
 Iter 41: f = 9.416167e-01 f-delta = 2.0e-03
 Iter 42: f = 9.436525e-01 f-delta = 2.0e-03
 Iter 43: f = 9.456825e-01 f-delta = 2.0e-03
 Iter 44: f = 9.477010e-01 f-delta = 2.0e-03
 Iter 45: f = 9.496999e-01 f-delta = 2.0e-03
 Iter 46: f = 9.516689e-01 f-delta = 2.0e-03
 Iter 47: f = 9.535964e-01 f-delta = 1.9e-03
 Iter 48: f = 9.554701e-01 f-delta = 1.9e-03
 Iter 49: f = 9.572772e-01 f-delta = 1.8e-03
 Iter 50: f = 9.590055e-01 f-delta = 1.7e-03
 Final f = 9.590055e-01 

Increase the maximium number of iterations

Note that the previous run kicked out at only 50 iterations before reaching the specified convegence tolerance. Let's increate the maximum number of iterations and try again, using the same initial guess.

M2 = cp_als(X,3,'maxiters',100,'init',U2);
CP_ALS:
 Iter  1: f = 5.512552e-01 f-delta = 5.5e-01
 Iter  2: f = 6.377027e-01 f-delta = 8.6e-02
 Iter  3: f = 6.438691e-01 f-delta = 6.2e-03
 Iter  4: f = 6.728753e-01 f-delta = 2.9e-02
 Iter  5: f = 7.088155e-01 f-delta = 3.6e-02
 Iter  6: f = 7.144994e-01 f-delta = 5.7e-03
 Iter  7: f = 7.188603e-01 f-delta = 4.4e-03
 Iter  8: f = 7.245002e-01 f-delta = 5.6e-03
 Iter  9: f = 7.313569e-01 f-delta = 6.9e-03
 Iter 10: f = 7.398597e-01 f-delta = 8.5e-03
 Iter 11: f = 7.514761e-01 f-delta = 1.2e-02
 Iter 12: f = 7.690794e-01 f-delta = 1.8e-02
 Iter 13: f = 7.939227e-01 f-delta = 2.5e-02
 Iter 14: f = 8.166727e-01 f-delta = 2.3e-02
 Iter 15: f = 8.299350e-01 f-delta = 1.3e-02
 Iter 16: f = 8.374720e-01 f-delta = 7.5e-03
 Iter 17: f = 8.422694e-01 f-delta = 4.8e-03
 Iter 18: f = 8.458255e-01 f-delta = 3.6e-03
 Iter 19: f = 8.488925e-01 f-delta = 3.1e-03
 Iter 20: f = 8.518580e-01 f-delta = 3.0e-03
 Iter 21: f = 8.549459e-01 f-delta = 3.1e-03
 Iter 22: f = 8.583117e-01 f-delta = 3.4e-03
 Iter 23: f = 8.620862e-01 f-delta = 3.8e-03
 Iter 24: f = 8.663939e-01 f-delta = 4.3e-03
 Iter 25: f = 8.713540e-01 f-delta = 5.0e-03
 Iter 26: f = 8.770652e-01 f-delta = 5.7e-03
 Iter 27: f = 8.835659e-01 f-delta = 6.5e-03
 Iter 28: f = 8.907687e-01 f-delta = 7.2e-03
 Iter 29: f = 8.983839e-01 f-delta = 7.6e-03
 Iter 30: f = 9.058962e-01 f-delta = 7.5e-03
 Iter 31: f = 9.126883e-01 f-delta = 6.8e-03
 Iter 32: f = 9.183110e-01 f-delta = 5.6e-03
 Iter 33: f = 9.226911e-01 f-delta = 4.4e-03
 Iter 34: f = 9.260765e-01 f-delta = 3.4e-03
 Iter 35: f = 9.288145e-01 f-delta = 2.7e-03
 Iter 36: f = 9.311877e-01 f-delta = 2.4e-03
 Iter 37: f = 9.333745e-01 f-delta = 2.2e-03
 Iter 38: f = 9.354731e-01 f-delta = 2.1e-03
 Iter 39: f = 9.375329e-01 f-delta = 2.1e-03
 Iter 40: f = 9.395775e-01 f-delta = 2.0e-03
 Iter 41: f = 9.416167e-01 f-delta = 2.0e-03
 Iter 42: f = 9.436525e-01 f-delta = 2.0e-03
 Iter 43: f = 9.456825e-01 f-delta = 2.0e-03
 Iter 44: f = 9.477010e-01 f-delta = 2.0e-03
 Iter 45: f = 9.496999e-01 f-delta = 2.0e-03
 Iter 46: f = 9.516689e-01 f-delta = 2.0e-03
 Iter 47: f = 9.535964e-01 f-delta = 1.9e-03
 Iter 48: f = 9.554701e-01 f-delta = 1.9e-03
 Iter 49: f = 9.572772e-01 f-delta = 1.8e-03
 Iter 50: f = 9.590055e-01 f-delta = 1.7e-03
 Iter 51: f = 9.606440e-01 f-delta = 1.6e-03
 Iter 52: f = 9.621834e-01 f-delta = 1.5e-03
 Iter 53: f = 9.636167e-01 f-delta = 1.4e-03
 Iter 54: f = 9.649394e-01 f-delta = 1.3e-03
 Iter 55: f = 9.661493e-01 f-delta = 1.2e-03
 Iter 56: f = 9.672469e-01 f-delta = 1.1e-03
 Iter 57: f = 9.682348e-01 f-delta = 9.9e-04
 Iter 58: f = 9.691174e-01 f-delta = 8.8e-04
 Iter 59: f = 9.699007e-01 f-delta = 7.8e-04
 Iter 60: f = 9.705915e-01 f-delta = 6.9e-04
 Iter 61: f = 9.711975e-01 f-delta = 6.1e-04
 Iter 62: f = 9.717263e-01 f-delta = 5.3e-04
 Iter 63: f = 9.721858e-01 f-delta = 4.6e-04
 Iter 64: f = 9.725836e-01 f-delta = 4.0e-04
 Iter 65: f = 9.729269e-01 f-delta = 3.4e-04
 Iter 66: f = 9.732222e-01 f-delta = 3.0e-04
 Iter 67: f = 9.734758e-01 f-delta = 2.5e-04
 Iter 68: f = 9.736930e-01 f-delta = 2.2e-04
 Iter 69: f = 9.738788e-01 f-delta = 1.9e-04
 Iter 70: f = 9.740375e-01 f-delta = 1.6e-04
 Iter 71: f = 9.741730e-01 f-delta = 1.4e-04
 Iter 72: f = 9.742885e-01 f-delta = 1.2e-04
 Iter 73: f = 9.743869e-01 f-delta = 9.8e-05
 Final f = 9.743869e-01 

This solution looks more or less the same as the previous one.

info2 = viz(M2,'Figure',2,vizopts{:});

Compare the two solutions

Use the ktensor/score function to compare the two solutions. A score of 1 indicates a perfect match. These are not exactly the same, but they are pretty close.

score(M1,M2)
ans =

    0.9981

Rerun with same initial guess

Using the same initial guess (and all other parameters) gives the exact same solution.

M2alt = cp_als(X,3,'maxiters',100,'init',U2);
score(M2, M2alt) %<- Score of 1 indicates the same solution
CP_ALS:
 Iter  1: f = 5.512552e-01 f-delta = 5.5e-01
 Iter  2: f = 6.377027e-01 f-delta = 8.6e-02
 Iter  3: f = 6.438691e-01 f-delta = 6.2e-03
 Iter  4: f = 6.728753e-01 f-delta = 2.9e-02
 Iter  5: f = 7.088155e-01 f-delta = 3.6e-02
 Iter  6: f = 7.144994e-01 f-delta = 5.7e-03
 Iter  7: f = 7.188603e-01 f-delta = 4.4e-03
 Iter  8: f = 7.245002e-01 f-delta = 5.6e-03
 Iter  9: f = 7.313569e-01 f-delta = 6.9e-03
 Iter 10: f = 7.398597e-01 f-delta = 8.5e-03
 Iter 11: f = 7.514761e-01 f-delta = 1.2e-02
 Iter 12: f = 7.690794e-01 f-delta = 1.8e-02
 Iter 13: f = 7.939227e-01 f-delta = 2.5e-02
 Iter 14: f = 8.166727e-01 f-delta = 2.3e-02
 Iter 15: f = 8.299350e-01 f-delta = 1.3e-02
 Iter 16: f = 8.374720e-01 f-delta = 7.5e-03
 Iter 17: f = 8.422694e-01 f-delta = 4.8e-03
 Iter 18: f = 8.458255e-01 f-delta = 3.6e-03
 Iter 19: f = 8.488925e-01 f-delta = 3.1e-03
 Iter 20: f = 8.518580e-01 f-delta = 3.0e-03
 Iter 21: f = 8.549459e-01 f-delta = 3.1e-03
 Iter 22: f = 8.583117e-01 f-delta = 3.4e-03
 Iter 23: f = 8.620862e-01 f-delta = 3.8e-03
 Iter 24: f = 8.663939e-01 f-delta = 4.3e-03
 Iter 25: f = 8.713540e-01 f-delta = 5.0e-03
 Iter 26: f = 8.770652e-01 f-delta = 5.7e-03
 Iter 27: f = 8.835659e-01 f-delta = 6.5e-03
 Iter 28: f = 8.907687e-01 f-delta = 7.2e-03
 Iter 29: f = 8.983839e-01 f-delta = 7.6e-03
 Iter 30: f = 9.058962e-01 f-delta = 7.5e-03
 Iter 31: f = 9.126883e-01 f-delta = 6.8e-03
 Iter 32: f = 9.183110e-01 f-delta = 5.6e-03
 Iter 33: f = 9.226911e-01 f-delta = 4.4e-03
 Iter 34: f = 9.260765e-01 f-delta = 3.4e-03
 Iter 35: f = 9.288145e-01 f-delta = 2.7e-03
 Iter 36: f = 9.311877e-01 f-delta = 2.4e-03
 Iter 37: f = 9.333745e-01 f-delta = 2.2e-03
 Iter 38: f = 9.354731e-01 f-delta = 2.1e-03
 Iter 39: f = 9.375329e-01 f-delta = 2.1e-03
 Iter 40: f = 9.395775e-01 f-delta = 2.0e-03
 Iter 41: f = 9.416167e-01 f-delta = 2.0e-03
 Iter 42: f = 9.436525e-01 f-delta = 2.0e-03
 Iter 43: f = 9.456825e-01 f-delta = 2.0e-03
 Iter 44: f = 9.477010e-01 f-delta = 2.0e-03
 Iter 45: f = 9.496999e-01 f-delta = 2.0e-03
 Iter 46: f = 9.516689e-01 f-delta = 2.0e-03
 Iter 47: f = 9.535964e-01 f-delta = 1.9e-03
 Iter 48: f = 9.554701e-01 f-delta = 1.9e-03
 Iter 49: f = 9.572772e-01 f-delta = 1.8e-03
 Iter 50: f = 9.590055e-01 f-delta = 1.7e-03
 Iter 51: f = 9.606440e-01 f-delta = 1.6e-03
 Iter 52: f = 9.621834e-01 f-delta = 1.5e-03
 Iter 53: f = 9.636167e-01 f-delta = 1.4e-03
 Iter 54: f = 9.649394e-01 f-delta = 1.3e-03
 Iter 55: f = 9.661493e-01 f-delta = 1.2e-03
 Iter 56: f = 9.672469e-01 f-delta = 1.1e-03
 Iter 57: f = 9.682348e-01 f-delta = 9.9e-04
 Iter 58: f = 9.691174e-01 f-delta = 8.8e-04
 Iter 59: f = 9.699007e-01 f-delta = 7.8e-04
 Iter 60: f = 9.705915e-01 f-delta = 6.9e-04
 Iter 61: f = 9.711975e-01 f-delta = 6.1e-04
 Iter 62: f = 9.717263e-01 f-delta = 5.3e-04
 Iter 63: f = 9.721858e-01 f-delta = 4.6e-04
 Iter 64: f = 9.725836e-01 f-delta = 4.0e-04
 Iter 65: f = 9.729269e-01 f-delta = 3.4e-04
 Iter 66: f = 9.732222e-01 f-delta = 3.0e-04
 Iter 67: f = 9.734758e-01 f-delta = 2.5e-04
 Iter 68: f = 9.736930e-01 f-delta = 2.2e-04
 Iter 69: f = 9.738788e-01 f-delta = 1.9e-04
 Iter 70: f = 9.740375e-01 f-delta = 1.6e-04
 Iter 71: f = 9.741730e-01 f-delta = 1.4e-04
 Iter 72: f = 9.742885e-01 f-delta = 1.2e-04
 Iter 73: f = 9.743869e-01 f-delta = 9.8e-05
 Final f = 9.743869e-01 

ans =

    1.0000

Changing the output frequency

Using the 'printitn' option to change the output frequency.

M2alt2 = cp_als(X,3,'maxiters',100,'init',U2,'printitn',10);
CP_ALS:
 Iter 10: f = 7.398597e-01 f-delta = 8.5e-03
 Iter 20: f = 8.518580e-01 f-delta = 3.0e-03
 Iter 30: f = 9.058962e-01 f-delta = 7.5e-03
 Iter 40: f = 9.395775e-01 f-delta = 2.0e-03
 Iter 50: f = 9.590055e-01 f-delta = 1.7e-03
 Iter 60: f = 9.705915e-01 f-delta = 6.9e-04
 Iter 70: f = 9.740375e-01 f-delta = 1.6e-04
 Iter 73: f = 9.743869e-01 f-delta = 9.8e-05
 Final f = 9.743869e-01 

Suppress all output

Set 'printitn' to zero to suppress all output.

M2alt3 = cp_als(X,3,'maxiters',100,'init',U2,'printitn',0); % <-No output

Use HOSVD initial guess

Use the 'nvecs' option to use the leading mode-n singular vectors as the initial guess.

M3 = cp_als(X,3,'init','nvecs','printitn',10);
CP_ALS:
 Iter 10: f = 9.334888e-01 f-delta = 3.5e-03
 Iter 20: f = 9.604549e-01 f-delta = 1.9e-03
 Iter 30: f = 9.712518e-01 f-delta = 5.8e-04
 Iter 40: f = 9.741285e-01 f-delta = 1.4e-04
 Iter 43: f = 9.744312e-01 f-delta = 8.6e-05
 Final f = 9.744312e-01 

Compare to the first solution using score, and see they are nearly the same because the score is close to 1.

score(M1,M3)
ans =

    0.9847

Change the order of the dimensions in CP

[M4,~,info] = cp_als(X,3,'dimorder',[2 3 1],'init','nvecs','printitn',10);
score(M1,M4)
CP_ALS:
 Iter 10: f = 9.449957e-01 f-delta = 3.1e-03
 Iter 20: f = 9.657394e-01 f-delta = 1.3e-03
 Iter 30: f = 9.727566e-01 f-delta = 3.5e-04
 Iter 39: f = 9.743928e-01 f-delta = 9.2e-05
 Final f = 9.743928e-01 

ans =

    0.9844

In the last example, we also collected the third output argument which has some extra information in it. The field info.iters has the total number of iterations. The field info.params has the information used to run the method. Unless the initialization method is 'random', passing the parameters back to the method will yield the exact same results.

M4alt = cp_als(X,3,info.params);
score(M4,M4alt)
CP_ALS:
 Iter 10: f = 9.449957e-01 f-delta = 3.1e-03
 Iter 20: f = 9.657394e-01 f-delta = 1.3e-03
 Iter 30: f = 9.727566e-01 f-delta = 3.5e-04
 Iter 39: f = 9.743928e-01 f-delta = 9.2e-05
 Final f = 9.743928e-01 

ans =

     1

Change the tolerance

It's also possible to loosen or tighten the tolerance on the change in the fit. You may need to increase the number of iterations for it to converge.

M5 = cp_als(X,3,'init','nvecs','tol',1e-6,'maxiters',1000,'printitn',10);
CP_ALS:
 Iter 10: f = 9.334888e-01 f-delta = 3.5e-03
 Iter 20: f = 9.604549e-01 f-delta = 1.9e-03
 Iter 30: f = 9.712518e-01 f-delta = 5.8e-04
 Iter 40: f = 9.741285e-01 f-delta = 1.4e-04
 Iter 50: f = 9.747733e-01 f-delta = 2.9e-05
 Iter 60: f = 9.749128e-01 f-delta = 6.4e-06
 Iter 70: f = 9.749430e-01 f-delta = 1.4e-06
 Iter 73: f = 9.749461e-01 f-delta = 8.8e-07
 Final f = 9.749461e-01 

Recommendations