def _init_clusters(self): """Initialization of clusters. Returns: Tuple with following elements: cluster_centers: a Tensor for storing cluster centers cluster_counts: a Tensor for storing counts of points assigned to this cluster. This is used by mini-batch training. """ init = self._initial_clusters if init == RANDOM_INIT: clusters_init = self._init_clusters_random() elif init == KMEANS_PLUS_PLUS_INIT: # Points from only the first shard are used for initializing centers. # TODO(ands): Use all points. clusters_init = gen_clustering_ops.kmeans_plus_plus_initialization( self._inputs[0], self._num_clusters, self._random_seed, self._kmeans_plus_plus_num_retries) elif callable(init): clusters_init = init(self._inputs, self._num_clusters) elif not isinstance(init, str): clusters_init = init else: assert False, 'Unsupported init passed to Kmeans %s' % str(init) if self._distance_metric == COSINE_DISTANCE and clusters_init is not None: clusters_init = nn_impl.l2_normalize(clusters_init, dim=1) clusters_init = clusters_init if clusters_init is not None else [] cluster_centers = variables.Variable( clusters_init, name='clusters', validate_shape=False) cluster_counts = (variables.Variable( array_ops.ones( [self._num_clusters], dtype=dtypes.int64)) if self._use_mini_batch else None) return cluster_centers, cluster_counts
def _init_clusters(self): """Initialization of clusters. Returns: Tuple with following elements: cluster_centers: a Tensor for storing cluster centers cluster_counts: a Tensor for storing counts of points assigned to this cluster. This is used by mini-batch training. """ init = self._initial_clusters if init == RANDOM_INIT: clusters_init = self._init_clusters_random() elif init == KMEANS_PLUS_PLUS_INIT: # Points from only the first shard are used for initializing centers. # TODO(ands): Use all points. clusters_init = gen_clustering_ops.kmeans_plus_plus_initialization( self._inputs[0], self._num_clusters, self._random_seed, self._kmeans_plus_plus_num_retries) elif callable(init): clusters_init = init(self._inputs, self._num_clusters) elif not isinstance(init, str): clusters_init = init else: assert False, 'Unsupported init passed to Kmeans %s' % str(init) if self._distance_metric == COSINE_DISTANCE and clusters_init is not None: clusters_init = tf.nn.l2_normalize(clusters_init, dim=1) clusters_init = clusters_init if clusters_init is not None else [] cluster_centers = tf.Variable(clusters_init, name='clusters', validate_shape=False) cluster_counts = (tf.Variable( tf.zeros([self._num_clusters], dtype=tf.int64)) if self._use_mini_batch else None) return cluster_centers, cluster_counts
def _kmeans_plus_plus(self): # Points from only the first shard are used for initializing centers. # TODO(ands): Use all points. inp = self._inputs[0] if self._distance_metric == COSINE_DISTANCE: inp = nn_impl.l2_normalize(inp, dim=1) return gen_clustering_ops.kmeans_plus_plus_initialization( inp, math_ops.to_int64(self._num_remaining), self._random_seed, self._kmeans_plus_plus_num_retries)
def _init_clusters(self): """Initialization of clusters. Returns: Tuple with following elements: cluster_centers: a Tensor for storing cluster centers cluster_counts: a Tensor for storing counts of points assigned to this cluster. This is used by mini-batch training. """ init = self._initial_clusters if init == RANDOM_INIT: clusters_init = self._init_clusters_random() elif init == KMEANS_PLUS_PLUS_INIT: # Points from only the first shard are used for initializing centers. # TODO(ands): Use all points. inp = self._inputs[0] if self._distance_metric == COSINE_DISTANCE: inp = nn_impl.l2_normalize(inp, dim=1) clusters_init = gen_clustering_ops.kmeans_plus_plus_initialization( inp, self._num_clusters, self._random_seed, self._kmeans_plus_plus_num_retries) elif callable(init): clusters_init = init(self._inputs, self._num_clusters) elif not isinstance(init, str): clusters_init = init else: assert False, 'Unsupported init passed to Kmeans %s' % str(init) if self._distance_metric == COSINE_DISTANCE and clusters_init is not None: clusters_init = nn_impl.l2_normalize(clusters_init, dim=1) clusters_init = clusters_init if clusters_init is not None else [] # TODO(agarwal): Locally cache cluster_centers on the worker to avoid # copying them each step. cluster_centers = variables.Variable(clusters_init, name='clusters', validate_shape=False) if self._use_mini_batch and self._mini_batch_steps_per_iteration > 1: # Copy of cluster centers actively updated each step according to # mini-batch update rule. cluster_centers_updated = variables.Variable( clusters_init, name='clusters_updated', validate_shape=False) # How many steps till we copy the updated clusters to cluster_centers. update_in_steps = variables.Variable( self._mini_batch_steps_per_iteration, dtype=dtypes.int64, name='update_in_steps') # Count of points assigned to cluster_centers_updated. cluster_counts = variables.Variable( array_ops.zeros([self._num_clusters], dtype=dtypes.int64)) else: cluster_centers_updated = cluster_centers update_in_steps = None cluster_counts = (variables.Variable( array_ops.ones([self._num_clusters], dtype=dtypes.int64)) if self._use_mini_batch else None) return (cluster_centers, cluster_counts, cluster_centers_updated, update_in_steps)
def _init_clusters(self): """Initialization of clusters. Returns: Tuple with following elements: cluster_centers: a Tensor for storing cluster centers cluster_counts: a Tensor for storing counts of points assigned to this cluster. This is used by mini-batch training. """ init = self._initial_clusters if init == RANDOM_INIT: clusters_init = self._init_clusters_random() elif init == KMEANS_PLUS_PLUS_INIT: # Points from only the first shard are used for initializing centers. # TODO(ands): Use all points. inp = self._inputs[0] if self._distance_metric == COSINE_DISTANCE: inp = nn_impl.l2_normalize(inp, dim=1) clusters_init = gen_clustering_ops.kmeans_plus_plus_initialization( inp, self._num_clusters, self._random_seed, self._kmeans_plus_plus_num_retries) elif callable(init): clusters_init = init(self._inputs, self._num_clusters) elif not isinstance(init, str): clusters_init = init else: assert False, 'Unsupported init passed to Kmeans %s' % str(init) if self._distance_metric == COSINE_DISTANCE and clusters_init is not None: clusters_init = nn_impl.l2_normalize(clusters_init, dim=1) clusters_init = clusters_init if clusters_init is not None else [] # TODO(agarwal): Locally cache cluster_centers on the worker to avoid # copying them each step. cluster_centers = variables.Variable(clusters_init, name='clusters', validate_shape=False) if self._use_mini_batch and self._mini_batch_steps_per_iteration > 1: # Copy of cluster centers actively updated each step according to # mini-batch update rule. cluster_centers_updated = variables.Variable(clusters_init, name='clusters_updated', validate_shape=False) # How many steps till we copy the updated clusters to cluster_centers. update_in_steps = variables.Variable(self._mini_batch_steps_per_iteration, dtype=dtypes.int64, name='update_in_steps') # Count of points assigned to cluster_centers_updated. cluster_counts = variables.Variable(array_ops.zeros([self._num_clusters], dtype=dtypes.int64)) else: cluster_centers_updated = cluster_centers update_in_steps = None cluster_counts = (variables.Variable(array_ops.ones([self._num_clusters], dtype=dtypes.int64)) if self._use_mini_batch else None) return (cluster_centers, cluster_counts, cluster_centers_updated, update_in_steps)
def _kmeans_plus_plus(self): # Points from only the first shard are used for initializing centers. # TODO(ands): Use all points. inp = self._inputs[0] if self._distance_metric == COSINE_DISTANCE: inp = nn_impl.l2_normalize(inp, dim=1) return gen_clustering_ops.kmeans_plus_plus_initialization( inp, math_ops.to_int64(self._num_remaining), self._random_seed, self._kmeans_plus_plus_num_retries)
def _initialize_clusters(self, cluster_centers, cluster_centers_initialized, cluster_centers_updated): """Returns an op to initialize the cluster centers.""" init = self._initial_clusters if init == RANDOM_INIT: clusters_init = self._init_clusters_random() elif init == KMEANS_PLUS_PLUS_INIT: # Points from only the first shard are used for initializing centers. # TODO(ands): Use all points. inp = self._inputs[0] if self._distance_metric == COSINE_DISTANCE: inp = nn_impl.l2_normalize(inp, dim=1) clusters_init = gen_clustering_ops.kmeans_plus_plus_initialization( inp, self._num_clusters, self._random_seed, self._kmeans_plus_plus_num_retries) elif callable(init): clusters_init = init(self._inputs, self._num_clusters) elif not isinstance(init, str): clusters_init = init else: assert False, 'Unsupported init passed to Kmeans %s' % str(init) if self._distance_metric == COSINE_DISTANCE and clusters_init is not None: clusters_init = nn_impl.l2_normalize(clusters_init, dim=1) with ops.colocate_with(cluster_centers_initialized): initialized = control_flow_ops.with_dependencies( [clusters_init], array_ops.identity(cluster_centers_initialized)) with ops.colocate_with(cluster_centers): assign_centers = state_ops.assign(cluster_centers, clusters_init, validate_shape=False) if cluster_centers_updated != cluster_centers: assign_centers = control_flow_ops.group( assign_centers, state_ops.assign(cluster_centers_updated, clusters_init, validate_shape=False)) assign_centers = control_flow_ops.with_dependencies( [assign_centers], state_ops.assign(cluster_centers_initialized, True)) return control_flow_ops.cond(initialized, control_flow_ops.no_op, lambda: assign_centers).op
def _initialize_clusters(self, cluster_centers, cluster_centers_initialized, cluster_centers_updated): """Returns an op to initialize the cluster centers.""" init = self._initial_clusters if init == RANDOM_INIT: clusters_init = self._init_clusters_random() elif init == KMEANS_PLUS_PLUS_INIT: # Points from only the first shard are used for initializing centers. # TODO(ands): Use all points. inp = self._inputs[0] if self._distance_metric == COSINE_DISTANCE: inp = nn_impl.l2_normalize(inp, dim=1) clusters_init = gen_clustering_ops.kmeans_plus_plus_initialization( inp, self._num_clusters, self._random_seed, self._kmeans_plus_plus_num_retries) elif callable(init): clusters_init = init(self._inputs, self._num_clusters) elif not isinstance(init, str): clusters_init = init else: assert False, 'Unsupported init passed to Kmeans %s' % str(init) if self._distance_metric == COSINE_DISTANCE and clusters_init is not None: clusters_init = nn_impl.l2_normalize(clusters_init, dim=1) with ops.colocate_with(cluster_centers_initialized): initialized = control_flow_ops.with_dependencies( [clusters_init], array_ops.identity(cluster_centers_initialized)) with ops.colocate_with(cluster_centers): assign_centers = state_ops.assign(cluster_centers, clusters_init, validate_shape=False) if cluster_centers_updated != cluster_centers: assign_centers = control_flow_ops.group( assign_centers, state_ops.assign(cluster_centers_updated, clusters_init, validate_shape=False)) assign_centers = control_flow_ops.with_dependencies( [assign_centers], state_ops.assign(cluster_centers_initialized, True)) return control_flow_ops.cond(initialized, control_flow_ops.no_op, lambda: assign_centers).op