Skip to main content

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 a pytensor symbolic graph
  • pytensor_function returns a compiled pytensor.function that computes the expression. Keyword arguments to pytensor.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

sympytensor-1.3.2.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sympytensor-1.3.2-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file sympytensor-1.3.2.tar.gz.

File metadata

  • Download URL: sympytensor-1.3.2.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sympytensor-1.3.2.tar.gz
Algorithm Hash digest
SHA256 2c09e23dff60d4053dba70d4b28213ecb036f97a725f3bb5954be294b4c2f603
MD5 076d7e3cd4414143394454fdd8a851c5
BLAKE2b-256 05bd0b028a64ed92fd39d62a48f7a21153a038a0f9089caf297827ef5829ae3c

See more details on using hashes here.

Provenance

The following attestation bundles were made for sympytensor-1.3.2.tar.gz:

Publisher: release.yml on jessegrabowski/sympytensor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sympytensor-1.3.2-py3-none-any.whl.

File metadata

  • Download URL: sympytensor-1.3.2-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sympytensor-1.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c05d192723a586583eb828d4fc1b6050bfd74f58816b7295714a6f110758633b
MD5 f3772a65d11bd136152c22832ce7fd98
BLAKE2b-256 49be3b034d90935a36ec956ef8cb4f63d25a18e05df5d3eab70dda2ad60abfa1

See more details on using hashes here.

Provenance

The following attestation bundles were made for sympytensor-1.3.2-py3-none-any.whl:

Publisher: release.yml on jessegrabowski/sympytensor

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page