Registry / data / pytensor

pytensor

JSON →
library3.3.0pypypi✓ verified 25d ago

PyTensor (version 2.38.2) is a Python library that functions as an optimizing compiler for evaluating complex mathematical expressions, especially those involving multi-dimensional arrays, on CPUs and GPUs. It is a community-driven fork of Aesara, which itself was a fork of the original Theano project, and serves as the computational backend for the PyMC probabilistic programming library. PyTensor focuses on defining static computational graphs, performing efficient symbolic differentiation, and optimizing execution speed through code generation for C, JAX, or Numba. While it has a somewhat flexible release cadence, it generally aligns with the Scientific Python ecosystem's schedules.

pip install pytensor
INSTALL
IMPORT
SIG · PYTENSOR
P
pytensor
datapythonv3.3.0
Install
10.8s avg
Import
1012ms
Disk
331MB
Pass rate
7/ 10
Env Coverage7 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.34.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
glibc
py 3.10
✓ —
✓ 8.6s
py 3.11
✕ build_error
✓ 11.5s
py 3.12
✕ build_error
✓ 12.6s
py 3.13
✕ build_error
✓ 12.2s
py 3.9
✓ —
✓ 9.1s
331MB installed
● package 331MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pytensor
import pytensor
tensor
from pytensor import tensor as pt
import theano.tensor as T
PyTensor is a fork of Theano/Aesara; 'theano.tensor' or 'aesara.tensor' imports are for older codebases and should be updated to 'pytensor.tensor'.
function
pytensor.function
theano.function
PyTensor's function compilation API is `pytensor.function`.

This quickstart demonstrates how to define symbolic variables, build a simple mathematical expression, compile it into a callable PyTensor function, and compute its value and gradient. It showcases PyTensor's core functionality for symbolic computation and automatic differentiation.

import pytensor import pytensor.tensor as pt import numpy as np # Declare two symbolic floating-point scalars a = pt.dscalar("a") b = pt.dscalar("b") # Create a simple expression c = a + b # Convert the expression into a callable object f_c = pytensor.function([a, b], c) # Evaluate the function result = f_c(1.5, 2.5) print(f"a + b = {result}") # Compute the gradient with respect to 'a' dc = pytensor.grad(c, a) f_dc = pytensor.function([a, b], dc) gradient_result = f_dc(1.5, 2.5) print(f"Gradient of (a + b) w.r.t. a = {gradient_result}")
Debug
Known issues
breakingPyTensor is a fork of Aesara/Theano. Direct usage of `theano` or `aesara` imports and constructs (e.g., `theano.scan`) are not compatible and require migration to `pytensor` equivalents. While many functions are similar, import paths and some behaviors have diverged.
fix
Update all `theano` or `aesara` imports to `pytensor`. Review documentation for specific functions like `scan` which may have different usage patterns or may need to be replaced with explicit Python loops in some cases.
affects: All versions migrating from Theano/Aesara
breakingPyTensor's Python version support (>=3.11, <3.15) and NumPy compatibility evolve. Future PyMC releases, which depend on PyTensor, have indicated dropping support for older Python versions (e.g., 3.10) and NumPy versions (e.g., <2.0). Ensure your environment uses compatible Python and NumPy versions to avoid breakage.
fix
Regularly check PyTensor's and PyMC's `requires_python` and `install_requires` for compatible Python and NumPy versions. Upgrade your environment as needed.
affects: Dependent on PyTensor versions, particularly for integration with PyMC 5.x and newer NumPy releases.
gotchaPyTensor symbolic variables do not support the `__len__` operation directly because their length is symbolic, not a concrete integer. Attempting to use `len(symbolic_var)` will result in a TypeError.
fix
Use `symbolic_var.shape[0]` to get the symbolic representation of the first dimension's length.
affects: All versions
gotchaSmall numerical differences can occur when comparing PyTensor's output across different flags, versions, CPU/GPU devices, or with other software like NumPy. This is a normal consequence of floating-point arithmetic and optimizations.
fix
When comparing numerical results, use appropriate tolerances rather than strict equality checks. For debugging, use the `warn_float64` flag to identify where `float64` values are being introduced if you expect `float32`.
affects: All versions
gotcha`pytensor.function` compilation can be time-consuming due to extensive graph optimizations and C/CUDA code generation. Debugging issues may be hindered by these optimizations.
fix
For quick testing, use `mode=FAST_COMPILE` as a `PYTENSOR_FLAGS` environment variable or function argument to skip most rewrites and C/CUDA generation. For debugging, use `optimizer=fast_compile` or `optimizer=None` and `exception_verbosity=high` to disable optimizations and get more detailed error traces.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pytensor'
PyTensor is not installed in the current Python environment, or the environment where it's installed is not activated.
fix
Run `pip install pytensor` (or `conda install pytensor` if using Anaconda/Miniconda) in your terminal to install the library.
AttributeError: module 'pytensor.tensor' has no attribute 'matrix'
Users migrating from Theano or Aesara often attempt to use `tt.matrix()`, but PyTensor requires explicitly specifying the floating-point precision for matrix types.
fix
Use `pytensor.tensor.dmatrix()` for double-precision (float64) matrices or `pytensor.tensor.fmatrix()` for single-precision (float32) matrices instead of the generic `matrix()`.
TypeError: For '{op_name}', the input variable '{variable_name}' must have a dtype in {...}, but got {actual_dtype}.
The data type (dtype) of a NumPy array provided as input to a `pytensor.function` does not match the symbolic data type expected by the compiled PyTensor graph.
fix
Ensure the NumPy array's `dtype` matches the symbolic variable's type. For instance, use `input_array.astype(pytensor.config.floatX)` for `dmatrix` or `fmatrix` inputs, or define symbolic integer types (e.g., `tt.lvector()` for int64) to match your NumPy array's integer `dtype`.
Upgrade
Version history
3.3.0latest on PyPI · released Aug 12, 2026
Audit
Dependencies
pythonrequiredRequired Python version range.
numpyrequiredCore dependency for array manipulation; major version changes can cause compatibility issues.
Agent activity
9 hits · last 30 days
node
6
OpenAI (training)
1
Resources