pyttb.ttensor

class pyttb.ttensor(core: tensor | sptensor | None = None, factors: Sequence[ndarray] | None = None, copy: bool = True)[source]

Bases: object

Class for Tucker tensors (decomposed).

Construct an ttensor from fully defined core tensor and factor matrices.

Parameters:
  • core – Core of tucker tensor.

  • factors – Factor matrices.

  • copy – Whether to make a copy of provided data or just reference it.

Returns:

Constructed tucker tensor.

Examples

Import required modules:

>>> import pyttb as ttb
>>> import numpy as np

Set up input data # Create ttensor with explicit data description

>>> core_values = np.ones((2, 2, 2))
>>> core = ttb.tensor(core_values)
>>> factors = [np.ones((1, 2))] * len(core_values.shape)
>>> K0 = ttb.ttensor(core, factors)
property order: Literal['F']

Return the data layout of the underlying storage.

copy() ttensor[source]

Make a deep copy of a pyttb.ttensor.

Returns:

Copy of original ttensor.

Examples

>>> core_values = np.ones((2, 2, 2))
>>> core = ttb.tensor(core_values)
>>> factors = [np.ones((1, 2))] * len(core_values.shape)
>>> first = ttb.ttensor(core, factors)
>>> second = first
>>> third = second.copy()
>>> first.factor_matrices[0][0, 0] = 2

# Item to convert numpy boolean to python boolena for nicer printing

>>> (first.factor_matrices[0][0, 0] == second.factor_matrices[0][0, 0]).item()
True
>>> (first.factor_matrices[0][0, 0] == third.factor_matrices[0][0, 0]).item()
False
__deepcopy__(memo)[source]

Return deepcopy of class.

property shape: Tuple[int, ...]

Shape of the tensor this deconstruction represents.

__repr__()[source]

Return string representation of a tucker tensor.

Returns:

str – Contains the core, and factor matrices as strings on different lines.

__str__()

Return string representation of a tucker tensor.

Returns:

str – Contains the core, and factor matrices as strings on different lines.

to_tensor() tensor[source]

Convert to tensor.

Same as pyttb.ttensor.full()

full() tensor[source]

Convert a ttensor to a (dense) tensor.

double() ndarray[source]

Convert ttensor to an array of doubles.

Returns:

Copy of tensor data.

property ndims: int

Number of dimensions of a ttensor.

Returns:

Number of dimensions of ttensor

isequal(other: ttensor) bool[source]

Component equality for ttensors.

Parameters:

other – TTensor to compare against.

Returns:

True if ttensors decompositions are identical, false otherwise

__pos__()[source]

Unary plus (+) for ttensors. Does nothing.

Returns:

pyttb.ttensor, copy of tensor

__neg__()[source]

Unary minus (-) for ttensors.

Returns:

pyttb.ttensor, copy of tensor

innerprod(other: tensor | sptensor | ktensor | ttensor) float[source]

Efficient inner product with a ttensor.

Parameters:

other – Tensor to take an innerproduct with.

Returns:

Result of the innerproduct.

__mul__(other)[source]

Element wise multiplication (*) for ttensors (only scalars supported).

Parameters:

other (float, int)

Returns:

pyttb.ttensor

__rmul__(other)[source]

Element wise right multiplication (*) for ttensors (only scalars supported).

Parameters:

other (float, int)

Returns:

pyttb.ttensor

ttv(vector: Sequence[ndarray] | ndarray, dims: int | float | Iterable[int] | Iterable[float] | ndarray | None = None, exclude_dims: int | float | Iterable[int] | Iterable[float] | ndarray | None = None) float | ttensor[source]

TTensor times vector.

Parameters:
  • vector – Vector to multiply by.

  • dims – Dimensions to multiply in.

  • exclude_dims – Alternative multiply by all dimensions but these.

mttkrp(U: ktensor | Sequence[ndarray], n: int | integer) ndarray[source]

Matricized tensor times Khatri-Rao product for ttensors.

Parameters:
  • U – Array of matrices or ktensor

  • n – Multiplies by all modes except n

Returns:

numpy.ndarray

norm() float[source]

Compute the norm of a ttensor.

Returns:

Frobenius norm of Tensor.

permute(order: int | float | Iterable[int] | Iterable[float] | ndarray) ttensor[source]

Permute pyttb.ttensor dimensions.

Rearranges the dimensions of a pyttb.ttensor so that they are in the order specified by order. The corresponding ttensor has the same components as self but the order of the subscripts needed to access any particular element is rearranged as specified by order.

Parameters:

order – Permutation of [0,…,self.ndims].

Returns:

Permuted pyttb.ttensor.

ttm(matrix: ndarray | Sequence[ndarray], dims: float | ndarray | None = None, exclude_dims: ndarray | int | None = None, transpose: bool = False) ttensor[source]

Tensor times matrix for ttensor.

Parameters:
  • matrix – Matrix or matrices to multiple by

  • dims – Dimensions to multiply against

  • exclude_dims – Use all dimensions but these

  • transpose – Transpose matrices during multiplication

reconstruct(samples: ndarray | Sequence[ndarray] | None = None, modes: ndarray | Sequence[ndarray] | None = None) tensor[source]

Reconstruct or partially reconstruct tensor from ttensor.

Parameters:
  • samples

  • modes

Returns:

pyttb.ttensor

nvecs(n: int, r: int, flipsign: bool = True) ndarray[source]

Compute the leading mode-n vectors for a ttensor.

Parameters:
  • n – Mode for tensor matricization

  • r – Number of eigenvalues

  • flipsign – Make each column’s largest element positive if true

Returns:

Computed eigenvectors.