def _alternate_descent(self): n, d = self.X.shape self.centers = cluster._init_centers(self.centers, self.X, self.n_clusters) self.memberships = cluster._init_memberships(self.memberships, self.centers, self.X, self.n_clusters) self.weights = cluster._init_weights(self.weights, self.X, self.n_clusters) self.memberships, self.centers = super()._alternate_descent() for niter in range(self.max_iter): if self.verbose & Verbosity.COST_FUNCTION: self._log_cost(self.centers, self.memberships, self.weights) cst = np.min( 1 / self._memberships_hessian(self.memberships, self.centers)) print("Cst is {}".format(cst)) #print("Cst is now {}".format(cst)) new_memberships = _pfscm_prox_gradient( self.memberships, self._memberships_gradient, _prox_op, cst, prox_arg=cst * self.memberships_gamma, max_iter=10, tol=self.tol, grad_args=(self.X, self.centers)) new_centers = self._update_centers(self.X, self.n_clusters, self.weights, new_memberships) gain = np.linalg.norm(new_centers - self.centers) if gain < self.tol: break self.centers = new_centers self.memberships = new_memberships return self.memberships, self.centers
def _alternate_descent(self): n, d = self.X.shape self.centers = cluster._init_centers(self.centers, self.X, self.n_clusters) self.memberships = cluster._init_memberships(self.memberships, self.centers, self.X, self.n_clusters) self.weights = cluster._init_weights(self.weights, self.X, self.n_clusters) fcm_init = cluster.FCMeans(self.n_clusters) fcm_init.fit(self.X) # self.memberships, self.centers = fcm_init.memberships, fcm_init.centers for i in range(self.max_iter): if self.verbose & Verbosity.COST_FUNCTION: self._log_cost(self.centers, self.memberships, self.weights) new_memberships = self._update_memberships(self.X, self.n_clusters, self.weights, self.centers, self.memberships) new_centers = self._update_centers(self.X, self.n_clusters, self.weights, new_memberships) new_weights = self._update_weights(self.X, self.n_clusters, new_memberships, new_centers) if np.linalg.norm(new_centers - self.centers) < self.tol: break self.memberships = new_memberships self.centers = new_centers self.weights = new_weights if self.verbose & Verbosity.COST_FUNCTION: self._log_cost(self.centers, self.memberships, self.weights) self._fcm_init = fcm_init return self.memberships, self.centers, self.weights
def _alternate_descent(self): n, d = self.X.shape self.centers = cluster._init_centers(self.centers, self.X, self.n_clusters) self.memberships = cluster._init_memberships(self.memberships, self.centers, self.X, self.n_clusters) self.weights = cluster._init_weights(self.weights, self.X, self.n_clusters) self.memberships, self.centers = cluster.FCMeans._alternate_descent( self) for niter in range(self.max_iter): if self.verbose & Verbosity.COST_FUNCTION: self._log_cost(self.centers, self.memberships, self.weights) memberships_cst = np.min(1 / self._memberships_hessian( self.memberships, self.centers, self.weights)) new_memberships = _pfscm_prox_gradient( self.memberships, self._memberships_gradient, _prox_op, memberships_cst, prox_arg=memberships_cst * self.memberships_gamma, max_iter=10, tol=self.tol, grad_args=(self.X, self.centers, self.weights)) #print(np.sum(new_memberships)) new_centers = self._update_centers(self.X, self.n_clusters, self.weights, new_memberships) weights_cst = np.min(1 / self._weights_hessian( new_memberships, new_centers, n, d, self.n_clusters)) new_weights = _pfscm_prox_gradient( self.weights, self._weights_gradient, _sparsity_prox_op, weights_cst, prox_arg=weights_cst * self.weights_gamma, max_iter=10, tol=self.tol, grad_args=(self.X, new_centers, new_memberships)) gain = np.linalg.norm(new_centers - self.centers) #print(self.centers) #print(gain) if gain < self.tol: break self.weights = new_weights self.centers = new_centers self.memberships = new_memberships return self.memberships, self.centers, self.weights
def _alternate_descent(self): n, d = self.X.shape centers = cluster._init_centers(self.centers, self.X, self.n_clusters) memberships = cluster._init_memberships(self.memberships, centers, self.X, self.n_clusters) weights = cluster._init_weights(self.weights, self.X, self.n_clusters) for i in range(self.max_iter): if self.verbose & Verbosity.COST_FUNCTION: self._log_cost(centers, memberships, weights) new_memberships = self._update_memberships(self.X, self.n_clusters, weights, centers) new_centers = self._update_centers(self.X, self.n_clusters, weights, new_memberships) new_weights = self._update_weights(self.X, self.n_clusters, new_memberships, new_centers) if np.linalg.norm(new_centers - centers) < self.tol: break memberships = new_memberships centers = new_centers weights = new_weights if self.verbose & Verbosity.COST_FUNCTION: self._log_cost(centers, memberships, weights) return memberships, centers, weights