def forward(self, x): radius = 1.2 adjacency_method = 'cyflann' adjacency_kwds = {'radius': radius} affinity_method = 'gaussian' affinity_kwds = {'radius': radius} laplacian_method = 'symmetricnormalized' laplacian_kwds = {'scaling_epps': radius} geom = Geometry(adjacency_method=adjacency_method, adjacency_kwds=adjacency_kwds, affinity_method=affinity_method, affinity_kwds=affinity_kwds, laplacian_method=laplacian_method, laplacian_kwds=laplacian_kwds) x = x.view(100, -1) #print(x.shape) geom.set_data_matrix(x) lle = LocallyLinearEmbedding(n_components=10, eigen_solver='dense', geom=geom) embed_lle = lle.fit_transform(x) embed_lle = torch.from_numpy(embed_lle).float() x = embed_lle.view(-1, 10) x = self.fc(x) return x
def forward(self, x): #print('x in:' , x.shape) rad1 = 0.7272 radius = rad1 adjacency_method = 'cyflann' adjacency_kwds = {'radius': radius} affinity_method = 'gaussian' affinity_kwds = {'radius': radius} laplacian_method = 'symmetricnormalized' laplacian_kwds = {'scaling_epps': radius} geom = Geometry(adjacency_method=adjacency_method, adjacency_kwds=adjacency_kwds, affinity_method=affinity_method, affinity_kwds=affinity_kwds, laplacian_method=laplacian_method, laplacian_kwds=laplacian_kwds) x = x.view(100, -1) #print('before embedding:', x.shape) geom.set_data_matrix(x) spectral = SpectralEmbedding(n_components=10, eigen_solver='amg', geom=geom, drop_first=False) # use 3 for spectral embed_spectral = spectral.fit_transform(x) embed_spectral = torch.from_numpy(embed_spectral).float() #print('x_totorch:', embed_spectral.shape) x = embed_spectral.view(-1, 10) x = self.fc(x) return x
def compute_geom_brute(self, diffusion_time, n_neighbors): data = self.data dim = self.dim #set radius according to paper (i think this is dim not d) radius = (diffusion_time * (diffusion_time * np.pi * 4)**(dim/2))**(0.5) #set adjacency radius large enough that points beyond it have affinity close to zero bigradius = 3 * radius adjacency_method = 'brute' adjacency_kwds = {'radius':bigradius} affinity_method = 'gaussian' affinity_kwds = {'radius':radius} laplacian_method = 'geometric' laplacian_kwds = {'scaling_epps':1} geom = Geometry(adjacency_method=adjacency_method, adjacency_kwds=adjacency_kwds, affinity_method=affinity_method, affinity_kwds=affinity_kwds, laplacian_method=laplacian_method, laplacian_kwds=laplacian_kwds) geom.set_data_matrix(data) geom.adjacency_matrix = geom.compute_adjacency_matrix() geom.laplacian_matrix = geom.compute_affinity_matrix() geom.laplacian_matrix = self.get_laplacian(geom, radius) return(geom)
def compute_geom(self, diffusion_time, n_neighbors): data = self.data dim = self.dim #set radius according to paper (i think this is dim not d) radius = (diffusion_time * (diffusion_time * np.pi * 4)**(dim/2))**(0.5) #set adjacency radius large enough that points beyond it have affinity close to zero bigradius = 3 * radius adjacency_method = 'cyflann' cyflann_kwds = {'index_type':'kdtrees', 'num_trees':10, 'num_checks':n_neighbors} adjacency_kwds = {'radius':bigradius, 'cyflann_kwds':cyflann_kwds} affinity_method = 'gaussian' affinity_kwds = {'radius':radius} laplacian_method = 'geometric' laplacian_kwds = {'scaling_epps':radius} geom = Geometry(adjacency_method=adjacency_method, adjacency_kwds=adjacency_kwds, affinity_method=affinity_method, affinity_kwds=affinity_kwds, laplacian_method=laplacian_method, laplacian_kwds=laplacian_kwds) geom.set_data_matrix(data) adjacency_matrix = geom.compute_adjacency_matrix() laplacian_matrix = geom.compute_laplacian_matrix() return(geom)
# Affinity an NxN (sparse) pairwise matrix insicated similarity between points # Laplacian an NxN (sparse) pairwsie matrix containing geometric manifold information radius = 5 adjacency_method = 'cyflann' adjacency_kwds = {'radius': radius} # ignore distances above this radius affinity_method = 'gaussian' affinity_kwds = {'radius': radius} # A = exp(-||x - y||/radius^2) laplacian_method = 'geometric' laplacian_kwds = { 'scaling_epps': radius } # scaling ensures convergence to Laplace-Beltrami operator geom = Geometry(adjacency_method=adjacency_method, adjacency_kwds=adjacency_kwds, affinity_method=affinity_method, affinity_kwds=affinity_kwds, laplacian_method=laplacian_method, laplacian_kwds=laplacian_kwds) # You can/should also use the set_data_matrix, set_adjacency_matrix, set_affinity_matrix # to send your data set (in whichever form it takes) this way. geom.set_data_matrix(X) # You can get the distance, affinity etc with e.g: Geometry.get_distance_matrix() # you can update the keyword arguments passed inially using these functions adjacency_matrix = geom.compute_adjacency_matrix() # by defualt this is pass-by-reference. Use copy=True to get a copied version. # If you don't want to pre-compute a Geometry you can pass a dictionary or geometry # arguments to one of the embedding classes. geom = {
random.seed(0) workingdirectory = os.popen('git rev-parse --show-toplevel').read()[:-1] os.chdir(workingdirectory) radius = 50 adjacency_method = 'cyflann' adjacency_kwds = {'radius': radius} affinity_method = 'gaussian' affinity_kwds = {'radius': radius} laplacian_method = 'symmetricnormalized' laplacian_kwds = {'scaling_epps': radius} dataname = workingdirectory + '/untracked_data/chemistry_data/tolueneangles020619_pca50' #dataname = '/Users/samsonkoelle/Downloads/manigrad-100818/mani-samk-gradients/untracked_data/chemistry_data/ethanolangles022119_pca50' data = np.load(dataname + '.npy') geom = Geometry(adjacency_method=adjacency_method, adjacency_kwds=adjacency_kwds, affinity_method=affinity_method, affinity_kwds=affinity_kwds, laplacian_method=laplacian_method, laplacian_kwds=laplacian_kwds) geom.set_data_matrix(data) geom.laplacian_method = 'geometric' geom.laplacian_kwds = {'scaling_epps': radius} laplacian_matrix = geom.compute_laplacian_matrix() geom.compute_laplacian_matrix() sample = np.arange(0, data.shape[0], 1000) distorion_vs_rad_dim2 = run_estimate_radius(data, geom.adjacency_matrix, sample=sample, d=1, rmin=1, rmax=10, ntry=50,
ax = fig.add_subplot(111, projection='3d') ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=color, s=1) plt.title('orignal data') plt.show() plt.savefig('tutorial_data_plot.png', format='png') plt.close() ## The Geometry Class from megaman.geometry import Geometry ''' Geometry is the main class that will Cache things like distance/adjacency, affinity, and laplacian. You instantiate the Geometry class with ''' geom = Geometry() ''' At class instantiation you can also pass parameters & methods for the three main components: Adjacency: an NxN (sparse) pairwise matrix indicating neighborhood regions Affinity an NxN (sparse) pairwise matrix insicated similarity between points Laplacian an NxN (sparse) pairwsie matrix containing geometric manifold information These parameters can also be overwritten when using the individual calculation functions so that you don't have to re-initialize every time you want to change a parameter. If you already have fixed parameters then you can initialize with, for example: '''