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

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. Note that efficient inference using a trained BNN requires the use of an optimized inference engine; we provide these for several platforms in Larq Compute Engine.

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.7, 3.8, 3.9, or 3.10
  • Tensorflow version 1.14, 1.15, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, or 2.10:
    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.

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.13.3.tar.gz (58.4 kB view details)

Uploaded Source

Built Distribution

larq-0.13.3-py3-none-any.whl (66.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: larq-0.13.3.tar.gz
  • Upload date:
  • Size: 58.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for larq-0.13.3.tar.gz
Algorithm Hash digest
SHA256 d0f594c4f75c0efc4e54e39650cf8ed559b1af66a48b4d4fcdca38a05fe44a35
MD5 5dc9548b7039c9bdc428d432122019df
BLAKE2b-256 822fca60a6d6c36bb1da390c4b2cd89bd9f05e6bd85a8fed0634d100d6220f99

See more details on using hashes here.

File details

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

File metadata

  • Download URL: larq-0.13.3-py3-none-any.whl
  • Upload date:
  • Size: 66.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for larq-0.13.3-py3-none-any.whl
Algorithm Hash digest
SHA256 400b1381a98fe51db04678299cee1296a16afcf6c24c6445b0a5cf74d9850d03
MD5 f0ac0e38ca4a4f2d31531b617c99941a
BLAKE2b-256 eea773f3fcfea3b618f755dcd621d43073a3367428e9d16b7b6e0c34d5307c73

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page