Skip to main content

sympytorch

Turn SymPy expressions into PyTorch Modules.

SymPy floats (optionally) become trainable parameters. SymPy symbols are inputs to the Module.

Optimise your symbolic expressions via gradient descent!

Installation

pip install sympytorch

Requires Python 3.7+ and PyTorch 1.6.0+ and SymPy 1.7.1+.

Example

import sympy, torch, sympytorch

x = sympy.symbols('x_name')
cosx = 1.0 * sympy.cos(x)
sinx = 2.0 * sympy.sin(x)
mod = sympytorch.SymPyModule(expressions=[cosx, sinx])

x_ = torch.rand(3)
out = mod(x_name=x_)  # out has shape (3, 2)

assert torch.equal(out[:, 0], x_.cos())
assert torch.equal(out[:, 1], 2 * x_.sin())
assert out.requires_grad  # from the two Parameters initialised as 1.0 and 2.0
assert {x.item() for x in mod.parameters()} == {1.0, 2.0}

API

sympytorch.SymPyModule(*, expressions, extra_funcs=None)

Where:

  • expressions is a list of SymPy expressions.
  • extra_funcs is a dictionary mapping from custom sympy.Functions to their PyTorch implementation. Defaults to no extra functions.

Instances of SymPyModule can be called, passing the values of the symbols as in the above example.

SymPyModule has a method .sympy(), which returns the corresponding list of SymPy expressions. (Which may not be the same as the expressions it was initialised with, if the values of its Parameters have been changed, i.e. have been learnt.)

Wrapping floats in sympy.UnevaluatedExpr will cause them not to be trained, by registering them as buffers rather than parameters.

sympytorch.hide_floats(expression)

As a convenience, hide_floats will take an expression and return a new expression with every float wrapped in a sympy.UnevaluatedExpr, so that it is interpreted as a buffer rather than a parameter.

Extensions

Not every PyTorch or SymPy operation is supported -- just the ones that I found I've needed! There's a dictionary here that lists the supported operations. Feel free to submit PRs for any extra operations you think should be in by default. You can also use the extra_funcs argument to specify extra functions, including custom functions.

Metadata

Release files for sympytorch 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sympytorch 0.1.2
File Size Uploaded
sympytorch-0.1.2.tar.gz 8.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sympytorch 0.1.2
File Interpreter ABI Platform
sympytorch-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 17.7 kB

Release files / sympytorch-0.1.2.tar.gz

Download URL sympytorch-0.1.2.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
9fa4d0464fa3fcb59e65089a2595da6d7ce296528d57371d8bbb8cf7f362b06d
BLAKE2b-256 checksum
How to use checksums
0281c5b3d37dc8868ae2c6082c4b77502dcb4fbb2079d5d90fada0edd1ccdf19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.16

Release files / sympytorch-0.1.2-py3-none-any.whl

Download URL sympytorch-0.1.2-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
15617a2af84d65e0399b42328d274f02a8962081778b0e2965d773cff89e8b9f
BLAKE2b-256 checksum
How to use checksums
bed897f62a585b48921f491bd5197b76d2936f65352d71a8c5f4c5a6ceb7278d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.16

Release history Release notifications | RSS feed

0.1.4

2 release files

0.1.3

2 release files

This release

0.1.2 This release

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page