PyTensor
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.
Warnings
- breaking PyTensor 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.
- breaking PyTensor'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.
- gotcha PyTensor 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.
- gotcha Small 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.
- 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.
Install
-
pip install pytensor -
conda install -c conda-forge pytensor
Imports
- pytensor
import pytensor
- tensor
from pytensor import tensor as pt
- function
pytensor.function
Quickstart
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}")