{"id":6262,"library":"tensorly","title":"TensorLy","description":"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.","status":"active","version":"0.9.0","language":"en","source_language":"en","source_url":"https://github.com/tensorly/tensorly","tags":["tensor","decomposition","machine learning","numerical computation","deep learning","multi-dimensional arrays"],"install":[{"cmd":"pip install -U tensorly","lang":"bash","label":"Recommended installation via pip"}],"dependencies":[{"reason":"Default backend for tensor operations.","package":"numpy","optional":false},{"reason":"Optional backend for GPU acceleration and deep learning integration.","package":"torch","optional":true},{"reason":"Optional backend for high-performance numerical computation and auto-differentiation.","package":"jax","optional":true},{"reason":"Optional backend for deep learning integration.","package":"tensorflow","optional":true},{"reason":"Optional backend for NVIDIA CUDA GPU acceleration.","package":"cupy","optional":true},{"reason":"Optional backend for deep learning integration, added in 0.9.0.","package":"paddlepaddle","optional":true}],"imports":[{"note":"Standard alias for core TensorLy functionalities.","symbol":"tensorly","correct":"import tensorly as tl"},{"note":"Import specific decomposition algorithms from the decomposition module.","symbol":"decomposition modules","correct":"from tensorly.decomposition import parafac, tucker"},{"note":"Always create tensors using `tl.tensor` or `tl.random.random_tensor` to ensure compatibility with the active backend, rather than native backend array constructors.","wrong":"tensor = numpy.array(...)","symbol":"tl.tensor","correct":"tensor = tl.tensor(numpy_array)"},{"note":"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`).","wrong":"numpy.mean(tensor)","symbol":"Backend functions","correct":"tl.mean(tensor)"},{"note":"Direct NumPy-style index assignment (`tensor[indices] = values`) is not universally supported across all backends. Use `tl.index_update` for cross-backend compatibility.","wrong":"tensor[indices] = values","symbol":"Index assignment","correct":"tensor = tl.index_update(tensor, (indices), values)"}],"quickstart":{"code":"import tensorly as tl\nimport numpy as np\nfrom tensorly.decomposition import parafac\n\n# Set the backend (optional, defaults to numpy)\ntl.set_backend('numpy') # Or 'pytorch', 'jax', 'tensorflow', 'cupy', 'paddle'\n\n# Create a random tensor\n# tensor = tl.random.random_tensor((3, 4, 2))\n# Create a tensor from a NumPy array\ntensor = tl.tensor(np.arange(24).reshape((3, 4, 2)), dtype=tl.float64)\n\nprint(f\"Original tensor shape: {tensor.shape}\")\n\n# Perform CP decomposition\nrank = 2\nfactors = parafac(tensor, rank=rank)\n\n# Reconstruct the tensor from factors\nreconstructed_tensor = tl.cp_to_tensor(factors)\n\nprint(f\"Reconstructed tensor shape: {reconstructed_tensor.shape}\")\nprint(f\"Reconstruction error: {tl.norm(tensor - reconstructed_tensor) / tl.norm(tensor):.4f}\")","lang":"python","description":"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."},"warnings":[{"fix":"Update imports and class instantiations from `tensorly.kruskal_tensor` to `tensorly.cp_tensor` and from `tensorly.mps_tensor` to `tensorly.tt_tensor`.","message":"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.","severity":"breaking","affected_versions":">=0.5.1"},{"fix":"Migrate to another supported backend such as PyTorch, JAX, TensorFlow, CuPy, or PaddlePaddle.","message":"The MXNet backend was deprecated in version 0.8.0.","severity":"deprecated","affected_versions":">=0.8.0"},{"fix":"Always remember that TensorLy's mode indexing starts from 0, consistent with Python's array indexing.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=0.8.0"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}