Theano-PyMC
Theano-PyMC is a Python library that serves as an optimizing compiler for evaluating mathematical expressions on CPUs and GPUs, featuring efficient symbolic differentiation. It is a fork of the original Theano library, specifically maintained by the PyMC developers to support PyMC3. Its current version is 1.1.2, released in January 2021. While PyMC has since transitioned to other backends (Aesara, then PyTensor), Theano-PyMC remains the foundational backend for PyMC3, meaning its release cadence is tied to critical compatibility and bug fixes for that specific PyMC version.
Common errors
-
AttributeError: module 'numpy' has no attribute 'bool'
cause Using Theano-PyMC with a newer version of NumPy (>= 1.20.0) where `np.bool` has been deprecated.fixDowngrade NumPy to a compatible version, typically `<1.20.0`. For example: `pip install 'numpy<1.20.0'` or `conda install 'numpy<1.20.0'`. -
WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions
cause Theano is unable to find an optimized BLAS (Basic Linear Algebra Subprograms) library (like OpenBLAS or MKL) or is not configured to use it, defaulting to a less efficient NumPy-provided implementation.fixInstall and configure an optimized BLAS library. For conda users, installing from `conda-forge` often includes MKL or OpenBLAS automatically (`conda install -c conda-forge theano-pymc`). For pip users, ensure a properly configured scientific Python stack or refer to Theano's BLAS configuration documentation. This may involve setting environment variables like `MKL_THREADING_LAYER=GNU` or `OMP_NUM_THREADS`. -
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. pymc3 3.10.0 requires theano-pymc==1.0.11, but you have theano-pymc 1.1.2 which is incompatible.
cause A specific version of PyMC3 (e.g., 3.10.0) is strictly dependent on an exact version of Theano-PyMC (e.g., 1.0.11), and a different version has been installed.fixExplicitly install the exact `theano-pymc` version required by your `PyMC3` installation. For example: `pip uninstall theano-pymc pymc3 && pip install 'theano-pymc==1.0.11' 'pymc3==3.10.0'` (adjust versions as needed). Using `conda` is often recommended for better dependency resolution: `conda install -c conda-forge theano-pymc=1.0.11 pymc3=3.10.0`.
Warnings
- breaking Theano-PyMC's compatibility with NumPy versions above 1.19.x can be problematic due to the deprecation of `np.bool` and other internal changes. Using newer NumPy versions with older Theano-PyMC (or PyMC3 versions that depend on it) will lead to `AttributeError`.
- gotcha Theano-PyMC relies on C/C++ compilers (like `g++`) for optimal performance by compiling symbolic graphs into efficient machine code. Without a detected compiler, it will silently fall back to slower Python implementations, severely degrading performance.
- deprecated The broader PyMC project has transitioned from Theano-PyMC to `Aesara` and then `PyTensor` as its primary backend for symbolic computation. While Theano-PyMC remains important for PyMC3, direct new development or feature additions for Theano-PyMC as a standalone library are minimal, focusing on critical fixes for PyMC3.
Install
-
pip install Theano-PyMC -
conda install -c conda-forge theano-pymc
Imports
- theano
import theano
- tensor
import theano.tensor as T
import theano.tensor as tt
- function
from theano import function
- shared
from theano import shared
Quickstart
import theano
import theano.tensor as tt
from theano import function
# Define symbolic variables
x = tt.dscalar('x') # A double-precision scalar
y = tt.dscalar('y')
# Define a symbolic expression
z = x ** 2 + y
# Compile the expression into a callable function
f = function([x, y], z)
# Evaluate the function with numerical values
result = f(2.0, 3.0)
print(f"Result of x^2 + y for x=2, y=3: {result}")
# Example with shared variable
import numpy as np
from theano import shared
shared_val = shared(np.array(10.0, dtype=theano.config.floatX), name='shared_val')
output = x * shared_val
g_func = function([x], output)
print(f"Result with shared_val=10 and x=5: {g_func(5.0)}")
shared_val.set_value(np.array(20.0, dtype=theano.config.floatX))
print(f"Result with shared_val=20 and x=5: {g_func(5.0)}")