API reference

tensorly: Manipulating the backend with a unified interface

For each backend, tensorly provides the following uniform functions:

set_backend(backend[, local_threadsafe])

Changes the backend to the specified one


Returns the name of the current backend


Returns the context of a tensor

tensor(data, **context)

Tensor class


Returns if obj is a tensor for the current backend


Return the shape of a tensor


Return the number of dimensions of a tensor


Returns a copy of the tensor as a NumPy array.


Return a copy of the given tensor

concatenate(tensors[, axis])

Concatenate tensors along an axis.

reshape(tensor, newshape)

Gives a new shape to a tensor without changing its data.


Permute the dimensions of a tensor.

moveaxis(tensor, source, destination)

Move axes of a tensor to new positions.

arange([start, stop, step])

Return evenly spaced values within a given interval.

ones(shape[, dtype])

Return a new tensor of given shape and type, filled with ones.

zeros(shape[, dtype])

Return a new tensor of given shape and type, filled with zeros.


Return at tensor of zeros with the same shape and type as a given tensor.


Return a 2-D tensor with ones on the diagonal and zeros elsewhere.

where(condition, x, y)

Return elements, either from x or y, depending on condition.

clip(tensor[, a_min, a_max])

Clip the values of a tensor to within an interval.


The max value in a tensor.


The min value in a tensor.


Returns if all array elements in a tensor are True.

mean(tensor[, axis])

Compute the mean of a tensor, optionally along an axis.

sum(tensor[, axis])

Compute the sum of a tensor, optionally along an axis.

prod(tensor[, axis])

Compute the product of a tensor, optionally along an axis.


Computes the element-wise sign of the given input tensor.


Computes the element-wise absolute value of the given input tensor.


Computes the element-wise sqrt of the given input tensor.

norm(tensor[, order, axis])

Computes the l-order norm of a tensor.

dot(a, b)

Dot product of two tensors.

kron(a, b)

Kronecker product of two tensors.

solve(a, b)

Solve a linear matrix equation, or system of linear scalar equations.


Compute the qr factorization of a matrix.

kr(matrices[, weights, mask])

Khatri-Rao product of a list of matrices

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


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.


Turns the Khatri-product of matrices into a full tensor

kruskal_to_unfolded(kruskal_tensor, mode)

Turns the khatri-product of matrices into an unfolded tensor


Turns the khatri-product of matrices into a vector

kruskal_mode_dot(kruskal_tensor, …[, …])

n-mode product of a Kruskal tensor and a matrix or vector at the specified mode

unfolding_dot_khatri_rao(tensor, …)

mode-n unfolding times khatri-rao product of factors

tensorly.tucker_tensor: Tensors in Tucker format

Core operations on Tucker tensors.

tucker_to_tensor(tucker_tensor[, …])

Converts the Tucker tensor into a full tensor

tucker_to_unfolded(tucker_tensor[, mode, …])

Converts the Tucker decomposition into an unfolded tensor (i.e.

tucker_to_vec(tucker_tensor[, skip_factor, …])

Converts a Tucker decomposition into a vectorised tensor

tucker_mode_dot(tucker_tensor, …[, …])

n-mode product of a Tucker tensor and a matrix or vector at the specified mode

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)


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


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[, weights, skip_matrix, …])

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


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) Computes a rank-rank decomposition of tensor [R3] such that,

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 tensor regression


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


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


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