Source code for gemben.embedding.lap

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import scipy.io as sio
import scipy.sparse as sp
import scipy.sparse.linalg as lg
from time import time


from .static_graph_embedding import StaticGraphEmbedding
from gemben.utils import graph_util, plot_util
from gemben.evaluation import visualize_embedding as viz


[docs]class LaplacianEigenmaps(StaticGraphEmbedding): """`Laplacian Eigenmaps`_. LaplacianEigenmaps penalizes the weighted square of distance between neighbors. This is equivalent to factorizing the normalized Laplacian matrix. Args: hyper_dict (object): Hyper parameters. kwargs (dict): keyword arguments, form updating the parameters Examples: >>> from gemben.embedding.lap import LaplacianEigenmaps >>> edge_f = 'data/karate.edgelist' >>> G = graph_util.loadGraphFromEdgeListTxt(edge_f, directed=False) >>> G = G.to_directed() >>> res_pre = 'results/testKarate' >>> graph_util.print_graph_stats(G) >>> t1 = time() >>> embedding = LaplacianEigenmaps(2) >>> embedding.learn_embedding(graph=G, edge_f=None, is_weighted=True, no_python=True) >>> print('Laplacian Eigenmaps:Training time: %f' % (time() - t1)) >>> viz.plot_embedding2D(embedding.get_embedding(), di_graph=G, node_colors=None) >>> plt.show() .. _Laplacian Eigenmaps: http://web.cse.ohio-state.edu/~belkin.8/papers/LEM_NIPS_01.pdf """ def __init__(self, *hyper_dict, **kwargs): ''' Initialize the LaplacianEigenmaps class Args: d: dimension of the embedding ''' hyper_params = { 'method_name': 'lap_eigmap_svd' } hyper_params.update(kwargs) for key in hyper_params.keys(): self.__setattr__('_%s' % key, hyper_params[key]) for dictionary in hyper_dict: for key in dictionary: self.__setattr__('_%s' % key, dictionary[key])
[docs] def get_method_name(self): return self._method_name
[docs] def get_method_summary(self): return '%s_%d' % (self._method_name, self._d)
[docs] def learn_embedding(self, graph=None, edge_f=None, is_weighted=False, no_python=False): if not graph and not edge_f: raise Exception('graph/edge_f needed') if not graph: graph = graph_util.loadGraphFromEdgeListTxt(edge_f) graph = graph.to_undirected() t1 = time() L_sym = nx.normalized_laplacian_matrix(graph) try: w, v = lg.eigs(L_sym, k=self._d + 1, which='SM') t2 = time() self._X = v[:, 1:] p_d_p_t = np.dot(v, np.dot(np.diag(w), v.T)) eig_err = np.linalg.norm(p_d_p_t - L_sym) print('Laplacian matrix recon. error (low rank): %f' % eig_err) return self._X, (t2 - t1) except: print('SVD did not converge. Assigning random emebdding') self._X = np.random.randn(L_sym.shape[0], self._d) t2 = time() return self._X, (t2 - t1)
[docs] def get_embedding(self): return self._X
[docs] def get_edge_weight(self, i, j): return np.exp( -np.power(np.linalg.norm(self._X[i, :] - self._X[j, :]), 2) )
[docs] def get_reconstructed_adj(self, X=None, node_l=None): if X is not None: node_num = X.shape[0] self._X = X else: node_num = self._node_num adj_mtx_r = np.zeros((node_num, node_num)) for v_i in range(node_num): for v_j in range(node_num): if v_i == v_j: continue adj_mtx_r[v_i, v_j] = self.get_edge_weight(v_i, v_j) return adj_mtx_r
if __name__ == '__main__': # load Zachary's Karate graph edge_f = 'data/karate.edgelist' G = graph_util.loadGraphFromEdgeListTxt(edge_f, directed=False) G = G.to_directed() res_pre = 'results/testKarate' graph_util.print_graph_stats(G) t1 = time() embedding = LaplacianEigenmaps(2) embedding.learn_embedding(graph=G, edge_f=None, is_weighted=True, no_python=True) print('Laplacian Eigenmaps:\n\tTraining time: %f' % (time() - t1)) viz.plot_embedding2D(embedding.get_embedding(), di_graph=G, node_colors=None) plt.show()