Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,33 +1,4 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"celltoolbar": "Raw Cell Format",
"colab": {
"name": "clustering_comprehensive_guide.ipynb",
"provenance": [],
"private_outputs": true,
"collapsed_sections": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
}
},
"cells": [
{
"cell_type": "markdown",
Expand All @@ -41,12 +12,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"colab": {},
"colab_type": "code",
"id": "ITj3u97-tNR7",
"colab": {}
"id": "ITj3u97-tNR7"
},
"outputs": [],
"source": [
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
Expand All @@ -59,9 +32,7 @@
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -80,20 +51,20 @@
"id": "IFva_Ed5N4ru"
},
"source": [
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://www.tensorflow.org/model_optimization/guide/clustering/clustering_comprehensive_guide\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on TensorFlow.org</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/model-optimization/blob/master/tensorflow_model_optimization/g3doc/guide/clustering/clustering_comprehensive_guide.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://github.com/tensorflow/model-optimization/blob/master/tensorflow_model_optimization/g3doc/guide/clustering/clustering_comprehensive_guide.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://storage.googleapis.com/tensorflow_docs/model-optimization/tensorflow_model_optimization/g3doc/guide/clustering/clustering_comprehensive_guide.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
" </td>\n",
"</table>"
"\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n",
" \u003ctd\u003e\n",
" \u003ca target=\"_blank\" href=\"https://www.tensorflow.org/model_optimization/guide/clustering/clustering_comprehensive_guide\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" /\u003eView on TensorFlow.org\u003c/a\u003e\n",
" \u003c/td\u003e\n",
" \u003ctd\u003e\n",
" \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/model-optimization/blob/master/tensorflow_model_optimization/g3doc/guide/clustering/clustering_comprehensive_guide.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n",
" \u003c/td\u003e\n",
" \u003ctd\u003e\n",
" \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/model-optimization/blob/master/tensorflow_model_optimization/g3doc/guide/clustering/clustering_comprehensive_guide.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n",
" \u003c/td\u003e\n",
" \u003ctd\u003e\n",
" \u003ca href=\"https://storage.googleapis.com/tensorflow_docs/model-optimization/tensorflow_model_optimization/g3doc/guide/clustering/clustering_comprehensive_guide.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/download_logo_32px.png\" /\u003eDownload notebook\u003c/a\u003e\n",
" \u003c/td\u003e\n",
"\u003c/table\u003e"
]
},
{
Expand All @@ -114,8 +85,7 @@
"* Define a clustered model.\n",
"* Checkpoint and deserialize a clustered model.\n",
"* Improve the accuracy of the clustered model.\n",
"* For deployment only, you must take steps to see compression benefits.\n",
"\n"
"* For deployment only, you must take steps to see compression benefits.\n"
]
},
{
Expand All @@ -130,12 +100,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "08dJRvOqN4rw",
"scrolled": true,
"colab": {}
"scrolled": true
},
"outputs": [],
"source": [
"! pip install -q tensorflow-model-optimization\n",
"\n",
Expand Down Expand Up @@ -203,9 +175,7 @@
"\n",
"setup_model()\n",
"pretrained_weights = setup_pretrained_weights()"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -244,12 +214,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "29g7OADjN4r1",
"scrolled": true,
"colab": {}
"scrolled": true
},
"outputs": [],
"source": [
"import tensorflow_model_optimization as tfmot\n",
"\n",
Expand All @@ -267,9 +239,7 @@
"clustered_model = cluster_weights(model, **clustering_params)\n",
"\n",
"clustered_model.summary()"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -278,8 +248,7 @@
"id": "zEOHK4OON4r7"
},
"source": [
"### Cluster some layers (sequential and functional models)\n",
"\n"
"### Cluster some layers (sequential and functional models)\n"
]
},
{
Expand All @@ -301,12 +270,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "IqBdl3uJN4r_",
"scrolled": true,
"colab": {}
"scrolled": true
},
"outputs": [],
"source": [
"# Create a base model\n",
"base_model = setup_model()\n",
Expand All @@ -327,9 +298,7 @@
")\n",
"\n",
"clustered_model.summary()"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -353,11 +322,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "w7P67mPk6RkQ",
"colab": {}
"id": "w7P67mPk6RkQ"
},
"outputs": [],
"source": [
"# Define the model.\n",
"base_model = setup_model()\n",
Expand All @@ -373,9 +344,7 @@
" loaded_model = tf.keras.models.load_model(keras_model_file)\n",
"\n",
"loaded_model.summary()"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -435,11 +404,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "ZvuiCBsVN4sR",
"colab": {}
"id": "ZvuiCBsVN4sR"
},
"outputs": [],
"source": [
"model = setup_model()\n",
"clustered_model = cluster_weights(model, **clustering_params)\n",
Expand All @@ -465,9 +436,21 @@
" % (get_gzipped_model_size(clustered_model)))\n",
"print(\"Size of gzipped clustered model with stripping: %.2f bytes\" \n",
" % (get_gzipped_model_size(final_model)))"
],
"execution_count": null,
"outputs": []
]
}
]
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "clustering_comprehensive_guide.ipynb",
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@
"id": "Y2wKK7w9SGPS"
},
"source": [
"Apply the `cluster_weights()` API to a whole pre-trained model to demonstrate its effectiveness in reducing the model size after applying zip while keeping decent accuracy. For how best to balance the accuracy and compression rate for your use case, please refer to the per layer example in the [comprehensive guide](https://www.tensorflow.org/model_optimization/guide/clustering/clustering_comprehensive_guide).\n",
"\n"
"Apply the `cluster_weights()` API to a whole pre-trained model to demonstrate its effectiveness in reducing the model size after applying zip while keeping decent accuracy. For how best to balance the accuracy and compression rate for your use case, please refer to the per layer example in the [comprehensive guide](https://www.tensorflow.org/model_optimization/guide/clustering/clustering_comprehensive_guide).\n"
]
},
{
Expand Down Expand Up @@ -646,26 +645,12 @@
"colab": {
"collapsed_sections": [],
"name": "clustering_example.ipynb",
"private_outputs": true,
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
}
},
"nbformat": 4,
Expand Down
Loading