tfmot.quantization.keras.quantize_apply
Stay organized with collections
Save and categorize content based on your preferences.
Quantize a tf.keras
model that has been annotated for quantization.
tfmot.quantization.keras.quantize_apply(
model,
scheme=default_8bit_quantize_scheme.Default8BitQuantizeScheme()
,
quantized_layer_name_prefix='quant_'
)
Used in the notebooks
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
This function takes a tf.keras
model in which the desired layers for
quantization have already been annotated. See quantize_annotate_model
and quantize_annotate_layer
.
Example:
model = keras.Sequential([
layers.Dense(10, activation='relu', input_shape=(100,)),
quantize_annotate_layer(layers.Dense(2, activation='sigmoid'))
])
# Only the second Dense layer is quantized.
quantized_model = quantize_apply(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.
Args |
model
|
A tf.keras Sequential or Functional model which has been annotated
with quantize_annotate . It can have pre-trained weights.
|
scheme
|
A QuantizeScheme which specifies transformer and quantization
registry. The default is Default8BitQuantizeScheme() .
|
quantized_layer_name_prefix
|
A name prefix for quantized layers. The default
is quant_ .
|
Returns |
Returns a new tf.keras model in which the annotated layers have been
prepared for quantization.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-05-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-05-26 UTC."],[],[],null,["# tfmot.quantization.keras.quantize_apply\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/model-optimization/blob/v0.7.5/tensorflow_model_optimization/python/core/quantization/keras/quantize.py#L288-L497) |\n\nQuantize a [`tf.keras`](https://www.tensorflow.org/api_docs/python/tf/keras) model that has been annotated for quantization. \n\n tfmot.quantization.keras.quantize_apply(\n model,\n scheme=../../../tfmot/quantization/keras/default_8bit/Default8BitQuantizeScheme,\n quantized_layer_name_prefix='quant_'\n )\n\n### Used in the notebooks\n\n| Used in the guide |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [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) |\n\nQuantization constructs a model which emulates quantization during training.\nThis allows the model to learn parameters robust to quantization loss, and\nalso model the accuracy of a quantized model.\n\nFor more information, see\n\u003chttps://www.tensorflow.org/model_optimization/guide/quantization/training\u003e\n\nThis function takes a [`tf.keras`](https://www.tensorflow.org/api_docs/python/tf/keras) model in which the desired layers for\nquantization have already been annotated. See `quantize_annotate_model`\nand `quantize_annotate_layer`.\n\n#### Example:\n\n model = keras.Sequential([\n layers.Dense(10, activation='relu', input_shape=(100,)),\n quantize_annotate_layer(layers.Dense(2, activation='sigmoid'))\n ])\n\n # Only the second Dense layer is quantized.\n quantized_model = quantize_apply(model)\n\nNote that this function removes the optimizer from the original model.\n\nThe returned model copies over weights from the original model. So while\nit preserves the original weights, training it will not modify the weights\nof the original model.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `model` | A [`tf.keras`](https://www.tensorflow.org/api_docs/python/tf/keras) Sequential or Functional model which has been annotated with `quantize_annotate`. It can have pre-trained weights. |\n| `scheme` | A `QuantizeScheme` which specifies transformer and quantization registry. The default is `Default8BitQuantizeScheme()`. |\n| `quantized_layer_name_prefix` | A name prefix for quantized layers. The default is `quant_`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| Returns a new [`tf.keras`](https://www.tensorflow.org/api_docs/python/tf/keras) model in which the annotated layers have been prepared for quantization. ||\n\n\u003cbr /\u003e"]]