Registry / ai-ml / tensorly

tensorly

JSON →
library0.9.0pypypiunverified

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.

pip install -U tensorly
INSTALL
IMPORT
SIG · TENSORLY
T
tensorly
ai-mlpythonv0.9.0
Install
7.5s avg
Import
1581ms
Disk
245MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
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
musl
py 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 1.605s · 245.4MB
glibc
py 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}")
Debug
Known issues
breakingIn 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.
fix
Update imports and class instantiations from `tensorly.kruskal_tensor` to `tensorly.cp_tensor` and from `tensorly.mps_tensor` to `tensorly.tt_tensor`.
affects: >=0.5.1
deprecatedThe MXNet backend was deprecated in version 0.8.0.
fix
Migrate to another supported backend such as PyTorch, JAX, TensorFlow, CuPy, or PaddlePaddle.
affects: >=0.8.0
gotchaTensorLy'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.
fix
Always remember that TensorLy's mode indexing starts from 0, consistent with Python's array indexing.
affects: All versions
gotchaWhile 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.
fix
If encountering unexpected behavior or performance, be aware of the `tensorly.tenalg.set_backend` and `tensorly.plugins` functions (`use_opt_einsum`, `use_cuquantum`, `use_default_einsum`) which control `einsum` dispatching and optimization.
affects: >=0.8.0
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.
Agent activity
17 hits · last 30 days
node
16
OpenAI (training)
1
Resources
tensorly — pip install tensorly · libregistry