def gd(self, batch_size=None, **kwargs): if self.verbose > 0: print(self.indent + ' TSNE.gd():') print(self.indent + ' specs:') if batch_size is None or batch_size >= self.n_samples: Xi = None F = lambda embedding: self.gradient(embedding) if self.verbose > 0: print(self.indent + ' gradient type : full') else: def Xi(): indices = np.arange(self.n_samples) np.random.shuffle(indices) xi = {'indices': indices} return xi F = lambda X, indices: self.gradient(X, batch_size, indices) if self.verbose > 0: print(self.indent + ' gradient type : batch') print(self.indent + ' batch size :', batch_size) self.embedding, H = gd.single(self.embedding, F, Xi=Xi, verbose=self.verbose, indent=self.indent + ' ', **kwargs) self.update() if self.verbose > 0: print(self.indent + f' final stress : {self.cost:0.2e}')
def smart_initialize(self,max_iter=[50,30],lr=[1,0.1], batch_size=10,**kwargs): """\ Computes an mds embedding (dimension embedding_dimension) of the combined distances. Only works when self.Q_is_fixed is False (as this is unnecessary otherwhise). Parameters : X0 : None or array Optional initial embedding (used to compute mds embedding) Q0 : None or list of arrays Optional initial projection parameters. """ assert self.fixed_embedding is False assert self.fixed_projections is False if self.verbose > 0: print(self.indent+' MPSE.smart_initialize():') distances = np.sum(self.distances,axis=0)/self.n_perspectives if self.weights is not None and self.weights[0] is not None: weights = np.product(self.weights,axis=0) else: weights = None vis = mds.MDS(distances,dim=self.embedding_dimension,min_grad=1e-4, indent=self.indent+' ', initial_embedding=self.embedding, weights = weights, verbose=self.verbose) vis.gd(batch_size=batch_size, max_iter=max_iter[0],lr=lr[0],**kwargs) self.embedding = vis.X H = vis.computation_history[0] H['fixed_embedding'] = False H['fixed_projections'] = True self.computation_history.append(H) def Xi(): indices = np.arange(self.n_samples) np.random.shuffle(indices) xi = { 'indices' : indices } return xi F = lambda Q, indices : self.gradient(self.embedding,Q, batch_size=batch_size, return_embedding=False) Q0 = np.array(self.projections) self.projections, H = gd.single(Q0,F,Xi=Xi,p=self.proj.restrict, max_iter=max_iter[1],lr=lr[1], verbose=self.verbose,indent=self.indent+' ', **kwargs) H['fixed_embedding'] = True H['fixed_projections'] = False self.computation_history.append(H) return
def gd(self, batch_size=None, lr=None, fixed_projections='default', fixed_embedding='default', **kwargs): if fixed_projections == 'default': fixed_projections = self.fixed_projections if fixed_embedding == 'default': fixed_embedding = self.fixed_embedding assert batch_size is None or isinstance(batch_size,int) assert fixed_embedding is False or fixed_projections is False if lr is None: if self.visualization_method == 'mds': if fixed_projections: lr = 1 elif fixed_embedding: lr = 0.01 else: lr = [1,0.01] elif self.visualization_method == 'tsne': if fixed_projections: lr = 100 elif fixed_embedding: lr = 10 else: lr = [100,10] else: lr = [1,1] if self.verbose > 0: print(self.indent+' MPSE.gd():') print(self.indent+f' initial stress : {self.cost:0.2e}') if fixed_projections: if self.verbose > 0: print(self.indent+' mpse method : fixed projections') if batch_size is None or batch_size >= self.n_samples: Xi = None F = lambda X : self.gradient(X,self.projections, return_projections=False) else: if self.verbose > 0: print(self.indent+' batch size :',batch_size) def Xi(): indices = np.arange(self.n_samples) np.random.shuffle(indices) xi = { 'indices' : indices } return xi F = lambda X, indices: self.gradient(X,self.projections, batch_size=batch_size, indices=indices, return_projections=False) self.embedding, H = gd.single( self.embedding,F,Xi=Xi, verbose=self.verbose, lr=lr, indent=self.indent+' ',**kwargs) H['fixed_projections'] = True H['fixed_embedding'] = False self.computation_history.append(H) elif fixed_embedding: if self.verbose > 0: print(self.indent+' mpse method : fixed embedding') if batch_size is None or batch_size >= self.n_samples: Xi = None F = lambda Q : self.gradient(self.embedding,Q, return_embedding=False) else: if self.verbose > 0: print(self.indent+' batch size :',batch_size) def Xi(): indices = np.arange(self.n_samples) np.random.shuffle(indices) xi = { 'indices' : indices } return xi F = lambda Q, indices: self.gradient(self.embedding,Q, batch_size=batch_size, indices=indices, return_embedding=False) Q0 = np.array(self.projections) self.projections, H = gd.single(Q0,F,Xi=Xi,p=self.proj.restrict, verbose=self.verbose,lr=lr, indent=self.indent+' ',**kwargs) H['fixed_projections'] = False H['fixed_embedding'] = True self.computation_history.append(H) else: if self.verbose > 0: print(self.indent+' fixed : None') if batch_size is None or batch_size >= self.n_samples: Xi = None F = lambda params : self.gradient(params[0],params[1]) else: if self.verbose > 0: print(self.indent+' batch size :',batch_size) def Xi(): indices = np.arange(self.n_samples) np.random.shuffle(indices) xi = { 'indices' : indices } return xi F = lambda params, indices: self.gradient(params[0],params[1], batch_size=batch_size, indices=indices) params = [self.embedding,np.array(self.projections)] params, H = gd.multiple(params,F,Xi=Xi,p=[None,self.proj.restrict], verbose=self.verbose,lr=lr, indent=self.indent+' ',**kwargs) self.embedding = params[0] self.projections = params[1] H['fixed_projections'] = False H['fixed_embedding'] = False self.computation_history.append(H) self.update() if self.verbose > 0: print(self.indent+f' final cost : {self.cost:0.2f}') costs = ', '.join(f'{x:0.2f}' for x in self.individual_cost) print(self.indent+f' individual costs : {costs}')