def train_kmeans(self, prev_score, data): """We are using pre-made tensorflow estimators to train and predict. Args: prev_score: a sum of the distance between each sample to their nearest center data: list of weights of user model Return: updated score """ seed = np.random.randint(5667799881, size=1)[0] self._kmeans = tf.contrib.factorization.KMeansClustering( random_seed=seed, num_clusters=self._num_clusters, use_mini_batch=False) # composed of weights of every client model score = self.train_iteation(data) # evaluate the samples to compute the distance between # each sample and each center, forming a matrix have each # row for each sample, and the column is distance to each # center. y = self._kmeans.transform(lambda: input_fn(data)) point_distance = list(y) self._learned = np.argmin(point_distance, 1) # removing not used files of this model #shutil.rmtree(temp_modeldir, ignore_errors=True) return None, score
def train_iteation(self, data): train_data = lambda: input_fn(data) self._kmeans.train(train_data) cluster_centers = self._kmeans.cluster_centers() # print("cluster centers:", cluster_centers) score = self._kmeans.score(train_data) return score