{"id":4209,"library":"pytensor","title":"PyTensor","description":"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.","status":"active","version":"2.38.2","language":"en","source_language":"en","source_url":"https://github.com/pymc-devs/pytensor","tags":["numerical computation","symbolic differentiation","scientific computing","optimizing compiler","tensor","deep learning backend","probabilistic programming"],"install":[{"cmd":"pip install pytensor","lang":"bash","label":"Install from PyPI"},{"cmd":"conda install -c conda-forge pytensor","lang":"bash","label":"Install with Conda"}],"dependencies":[{"reason":"Required Python version range.","package":"python","version":">=3.11, <3.15","optional":false},{"reason":"Core dependency for array manipulation; major version changes can cause compatibility issues.","package":"numpy","optional":false}],"imports":[{"symbol":"pytensor","correct":"import pytensor"},{"note":"PyTensor is a fork of Theano/Aesara; 'theano.tensor' or 'aesara.tensor' imports are for older codebases and should be updated to 'pytensor.tensor'.","wrong":"import theano.tensor as T","symbol":"tensor","correct":"from pytensor import tensor as pt"},{"note":"PyTensor's function compilation API is `pytensor.function`.","wrong":"theano.function","symbol":"function","correct":"pytensor.function"}],"quickstart":{"code":"import pytensor\nimport pytensor.tensor as pt\nimport numpy as np\n\n# Declare two symbolic floating-point scalars\na = pt.dscalar(\"a\")\nb = pt.dscalar(\"b\")\n\n# Create a simple expression\nc = a + b\n\n# Convert the expression into a callable object\nf_c = pytensor.function([a, b], c)\n\n# Evaluate the function\nresult = f_c(1.5, 2.5)\nprint(f\"a + b = {result}\")\n\n# Compute the gradient with respect to 'a'\ndc = pytensor.grad(c, a)\nf_dc = pytensor.function([a, b], dc)\ngradient_result = f_dc(1.5, 2.5)\nprint(f\"Gradient of (a + b) w.r.t. a = {gradient_result}\")","lang":"python","description":"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."},"warnings":[{"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.","message":"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.","severity":"breaking","affected_versions":"All versions migrating from Theano/Aesara"},{"fix":"Regularly check PyTensor's and PyMC's `requires_python` and `install_requires` for compatible Python and NumPy versions. Upgrade your environment as needed.","message":"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.","severity":"breaking","affected_versions":"Dependent on PyTensor versions, particularly for integration with PyMC 5.x and newer NumPy releases."},{"fix":"Use `symbolic_var.shape[0]` to get the symbolic representation of the first dimension's length.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"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`.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"`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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}