首页 > 解决方案 > 如何在 Python 中可视化 torch_geometric 图?

问题描述

让我们考虑一个例子,我有以下坐标格式的邻接矩阵:

> edge_index.numpy() = array([[    0,     1,     0,   3,   2],
                              [    1,     0,     3,   2,   1]], dtype=int64)

这意味着节点0链接到节点1,反之亦然,节点0链接到3等等......

你知道用 nx.draw() 在 networkx 中绘制这个图的方法吗?

谢谢你。

标签: graphpytorchdraw

解决方案


import networkx as nx

edge_index = torch.tensor([[0, 1, 1, 2],
                           [1, 0, 2, 1]], dtype=torch.long)
x = torch.tensor([[-1], [0], [1]], dtype=torch.float)

data = torch_geometric.data.Data(x=x, edge_index=edge_index)
g = torch_geometric.utils.to_networkx(data, to_undirected=True)
nx.draw(g)

推荐阅读