tfmot.quantization.keras.quantize_annotate_model
Stay organized with collections
Save and categorize content based on your preferences.
Annotate a tf.keras
model to be quantized.
tfmot.quantization.keras.quantize_annotate_model(
to_annotate
)
Used in the notebooks
This function does not actually quantize the model. It merely specifies
that the model needs to be quantized. quantize_apply
can then be used
to quantize the model.
This function is intended to be used in conjunction with the
quantize_annotate_layer
API. Otherwise, it is simpler to use
quantize_model
.
Annotate a model while overriding the default behavior for a layer:
quantize_config = MyDenseQuantizeConfig()
model = quantize_annotate_model(
keras.Sequential([
layers.Dense(10, activation='relu', input_shape=(100,)),
quantize_annotate_layer(
layers.Dense(2, activation='sigmoid'),
quantize_config=quantize_config)
]))
# The first Dense layer gets quantized with the default behavior,
# but the second layer uses `MyDenseQuantizeConfig` for quantization.
quantized_model = quantize_apply(model)
Note that this function removes the optimizer from the original model.
Args |
to_annotate
|
tf.keras model which needs to be quantized.
|
Returns |
New tf.keras model with each layer in the model wrapped with
QuantizeAnnotate . The new model preserves weights from the original
model.
|
Raises |
ValueError
|
if the model cannot be annotated.
|
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_annotate_model\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#L151-L232) |\n\nAnnotate a [`tf.keras`](https://www.tensorflow.org/api_docs/python/tf/keras) model to be quantized. \n\n tfmot.quantization.keras.quantize_annotate_model(\n to_annotate\n )\n\n### Used in the notebooks\n\n| Used in the guide |\n|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [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\nThis function does not actually quantize the model. It merely specifies\nthat the model needs to be quantized. `quantize_apply` can then be used\nto quantize the model.\n\nThis function is intended to be used in conjunction with the\n`quantize_annotate_layer` API. Otherwise, it is simpler to use\n`quantize_model`.\n\nAnnotate a model while overriding the default behavior for a layer: \n\n quantize_config = MyDenseQuantizeConfig()\n\n model = quantize_annotate_model(\n keras.Sequential([\n layers.Dense(10, activation='relu', input_shape=(100,)),\n quantize_annotate_layer(\n layers.Dense(2, activation='sigmoid'),\n quantize_config=quantize_config)\n ]))\n\n # The first Dense layer gets quantized with the default behavior,\n # but the second layer uses `MyDenseQuantizeConfig` for quantization.\n quantized_model = quantize_apply(model)\n\nNote that this function removes the optimizer from the original model.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------------|------------------------------------------------------------------------------------------------------|\n| `to_annotate` | [`tf.keras`](https://www.tensorflow.org/api_docs/python/tf/keras) model which needs to be quantized. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| New tf.keras model with each layer in the model wrapped with `QuantizeAnnotate`. The new model preserves weights from the original model. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-----------------------------------|\n| `ValueError` | if the model cannot be annotated. |\n\n\u003cbr /\u003e"]]