"git@developer.sourcefind.cn:renzhc/diffusers_dcu.git" did not exist on "24c5e7708bb75076dd8e79ccaea195640555f945"
Unverified Commit 03839413 authored by Hongzhi (Steve), Chen's avatar Hongzhi (Steve), Chen Committed by GitHub
Browse files

[Sparse] Add page rank example to DGL sparse Api. (#6077)


Co-authored-by: default avatarUbuntu <ubuntu@ip-172-31-28-63.ap-northeast-1.compute.internal>
parent 47d37e91
import dgl.sparse as dglsp
import networkx as nx
import torch
N = 100
DAMP = 0.85
K = 10
def pagerank(A):
D = A.sum(0)
V = torch.ones(N) / N
for _ in range(K):
########################################################################
# (HIGHLIGHT) Take the advantage of DGL sparse APIs to calculate the
# page rank.
########################################################################
V = (1 - DAMP) / N + DAMP * A @ (V / D)
return V
if __name__ == "__main__":
g = nx.erdos_renyi_graph(N, 0.05, seed=10086)
# Create the adjacency matrix of graph.
edges = list(g.to_directed().edges())
indices = torch.tensor(edges).transpose(0, 1)
A = dglsp.spmatrix(indices, shape=(N, N))
V = pagerank(A)
print(V)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment