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 or 3.7
  • Tensorflow version 1.14, 1.15, 2.0.0, or 2.1.0:
    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.1.tar.gz (46.8 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.1-py3-none-any.whl (58.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: larq-0.9.1.tar.gz
  • Upload date:
  • Size: 46.8 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.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.7

File hashes

Hashes for larq-0.9.1.tar.gz
Algorithm Hash digest
SHA256 67b3ae8c7ab0b715896494dc324e03ef7cfc3776006e8cd0577b7db30e430145
MD5 288b678819cc7b92fbad512ea17b3630
BLAKE2b-256 d5dd768913d2bdd539092f969c5a79cfa6d76b858df2bdf144d52c0e841f81bf

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for larq-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 51d180c76032353b7185e0c42bcbe4bf2909ecd8916a01c95f1c459ae6d54483
MD5 f6a9cc5920db6cf868f78e5025206891
BLAKE2b-256 1ae01b8dc6da13bce6b68134fd6e492053af8795c827834d594f9e0cc43a4a5a

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