Parafac2 tensor#

class ivy.data_classes.factorized_tensor.parafac2_tensor.Parafac2Tensor(parafac2_tensor)[source]#

Bases: FactorizedTensor

__init__(parafac2_tensor)[source]#
_abc_impl = <_abc._abc_data object>#
static apply_parafac2_projections(parafac2_tensor)[source]#

Apply the projection matrices to the evolving factor.

Parameters:

parafac2_tensor (Parafac2Tensor) –

Returns:

(weights, factors) – A tensor decomposition on the form A [B_i] C such that the \(X_{ijk}\) is given by \(sum_r A_{ir} [B_i]_{jr} C_{kr}\).

This is also equivalent to a coupled matrix factorisation, where each matrix, \(X_i = C diag([a_{i1}, ..., a_{ir}] B_i)\).

The first element of factors is the A matrix, the second element is a list of B-matrices and the third element is the C matrix.

classmethod from_CPTensor(cp_tensor, parafac2_tensor_ok=False)[source]#

Create a Parafac2Tensor from a CPTensor.

Parameters:
  • cp_tensor – CPTensor or Parafac2Tensor If it is a Parafac2Tensor, then the argument parafac2_tensor_ok must be True’

  • parafac2_tensor – Whether or not Parafac2Tensors can be used as input.

Returns:

Parafac2Tensor with factor matrices and weights extracted from a CPTensor

property n_param#
static parafac2_normalise(parafac2_tensor)[source]#

Return parafac2_tensor with factors normalised to unit length.

Turns factors = [|U_1, ... U_n|] into [weights; |V_1, ... V_n|], where the columns of each V_k are normalized to unit Euclidean length from the columns of U_k with the normalizing constants absorbed into weights. In the special case of a symmetric tensor, weights holds the eigenvalues of the tensor.

Parameters:

parafac2_tensor

Parafac2Tensor = (weight, factors, projections) factors is list of matrices, all with the same number of columns i.e.:

for u in U:
    u[i].shape == (s_i, R)

where R is fixed while s_i can vary with i

Returns:

Parafac2Tensor – normalisation_weights, normalised_factors, normalised_projections

static parafac2_to_slice(parafac2_tensor, slice_idx, validate=True)[source]#

Generate a single slice along the first mode from the PARAFAC2 tensor.

The decomposition is on the form \((A [B_i] C)\) such that the i-th frontal slice, \(X_i\), of \(X\) is given by

\[X_i = B_i diag(a_i) C^T,\]

where \(diag(a_i)\) is the diagonal matrix whose nonzero entries are equal to the \(i\)-th row of the \(I times R\) factor matrix \(A\), \(B_i`is a :math:`J_i times R\) factor matrix such that the cross product matrix \(B_{i_1}^T B_{i_1}\) is constant for all \(i\), and \(C\) is a \(K times R\) factor matrix. To compute this decomposition, we reformulate the expression for \(B_i\) such that

\[B_i = P_i B,\]

where \(P_i\) is a \(J_i times R\) orthogonal matrix and \(B\) is a \(R times R\) matrix.

An alternative formulation of the PARAFAC2 decomposition is that the tensor element \(X_{ijk}\) is given by

\[X_{ijk} = sum_{r=1}^R A_{ir} B_{ijr} C_{kr},\]

with the same constraints hold for \(B_i\) as above.

Parameters:

parafac2_tensor

weights

1D array of shape (rank, ) weights of the factors

factors

List of factors of the PARAFAC2 decomposition Contains the matrices \(A\), \(B\) and \(C\) described above

projection_matrices

List of projection matrices used to create evolving factors.

Returns:

Full tensor of shape [P[slice_idx].shape[1], C.shape[1]], where P is the projection matrices and C is the last factor matrix of the Parafac2Tensor.

static parafac2_to_slices(parafac2_tensor, validate=True)[source]#

Generate all slices along the first mode from a PARAFAC2 tensor.

Generates a list of all slices from a PARAFAC2 tensor. A list is returned since the tensor might have varying size along the second mode. To return a tensor, see the parafac2_to_tensor function instead.shape

The decomposition is on the form \((A [B_i] C)\) such that the i-th frontal slice, \(X_i\), of \(X\) is given by

\[X_i = B_i diag(a_i) C^T,\]

where \(diag(a_i)\) is the diagonal matrix whose nonzero entries are equal to the \(i\)-th row of the \(I times R\) factor matrix \(A\), \(B_i\) is a \(J_i times R\) factor matrix such that the cross product matrix \(B_{i_1}^T B_{i_1}\) is constant for all \(i\), and \(C\) is a \(K times R\) factor matrix.To compute this decomposition, we reformulate the expression for \(B_i\) such that

\[B_i = P_i B,\]

where \(P_i\) is a \(J_i times R\) orthogonal matrix and \(B\) is a \(R times R\) matrix.

An alternative formulation of the PARAFAC2 decomposition is that the tensor element \(X_{ijk}\) is given by

\[X_{ijk} = sum_{r=1}^R A_{ir} B_{ijr} C_{kr},\]

with the same constraints hold for \(B_i\) as above.

Parameters:

parafac2_tensor (Parafac2Tensor - (weight, factors, projection_matrices)) –

  • weights1D array of shape (rank, )

    weights of the factors

  • factorsList of factors of the PARAFAC2 decomposition

    Contains the matrices \(A\), \(B\) and \(C\) described above

  • projection_matricesList of projection matrices used to create evolving

    factors.

Returns:

A list of full tensors of shapes [P[i].shape[1], C.shape[1]], where P is the projection matrices and C is the last factor matrix of the Parafac2Tensor.

static parafac2_to_tensor(parafac2_tensor)[source]#

Construct a full tensor from a PARAFAC2 decomposition.

The decomposition is on the form \((A [B_i] C)\) such that the i-th frontal slice, \(X_i\), of \(X\) is given by

\[X_i = B_i diag(a_i) C^T,\]

where \(diag(a_i)\) is the diagonal matrix whose nonzero entries are equal to the \(i\)-th row of the \(I times R\) factor matrix \(A\), \(B_i\) is a \(J_i times R\) factor matrix such that the cross product matrix \(B_{i_1}^T B_{i_1}\) is constant for all \(i\), and \(C\) is a \(K times R\) factor matrix. To compute this decomposition, we reformulate the expression for \(B_i\) such that

\[B_i = P_i B,\]

where \(P_i\) is a \(J_i times R\) orthogonal matrix and \(B\) is a \(R times R\) matrix.

An alternative formulation of the PARAFAC2 decomposition is that the tensor element \(X_{ijk}\) is given by

\[X_{ijk} = sum_{r=1}^R A_{ir} B_{ijr} C_{kr},\]

with the same constraints hold for \(B_i\) as above.

Parameters:

parafac2_tensor (Parafac2Tensor - (weight, factors, projection_matrices)) –

  • weights1D array of shape (rank, )

    weights of the factors

  • factorsList of factors of the PARAFAC2 decomposition

    Contains the matrices \(A\), \(B\) and \(C\) described above

  • projection_matricesList of projection matrices used to create evolving

    factors.

Returns:

ndarray – Full constructed tensor. Uneven slices are padded with zeros.

static parafac2_to_unfolded(parafac2_tensor, mode)[source]#

Construct an unfolded tensor from a PARAFAC2 decomposition. Uneven slices are padded by zeros.

The decomposition is on the form \((A [B_i] C)\) such that the i-th frontal slice, \(X_i\), of \(X\) is given by

\[X_i = B_i diag(a_i) C^T,\]

where \(diag(a_i)\) is the diagonal matrix whose nonzero entries are equal to the \(i\)-th row of the \(I times R\) factor matrix \(A\), \(B_i\) is a \(J_i times R\) factor matrix such that the cross product matrix \(B_{i_1}^T B_{i_1}\) is constant for all \(i\), and \(C\) is a \(K times R\) factor matrix. To compute this decomposition, we reformulate the expression for \(B_i\) such that

\[B_i = P_i B,\]

where \(P_i\) is a \(J_i times R\) orthogonal matrix and \(B\) is a \(R times R\) matrix.

An alternative formulation of the PARAFAC2 decomposition is that the tensor element \(X_{ijk}\) is given by

\[X_{ijk} = sum_{r=1}^R A_{ir} B_{ijr} C_{kr},\]

with the same constraints hold for \(B_i\) as above.

Parameters:

parafac2_tensor (Parafac2Tensor - (weight, factors, projection_matrices)) –

weights

weights of the factors

factors

Contains the matrices \(A\), \(B\) and \(C\) described above

projection_matrices

factors

Returns:

Full constructed tensor. Uneven slices are padded with zeros.

static parafac2_to_vec(parafac2_tensor)[source]#

Construct a vectorized tensor from a PARAFAC2 decomposition. Uneven slices are padded by zeros.

The decomposition is on the form \((A [B_i] C)\) such that the i-th frontal slice, \(X_i\), of \(X\) is given by

\[X_i = B_i diag(a_i) C^T,\]

where \(diag(a_i)\) is the diagonal matrix whose nonzero entries are equal to the \(i\)-th row of the \(I times R\) factor matrix \(A\), \(B_i\) is a \(J_i times R\) factor matrix such that the cross product matrix :math: B_{i_1}^T B_{i_1}`is constant for all :math:`i, and \(C\) is a \(K times R\) factor matrix. To compute this decomposition, we reformulate the expression for \(B_i\) such that

\[B_i = P_i B,\]

where \(P_i\) is a \(J_i times R\) orthogonal matrix and \(B\) is a \(R times R\) matrix.

An alternative formulation of the PARAFAC2 decomposition is that the tensor element \(X_{ijk}\) is given by

\[X_{ijk} = sum_{r=1}^R A_{ir} B_{ijr} C_{kr},\]

with the same constraints hold for \(B_i\) as above.

Parameters:

parafac2_tensor (Parafac2Tensor - (weight, factors, projection_matrices)) –

  • weights

1D array of shape (rank, ) weights of the factors * factors List of factors of the PARAFAC2 decomposition Contains the matrices \(A, :math:`B\) and \(C\) described above * projection_matrices

List of projection matrices used to create evolving factors.

Returns:

Full constructed tensor. Uneven slices are padded with zeros.6

to_tensor()[source]#
to_unfolded(mode)[source]#
to_vec()[source]#
static validate_parafac2_tensor(parafac2_tensor)[source]#

Validate a parafac2_tensor in the form (weights, factors) Return the rank and shape of the validated tensor.

Parameters:

parafac2_tensor – Parafac2Tensor or (weights, factors)

Returns:

(shape, rank) – size of the full tensor and rank of the CP tensor