tfmot.sparsity.keras.PruningPolicy
Stay organized with collections
Save and categorize content based on your preferences.
Specifies what layers to prune in the model.
PruningPolicy controls application of PruneLowMagnitude
wrapper on per-layer
basis and checks that the model contains only supported layers.
PruningPolicy works together with prune_low_magnitude
through which it
provides fine-grained control over pruning in the model.
pruning_params = {
'pruning_schedule': ConstantSparsity(0.5, 0),
'block_size': (1, 1),
'block_pooling_type': 'AVG'
}
model = prune_low_magnitude(
keras.Sequential([
layers.Dense(10, activation='relu', input_shape=(100,)),
layers.Dense(2, activation='sigmoid')
]),
pruning_policy=PruneForLatencyOnXNNPack(),
**pruning_params)
You can inherit this class to write your own custom pruning policy.
The API is experimental and is subject to change.
Methods
allow_pruning
View source
@abc.abstractmethod
allow_pruning(
layer
)
Checks if pruning wrapper should be applied for the current layer.
Args |
layer
|
Current layer in the model.
|
Returns |
True/False, whether the pruning wrapper should be applied for the layer.
|
ensure_model_supports_pruning
View source
@abc.abstractmethod
ensure_model_supports_pruning(
model
)
Checks that the model contains only supported layers.
Raises |
ValueError
|
if the keras model doesn't support pruning policy, i.e. keras
model contains an unsupported layer.
|
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.
[null,null,["Last updated 2023-05-26 UTC."],[],[],null,["# tfmot.sparsity.keras.PruningPolicy\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/sparsity/keras/pruning_policy.py#L28-L80) |\n\nSpecifies what layers to prune in the model.\n\nPruningPolicy controls application of `PruneLowMagnitude` wrapper on per-layer\nbasis and checks that the model contains only supported layers.\nPruningPolicy works together with `prune_low_magnitude` through which it\nprovides fine-grained control over pruning in the model. \n\n pruning_params = {\n 'pruning_schedule': ConstantSparsity(0.5, 0),\n 'block_size': (1, 1),\n 'block_pooling_type': 'AVG'\n }\n\n model = prune_low_magnitude(\n keras.Sequential([\n layers.Dense(10, activation='relu', input_shape=(100,)),\n layers.Dense(2, activation='sigmoid')\n ]),\n pruning_policy=PruneForLatencyOnXNNPack(),\n **pruning_params)\n\nYou can inherit this class to write your own custom pruning policy.\n\nThe API is experimental and is subject to change.\n\nMethods\n-------\n\n### `allow_pruning`\n\n[View source](https://github.com/tensorflow/model-optimization/blob/v0.7.5/tensorflow_model_optimization/python/core/sparsity/keras/pruning_policy.py#L57-L67) \n\n @abc.abstractmethod\n allow_pruning(\n layer\n )\n\nChecks if pruning wrapper should be applied for the current layer.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|---------|-----------------------------|\n| `layer` | Current layer in the model. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| True/False, whether the pruning wrapper should be applied for the layer. ||\n\n\u003cbr /\u003e\n\n### `ensure_model_supports_pruning`\n\n[View source](https://github.com/tensorflow/model-optimization/blob/v0.7.5/tensorflow_model_optimization/python/core/sparsity/keras/pruning_policy.py#L69-L80) \n\n @abc.abstractmethod\n ensure_model_supports_pruning(\n model\n )\n\nChecks that the model contains only supported layers.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|---------|-----------------------------------------------------------------------------------------------------------------------|\n| `model` | A [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model) instance which is going to be pruned. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ||\n|--------------|----------------------------------------------------------------------------------------------------|\n| `ValueError` | if the keras model doesn't support pruning policy, i.e. keras model contains an unsupported layer. |\n\n\u003cbr /\u003e"]]