Leibniz
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.
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 import UNet
from leibniz.nn.layer.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:
- package torchdiffeq is not indexed by pypi
- package torchdiffeq is very convenient and mandatory
All our contribution is based on Ricky's Neural ODE paper (NIPS 2018) and his package.
Metadata
Release files for leibniz 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| leibniz-0.2.0.tar.gz | 60.3 kB | Without provenance | Details for leibniz-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| leibniz-0.2.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for leibniz-0.2.0-py2.py3-none-any.whl |
Total release size: 137.1 kB
Release files / leibniz-0.2.0.tar.gz
| Download URL | leibniz-0.2.0.tar.gz |
|---|---|
| Size | 60.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c1ab105030c663b14e9e0e9ff129be8513cd518569b1077cf98e2142058ddb99
|
|
BLAKE2b-256 checksum How to use checksums |
67c8e9af4befdd028eea6307584ab65c190ed8a88df2ce2bd535b6eb8a49f968
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.0
|
Release files / leibniz-0.2.0-py2.py3-none-any.whl
| Download URL | leibniz-0.2.0-py2.py3-none-any.whl |
|---|---|
| Size | 76.8 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
3e87cf9b7e09e0ed4d3a69eb5d50b42b070d04a08bd819ee75dfe60814f69608
|
|
BLAKE2b-256 checksum How to use checksums |
95853c4c285f7222e2252bb128b71dfddcc01b99e8c70f50edacaecea2332f0c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.0
|
Security for leibniz 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.