Print sympy expressions to pytensor graphs
Project description
Sympytensor
A tool for converting Sympy expressions to a Pytensor graph, with support for working with PyMC models.
Installation
pip install sympytensor
Examples
Writing expressions to pytensor
Two functions are provided to convert sympy expressions:
as_tensor
converts a sympy expression to apytensor
symbolic graphpytensor_function
returns a compiledpytensor.function
that computes the expression. Keyword arguments topytensor.function
can be provided as**kwargs
Use sympy to compute 1d splines, then convert the splines to a symbolic pytensor variable:
import pytensor
import sympy as sp
from sympytensor import as_tensor
from sympy.abc import x
x_data = [0, 1, 2, 3, 4, 5]
y_data = [3, 6, 5, 7, 9, 1]
s = sp.interpolating_spline(d=3, x=x, X=x_data, Y=y_data)
s_pt = as_tensor(s)
This generates the following function graph:
pytensor.dprint(s_pt)
>>>Out: Elemwise{switch,no_inplace} [id A]
>>> |Elemwise{and_,no_inplace} [id B]
>>> | |Elemwise{ge,no_inplace} [id C]
>>> | | |x [id D]
>>> | | |TensorConstant{0} [id E]
>>> | |Elemwise{le,no_inplace} [id F]
>>> | |x [id D]
>>> | |TensorConstant{2} [id G]
>>> |Elemwise{add,no_inplace} [id H]
>>> | |TensorConstant{3} [id I]
>>> | |Elemwise{mul,no_inplace} [id J]
>>> | | |Elemwise{true_div,no_inplace} [id K]
>>> | | | |TensorConstant{-33} [id L]
>>> | | | |TensorConstant{5} [id M]
>>> | | |Elemwise{pow,no_inplace} [id N]
>>> | | |x [id D]
>>> | | |TensorConstant{2} [id O]
>>> | |Elemwise{mul,no_inplace} [id P]
>>> | | |Elemwise{true_div,no_inplace} [id Q]
>>> | | | |TensorConstant{23} [id R]
>>> | | | |TensorConstant{15} [id S]
>>> | | |Elemwise{pow,no_inplace} [id T]
>>> | | |x [id D]
>>> | | |TensorConstant{3} [id U]
>>> | |Elemwise{mul,no_inplace} [id V]
>>> | |Elemwise{true_div,no_inplace} [id W]
>>> | | |TensorConstant{121} [id X]
>>> | | |TensorConstant{15} [id Y]
>>> | |x [id D]
>>> |Elemwise{switch,no_inplace} [id Z]
>>> |Elemwise{and_,no_inplace} [id BA]
>>> | |Elemwise{ge,no_inplace} [id BB]
>>> | | |x [id D]
>>> | | |TensorConstant{2} [id BC]
>>> | |Elemwise{le,no_inplace} [id BD]
>>> | |x [id D]
>>> | |TensorConstant{3} [id BE]
>>> |Elemwise{add,no_inplace} [id BF]
>>> | |Elemwise{true_div,no_inplace} [id BG]
>>> | | |TensorConstant{103} [id BH]
>>> | | |TensorConstant{5} [id BI]
>>> | |Elemwise{mul,no_inplace} [id BJ]
>>> | | |Elemwise{true_div,no_inplace} [id BK]
>>> | | | |TensorConstant{-55} [id BL]
>>> | | | |TensorConstant{3} [id BM]
>>> | | |x [id D]
>>> | |Elemwise{mul,no_inplace} [id BN]
>>> | | |Elemwise{true_div,no_inplace} [id BO]
>>> | | | |TensorConstant{-2} [id BP]
>>> | | | |TensorConstant{3} [id BQ]
>>> | | |Elemwise{pow,no_inplace} [id BR]
>>> | | |x [id D]
>>> | | |TensorConstant{3} [id BS]
>>> | |Elemwise{mul,no_inplace} [id BT]
>>> | |Elemwise{true_div,no_inplace} [id BU]
>>> | | |TensorConstant{33} [id BV]
>>> | | |TensorConstant{5} [id BW]
>>> | |Elemwise{pow,no_inplace} [id BX]
>>> | |x [id D]
>>> | |TensorConstant{2} [id BY]
>>> |Elemwise{switch,no_inplace} [id BZ]
>>> |Elemwise{and_,no_inplace} [id CA]
>>> | |Elemwise{ge,no_inplace} [id CB]
>>> | | |x [id D]
>>> | | |TensorConstant{3} [id CC]
>>> | |Elemwise{le,no_inplace} [id CD]
>>> | |x [id D]
>>> | |TensorConstant{5} [id CE]
>>> |Elemwise{add,no_inplace} [id CF]
>>> | |TensorConstant{53} [id CG]
>>> | |Elemwise{mul,no_inplace} [id CH]
>>> | | |Elemwise{true_div,no_inplace} [id CI]
>>> | | | |TensorConstant{-761} [id CJ]
>>> | | | |TensorConstant{15} [id CK]
>>> | | |x [id D]
>>> | |Elemwise{mul,no_inplace} [id CL]
>>> | | |Elemwise{true_div,no_inplace} [id CM]
>>> | | | |TensorConstant{-28} [id CN]
>>> | | | |TensorConstant{15} [id CO]
>>> | | |Elemwise{pow,no_inplace} [id CP]
>>> | | |x [id D]
>>> | | |TensorConstant{3} [id CQ]
>>> | |Elemwise{mul,no_inplace} [id CR]
>>> | |Elemwise{true_div,no_inplace} [id CS]
>>> | | |TensorConstant{87} [id CT]
>>> | | |TensorConstant{5} [id CU]
>>> | |Elemwise{pow,no_inplace} [id CV]
>>> | |x [id D]
>>> | |TensorConstant{2} [id CW]
>>> |TensorConstant{nan} [id CX]
Inserting PyMC random variables into an expression
The SympyDeterministic
function works as a drop-in replacement for pm.Deterministic, except a sympy
expression is
expected. It will automatically search the active model context for random variables corresponding to symbols in the
expression and make substitutions.
Here is an example using sympy to symbolically compute the inverse of a matrix, which is then used in a model:
from sympytensor import SympyDeterministic
import pymc as pm
import sympy as sp
from sympy.abc import a, b, c, d
A = sp.Matrix([[a, b],
[c, d]])
A_inv = sp.matrices.Inverse(A).doit()
with pm.Model() as m:
a_pm = pm.Normal('a')
b_pm = pm.Normal('b')
c_pm = pm.Normal('c')
c_pm = pm.Normal('d')
A_inv_pm = SympyDeterministic('A_inv', A_inv)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sympytensor-1.3.0.tar.gz
.
File metadata
- Download URL: sympytensor-1.3.0.tar.gz
- Upload date:
- Size: 12.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 011c97493ad81b51f66c5f2e57004eff8556cc6b5679f71466e150036fe206b2 |
|
MD5 | e7f572be1a82f92436ef8b113503d424 |
|
BLAKE2b-256 | e790cbcbc8e98f3d5076e03aaa52f2bae3c052f31a6426a4685186c0dc122c24 |
Provenance
The following attestation bundles were made for sympytensor-1.3.0.tar.gz
:
Publisher:
release.yml
on jessegrabowski/sympytensor
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
sympytensor-1.3.0.tar.gz
- Subject digest:
011c97493ad81b51f66c5f2e57004eff8556cc6b5679f71466e150036fe206b2
- Sigstore transparency entry: 149119703
- Sigstore integration time:
- Predicate type:
File details
Details for the file sympytensor-1.3.0-py3-none-any.whl
.
File metadata
- Download URL: sympytensor-1.3.0-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccf69b95c79904eb68fe6acbb2b0020aea53f85eed37575dce212e36433a3e21 |
|
MD5 | 1209d3e9956334a9221040547d025482 |
|
BLAKE2b-256 | 925833cb489fef85996e8715fca7f60030558ab1962f652b8d5287afd8ec9a9e |
Provenance
The following attestation bundles were made for sympytensor-1.3.0-py3-none-any.whl
:
Publisher:
release.yml
on jessegrabowski/sympytensor
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
sympytensor-1.3.0-py3-none-any.whl
- Subject digest:
ccf69b95c79904eb68fe6acbb2b0020aea53f85eed37575dce212e36433a3e21
- Sigstore transparency entry: 149119709
- Sigstore integration time:
- Predicate type: