All Projects → patrick-kidger → sympytorch

patrick-kidger / sympytorch

Licence: Apache-2.0 license
Turning SymPy expressions into PyTorch modules.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to sympytorch

Google-Summer-of-Code-with-SymPy
This repository showcases my proposal, final report, and the work done during Google Summer of Code 2020 with the SymPy project.
Stars: ✭ 12 (-82.61%)
Mutual labels:  sympy
PyLMI-SDP
[UNMAINTAINED] Symbolic linear matrix inequalities (LMI) and semi-definite programming (SDP) tools for Python
Stars: ✭ 20 (-71.01%)
Mutual labels:  sympy
pytheory
Music Theory for Humans.
Stars: ✭ 1,358 (+1868.12%)
Mutual labels:  sympy
CC33Z
Curso de Ciência da Computação
Stars: ✭ 50 (-27.54%)
Mutual labels:  sympy
SymPyBotics
[UNMAINTAINED] Symbolic Framework for Modeling and Identification of Robot Dynamics
Stars: ✭ 137 (+98.55%)
Mutual labels:  sympy
pyodesys
∫ Straightforward numerical integration of systems of ordinary differential equations
Stars: ✭ 85 (+23.19%)
Mutual labels:  sympy
jupyter boilerplate
Adds a customizable menu item to Jupyter (IPython) notebooks to insert boilerplate snippets of code
Stars: ✭ 69 (+0%)
Mutual labels:  sympy
sympy-paper
Repo for the paper "SymPy: symbolic computing in python"
Stars: ✭ 42 (-39.13%)
Mutual labels:  sympy
pyccel
Python extension language using accelerators
Stars: ✭ 189 (+173.91%)
Mutual labels:  sympy
Python-Matematica
Explorando aspectos fundamentais da matemática com Python e Jupyter
Stars: ✭ 41 (-40.58%)
Mutual labels:  sympy
continuum mechanics
Utilities for doing calculations in continuum mechanics.
Stars: ✭ 18 (-73.91%)
Mutual labels:  sympy

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].