tt_sketch.tensor

Implements various types of tensors

class tt_sketch.tensor.CPTensor(cores: List[ndarray[Any, dtype[float64]]])[source]

Implements CP tensors.

The cores are stored as list of shape (shape[i], rank).

property T: CPTensor

Transpose of the tensor.

If a tensor has shape (n1,n2,…,nd), then transpose of the tensor has shape (nd,…,n2,n1).

cores: List[ndarray[Any, dtype[float64]]]
gather(idx: Tuple[ndarray[Any, dtype[int64]], ...]) ndarray[Any, dtype[ScalarType]][source]

Obtain the values of the tensor at the given indices.

classmethod random(shape: Tuple[int, ...], rank: int, seed: Optional[int] = None) CPTensor[source]
rank: int
shape: Tuple[int, ...]

The shape of the tensor

size() int[source]

Number of floating point elements used to store the tensor.

to_numpy() ndarray[Any, dtype[ScalarType]][source]

Converts the tensor to a (dense) numpy array of same shape.

class tt_sketch.tensor.DenseTensor(data: ndarray[Any, dtype[ScalarType]])[source]
property T: DenseTensor

Transpose of the tensor.

If a tensor has shape (n1,n2,…,nd), then transpose of the tensor has shape (nd,…,n2,n1).

data: ndarray[Any, dtype[float64]]
classmethod random(shape: Tuple[int, ...]) DenseTensor[source]
shape: Tuple[int, ...]

The shape of the tensor

property size: int

Number of floating point elements used to store the tensor.

to_numpy() ndarray[Any, dtype[float64]][source]

Converts the tensor to a (dense) numpy array of same shape.

to_sparse() SparseTensor[source]

Converts to a sparse tensor.

This is mainly used for testing sketching algorithms, as there is otherwise no reason to convert a dense tensor to a sparse tensor.

class tt_sketch.tensor.SparseTensor(shape: 'Tuple[int, ...]', indices: 'Union[npt.NDArray, Tuple[npt.NDArray, ...]]', entries: 'npt.NDArray[np.float64]')[source]
property T: SparseTensor

Transpose of the tensor.

If a tensor has shape (n1,n2,…,nd), then transpose of the tensor has shape (nd,…,n2,n1).

property dict: Dict[int, float]
dot(other: Tensor, reverse=False) float[source]

Dot product of two tensors

entries: ndarray[Any, dtype[float64]]

The entries of the non-zero entries

gather(indices: Tuple[ndarray[Any, dtype[ScalarType]], ...]) ndarray[Any, dtype[float64]][source]

Gathers the entries corresponding to the given indices.

indices: Union[ndarray[Any, dtype[ScalarType]], Tuple[ndarray[Any, dtype[ScalarType]], ...]]

The indices of the non-zero entries

property nnz: int

Number of non-zero entries.

norm() float[source]

L2 norm of the tensor

classmethod random(shape: Tuple[int, ...], nnz: int, seed: Optional[int] = None) SparseTensor[source]

Generates a random sparse tensor with nnz non-zero gaussian entries

shape: Tuple[int, ...]

The shape of the tensor

property size: int

Number of floating point elements used to store the tensor.

split(n_summands: int) TensorSum[source]

Splits the tensor a TensorSum containing n_summands tensors.

to_numpy() ndarray[Any, dtype[float64]][source]

Converts the tensor to a (dense) numpy array of same shape.

class tt_sketch.tensor.Tensor[source]

Abstract base class for tensors.

abstract property T: TType

Transpose of the tensor.

If a tensor has shape (n1,n2,…,nd), then transpose of the tensor has shape (nd,…,n2,n1).

dense() DenseTensor[source]

Converts to DenseTensor object

dot(other: TType, reverse=False) float[source]

Dot product of two tensors

error(other: TType, relative: bool = False, rmse: bool = False, fast: bool = False) float[source]

L2 error of the tensor.

If fast=True, then error is computed using inner product formula. This is not numerically stable, and gives inaccurate results below relative errors of around 1e-8.

property ndim: int

Number of modes of the tensor.

norm() float[source]

L2 norm of the tensor

shape: Tuple[int, ...]

The shape of the tensor

abstract property size: int

Number of floating point elements used to store the tensor.

abstract to_numpy() ndarray[Any, dtype[float64]][source]

Converts the tensor to a (dense) numpy array of same shape.

class tt_sketch.tensor.TensorSum(tensors: List[TType], shape=None)[source]

Container for sums of tensors

property T: TensorSum

Transpose of the tensor.

If a tensor has shape (n1,n2,…,nd), then transpose of the tensor has shape (nd,…,n2,n1).

dot(other: Tensor, reverse=False) float[source]

Dot product of two tensors

property num_summands: int
shape: Tuple[int, ...]

The shape of the tensor

property size: int

Number of floating point elements used to store the tensor.

tensors: List[TType]
to_numpy() ndarray[Any, dtype[float64]][source]

Converts the tensor to a (dense) numpy array of same shape.

class tt_sketch.tensor.TensorTrain(cores: List[ndarray[Any, dtype[float64]]])[source]
property T: TensorTrain

Transpose of the tensor.

If a tensor has shape (n1,n2,…,nd), then transpose of the tensor has shape (nd,…,n2,n1).

add(other: TensorTrain) TensorTrain[source]

Add two TensorTrain by taking direct sums of cores.

Note, this does not overload the addition operator +; the addition operator instead returns a lazy TensorSum object.

cores: List[ndarray[Any, dtype[float64]]]

A list containing the cores of the tensor train

dot(other: Tensor, reverse=False) float[source]

Compute the dot product of two tensor trains with the same shape.

Result is computed in a left-to-right sweep.

error(other: TType, relative: bool = False, rmse: bool = False, fast: bool = False) float[source]

L2 error of tensor, see docs for Tensor::error.

Overloads only the error between two Tensor Trains using much faster accurate method.

gather(idx: Union[ndarray[Any, dtype[int64]], Tuple[ndarray[Any, dtype[int64]], ...]]) ndarray[Any, dtype[ScalarType]][source]

Gather entries of dense tensor according to indices.

For each row of idx this returns one number. This number is obtained by multiplying the slices of each core corresponding to each index (in a left-to-right fashion).

norm() float[source]

L2 norm of the tensor

orthogonalize() TensorTrain[source]

Do QR sweep to left-orthogonalize

partial_dense(dir: str = 'lr') List[ndarray[Any, dtype[float64]]][source]

Do partial contractions to dense tensor; X[0].X[1]...X[mu]

classmethod random(shape: Tuple[int, ...], rank: Union[int, Tuple[int, ...]], seed: Optional[int] = None, orthog: bool = False, trim: Optional[bool] = None, norm_goal: str = 'norm-1') TensorTrain[source]

Generate random orthogonal tensor train cores

By default, a core of (r1, n, r2) has Gaussian entries with zero mean and variance 1 / r1 * n * r2, so that expected Frobenius norm is 1.

If trim=True, the ranks are trimmed; i.e. we enforce that r1*n>=r2 and r2*n>=r1a.

If orthog is set to True, all cores except the last are left-orthogonalized. Trim must be enabled in this case.

rank: Tuple[int, ...]

Tuple encoding the tensor train rank

round(eps: Optional[float] = None, max_rank: Optional[Union[int, Tuple[int, ...]]] = None, orthogonalized: bool = False) TensorTrain[source]

Standard TT-SVD rounding scheme.

First left orthogonalize in LR sweep, then apply SVD-based rounding in a RL sweep. Leaves the tensor right-orthogonalized.

If the tensor is already orthogonalized, then pass orthogonalized=True to avoid unnecessary re-orthogonalization.

shape: Tuple[int, ...]

The shape of the tensor

property size: int

Number of floating point elements used to store the tensor.

svdvals() List[ndarray[Any, dtype[ScalarType]]][source]

Return singular value of each mode

to_numpy() ndarray[Any, dtype[ScalarType]][source]

Converts the tensor to a (dense) numpy array of same shape.

classmethod zero(shape: Tuple[int, ...], rank: Union[int, Tuple[int, ...]]) TensorTrain[source]
class tt_sketch.tensor.TuckerTensor(factors: List[ndarray[Any, dtype[float64]]], core: ndarray[Any, dtype[ScalarType]])[source]

Implements Tucker tensors.

This consists of a core tensor of shape (s1, ..., sd) and d factor matrices of shape (si, ni), where (n1, ..., nd) is the overall shape of the tensor.

property T: TuckerTensor

Transpose of the tensor.

If a tensor has shape (n1,n2,…,nd), then transpose of the tensor has shape (nd,…,n2,n1).

classmethod random(shape: Tuple[int, ...], rank: Union[int, Tuple[int, ...]], seed: Optional[int] = None) TuckerTensor[source]
shape: Tuple[int, ...]

The shape of the tensor

property size: int

Number of floating point elements used to store the tensor.

to_numpy() ndarray[Any, dtype[float64]][source]

Converts the tensor to a (dense) numpy array of same shape.