如何将图形从networkx加载到PyTorch几何图形中,并设置节点特征和标签? [英] How to load in graph from networkx into PyTorch geometric and set node features and labels?

查看:106
本文介绍了如何将图形从networkx加载到PyTorch几何图形中,并设置节点特征和标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:我正在尝试将图形networkx导入到PyTorch几何图形并设置标签和节点功能

(这是用Python编写的)

问题

  1. 如何进行[从networkx到PyTorch几何图形的转换]?(可能使用from_networkx函数)
  2. 如何转移节点功能和标签?(更重要的问题)

我看过其他/以前的一些关于此问题的帖子,但没有得到回答(如果我错了,请纠正我)。

尝试:(我刚才使用了下面一个不切实际的例子,因为我不能在这里发布任何真实的东西)

假设我们正在尝试对一组汽车执行图形学习任务(例如,节点分类)(正如我所说的,这不是很现实)。也就是说,我们有一组汽车、一个邻接矩阵和一些特性(例如年底的价格)。我们要预测节点标签(即汽车品牌)。

我将使用以下邻接矩阵:(抱歉,无法使用LaTeX格式化此矩阵)

A=[(0,1,0,1,1),(1,0,1,1,0),(0,1,0,0,1),(1,1,0,0,0),(1,0,1,0,0)]

代码如下(适用于Google Colab环境):

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
from torch_geometric.utils.convert import to_networkx, from_networkx
import torch

!pip install torch-scatter torch-sparse torch-cluster torch-spline-conv torch-geometric -f https://data.pyg.org/whl/torch-1.10.0+cpu.html

# Make the networkx graph
G = nx.Graph()

# Add some cars (just do 4 for now)
G.add_nodes_from([
      (1, {'Brand': 'Ford'}),
      (2, {'Brand': 'Audi'}),
      (3, {'Brand': 'BMW'}),
      (4, {'Brand': 'Peugot'}),
      (5, {'Brand': 'Lexus'}),
])

# Add some edges
G.add_edges_from([
                  (1, 2), (1, 4), (1, 5),
                  (2, 3), (2, 4),
                  (3, 2), (3, 5), 
                  (4, 1), (4, 2),
                  (5, 1), (5, 3)
])

# Convert the graph into PyTorch geometric
pyg_graph = from_networkx(G)

这样可以正确地将networkx图转换为PyTorch几何图形。但是,我仍然不知道如何正确设置标签。

每个节点的品牌值已转换并存储在:

pyg_graph.Brand

下面,我刚刚为每个节点制作了一些长度为5的随机数组(假设这些数组是真实的)。

ford_prices = np.random.randint(100, size = 5)
lexus_prices = np.random.randint(100, size = 5)
audi_prices = np.random.randint(100, size = 5)
bmw_prices = np.random.randint(100, size = 5)
peugot_prices = np.random.randint(100, size = 5)

这就引出了主要问题:

  • 如何将价格设置为此图表的节点功能?
  • 如何设置节点标签?(在培训网络时,我是否需要从pyg_graph.Brand中删除标签?)

提前感谢,节日快乐。

推荐答案

最简单的方法是将所有信息添加到网络x图中,然后按您需要的方式直接创建它。我猜你想用一些图形神经网络。那么您想要如下所示的内容。

  1. 您可能希望使用类别表示,而不是使用文本作为标签,例如,1代表福特。
  2. 如果要匹配";通常的约定";。然后将输入要素命名为x,将标签/基本事实命名为y
  3. 通过掩码将数据拆分成训练和测试。因此,该图仍然包含所有信息,但只有部分信息用于培训。有关使用CORA数据集的示例,请查看PyTorch Geometric introduction
import networkx as nx
import numpy as np
import torch
from torch_geometric.utils.convert import from_networkx


# Make the networkx graph
G = nx.Graph()

# Add some cars (just do 4 for now)
G.add_nodes_from([
      (1, {'y': 1, 'x': 0.5}),
      (2, {'y': 2, 'x': 0.2}),
      (3, {'y': 3, 'x': 0.3}),
      (4, {'y': 4, 'x': 0.1}),
      (5, {'y': 5, 'x': 0.2}),
])

# Add some edges
G.add_edges_from([
                  (1, 2), (1, 4), (1, 5),
                  (2, 3), (2, 4),
                  (3, 2), (3, 5),
                  (4, 1), (4, 2),
                  (5, 1), (5, 3)
])

# Convert the graph into PyTorch geometric
pyg_graph = from_networkx(G)

print(pyg_graph)
# Data(edge_index=[2, 12], x=[5], y=[5])
print(pyg_graph.x)
# tensor([0.5000, 0.2000, 0.3000, 0.1000, 0.2000])
print(pyg_graph.y)
# tensor([1, 2, 3, 4, 5])
print(pyg_graph.edge_index)
# tensor([[0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 4, 4],
#         [1, 3, 4, 0, 2, 3, 1, 4, 0, 1, 0, 2]])


# Split the data 
train_ratio = 0.2
num_nodes = pyg_graph.x.shape[0]
num_train = int(num_nodes * train_ratio)
idx = [i for i in range(num_nodes)]

np.random.shuffle(idx)
train_mask = torch.full_like(pyg_graph.y, False, dtype=bool)
train_mask[idx[:num_train]] = True
test_mask = torch.full_like(pyg_graph.y, False, dtype=bool)
test_mask[idx[num_train:]] = True

print(train_mask)
# tensor([ True, False, False, False, False])
print(test_mask)
# tensor([False,  True,  True,  True,  True])

这篇关于如何将图形从networkx加载到PyTorch几何图形中,并设置节点特征和标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆