用NetWorkX绘制DGL图

这里用DGL链接预测的例子,绘制前50个边构成的有向图

u, v = g.edges()
print("In drawing......")
u1=u.numpy()
v1=v.numpy()
utemp=[]
vtemp=[]
nodes=[]
for i in range(50):
    utemp.append(u1[i])
for i in range(50):
    vtemp.append(v1[i])
u_temp=torch.tensor(utemp)
v_temp=torch.tensor(vtemp)
nodes=utemp+vtemp

g_temp = nx.DiGraph()
adj_temp = sp.coo_matrix((np.ones(len(utemp)), (utemp, vtemp))) ## 传入的参数(data, (row, col))
S = adj_temp
edges=np.r_[[S.row], [S.col]].T
g_temp.add_edges_from(edges)
g_temp.add_nodes_from(nodes)

nx.draw(g_temp, with_labels=True)
plt.show()

效果:
用NetWorkX绘制DGL图_第1张图片

你可能感兴趣的:(知识图谱,pytorch)