All Projects → IvanYashchuk → Jax Fenics Adjoint

IvanYashchuk / Jax Fenics Adjoint

Licence: mit
Differentiable interface to FEniCS for JAX using dolfin-adjoint/pyadjoint

Projects that are alternatives of or similar to Jax Fenics Adjoint

Pygotham2018 graphmining
Large-scale Graph Mining with Spark
Stars: ✭ 31 (-3.12%)
Mutual labels:  jupyter-notebook
Data Science Poker Projects
Stars: ✭ 31 (-3.12%)
Mutual labels:  jupyter-notebook
Lstmvis
Visualization Toolbox for Long Short Term Memory networks (LSTMs)
Stars: ✭ 959 (+2896.88%)
Mutual labels:  jupyter-notebook
Going Dutch
Using Data Science and Machine Learning to find an apartment in Amsterdam.
Stars: ✭ 31 (-3.12%)
Mutual labels:  jupyter-notebook
Testrepo
Stars: ✭ 31 (-3.12%)
Mutual labels:  jupyter-notebook
Betalyzer
A fintech tutorial using Python
Stars: ✭ 31 (-3.12%)
Mutual labels:  jupyter-notebook
Crnn Pytorch
✍️ Convolutional Recurrent Neural Network in Pytorch | Text Recognition
Stars: ✭ 31 (-3.12%)
Mutual labels:  jupyter-notebook
Medium blog
Stars: ✭ 30 (-6.25%)
Mutual labels:  jupyter-notebook
Odsc neural nets 11 04 17
Talk "Deep Learning From Scratch Using Python" delivered at ODSC West on November 4, 2017
Stars: ✭ 31 (-3.12%)
Mutual labels:  jupyter-notebook
Imgcluster
Image clustering using the similarity algorithms: SIFT, SSIM, CW-SSIM, MSE
Stars: ✭ 31 (-3.12%)
Mutual labels:  jupyter-notebook
Gans
Various GANs for playing around
Stars: ✭ 31 (-3.12%)
Mutual labels:  jupyter-notebook
Synthetic Medical Images
Synthetic Medical Images from Dual Generative Adversarial Networks
Stars: ✭ 31 (-3.12%)
Mutual labels:  jupyter-notebook
Inception Resnet V2
Implementation of Google's Inception + ResNet v2 architecture in Keras
Stars: ✭ 31 (-3.12%)
Mutual labels:  jupyter-notebook
Bigdataanalytics
MET CS 777 Big Data Analytics
Stars: ✭ 31 (-3.12%)
Mutual labels:  jupyter-notebook
Discogan Pytorch
PyTorch implementation of "Learning to Discover Cross-Domain Relations with Generative Adversarial Networks"
Stars: ✭ 961 (+2903.13%)
Mutual labels:  jupyter-notebook
Signdetect Face
Stars: ✭ 31 (-3.12%)
Mutual labels:  jupyter-notebook
Asrgen
Attacking Speaker Recognition with Deep Generative Models
Stars: ✭ 31 (-3.12%)
Mutual labels:  jupyter-notebook
Deeplabv3 Plus
Tensorflow 2.3.0 implementation of DeepLabV3-Plus
Stars: ✭ 32 (+0%)
Mutual labels:  jupyter-notebook
2015 Python
(Winter 2015) Python for Data Mining mini-course.
Stars: ✭ 31 (-3.12%)
Mutual labels:  jupyter-notebook
Notebooks
Stars: ✭ 31 (-3.12%)
Mutual labels:  jupyter-notebook

jax-fenics-adjoint · Build FEniCS Build Firedrake Coverage Status

This package enables use of FEniCS or Firedrake for solving differentiable variational problems in JAX.

Automatic tangent linear and adjoint solvers for FEniCS/Firedrake programs are derived with dolfin-adjoint/pyadjoint. These solvers make it possible to use JAX's forward and reverse Automatic Differentiation with FEniCS/Firedrake.

For using JAX-FEniCS without dolfin-adjoint (still differentiable with automatic tangent and adjoint solvers using UFL) check out jax-fenics.

Current limitations:

  • Composition of forward and reverse modes for higher-order derivatives is not implemented yet.
  • Differentiation wrt Dirichlet boundary conditions and mesh coordinates is not implemented yet.

Example

Here is the demonstration of solving the Poisson's PDE on 2D square domain and calculating the solution Jacobian matrix (du/df) using the reverse (adjoint) mode Automatic Differentiation.

import jax
import jax.numpy as np
from jax.config import config
config.update("jax_enable_x64", True)

import fenics
import fenics_adjoint
import ufl

from jaxfenics_adjoint import build_jax_fem_eval
from fecr import from_numpy

# Create mesh for the unit square domain
n = 10
mesh = fenics_adjoint.UnitSquareMesh(n, n)

# Define discrete function spaces and functions
V = fenics.FunctionSpace(mesh, "CG", 1)
W = fenics.FunctionSpace(mesh, "DG", 0)

# Define FEniCS template representation of JAX input
templates = (fenics_adjoint.Function(W),)

@build_jax_fem_eval(templates)
def fenics_solve(f):
    # This function inside should be traceable by fenics_adjoint
    u = fenics_adjoint.Function(V, name="PDE Solution")
    v = fenics.TestFunction(V)
    inner, grad, dx = ufl.inner, ufl.grad, ufl.dx
    F = (inner(grad(u), grad(v)) - f * v) * dx
    bcs = [fenics_adjoint.DirichletBC(V, 0.0, "on_boundary")]
    fenics_adjoint.solve(F == 0, u, bcs)
    return u

# build_jax_fem_eval is a wrapper decorator that registers `fenics_solve` for JAX

# Let's create a vector of ones with size equal to the number of cells in the mesh
f = np.ones(W.dim())
u = fenics_solve(f) # u is JAX's array
u_fenics = from_numpy(u, fenics.Function(V)) # we need to explicitly provide template function for conversion

# now we can calculate vector-Jacobian product with `jax.vjp`
jvp_result = jax.vjp(fenics_solve, f)[1](np.ones_like(u))

# or the full (dense) Jacobian matrix du/df with `jax.jacrev`
dudf = jax.jacrev(fenics_solve)(f)

# function `fenics_solve` maps R^200 (dimension of W) to R^121 (dimension of V)
# therefore the Jacobian matrix dimension is dim V x dim W
assert dudf.shape == (V.dim(), W.dim())

Check examples/ or tests/ folders for the additional examples.

Installation

First install FEniCS or Firedrake. Then install pyadjoint with:

python -m pip install git+https://github.com/dolfin-adjoint/[email protected]

Then install fecr with:

python -m pip install git+https://github.com/IvanYashchuk/[email protected]

Then install JAX with:

python -m pip install --upgrade jax jaxlib  # CPU-only version

After that install jax-fenics-adjoint with:

python -m pip install git+https://github.com/IvanYashchuk/[email protected]

Reporting bugs

If you found a bug, create an issue.

Asking questions and general discussion

If you have a question or anything else, create a new discussion. Using issues is also fine!

Contributing

Pull requests are welcome from everyone.

Fork, then clone the repository:

git clone https://github.com/IvanYashchuk/jax-fenics-adjoint.git

Make your change. Add tests for your change. Make the tests pass:

pytest tests/fenics  # or pytest tests/firedrake

Check the formatting with black and flake8. Push to your fork and submit a pull request.

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].