<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/quantize.py#L83-L142) |

Quantize a `keras` model with the default quantization implementation.

    tfmot.quantization.keras.quantize_model(
        to_quantize, quantized_layer_name_prefix='quant_'
    )

### Used in the notebooks

| Used in the guide |
|---|
| - [Quantization aware training comprehensive guide](https://www.tensorflow.org/model_optimization/guide/quantization/training_comprehensive_guide) - [Cluster preserving quantization aware training (CQAT) Keras example](https://www.tensorflow.org/model_optimization/guide/combine/cqat_example) - [Sparsity and cluster preserving quantization aware training (PCQAT) Keras example](https://www.tensorflow.org/model_optimization/guide/combine/pcqat_example) - [Pruning preserving quantization aware training (PQAT) Keras example](https://www.tensorflow.org/model_optimization/guide/combine/pqat_example) |

Quantization constructs a model which emulates quantization during training.
This allows the model to learn parameters robust to quantization loss, and
also model the accuracy of a quantized model.

For more information, see
<https://www.tensorflow.org/model_optimization/guide/quantization/training>

#### Quantize a model:

    # Quantize sequential model
    model = quantize_model(
        keras.Sequential([
            layers.Dense(10, activation='relu', input_shape=(100,)),
            layers.Dense(2, activation='sigmoid')
        ]))

    # Quantize functional model
    in = keras.Input((3,))
    out = keras.Dense(2)(in)
    model = keras.Model(in, out)

    quantized_model = quantize_model(model)

Note that this function removes the optimizer from the original model.

The returned model copies over weights from the original model. So while
it preserves the original weights, training it will not modify the weights
of the original model.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `to_quantize` | keras model to be quantized. It can have pre-trained weights. |
| `quantized_layer_name_prefix` | Name prefix for the quantized layers. The default is `quant_`. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| Returns a new `keras` model prepared for quantization. ||

<br />