Skip to main content

An Open Source Machine Learning Library for Training Binarized Neural Networks

Project description

logo

Codecov PyPI - Python Version PyPI PyPI - License DOI Code style: black Binder Join the community on Spectrum

Larq is an open-source deep learning library for training neural networks with extremely low precision weights and activations, such as Binarized Neural Networks (BNNs).

Existing deep neural networks use 32 bits, 16 bits or 8 bits to encode each weight and activation, making them large, slow and power-hungry. This prohibits many applications in resource-constrained environments. Larq is the first step towards solving this. It is designed to provide an easy to use, composable way to train BNNs (1 bit) and other types of Quantized Neural Networks (QNNs) and is based on the tf.keras interface.

Larq is part of a family of libraries for BNN development; you can also check out Larq Zoo for pretrained models and Larq Compute Engine for deployment on mobile and edge devices.

Getting Started

To build a QNN, Larq introduces the concept of quantized layers and quantizers. A quantizer defines the way of transforming a full precision input to a quantized output and the pseudo-gradient method used for the backwards pass. Each quantized layer requires an input_quantizer and a kernel_quantizer that describe the way of quantizing the incoming activations and weights of the layer respectively. If both input_quantizer and kernel_quantizer are None the layer is equivalent to a full precision layer.

You can define a simple binarized fully-connected Keras model using the Straight-Through Estimator the following way:

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    larq.layers.QuantDense(512,
                           kernel_quantizer="ste_sign",
                           kernel_constraint="weight_clip"),
    larq.layers.QuantDense(10,
                           input_quantizer="ste_sign",
                           kernel_quantizer="ste_sign",
                           kernel_constraint="weight_clip",
                           activation="softmax")])

This layer can be used inside a Keras model or with a custom training loop.

Examples

Check out our examples on how to train a Binarized Neural Network in just a few lines of code:

Installation

Before installing Larq, please install:

  • Python version 3.6, 3.7 or 3.8
  • Tensorflow version 1.14, 1.15, 2.0, 2.1 or 2.2:
    pip install tensorflow  # or tensorflow-gpu
    

You can install Larq with Python's pip package manager:

pip install larq

About

Larq is being developed by a team of deep learning researchers and engineers at Plumerai to help accelerate both our own research and the general adoption of Binarized Neural Networks.

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

larq-0.9.6.tar.gz (47.7 kB view details)

Uploaded Source

Built Distribution

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

larq-0.9.6-py3-none-any.whl (59.6 kB view details)

Uploaded Python 3

File details

Details for the file larq-0.9.6.tar.gz.

File metadata

  • Download URL: larq-0.9.6.tar.gz
  • Upload date:
  • Size: 47.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for larq-0.9.6.tar.gz
Algorithm Hash digest
SHA256 bbf75a1536ea37b84585ae4a295410bb81d1f44ee2d02b52d9eb4082efd1c786
MD5 0388b98e9d754f36f43eb09168f132f3
BLAKE2b-256 b3246485443af878f1d94fb93ac720436f22fc4fbe47618395f45c41d8164e0b

See more details on using hashes here.

File details

Details for the file larq-0.9.6-py3-none-any.whl.

File metadata

  • Download URL: larq-0.9.6-py3-none-any.whl
  • Upload date:
  • Size: 59.6 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/46.3.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for larq-0.9.6-py3-none-any.whl
Algorithm Hash digest
SHA256 2f0f359e91131c613df62571c4b314a36f57d4d32783d47157e1557fedebd911
MD5 9253c90d5fdef30473d836205d167f7d
BLAKE2b-256 9fb6076a715f5385dd22848099463ce7768c8b27adaff2ce93cb0c222957465c

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