pyttb.cp_als
CP Decomposition via Alternating Least Squares.
- pyttb.cp_als.cp_als(input_tensor: tensor | sptensor | ttensor | sumtensor, rank: int, stoptol: float = 1e-4, maxiters: int = 1000, dimorder: int | float | Iterable[int] | Iterable[float] | ndarray | None = None, optdims: int | float | Iterable[int] | Iterable[float] | ndarray | None = None, init: Literal['random'] | Literal['nvecs'] | ktensor = 'random', printitn: int = 1, fixsigns: bool = True) Tuple[ktensor, ktensor, Dict] [source]
Compute CP decomposition with alternating least squares.
- Parameters:
input_tensor – Tensor to decompose
rank – Rank of the decomposition
stoptol – Tolerance used for termination - when the change in the fitness function in successive iterations drops below this value, the iterations terminate
dimorder – Order to loop through dimensions (default: [range(tensor.ndims)])
optdims – Whether factor for corresponding mode should be optimized or not. Defaults to optimizing all modes ([range(tensor.ndims)]).
maxiters – Maximum number of iterations
init –
- Initial guess (default: “random”)
- “random”: initialize using a
pyttb.ktensor
with values chosen from a Normal distribution with mean 0 and standard deviation 1
- “random”: initialize using a
- “nvecs”: initialize factor matrices of a
pyttb.ktensor
using the eigenvectors of the outer product of the matricized input tensor
- “nvecs”: initialize factor matrices of a
pyttb.ktensor
: initialize using a specificpyttb.ktensor
as input - must be the same shape as the input tensor and have the same rank as the input rank
printitn – Number of iterations to perform before printing iteration status - 0 for no status printing
fixsigns – Align the signs of the columns of the factorization to align with the input tensor data
- Returns:
M – Resulting ktensor from CP-ALS factorization
Minit – Initial guess
output –
- Information about the computation. Dictionary keys:
params : tuple of (stoptol, maxiters, printitn, dimorder)
iters: number of iterations performed
- normresidual: norm of the difference between the input tensor
and ktensor factorization
- fit: value of the fitness function (fraction of tensor data
explained by the model)
Example
Random initialization causes slight perturbation in intermediate results. … is our place holder for these numeric values. Example using default values (“random” initialization):
>>> weights = np.array([1., 2.]) >>> fm0 = np.array([[1., 2.], [3., 4.]]) >>> fm1 = np.array([[5., 6.], [7., 8.]]) >>> K = ttb.ktensor([fm0, fm1], weights) >>> np.random.seed(1) >>> M, Minit, output = ttb.cp_als(K.full(), 2) CP_ALS: Iter 0: f = ... f-delta = ... Iter 1: f = ... f-delta = ... Final f = ... >>> print(M) ktensor of shape (2, 2) with order F weights=[108.4715... 8.6114...] factor_matrices[0] = [[0.4187... 0.3989...] [0.9080... 0.9169...]] factor_matrices[1] = [[0.6188... 0.2581...] [0.7854... 0.9661...]] >>> print(Minit) ktensor of shape (2, 2) with order F weights=[1. 1.] factor_matrices[0] = [[4.1702...e-01 7.2032...e-01] [1.1437...e-04 3.0233...e-01]] factor_matrices[1] = [[0.1467... 0.0923...] [0.1862... 0.3455...]] >>> print(output["params"]) {'stoptol': 0.0001, 'maxiters': 1000, 'dimorder': array([0, 1]), 'optdims': array([0, 1]), 'printitn': 1, 'fixsigns': True}
Example using “nvecs” initialization:
>>> M, Minit, output = ttb.cp_als(K.full(), 2, init="nvecs") CP_ALS: Iter 0: f = ... f-delta = ... Iter 1: f = ... f-delta = ... Final f = ...
Example using
pyttb.ktensor
initialization:>>> M, Minit, output = ttb.cp_als(K.full(), 2, init=K) CP_ALS: Iter 0: f = ... f-delta = ... Iter 1: f = ... f-delta = ... Final f = ...