PyG笔记

1. 如果把pyg转换成networkx时选择转换成无向图(to_undirected=True),则pyg的每一组edge_index一定要先写index小的node,再写index大的node,否则该条边会被直接忽略掉。

例1:转成有向图,正确

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

data = Data(x=x, edge_index=edge_index)

G1 = to_networkx(data, to_undirected=False)  # 有向图
nx.draw(G1)
plt.show()

PyG笔记_第1张图片


 例2:转成无向图,每条边用“index小的节点” → “index大的节点”,正确

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

data = Data(x=x, edge_index=edge_index)

G2 = to_networkx(data, to_undirected=True)  # 无向图
nx.draw(G2)
plt.show()

PyG笔记_第2张图片


 例3:转成无向图,把第一条边从 0→1改成了1→0,出错!

x = torch.tensor([[-1], 
                  [0], 
                  [1]], dtype=torch.float)
edge_index = torch.tensor([[1, 1],
                           [0, 2]], dtype=torch.long)  # 调换了第一条边两个node的顺序

data = Data(x=x, edge_index=edge_index)

G3 = to_networkx(data, to_undirected=True)  # 无向图
nx.draw(G3)
plt.show()

PyG笔记_第3张图片

2. 把data放在GPU上的代码:(注意:该函数返回的是这个对象自己)

device = torch.device('cuda')
data = data.to(device)  # 或者直接 data.to(device)即可

你可能感兴趣的:(python,python,深度学习,机器学习)