class_docstr: Unit tests for the cluster_wrapper module.
docstr: Verifies that the association of weights to cluster centroids are updated every iteration.
docstr_tok: ["Verifies","that","the","association","of","weights","to","cluster","centroids","are","updated","every","iteration","."]
code: def testClusterReassociation(self):
      """Verifies that the association of weights to cluster centroids are updated every iteration."""
  
      # Create a dummy layer for this test
      input_shape = (
          1,
          2,
      )
      l = cluster_wrapper.ClusterWeights(
          keras.layers.Dense(8, input_shape=input_shape),
          number_of_clusters=2,
      )
      # Build a layer with the given shape
      l.build(input_shape)
  
      # Get name of the clusterable weights
      clusterable_weights = l.layer.get_clusterable_weights()
      self.assertLen(clusterable_weights, 1)
      weights_name = clusterable_weights[0][0]
      self.assertEqual(weights_name, "kernel")
      # Get cluster centroids
      centroids = l.cluster_centroids[weights_name]
  
      # Calculate some statistics of the weights to set the centroids later on
      mean_weight = tf.reduce_mean(l.layer.kernel)
      min_weight = tf.reduce_min(l.layer.kernel)
      max_weight = tf.reduce_max(l.layer.kernel)
      max_dist = max_weight - min_weight
  
      def assert_all_weights_associated(weights, centroid_index):
          """Helper function to make sure that all weights are associated with one centroid."""
          all_associated = tf.reduce_all(
              tf.equal(
                  weights,
                  tf.constant(
                      centroids[centroid_index], shape=weights.shape
                  ),
              )
          )
          self.assertTrue(all_associated)
  
      # Set centroids so that all weights should be re-associated with centroid 0
      centroids[0].assign(mean_weight)
      centroids[1].assign(mean_weight + 2.0 * max_dist)
  
      # Update associations of weights to centroids
      l.call(tf.ones(shape=input_shape))
  
      # Weights should now be all clustered with the centroid 0
      assert_all_weights_associated(l.layer.kernel, centroid_index=0)
  
      # Set centroids so that all weights should be re-associated with centroid 1
      centroids[0].assign(mean_weight - 2.0 * max_dist)
      centroids[1].assign(mean_weight)
  
      # Update associations of weights to centroids
      l.call(tf.ones(shape=input_shape))
  
      # Weights should now be all clustered with the centroid 1
      assert_all_weights_associated(l.layer.kernel, centroid_index=1)
  
imports: import itertools
  import os
  import tempfile
  from absl.testing import parameterized
  import tensorflow
  from tensorflow_model_optimization.python.core.clustering.keras import cluster
  from tensorflow_model_optimization.python.core.clustering.keras import cluster_config
  from tensorflow_model_optimization.python.core.clustering.keras import cluster_wrapper
  from tensorflow_model_optimization.python.core.clustering.keras import clusterable_layer
label_desc: Use image node_id 11 for calling the ClusterWeightsTest obj's underlying member method code with example usage: obj.testClusterReassociation() without return types