pytensor
Optimizing compiler for evaluating mathematical expressions on CPUs and GPUs.
What it is and what it does
PyTensor is a symbolic computation library that lets you define mathematical expressions as static graphs, then compile and optimize them before execution. Unlike eager frameworks, you first declare symbolic variables and operations, then convert the graph into a callable function that PyTensor optimizes—removing redundant operations and replacing computations with efficient BLAS routines. It supports automatic differentiation (gradients) and can compile expressions to C, JAX, or Numba for performance.
The library is primarily used as the computational backend for PyMC, a probabilistic programming framework, but works standalone for any numerical computing task involving arrays and mathematical operations. It depends on numpy for array operations, scipy for scientific functions, and numba for JIT compilation; setuptools and filelock are build and locking utilities. The static-graph design allows advanced optimizations that dynamic frameworks cannot perform.
Use it for:
- Build probabilistic models and Bayesian inference pipelines using PyMC, which relies on PyTensor for expression evaluation.
- Define and optimize complex mathematical expressions (e.g., neural network layers, physics simulations) before compilation.
- Compute gradients automatically for optimization and machine learning without manually writing backpropagation.
- Compile symbolic expressions to efficient C or GPU code via JAX or Numba for production inference.
- Prototype numerical algorithms with symbolic manipulation and graph visualization before deployment.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
PyTensor is a Python library for defining, optimizing, and evaluating symbolic mathematical expressions on multi-dimensional arrays, with automatic differentiation and compilation to C, JAX, or Numba.
Yes, if you need symbolic computation with automatic differentiation and graph optimization. PyTensor is actively maintained, has no known vulnerabilities, installs easily, and is the standard backend for PyMC. Install it if you're doing probabilistic programming, building custom optimized numerical pipelines, or need fine-grained control over computation graphs. Not necessary if you only need eager-mode tensor operations (use NumPy or PyTorch instead).
Install
pytensor on PyPI
pip
pip install pytensoruv
uv add pytensorpoetry
poetry add pytensorInstalling pytensor
Before you install
Low install friction with a pure-Python wheel distribution. Active maintenance with a release within 2 days and recent commits; supports current Python versions (3.12–3.14). Depends on well-established packages: numpy, scipy, numba, setuptools, and filelock.
License in practice
BSD-3-Clause is a permissive license; you may use, modify, and distribute PyTensor freely in commercial and private projects provided you include the license notice.
Quickstart
pip install pytensor
import pytensor
from pytensor import tensor as pt
a = pt.dscalar("a")
b = pt.dscalar("b")
c = a + b
f_c = pytensor.function([a, b], c)
print(f_c(1.5, 2.5)) # Output: 4.0
Verify before relying
- Whether JAX or Numba compilation backends require additional system dependencies or configuration beyond the listed runtime packages.
- Performance characteristics and memory overhead compared to eager-execution frameworks for typical workloads.
Package facts
| License | BSD-3-Clause (permissive) |
| Python support | supports the current Python release (<3.15,>=3.12) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 5 — setuptools, scipy, numpy, numba, filelock |
| Maintenance | actively maintained — 2 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 2,197,832/month — #3,217 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pytensor-3.3.0-py2.py3-none-any.whl
Keywords: pytensor, math, numerical, symbolic, blas, numpy, autodiff, differentiation
Tags
More Mathematics packages
NetworkX provides data structures and…
permissive · top 1,000 on PyPI
kiwisolverkiwisolver is a Python binding to a fast C++…
permissive · top 1,000 on PyPI
sympySymPy is a Python library for symbolic…
permissive · top 1,000 on PyPI
contourpyContourPy calculates contours of 2D…
permissive · top 1,000 on PyPI
torchPyTorch provides GPU-accelerated tensor…
permissive · top 1,000 on PyPI
onnxruntimeonnxruntime loads and executes Open Neural…
permissive · top 1,000 on PyPI
pytensor-distributionsPyTensor-distributions provides a collection of…
permissive · top 15,000 on PyPI
Theano-PyMCTheano-PyMC is an optimizing compiler for…
permissive · top 15,000 on PyPI
TheanoTheano is a Python library for defining,…
permissive · top 15,000 on PyPI
py-expression-evalParses and evaluates mathematical expressions…
permissive · top 15,000 on PyPI
numexprNumExpr evaluates numerical expressions on…
permissive · top 5,000 on PyPI
audmathaudmath provides mathematical functions…
permissive · top 15,000 on PyPI
casadiCasADi is a framework for algorithmic…
copyleft · top 5,000 on PyPI
sparsediffpySparseDiffPy provides Python bindings to…
permissive · top 5,000 on PyPI
jaxJAX is a Python library for automatic…
permissive · top 1,000 on PyPI