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
- __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()
- 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:
- __rmul__(other)[source]
Element wise right multiplication (*) for ttensors (only scalars supported).
- Parameters:
other (float, int)
- Returns:
- 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:
- 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