API reference

tensorly: Manipulating the backend with a unified interface

In addition to the basic functions such as shape, reshape, where, min, etc, each backend provides the following functions:

set_backend(backend_name) Sets the backend for TensorLy
get_backend() Returns the backend currently used
context(tensor) Returns the context of a tensor
to_numpy(tensor) Returns a copy of the tensor as a NumPy array
partial_svd(matrix[, n_eigenvecs]) Computes a fast partial SVD on matrix

tensorly.base: Core tensor functions

unfold(tensor, mode) Returns the mode-mode unfolding of tensor with modes starting at 0.
fold(unfolded_tensor, mode, shape) Refolds the mode-mode unfolding into a tensor of shape shape
tensor_to_vec(tensor) Vectorises a tensor
vec_to_tensor(vec, shape) Folds a vectorised tensor back into a tensor of shape shape
partial_unfold(tensor[, mode, skip_begin, …]) Partially unfolds a tensor while ignoring the specified number of dimensions at the beginning and the end.
partial_fold(unfolded, mode, shape[, …]) Re-folds a partially unfolded tensor
partial_tensor_to_vec(tensor[, skip_begin, …]) Partially vectorises a tensor
partial_vec_to_tensor(matrix, shape[, …]) Refolds a partially vectorised tensor into a full one

tensorly.kruskal_tensor: Tensors in the Kruskal format

Core operations on Kruskal tensors.

kruskal_to_tensor(factors[, weights]) Turns the Khatri-product of matrices into a full tensor
kruskal_to_unfolded(factors, mode) Turns the khatri-product of matrices into an unfolded tensor
kruskal_to_vec(factors) Turns the khatri-product of matrices into a vector

tensorly.tucker_tensor: Tensors in Tucker format

Core operations on Tucker tensors.

tucker_to_tensor(core, factors[, …]) Converts the Tucker tensor into a full tensor
tucker_to_unfolded(core, factors[, mode, …]) Converts the Tucker decomposition into an unfolded tensor (i.e.
tucker_to_vec(core, factors[, skip_factor, …]) Converts a Tucker decomposition into a vectorised tensor

tensorly.mps_tensor: Tensors in Matrix-Product-State format

Core operations on tensors in Matrix Product State (MPS) format, also known as Tensor-Train (TT)

mps_to_tensor(factors) Returns the full tensor whose MPS decomposition is given by ‘factors’
mps_to_unfolded(factors, mode) Returns the unfolding matrix of a tensor given in MPS (or Tensor-Train) format
mps_to_vec(factors) Returns the tensor defined by its MPS format (‘factors’) into

tensorly.tenalg: Tensor algebra

The tensorly.tenalg module contains utilities for Tensor Algebra operations such as khatri-rao or kronecker product, n-mode product, etc.

khatri_rao(matrices[, skip_matrix, reverse]) Khatri-Rao product of a list of matrices
kronecker(matrices[, skip_matrix, reverse]) Kronecker product of a list of matrices
mode_dot(tensor, matrix_or_vector, mode) n-mode product of a tensor and a matrix or vector at the specified mode
multi_mode_dot(tensor, matrix_or_vec_list[, …]) n-mode product of a tensor and several matrices or vectors over several modes
proximal.soft_thresholding(tensor, threshold) Soft-thresholding operator
proximal.svd_thresholding(matrix, threshold) Singular value thresholding operator
proximal.procrustes(matrix) Procrustes operator
inner(tensor1, tensor2[, n_modes]) Generalised inner products between tensors
contract(tensor1, modes1, tensor2, modes2) Tensor contraction between two tensors on specified modes

tensorly.decomposition: Tensor Decomposition

The tensorly.decomposition module includes utilities for performing tensor decomposition such as CANDECOMP-PARAFAC and Tucker.

parafac(tensor, rank[, n_iter_max, init, …]) CANDECOMP/PARAFAC decomposition via alternating least squares (ALS)
non_negative_parafac(tensor, rank[, …]) Non-negative CP decomposition
sample_khatri_rao(matrices, n_samples[, …]) Random subsample of the Khatri-Rao product of the given list of matrices
randomised_parafac(tensor, rank, n_samples) Randomised CP decomposition via sampled ALS
tucker(tensor[, rank, ranks, n_iter_max, …]) Tucker decomposition via Higher Order Orthogonal Iteration (HOI)
partial_tucker(tensor, modes[, rank, …]) Partial tucker decomposition via Higher Order Orthogonal Iteration (HOI)
non_negative_tucker(tensor, rank[, …]) Non-negative Tucker decomposition
robust_pca(X[, mask, tol, reg_E, reg_J, …]) Robust Tensor PCA via ALM with support for missing values
matrix_product_state(input_tensor, rank[, …]) MPS decomposition via recursive SVD

tensorly.regression: Tensor Regression

The tensorly.regression module includes classes for performing Tensor Regression.

tucker_regression.TuckerRegressor(weight_ranks) Tucker tensor regression
kruskal_regression.KruskalRegressor(weight_rank) Kruskal tensor regression

tensorly.metrics: Performance measures

The tensorly.metrics module includes utilities to measure performance (e.g. regression error).

regression.MSE(y_true, y_pred[, axis]) Returns the mean squared error between the two predictions
regression.RMSE(y_true, y_pred[, axis]) Returns the regularised mean squared error between the two predictions (the square-root is applied to the mean_squared_error)

tensorly.random: Sampling random tensors

random_kruskal(shape, rank[, full, …]) Generates a random CP tensor
random_tucker(shape, rank[, full, …]) Generates a random Tucker tensor
random_mps(shape, rank[, full, random_state]) Generates a random MPS/ttrain tensor
check_random_state(seed) Returns a valid RandomState

tensorly.datasets: Creating and loading data

The tensorly.datasets module includes utilities to load datasets and create synthetic data, e.g. for testing purposes.

synthetic.gen_image([region, image_height, …]) Generates an image for regression testing

tensorly.contrib: Experimental features

A module for experimental functions

Allows to add quickly and test new functions for which the API is not necessarily fixed

decomposition.matrix_product_state_cross(…) MPS (tensor-train) decomposition via cross-approximation (TTcross) [1]