Skip to main content

Automatic differentiation and generation of Torch/Tensorflow operations with pystencils (https://i10git.cs.fau.de/pycodegen/pystencils)

Project description

PyPI version Documentation Status=================== Gitlab CI https://travis-ci.org/theHamsta/pystencils_autodiff.svg?branch=master https://codecov.io/gh/theHamsta/pystencils_autodiff/branch/master/graph/badge.svg

pystencils_autodiff

This repo adds automatic differentiation to pystencils.

Installation

Install via pip:

pip install pystencils-autodiff

or if you downloaded this repository using:

pip install -e .

Then, you can access the submodule pystencils.autodiff.

import pystencils.autodiff

Usage

Create a pystencils.AssignmentCollection with pystencils:

import sympy
import pystencils

z, y, x = pystencils.fields("z, y, x: [20,30]")

forward_assignments = pystencils.AssignmentCollection({
    z[0, 0]: x[0, 0] * sympy.log(x[0, 0] * y[0, 0])
})

print(forward_assignments)
Subexpressions:
Main Assignments:
     z[0,0]  x_C*log(x_C*y_C)

You can then obtain the corresponding backward assignments:

from pystencils.autodiff import AutoDiffOp, create_backward_assignments
backward_assignments = create_backward_assignments(forward_assignments)

print(backward_assignments)

You can see the derivatives with respective to the two inputs multiplied by the gradient diffz_C of the output z_C.

Subexpressions:
Main Assignments:
    \hat{x}[0,0]  diffz_C*(log(x_C*y_C) + 1)
    \hat{y}[0,0]  diffz_C*x_C/y_C

You can also use the class AutoDiffOp to obtain both the assignments (if you are curious) and auto-differentiable operations for Tensorflow…

op = AutoDiffOp(forward_assignments)
backward_assignments = op.backward_assignments

x_tensor = pystencils.autodiff.tf_variable_from_field(x)
y_tensor = pystencils.autodiff.tf_variable_from_field(y)
tensorflow_op = op.create_tensorflow_op({x: x_tensor, y: y_tensor}, backend='tensorflow')

… or Torch:

x_tensor = pystencils.autodiff.torch_tensor_from_field(x, cuda=False, requires_grad=True)
y_tensor = pystencils.autodiff.torch_tensor_from_field(y, cuda=False, requires_grad=True)

z_tensor = op.create_tensorflow_op({x: x_tensor, y: y_tensor}, backend='torch')

Test Report and Coverage

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

pystencils_autodiff-0.3.0.tar.gz (53.7 kB view details)

Uploaded Source

File details

Details for the file pystencils_autodiff-0.3.0.tar.gz.

File metadata

  • Download URL: pystencils_autodiff-0.3.0.tar.gz
  • Upload date:
  • Size: 53.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5

File hashes

Hashes for pystencils_autodiff-0.3.0.tar.gz
Algorithm Hash digest
SHA256 b5dcf81e075db18c7d6a7002831f50ffe9c7e27ab3dffc7f974c689f0b90df01
MD5 ee809a4855bd33fbd9e472f037d71f30
BLAKE2b-256 a72933b49a9f619a7929d1b7c46f06fe9f63f5fe6fe7b38ca20bb8122180c160

See more details on using hashes here.

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