Install & Compatibility
Where this runs
tested against v0.9.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 1.605s · 245.4MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 7.5s · import 1.556s · 236MB
245MB installed
● package 245MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
tensorly
✓ import tensorly as tl
Standard alias for core TensorLy functionalities.
decomposition modules
✓ from tensorly.decomposition import parafac, tucker
Import specific decomposition algorithms from the decomposition module.
tl.tensor
✓ tensor = tl.tensor(numpy_array)
✗ tensor = numpy.array(...)
Always create tensors using `tl.tensor` or `tl.random.random_tensor` to ensure compatibility with the active backend, rather than native backend array constructors.
Backend functions
✓ tl.mean(tensor)
✗ numpy.mean(tensor)
To maintain backend transparency, always use TensorLy's dispatched functions (e.g., `tl.mean`, `tl.dot`) instead of directly calling functions from the native backend (e.g., `numpy.mean`).
Index assignment
✓ tensor = tl.index_update(tensor, (indices), values)
✗ tensor[indices] = values
Direct NumPy-style index assignment (`tensor[indices] = values`) is not universally supported across all backends. Use `tl.index_update` for cross-backend compatibility.
This quickstart demonstrates how to initialize TensorLy, create a tensor, optionally set a backend, perform a CANDECOMP/PARAFAC (CP) decomposition, and reconstruct the tensor from the learned factors.
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}")
Upgrade
Version history
0.9.0latest on PyPI · released Nov 12, 2024
Audit
Dependencies
numpyrequiredDefault backend for tensor operations.
torchoptionalOptional backend for GPU acceleration and deep learning integration.
jaxoptionalOptional backend for high-performance numerical computation and auto-differentiation.
tensorflowoptionalOptional backend for deep learning integration.
cupyoptionalOptional backend for NVIDIA CUDA GPU acceleration.
paddlepaddleoptionalOptional backend for deep learning integration, added in 0.9.0.