<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/model-optimization/blob/v0.8.1/tensorflow_model_optimization/python/core/quantization/keras/quantizers.py#L33-L125) |

ABC interface which encapsulates the logic of how to quantize tensors.

### Used in the notebooks

| Used in the guide |
|---|
| - [Quantization aware training comprehensive guide](https://www.tensorflow.org/model_optimization/guide/quantization/training_comprehensive_guide) |

This is an experimental API not subject to backward compatibility.

A `Quantizer` is used by the library code to apply the mathematical
transformations which actually quantize a tensor, hence allowing the user
precise control over the algorithm with which tensors are quantized. When used
in conjunction with `QuantizeConfig` it controls how a layer is quantized.

Create a custom quantizer:

    class FixedRangeQuantizer(Quantizer):
      # Example quantizer which clips tensors in a fixed range.

      def build(self, tensor_shape, name, layer):
        range_var = layer.add_weight(
          name + '_range',
          initializer=keras.initializers.Constant(6.0),
          trainable=False)

        return {
          'range_var': range_var,
        }

      def __call__(self, inputs, training, weights, **kwargs):
        return keras.backend.clip(
            inputs, 0.0, weights['range_var'])

      def get_config(self):
        # Not needed. No __init__ parameters to serialize.
        return {}

For a full example, see
<https://www.tensorflow.org/model_optimization/guide/quantization/training_comprehensive_guide.md>

## Methods

### `build`

[View source](https://github.com/tensorflow/model-optimization/blob/v0.8.1/tensorflow_model_optimization/python/core/quantization/keras/quantizers.py#L73-L90)

    @abc.abstractmethod
    build(
        tensor_shape, name, layer
    )

Construct the weights required by the quantizer.

A quantizer may need to construct variables to hold the state for its
algorithm. This function is invoked during the `build` stage of the layer
that the quantizer is used for. Any variables constructed are under the
scope of the `layer` and serialized as part of the layer.

<br />

<br />

<br />

| Args ||
|---|---|
| `tensor_shape` | Shape of tensor which needs to be quantized. |
| `name` | Name of tensor. |
| `layer` | Keras layer which is quantizing the tensors. The layer is needed to construct the weights, and is also the owner of the weights. |

<br />

Returns: Dictionary of constructed weights. This dictionary will be
passed to the quantizer's **call** function as a `weights` dictionary.

### `from_config`

[View source](https://github.com/tensorflow/model-optimization/blob/v0.8.1/tensorflow_model_optimization/python/core/quantization/keras/quantizers.py#L115-L125)

    @classmethod
    from_config(
        config
    )

Instantiates a `Quantizer` from its config.

<br />

<br />

<br />

| Args ||
|---|---|
| `config` | Output of `get_config()`. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A `Quantizer` instance. ||

<br />

### `get_config`

[View source](https://github.com/tensorflow/model-optimization/blob/v0.8.1/tensorflow_model_optimization/python/core/quantization/keras/quantizers.py#L110-L113)

    @abc.abstractmethod
    get_config()

Returns the config used to serialize the `Quantizer`.

### `__call__`

[View source](https://github.com/tensorflow/model-optimization/blob/v0.8.1/tensorflow_model_optimization/python/core/quantization/keras/quantizers.py#L92-L108)

    @abc.abstractmethod
    __call__(
        inputs, training, weights, **kwargs
    )

Apply quantization to the input tensor.

This is the main function of the `Quantizer` which implements the core logic
to quantize the tensor. It is invoked during the `call` stage of the layer,
and allows modifying the tensors used in graph construction.

<br />

<br />

<br />

| Args ||
|---|---|
| `inputs` | Input tensor to be quantized. |
| `training` | Whether the graph is currently training. |
| `weights` | Dictionary of weights the quantizer can use to quantize the tensor. This contains the weights created in the `build` function. |
| `**kwargs` | Additional variables which may be passed to the quantizer. |

<br />

Returns: quantized tensor.