环境配置与PyG的使用
PyTorch Geometric (PyG)是面向几何深度学习的PyTorch的扩展库,几何深度学习指的是应用于图和其他不规则、非结构化数据的深度学习。基于PyG库,我们可以轻松地根据数据生成一个图对象,然后很方便的使用它;我们也可以容易地为一个图数据集构造一个数据集类,然后很方便的将它用于神经网络。
环境配置
-
使用nvidia-smi命令检查显卡驱动是否正确安装
-
安装正确版本的pytorch和cudatoolkit,此处以pytorch1.8.1和cudatoolkit11.1版本为例
确认是否正确安装,通过以下代码查看版本号
python -c "import torch; print(torch.__version__)"
# 1.8.1
python -c "import torch; print(torch.version.cuda)"
# 11.1
- 安装PyG
pip install torch-scatter -f https://pytorch-geometric.com/whl/torch-1.8.0+cu111.html
pip install torch-sparse -f https://pytorch-geometric.com/whl/torch-1.8.0+cu111.html
pip install torch-cluster -f https://pytorch-geometric.com/whl/torch-1.8.0+cu111.html
pip install torch-spline-conv -f https://pytorch-geometric.com/whl/torch-1.8.0+cu111.html
pip install torch-geometric
不同硬件环境安装方法可参考Installation of of PyTorch Geometric 。
Data类
官方文档torch_geometric.data.Data。
Data
类的构造函数:
class Data(object):
def __init__(self, x=None, edge_index=None, edge_attr=None, y=None, **kwargs):
r"""
Args:
x (Tensor, optional): 节点属性矩阵,大小为`[num_nodes, num_node_features]`
edge_index (LongTensor, optional): 边索引矩阵,大小为`[2, num_edges]`,第0行为尾节点,第1行为头节点,头指向尾
edge_attr (Tensor, optional): 边属性矩阵,大小为`[num_edges, num_edge_features]`
y (Tensor, optional): 节点或图的标签,任意大小(,其实也可以是边的标签)
"""
self.x = x
self.edge_index = edge_index
self.edge_attr = edge_attr
self.y = y
for key, item in kwargs.items():
if key == 'num_nodes':
self.__num_nodes__ = item
else:
self[key] = item
edge_index
的每一列定义一条边,其中第一行为边起始节点的索引,第二行为边结束节点的索引。这种表示方法被称为COO格式(coordinate format),通常用于表示稀疏矩阵。PyG不是用稠密矩阵
A
∈
{
0
,
1
}
∣
V
∣
×
∣
V
∣
\mathbf{A} \in \{ 0, 1 \}^{|\mathcal{V}| \times |\mathcal{V}|}
A∈{0,1}∣V∣×∣V∣来持有邻接矩阵的信息,而是用仅存储邻接矩阵
A
\mathbf{A}
A中非
0
0
0元素的稀疏矩阵来表示图。
通常,一个图至少包含x, edge_index, edge_attr, y, num_nodes
5个属性,当图包含其他属性时,我们可以通过指定额外的参数使Data
对象包含其他的属性:
graph = Data(x=x, edge_index=edge_index, edge_attr=edge_attr, y=y, num_nodes=num_nodes, other_attr=other_attr)
Data
对象与dict
类型的转换
graph_dict = {
'x': x,
'edge_index': edge_index,
'edge_attr': edge_attr,
'y': y,
'num_nodes': num_nodes,
'other_attr': other_attr
}
graph_data = Data.from_dict(graph_dict)
注意:graph_dict
中属性值的类型与大小的要求与Data
类的构造函数的要求相同。
我们可以将Data
对象转换为dict
对象或转换为namedtuple
def to_dict(self):
return {key: item for key, item in self}
def to_namedtuple(self):
keys = self.keys
DataTuple = collections.namedtuple('DataTuple', keys)
return DataTuple(*[self[key] for key in keys])
# 获取`Data`对象属性
x = graph_data['x']
# 设置`Data`对象属性
graph_data['x'] = x
# 获取`Data`对象包含的属性的关键字
graph_data.keys()
# 对边排序并移除重复的边
graph_data.coalesce()
Data
对象的其他性质
我们通过观察PyG中内置的一个图来查看Data
对象的性质:
from torch_geometric.datasets import KarateClub
dataset = KarateClub()
data = dataset[0] # Get the first graph object.
print(data)
# 获取图的一些信息
print(f'Number of nodes: {data.num_nodes}') # 节点数量
print(f'Number of edges: {data.num_edges}') # 边数量
print(f'Number of node features: {data.num_node_features}') # 节点属性的维度
print(f'Number of node features: {data.num_features}') # 同样是节点属性的维度
print(f'Number of edge features: {data.num_edge_features}') # 边属性的维度
print(f'Average node degree: {data.num_edges / data.num_nodes:.2f}') # 平均节点度
print(f'if edge indices are ordered and do not contain duplicate entries.: {data.is_coalesced()}') # 是否边是有序的同时不含有重复的边
print(f'Number of training nodes: {data.train_mask.sum()}') # 用作训练集的节点
print(f'Training node label rate: {int(data.train_mask.sum()) / data.num_nodes:.2f}') # 用作训练集的节点的数量
print(f'Contains isolated nodes: {data.contains_isolated_nodes()}') # 此图是否包含孤立的节点
print(f'Contains self-loops: {data.contains_self_loops()}') # 此图是否包含自环的边
print(f'Is undirected: {data.is_undirected()}') # 此图是否是无向图
Dataset
类
PyG内置了大量常用的基准数据集,接下来我们以PyG内置的Planetoid
数据集为例来学习
Planetoid
数据集类的官方文档torch_geometric.datasets.Planetoid。
生成数据集对象并分析数据集
如下方代码所示,在第一次生成PyG内置的数据集时,程序自动下载原始文件,然后将原始文件处理成包含Data
对象的Dataset
对象并保存到文件。
from torch_geometric.datasets import Planetoid
dataset = Planetoid(root='dataset/Cora', name='Cora')
# Cora()
len(dataset)
# 1
# 分类数量
dataset.num_classes
# 7
# 属性维度
dataset.num_node_features
# 1433
分析数据集中样本
data = dataset[0]
# Data(edge_index=[2, 10556], test_mask=[2708],
# train_mask=[2708], val_mask=[2708], x=[2708, 1433], y=[2708])
data.is_undirected()
# True
data.train_mask.sum().item()
# 140
data.val_mask.sum().item()
# 500
data.test_mask.sum().item()
# 1000
现在我们看到该数据集包含的唯一的图,有2708个节点,节点特征为1433维,有10556条边,有140个用作训练集的节点,有500个用作验证集的节点,有1000个用作测试集的节点。PyG内置的其他数据集,请小伙伴一一试验,以观察不同数据集的不同。
数据集的使用
假设我们定义好了一个图神经网络模型,其名为Net
。在下方的代码中,我们展示了节点分类图数据集在训练过程中的使用。
model = Net().to(device)
data = dataset[0].to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)
model.train()
for epoch in range(200):
optimizer.zero_grad()
out = model(data)
loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask])
loss.backward()
optimizer.step()
自定义Data示例
下面的示例实现了继承Data
的一个类,专门用于表示“机构-作者-论文”的网络。该网络包含“机构“、”作者“和”论文”三类节点,以及“作者-机构“和“作者-论文“两类边。这里用了不同的属性存储不同节点的属性,用不同的属性存储不同的边(边没有属性),对不同节点用单独的方法获取其节点数量。
from torch_geometric.data import Data
class NetData(Data):
def __init__(self, organization=None, author=None, paper=None,
edge_author_organization_index=None,
edge_author_paper_index=None,
y=None, **kwargs):
# 机构 节点属性矩阵
self.organization = organization
# 作者 节点属性矩阵
self.author = author
# 论文 节点属性矩阵
self.paper = paper
# 作者-机构 边索引矩阵
self.edge_author_organization_index = edge_author_organization_index
# 作者-论文 边索引矩阵
self.edge_author_paper_index = edge_author_paper_index
self.y=y
for key, item in kwargs.items():
if key == 'num_nodes':
self.__num_nodes__ = item
else:
self[key] = item
# 机构节点数量
def num_organization_nodes(self):
return self.organization.shape[0]
# 作者节点数量
def num_author_nodes(self):
return self.author.shape[0]
# 论文节点数量
def num_paper_nodes(self):
return self.author.shape[0]