Skip to main content

Leibniz is a package providing facilities to express learnable differential equations based on PyTorch

Project description

Leibniz

Build Status

Leibniz is a python package which provide facilities to express learnable differential equations with PyTorch

We also provide UNet, ResUNet and their variations, especially the Hyperbolic blocks for ResUNet.

Install

pip install leibniz

How to use

Physics-informed

As an example we solve a very simple advection problem, a box-shaped material transported by a constant steady wind.

moving box

import torch as th
import leibniz as lbnz

from leibniz.core3d.gridsys.regular3 import RegularGrid
from leibniz.diffeq import odeint as odeint


def binary(tensor):
    return th.where(tensor > lbnz.zero, lbnz.one, lbnz.zero)

# setup grid system
lbnz.bind(RegularGrid(
    basis='x,y,z',
    W=51, L=151, H=51,
    east=16.0, west=1.0,
    north=6.0, south=1.0,
    upper=6.0, lower=1.0
))
lbnz.use('x,y,z') # use xyz coordinate

# giving a material field as a box 
fld = binary((lbnz.x - 8) * (9 - lbnz.x)) * \
      binary((lbnz.y - 3) * (4 - lbnz.y)) * \
      binary((lbnz.z - 3) * (4 - lbnz.z))

# construct a constant steady wind
wind = lbnz.one, lbnz.zero, lbnz.zero

# transport value by wind
def derivitive(t, clouds):
    return - lbnz.upwind(wind, clouds)

# integrate the system with rk4
pred = odeint(derivitive, fld, th.arange(0, 7, 1 / 100), method='rk4')

UNet, ResUNet and variations

from leibniz.unet.base import UNet
from leibniz.unet.hyperbolic import HyperBottleneck
from leibniz.nn.activation import CappingRelu


unet = UNet(6, 1, normalizor='batch', spatial=(32, 64), layers=5, ratio=1,
            vblks=[4, 4, 4, 4, 4], hblks=[1, 1, 1, 1, 1],
            scales=[-1, -1, -1, -1, -1], factors=[1, 1, 1, 1, 1],
            block=HyperBottleneck, relu=CappingRelu(), final_normalized=False)

We provide a ResUNet implementation, which is a UNet variation can insert ResNet blocks between layers. The supported ResNet blocks are include

  • Pure ResNet: Basic, Bottleneck block
  • SENet variations: Basic, Bottleneck block
  • Hyperbolic variations: Basic, Bottleneck block

We support 1d, 2d, 3d UNet.

normalizor are include:

  • batch: BatchNorm
  • layer: LayerNorm
  • instance: InstanceNorm

Other hyperparameters are include:

  • spatial: the sizes of the spatial dimentions
  • ratio: the ratio to decide the intial number of channels into the UNet
  • vblks: how many vertical blocks is inserted between two layers
  • hblks: how many horizontal blocks is inserted in the skip connections
  • scales: scale factors(power-2-based) on the spatial dimentions
  • factors: expand or shrink factors(power-2-based) on the channels
  • final_normalized: wheather to scale to final result between 0 to 1

Piecewise Linear normalizor

Piecewise Linear normalizor provide an learnable monotonic peicewise linear functions and its inverse fucntion. The API is shown as below

from leibniz.nn.normalizor import PWLNormalizor

# on 3 channels, given 128 segmented pieces, and assuming the input data have a zero mean and 1.0 std
pwln = PWLNormalizor(3, 128, mean=0.0, std=1.0)

normed = pwln(input)
output = pwln.inverse(normed)

How to release

python3 setup.py sdist bdist_wheel
python3 -m twine upload dist/*

git tag va.b.c master
git push origin va.b.c

Contributors

Acknowledge

We included source code with minor changes from torchdiffeq by Ricky Chen, because of two purpose:

  1. package torchdiffeq is not indexed by pypi
  2. package torchdiffeq is very convenient and mandatory

All our contribution is based on Ricky's Neural ODE paper (NIPS 2018) and his package.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

leibniz-0.0.49.tar.gz (39.5 kB view details)

Uploaded Source

Built Distribution

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

leibniz-0.0.49-py2.py3-none-any.whl (55.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file leibniz-0.0.49.tar.gz.

File metadata

  • Download URL: leibniz-0.0.49.tar.gz
  • Upload date:
  • Size: 39.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.4

File hashes

Hashes for leibniz-0.0.49.tar.gz
Algorithm Hash digest
SHA256 1482429e42c9799ef5bf2c18b4cfa9070200ad2da290f82a85cd15d2d7cd74c9
MD5 ce6b1e3491c0f5bec45da8183c741bee
BLAKE2b-256 305e7c9700ce6ae97f0a1f9b9a1ae283b88762eadcc13c902443132a5bbb8195

See more details on using hashes here.

File details

Details for the file leibniz-0.0.49-py2.py3-none-any.whl.

File metadata

  • Download URL: leibniz-0.0.49-py2.py3-none-any.whl
  • Upload date:
  • Size: 55.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.4

File hashes

Hashes for leibniz-0.0.49-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 049d0ab0a391e829be76666431ea20845b505a0c27d7c01cf0884f3dd51bbc9d
MD5 989dde4511c80909c2bc380d11f685de
BLAKE2b-256 cf5d0ddb56ff5fc8bd2acb5a96fe193ae8c4d69a0b78c7b55aef487de9d4c4a7

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