論文模型理解


論文模型復現過程
!pip install pgl !pip install cython
import scipy.sparse as sp degree=dataset.graph.indegree() norm = np.zeros_like(degree, dtype="float32") norm[degree > 0] = np.power(degree[degree > 0],-1.0) dataset.graph.node_feat["norm"] = np.expand_dims(norm, -1) def row_normalize(mx): """Row-normalize sparse matrix""" rowsum = np.array(mx.sum(1)) r_inv = np.power(rowsum, -1).flatten() r_inv[np.isinf(r_inv)] = 0. r_mat_inv = sp.diags(r_inv) mx = r_mat_inv.dot(mx) return mx def aug_normalized_adjacency(adj): adj = adj + sp.eye(adj.shape[0]) adj = sp.coo_matrix(adj) row_sum = np.array(adj.sum(1)) d_inv_sqrt = np.power(row_sum, -0.5).flatten() d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0. d_mat_inv_sqrt = sp.diags(d_inv_sqrt) return d_mat_inv_sqrt.dot(adj).dot(d_mat_inv_sqrt).tocoo()

def pre_sgc(dataset,feature,norm=None): """ Implementation of Simplifying graph convolutional networks (SGC) This is an implementation of the paper SEMI-SUPERVISED CLASSIFICATION WITH Simplifying graph convolutional networks (https://arxiv.org/abs/1902.07153). Args: gw: Graph wrapper object (:code:`StaticGraphWrapper` or :code:`GraphWrapper`) feature: A tensor with shape (num_nodes, feature_size). output: The output size for sgc. activation: The activation for the output name: Gcn layer names. norm: If :code:`norm` is not None, then the feature will be normalized. Norm must be tensor with shape (num_nodes,) and dtype float32. Return: A tensor with shape (num_nodes, hidden_size) """ num_nodes=np.shape(feature)[0] adj=np.zeros((num_nodes,num_nodes)) #print(np.shape(dataset.graph.edges)) for u in range(len(dataset.graph.edges)): adj[dataset.graph.edges[u][0],dataset.graph.edges[u][1]]=adj[dataset.graph.edges[u][1],dataset.graph.edges[u][0]]=1 feature=dataset.graph.node_feat['words'] feature=row_normalize(feature) if norm==True: adj=aug_normalized_adjacency(adj) for i in range(args.degree): feature=adj.dot(feature) return feature

