larq.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.

Quantizers can either be used through quantizer arguments that are supported for Larq layers, such as input_quantizer and kernel_quantizer; or they can be used similar to activations, i.e. either through an Activation layer, or through the activation argument supported by all forward layer:

import tensorflow as tf
import larq as lq
...
x = lq.layers.QuantDense(64, activation=None)(x)
x = lq.layers.QuantDense(64, input_quantizer="ste_sign")(x)

is equivalent to:

x = lq.layers.QuantDense(64)(x)
x = tf.keras.layers.Activation("ste_sign")(x)
x = lq.layers.QuantDense(64)(x)

as well as:

x = lq.layers.QuantDense(64, activation="ste_sign")(x)
x = lq.layers.QuantDense(64)(x)

We highly recommend using the first of these formulations: for the other two formulations, intermediate layers - like batch normalization or average pooling - and shortcut connections may result in non-binary input to the convolutions.

Quantizers can either be referenced by string or called directly. The following usages are equivalent:

lq.layers.QuantDense(64, kernel_quantizer="ste_sign")
lq.layers.QuantDense(64, kernel_quantizer=lq.quantizers.ste_sign)
lq.layers.QuantDense(64, kernel_quantizer=lq.quantizers.SteSign(clip_value=1.0))

ste_sign

ste_sign(x, clip_value=1.0)
Sign binarization function.

\[ q(x) = \begin{cases} -1 & x < 0 \\ 1 & x \geq 0 \end{cases} \]

The gradient is estimated using the Straight-Through Estimator (essentially the binarization is replaced by a clipped identity on the backward pass). \[\frac{\partial q(x)}{\partial x} = \begin{cases} 1 & \left|x\right| \leq \texttt{clip_value} \\ 0 & \left|x\right| > \texttt{clip_value} \end{cases}\]

Arguments

  • x: Input tensor.
  • clip_value: Threshold for clipping gradients. If None gradients are not clipped.

Returns

Binarized tensor.

References

SteSign

SteSign(clip_value=1.0)
Instantiates a serializable binary quantizer.

\[ q(x) = \begin{cases} -1 & x < 0 \\ 1 & x \geq 0 \end{cases} \]

The gradient is estimated using the Straight-Through Estimator (essentially the binarization is replaced by a clipped identity on the backward pass). \[\frac{\partial q(x)}{\partial x} = \begin{cases} 1 & \left|x\right| \leq \texttt{clip_value} \\ 0 & \left|x\right| > \texttt{clip_value} \end{cases}\]

Arguments

  • clip_value: Threshold for clipping gradients. If None gradients are not clipped.

References

magnitude_aware_sign

magnitude_aware_sign(x, clip_value=1.0)

Magnitude-aware sign for Bi-Real Net.

Arguments

  • x: Input tensor
  • clip_value: Threshold for clipping gradients. If None gradients are not clipped.

Returns

Scaled binarized tensor (with values in \(\{-a, a\}\), where a is a float).

References

MagnitudeAwareSign

MagnitudeAwareSign(clip_value=1.0)
Instantiates a serializable magnitude-aware sign quantizer for Bi-Real Net.

Arguments

  • clip_value: Threshold for clipping gradients. If None gradients are not clipped.

References

approx_sign

approx_sign(x)

Sign binarization function. \[ q(x) = \begin{cases} -1 & x < 0 \\ 1 & x \geq 0 \end{cases} \]

The gradient is estimated using the ApproxSign method. \[\frac{\partial q(x)}{\partial x} = \begin{cases} (2 - 2 \left|x\right|) & \left|x\right| \leq 1 \\ 0 & \left|x\right| > 1 \end{cases} \]

Arguments

  • x: Input tensor.

Returns

Binarized tensor.

References

swish_sign

swish_sign(x, beta=5.0)
Sign binarization function.

The gradient is estimated using the SignSwish method.

Arguments

  • x: Input tensor.
  • beta: Larger values result in a closer approximation to the derivative of the sign.

Returns

Binarized tensor.

References

SwishSign

SwishSign(beta=5.0)
Sign binarization function.

The gradient is estimated using the SignSwish method.

Arguments

  • beta: Larger values result in a closer approximation to the derivative of the sign.

Returns

SwishSign quantization function

References

ste_tern

ste_tern(x,
         threshold_value=0.05,
         ternary_weight_networks=False,
         clip_value=1.0)
Ternarization function.

\[ q(x) = \begin{cases} +1 & x > \Delta \\ 0 & |x| < \Delta \\ -1 & x < - \Delta \end{cases} \]

where \Delta is defined as the threshold and can be passed as an argument, or can be calculated as per the Ternary Weight Networks original paper, such that

\[ \Delta = \frac{0.7}{n} \sum_{i=1}^{n} |W_i| \] where we assume that W_i is generated from a normal distribution.

The gradient is estimated using the Straight-Through Estimator (essentially the Ternarization is replaced by a clipped identity on the backward pass). \[\frac{\partial q(x)}{\partial x} = \begin{cases} 1 & \left|x\right| \leq \texttt{clip_value} \\ 0 & \left|x\right| > \texttt{clip_value} \end{cases}\]

Arguments

  • x: Input tensor.
  • threshold_value: The value for the threshold, \Delta.
  • ternary_weight_networks: Boolean of whether to use the Ternary Weight Networks threshold calculation.
  • clip_value: Threshold for clipping gradients. If None gradients are not clipped.

Returns

Ternarized tensor.

References

SteTern

SteTern(threshold_value=0.05, ternary_weight_networks=False, clip_value=1.0)
Instantiates a serializable ternarization quantizer.

\[ q(x) = \begin{cases} +1 & x > \Delta \\ 0 & |x| < \Delta \\ -1 & x < - \Delta \end{cases} \]

where \Delta is defined as the threshold and can be passed as an argument, or can be calculated as per the Ternary Weight Networks original paper, such that

\[ \Delta = \frac{0.7}{n} \sum_{i=1}^{n} |W_i| \] where we assume that W_i is generated from a normal distribution.

The gradient is estimated using the Straight-Through Estimator (essentially the Ternarization is replaced by a clipped identity on the backward pass). \[\frac{\partial q(x)}{\partial x} = \begin{cases} 1 & \left|x\right| \leq \texttt{clip_value} \\ 0 & \left|x\right| > \texttt{clip_value} \end{cases}\]

Arguments

  • threshold_value: The value for the threshold, \Delta.
  • ternary_weight_networks: Boolean of whether to use the Ternary Weight Networks threshold calculation.
  • clip_value: Threshold for clipping gradients. If None gradients are not clipped.

References

SteHeaviside

SteHeaviside(clip_value=1.0)

Instantiates a binarization quantizer with output values 0 and 1. \[ q(x) = \begin{cases} +1 & x > 0 \\ 0 & x \leq 0 \end{cases} \]

The gradient is estimated using the Straight-Through Estimator (essentially the binarization is replaced by a clipped identity on the backward pass).

\[\frac{\partial q(x)}{\partial x} = \begin{cases} 1 & \left|x\right| \leq 1 \\ 0 & \left|x\right| > 1 \end{cases}\]

Arguments

  • clip_value: Threshold for clipping gradients. If None gradients are not clipped.

Returns

AND Binarization function

ste_heaviside

ste_heaviside(x, clip_value=1.0)

Binarization function with output values 0 and 1.

\[ q(x) = \begin{cases} +1 & x > 0 \\ 0 & x \leq 0 \end{cases} \]

The gradient is estimated using the Straight-Through Estimator (essentially the binarization is replaced by a clipped identity on the backward pass).

\[\frac{\partial q(x)}{\partial x} = \begin{cases} 1 & \left|x\right| \leq 1 \\ 0 & \left|x\right| > 1 \end{cases}\]

Arguments

  • x: Input tensor.
  • clip_value: Threshold for clipping gradients. If None gradients are not clipped.

Returns

AND-binarized tensor.

dorefa_quantizer

dorefa_quantizer(x, k_bit=2)
k_bit quantizer as in the DoReFa paper.

\[ q(x) = \begin{cases} 0 & x < \frac{1}{2n} \\ \frac{i}{n} & \frac{2i-1}{2n} < |x| < \frac{2i+1}{2n} \text{ for } i \in \{1,n-1\}\\ 1 & \frac{2n-1}{2n} < x \end{cases} \]

where \(n = 2^{\text{k_bit}} - 1\). The number of bits, k_bit, needs to be passed as an argument. The gradient is estimated using the Straight-Through Estimator (essentially the binarization is replaced by a clipped identity on the backward pass). \[\frac{\partial q(x)}{\partial x} = \begin{cases} 1 & 0 \leq x \leq 1 \\ 0 & \text{else} \end{cases}\]

Arguments

  • k_bit: number of bits for the quantization.

Returns

quantized tensor

References

DoReFaQuantizer

DoReFaQuantizer(k_bit)
Instantiates a serializable k_bit quantizer as in the DoReFa paper.

\[ q(x) = \begin{cases} 0 & x < \frac{1}{2n} \\ \frac{i}{n} & \frac{2i-1}{2n} < |x| < \frac{2i+1}{2n} \text{ for } i \in \{1,n-1\}\\ 1 & \frac{2n-1}{2n} < x \end{cases} \]

where \(n = 2^{\text{k_bit}} - 1\). The number of bits, k_bit, needs to be passed as an argument. The gradient is estimated using the Straight-Through Estimator (essentially the binarization is replaced by a clipped identity on the backward pass). \[\frac{\partial q(x)}{\partial x} = \begin{cases} 1 & 0 \leq x \leq 1 \\ 0 & \text{else} \end{cases}\]

Arguments

  • k_bit: number of bits for the quantization.

Returns

Quantization function

References