TensorLy
TensorLy is a Python library designed to simplify and make tensor learning accessible, offering tools for tensor decomposition, tensor learning, and tensor algebra. It features a flexible backend system that allows computations to be seamlessly performed using NumPy (default), PyTorch, JAX, TensorFlow, CuPy, or Paddle, enabling scalable operations on both CPU and GPU. The library is actively maintained, with its latest version 0.9.0 released in November 2024, and regular updates bringing new features and improvements.
Warnings
- breaking In version 0.5.1, `Kruskal-tensors` were renamed to `cp_tensors` and `Matrix-product-state` was renamed to `tensor-train` for API consistency. The old names (`kruskal_tensor`, `mps_tensor`) were deprecated and later removed.
- deprecated The MXNet backend was deprecated in version 0.8.0.
- gotcha TensorLy's `unfold` function uses 0-indexed modes, meaning `tl.unfold(tensor, 0)` unfolds along the *first* dimension. This can differ from conventions in some classical tensor literature.
- gotcha While TensorLy transparently supports `einsum` operations since 0.8.0, and `tenalg.set_backend('einsum')` can dispatch all tensor algebraic operations to the backend's `einsum`, this might change performance characteristics or require adjustments if you previously relied on TensorLy's 'hand-crafted' implementations. Plugins like `use_opt_einsum` further modify `einsum` behavior.
Install
-
pip install -U tensorly
Imports
- tensorly
import tensorly as tl
- decomposition modules
from tensorly.decomposition import parafac, tucker
- tl.tensor
tensor = tl.tensor(numpy_array)
- Backend functions
tl.mean(tensor)
- Index assignment
tensor = tl.index_update(tensor, (indices), values)
Quickstart
import tensorly as tl
import numpy as np
from tensorly.decomposition import parafac
# Set the backend (optional, defaults to numpy)
tl.set_backend('numpy') # Or 'pytorch', 'jax', 'tensorflow', 'cupy', 'paddle'
# Create a random tensor
# tensor = tl.random.random_tensor((3, 4, 2))
# Create a tensor from a NumPy array
tensor = tl.tensor(np.arange(24).reshape((3, 4, 2)), dtype=tl.float64)
print(f"Original tensor shape: {tensor.shape}")
# Perform CP decomposition
rank = 2
factors = parafac(tensor, rank=rank)
# Reconstruct the tensor from factors
reconstructed_tensor = tl.cp_to_tensor(factors)
print(f"Reconstructed tensor shape: {reconstructed_tensor.shape}")
print(f"Reconstruction error: {tl.norm(tensor - reconstructed_tensor) / tl.norm(tensor):.4f}")