Skip to main content

An Open Source Machine Learning Library for Training Binarized Neural Networks

Project description


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([

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


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


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


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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for larq, version 0.9.6
Filename, size File type Python version Upload date Hashes
Filename, size larq-0.9.6-py3-none-any.whl (59.6 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size larq-0.9.6.tar.gz (47.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page