Skip to main content

Flax: A neural network library for JAX designed for flexibility

Project description

Flax: A neural network library for JAX designed for flexibility

NOTE: This is alpha software, but we encourage trying it out. Changes will come to the API, but we'll use deprecation warnings when we can, and keep track of them our Changelog.

A growing community of researchers at Google are happily using Flax for their daily research and contributing to it, and now we're expanding that to the open source community.

GitHub issues are encouraged for open conversation, but in case you need to reach us directly, we're at flax-dev@google.com.

Quickstart

Full documentation and API reference

Annotated full end-to-end MNIST example

The Flax Guide -- a guided walkthrough of the parts of Flax

Background: JAX

JAX is NumPy + autodiff + GPU/TPU

It allows for fast scientific computing and machine learning with the normal NumPy API (+ additional APIs for special accelerator ops when needed)

JAX comes with powerful primitives, which you can compose arbitrarily:

  • Autodiff (jax.grad): Efficient any-order gradients w.r.t any variables
  • JIT compilation (jax.jit): Trace any function ⟶ fused accelerator ops
  • Vectorization (jax.vmap): Automatically batch code written for individual samples
  • Parallelization (jax.pmap): Automatically parallelize code across multiple accelerators (including across hosts, e.g. for TPU pods)

What is Flax?

Flax is a high-performance neural network library for JAX that is designed for flexibility: Try new forms of training by forking an example and by modifying the training loop, not by adding features to a framework.

Flax is being developed in close collaboration with the JAX team and comes with everything you need to start your research, including:

  • Common layers (flax.nn): Dense, Conv, {Batch|Layer|Group} Norm, Attention, Pooling, {LSTM|GRU} Cell, Dropout

  • Optimizers (flax.optim): SGD, Momentum, Adam, LARS

  • Utilities and patterns: replicated training, serialization and checkpointing, metrics, prefetching on device

  • Educational examples that work out of the box: MNIST, LSTM seq2seq, Graph Neural Networks, Sequence Tagging

  • HOWTO guides -- diffs that add functionality to educational base exampless

  • Fast, tuned large-scale end-to-end examples: CIFAR10, ResNet ImageNet, Transformer LM1b

An annotated MNIST example

See docs/annotated_mnist.md for an MNIST example with detailed annotations for each code block.

Flax Modules

The core of Flax is the Module abstraction. Modules allow you to write parameterized functions just as if you were writing a normal numpy function with JAX. The Module api allows you to declare parameters and use them directly with the JAX api’s.

Modules are the one part of Flax with "magic" -- the magic is constrained, and enables a very ergonomic style, where modules are defined in a single function with minimal boilerplate.

A few things to know about Modules:

  1. Create a new module by subclassing flax.nn.Module and implementing the apply method.

  2. Within apply, call self.param(name, shape, init_func) to register a new parameter and returns its initial value.

  3. Apply submodules by calling MySubModule(...args...) within MyModule.apply. Parameters of MySubModule are stored as a dictionary under the parameters MyModule. NOTE: this returns the output of MySubModule, not an instance. To get an access to an instance of MySubModule for re-use, use Module.partial or Module.shared

  4. MyModule.init(rng, ...) is a pure function that calls apply in "init mode" and returnes a nested Python dict of initialized parameter values

  5. MyModule.call(params, ...) is a pure function that calls apply in "call mode" and returnes the output of the module.

For example you can define a learned linear transformation as follows:

from flax import nn
import jax.numpy as jnp

class Linear(nn.Module):
  def apply(self, x, num_features, kernel_init_fn):
    input_features = x.shape[-1]
    W = self.param('W', (input_features, num_features), kernel_init_fn)
    return jnp.dot(x, W)

You can also use nn.module as a function decorator to create a new module, as long as you don't need access to self for creating parameters directly:

@nn.module
def DenseLayer(x, features):
  x = flax.nn.Dense(x, features)
  x = flax.nn.relu(x)
  return x

⟶ Read more about Modules in the Flax Guide

CPU-only Installation

You will need Python 3.5 or later.

Now install flax from Github:

> pip install git+https://github.com/google-research/flax.git@prerelease

GPU accelerated installation

First install jaxlib; please follow the instructions in the JAX readme. If they are not already installed, you will need to install CUDA and CuDNN runtimes.

Now install flax from Github:

> pip install git+https://github.com/google-research/flax.git@prerelease

List of end-to-end examples

NOTE: We are still testing these examples across all supported hardware configurations.

Note

This is not an official Google product.

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

flax-0.1.0rc2.tar.gz (51.3 kB view details)

Uploaded Source

Built Distribution

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

flax-0.1.0rc2-py3-none-any.whl (66.2 kB view details)

Uploaded Python 3

File details

Details for the file flax-0.1.0rc2.tar.gz.

File metadata

  • Download URL: flax-0.1.0rc2.tar.gz
  • Upload date:
  • Size: 51.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for flax-0.1.0rc2.tar.gz
Algorithm Hash digest
SHA256 3cc797122d47ed69a28f93e81a66d316c5463e156f7bf2cc3bd1ef59cdcad93a
MD5 9780d569f5086cfc42a91382ca708e64
BLAKE2b-256 9130955d6b6143c9d6b819ba85ebce654aae7a235017962fa8e8519110e5ac5f

See more details on using hashes here.

File details

Details for the file flax-0.1.0rc2-py3-none-any.whl.

File metadata

  • Download URL: flax-0.1.0rc2-py3-none-any.whl
  • Upload date:
  • Size: 66.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for flax-0.1.0rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 1d7b093b11efd7faee42262956ad3b62b7ad9c46f59671e6ba7fda5f2efb19f8
MD5 029a266dd17edafddb1a5e0b01378b2f
BLAKE2b-256 0e8f6d772aba2fa63aa6c1fd10d267324c44288c1c5705ac971c7d79cea219bb

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