权重聚类实现基于《深度压缩:使用剪枝、经过训练的量化和霍夫曼编码压缩深度神经网络》(Deep Compression: Compressing Deep Neural Networks With Pruning, Trained Quantization and Huffman Coding) 一文。请参阅第 3 章“经过训练的量化和权重共享”(Trained Quantization and Weight Sharing)。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2021-09-01。"],[],[],null,["# Weight clustering\n\n\u003cbr /\u003e\n\n~Maintained by Arm ML Tooling~\n\nThis document provides an overview on weight clustering to help you determine how it fits with your use case.\n\n- To dive right into an end-to-end example, see the [weight clustering example](/model_optimization/guide/clustering/clustering_example).\n- To quickly find the APIs you need for your use case, see the [weight clustering comprehensive guide](/model_optimization/guide/clustering/clustering_comprehensive_guide).\n\nOverview\n--------\n\nClustering, or weight sharing, reduces the number of unique weight values in a model, leading to benefits for deployment. It first groups the weights of each layer into *N* clusters, then shares the cluster's centroid value for all the weights belonging to the cluster.\n\nThis technique brings improvements via model compression. Future framework support can unlock memory footprint improvements that can make a crucial difference for deploying deep learning models on embedded systems with limited resources.\n\nWe have experimented with clustering across vision and speech tasks. We've seen up to 5x improvements in model compression with minimal loss of accuracy, as demonstrated by the [results](#results) presented below.\n\nPlease note that clustering will provide reduced benefits for convolution and dense layers that precede a batch normalization layer, as well as in combination with per-axis post-training quantization.\n\n### API compatibility matrix\n\nUsers can apply clustering with the following APIs:\n\n- Model building: `keras` with only Sequential and Functional models\n- TensorFlow versions: TF 1.x for versions 1.14+ and 2.x.\n - [`tf.compat.v1`](https://www.tensorflow.org/api_docs/python/tf/compat/v1) with a TF 2.X package and `tf.compat.v2` with a TF 1.X package are not supported.\n- TensorFlow execution mode: both graph and eager\n\nResults\n-------\n\n### Image classification\n\n| Model | Original || Clustered ||||\n| Model | Top-1 accuracy (%) | Size of compressed .tflite (MB) | Configuration | # of clusters | Top-1 accuracy (%) | Size of compressed .tflite (MB) |\n|-------------|--------------------|---------------------------------|----------------------------------|---------------|--------------------|---------------------------------|\n| MobileNetV1 | 70.976 | 14.97 |\n| MobileNetV1 | 70.976 | 14.97 | Selective (last 3 Conv2D layers) | 16, 16, 16 | 70.294 | 7.69 |\n| MobileNetV1 | 70.976 | 14.97 | Selective (last 3 Conv2D layers) | 32, 32, 32 | 70.69 | 8.22 |\n| MobileNetV1 | 70.976 | 14.97 | Full (all Conv2D layers) | 32 | 69.4 | 4.43 |\n| MobileNetV2 | 71.778 | 12.38 |\n| MobileNetV2 | 71.778 | 12.38 | Selective (last 3 Conv2D layers) | 16, 16, 16 | 70.742 | 6.68 |\n| MobileNetV2 | 71.778 | 12.38 | Selective (last 3 Conv2D layers) | 32, 32, 32 | 70.926 | 7.03 |\n| MobileNetV2 | 71.778 | 12.38 | Full (all Conv2D layers) | 32 | 69.744 | 4.05 |\n\nThe models were trained and tested on ImageNet.\n\n### Keyword spotting\n\n| Model | Original || Clustered ||||\n| Model | Top-1 accuracy (%) | Size of compressed .tflite (MB) | Configuration | # of clusters | Top-1 accuracy (%) | Size of compressed .tflite (MB) |\n|----------|--------------------|---------------------------------|--------------------------|---------------|--------------------|---------------------------------|\n| DS-CNN-L | 95.233 | 1.46 |\n| DS-CNN-L | 95.233 | 1.46 | Full (all Conv2D layers) | 32 | 95.09 | 0.39 |\n| DS-CNN-L | 95.233 | 1.46 | Full (all Conv2D layers) | 8 | 94.272 | 0.27 |\n\nThe model was trained and tested on SpeechCommands v0.02.\n| **Note:** *Size of compressed .tflite* refers to the size of the zipped .tflite file obtained from the model from the following process:\n\n1. Serialize the Keras model into .h5 file\n2. Convert the .h5 file into .tflite using `TFLiteConverter.from_keras_model_file()`\n3. Compress the .tflite file into a zip\n\nExamples\n--------\n\nIn addition to the\n[Weight clustering in Keras example](/model_optimization/guide/clustering/clustering_example), see the\nfollowing examples:\n\n- Cluster the weights of a CNN model trained on the MNIST handwritten digit classification dataset: [code](https://github.com/tensorflow/model-optimization/blob/master/tensorflow_model_optimization/python/examples/clustering/keras/mnist/mnist_cnn.py)\n\nThe weight clustering implementation is based on the *Deep Compression:\nCompressing Deep Neural Networks With Pruning, Trained Quantization and Huffman\nCoding* [paper](https://arxiv.org/abs/1510.00149). See chapter 3, titled\n*Trained Quantization and Weight Sharing*."]]