tfmot.clustering.keras.cluster_weights
Stay organized with collections
Save and categorize content based on your preferences.
Modifies a keras layer or model to be clustered during training.
tfmot.clustering.keras.cluster_weights(
to_cluster,
number_of_clusters,
cluster_centroids_init=CentroidInitialization.KMEANS_PLUS_PLUS,
**kwargs
)
Used in the notebooks
This function wraps a keras model or layer with clustering functionality
which clusters the layer's weights during training. For examples, using
this with number_of_clusters equals 8 will ensure that each weight tensor has
no more than 8 unique values.
Before passing to the clustering API, a model should already be trained and
show some acceptable performance on the testing/validation sets.
The function accepts either a single keras layer
(subclass of keras.layers.Layer
), list of keras layers or a keras model
(instance of keras.models.Model
) and handles them appropriately.
If it encounters a layer it does not know how to handle, it will throw an
error. While clustering an entire model, even a single unknown layer would
lead to an error.
Cluster a model:
clustering_params = {
'number_of_clusters': 8,
'cluster_centroids_init': CentroidInitialization.DENSITY_BASED
}
clustered_model = cluster_weights(original_model, **clustering_params)
Cluster a layer:
clustering_params = {
'number_of_clusters': 8,
'cluster_centroids_init': CentroidInitialization.DENSITY_BASED
}
model = tf.keras.Sequential([
layers.Dense(10, activation='relu', input_shape=(100,)),
cluster_weights(layers.Dense(2, activation='tanh'), **clustering_params)
])
Arguments |
to_cluster
|
A single keras layer, list of keras layers, or a
tf.keras.Model instance.
|
number_of_clusters
|
the number of cluster centroids to form when
clustering a layer/model. For example, if number_of_clusters=8 then only
8 unique values will be used in each weight array.
|
cluster_centroids_init
|
enum value that determines how the cluster
centroids will be initialized.
Can have following values:
- RANDOM : centroids are sampled using the uniform distribution
between the minimum and maximum weight values in a given layer
- DENSITY_BASED : density-based sampling. First, cumulative
distribution function is built for weights, then y-axis is evenly
spaced into number_of_clusters regions. After this the corresponding
x values are obtained and used to initialize clusters centroids.
- LINEAR : cluster centroids are evenly spaced between the minimum
and maximum values of a given weight
|
**kwargs
|
Additional keyword arguments to be passed to the keras layer.
Ignored when to_cluster is not a keras layer.
|
Returns |
Layer or model modified to include clustering related metadata.
|
Raises |
ValueError
|
if the keras layer is unsupported, or the 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.clustering.keras.cluster_weights\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/clustering/keras/cluster.py#L54-L130) |\n\nModifies a keras layer or model to be clustered during training. \n\n tfmot.clustering.keras.cluster_weights(\n to_cluster,\n number_of_clusters,\n cluster_centroids_init=CentroidInitialization.KMEANS_PLUS_PLUS,\n **kwargs\n )\n\n### Used in the notebooks\n\n| Used in the guide |\n|------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Weight clustering comprehensive guide](https://www.tensorflow.org/model_optimization/guide/clustering/clustering_comprehensive_guide) |\n\nThis function wraps a keras model or layer with clustering functionality\nwhich clusters the layer's weights during training. For examples, using\nthis with number_of_clusters equals 8 will ensure that each weight tensor has\nno more than 8 unique values.\n\nBefore passing to the clustering API, a model should already be trained and\nshow some acceptable performance on the testing/validation sets.\n\nThe function accepts either a single keras layer\n(subclass of [`keras.layers.Layer`](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer)), list of keras layers or a keras model\n(instance of [`keras.models.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model)) and handles them appropriately.\n\nIf it encounters a layer it does not know how to handle, it will throw an\nerror. While clustering an entire model, even a single unknown layer would\nlead to an error.\n\n#### Cluster a model:\n\n clustering_params = {\n 'number_of_clusters': 8,\n 'cluster_centroids_init': CentroidInitialization.DENSITY_BASED\n }\n\n clustered_model = cluster_weights(original_model, **clustering_params)\n\n#### Cluster a layer:\n\n clustering_params = {\n 'number_of_clusters': 8,\n 'cluster_centroids_init': CentroidInitialization.DENSITY_BASED\n }\n\n model = tf.keras.Sequential([\n layers.Dense(10, activation='relu', input_shape=(100,)),\n cluster_weights(layers.Dense(2, activation='tanh'), **clustering_params)\n ])\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Arguments --------- ||\n|--------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `to_cluster` | A single keras layer, list of keras layers, or a [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model) instance. |\n| `number_of_clusters` | the number of cluster centroids to form when clustering a layer/model. For example, if number_of_clusters=8 then only 8 unique values will be used in each weight array. |\n| `cluster_centroids_init` | enum value that determines how the cluster centroids will be initialized. Can have following values: \u003cbr /\u003e 1. RANDOM : centroids are sampled using the uniform distribution between the minimum and maximum weight values in a given layer 2. DENSITY_BASED : density-based sampling. First, cumulative distribution function is built for weights, then y-axis is evenly spaced into number_of_clusters regions. After this the corresponding x values are obtained and used to initialize clusters centroids. 3. LINEAR : cluster centroids are evenly spaced between the minimum and maximum values of a given weight |\n| `**kwargs` | Additional keyword arguments to be passed to the keras layer. Ignored when to_cluster is not a keras layer. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| Layer or model modified to include clustering related metadata. ||\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 layer is unsupported, or the keras model contains an unsupported layer. |\n\n\u003cbr /\u003e"]]