【PYG】Cora数据集简介

Planetoid 类是 PyTorch Geometric 中用于加载和处理著名的基准数据集(如 Cora、CiteSeer 和 PubMed)的一个工具。这些数据集是文献引用网络,每个节点表示一篇论文,每条边表示论文之间的引用关系。使用 Planetoid 类可以方便地加载这些数据集,并进行必要的预处理,如特征归一化。

Cora 数据集是图神经网络(GNN)领域中常用的基准数据集之一。它用于研究节点分类任务,特别是在文献引用网络中。

Cora 数据集简介

Cora 数据集由 2708 篇科学出版物组成,这些出版物被分为 7 个类别。节点代表论文,边代表引用关系。每个节点有一个 1433 维的特征向量,表示论文的词袋模型(Bag of Words),特征向量中的每个维度表示某个词汇是否出现在论文中。

数据集的具体结构:

节点(Nodes):2708 个节点,每个节点表示一篇论文。

边(Edges):5429 条边,每条边表示两篇论文之间的引用关系。

特征向量(Features):1433 维的二进制词袋模型。

标签(Labels):7 个类别,表示论文所属的领域。

应用:节点分类

节点分类任务的目标是根据节点的特征和图结构预测节点的类别。例如,在 Cora 数据集中,根据论文的特征和引用关系预测论文所属的领域

  • x:节点特征矩阵,形状为 [num_nodes, num_node_features],即x=[2708, 1433]
  • y:节点标签向量,形状为 [num_nodes],即y=[2708]
  • edge_index:边索引矩阵,形状为 [2, num_edges],即edge_index=[2, 10556](关于边的数量问题还需要进一步研究,5429 * 2 = 10858 条边不等于10556)
    • 在 Cora 数据集中,边是无向的。然而,在许多图神经网络库(包括 PyTorch Geometric)中,边通常被存储为有向边的形式。这意味着每条无向边在边列表中会被存储为两条有向边(一个从节点 A 到节点 B,另一个从节点 B 到节点 A)。因此,如果 Cora 数据集中有 5429 条无向边,当它们被存储为有向边时,总数会是 5429 * 2 = 10858 条边。这个解释可以解释为什么 num_edges 显示的数量是 10556 而不是 5429。
    • data.num_nodes: 节点的数量,应该是 2708。
      data.num_edges: 边的数量,应该是 10556(因为每条无向边在存储时变成了两条有向边)。
      data.is_undirected(): 检查图是否是无向的。这个方法会返回 True,表明原始图是无向的,尽管在内部存储时使用了有向边的表示。
  • Number of classes: 7对应标签(Labels):7 个类别,表示论文所属的领域。

代码示例:使用 Planetoid 类加载 Cora 数据集

以下是如何使用 Planetoid 类加载 Cora 数据集,并对特征进行归一化的示例代码:

python 复制代码
import torch
import torch.nn.functional as F
from torch_geometric.nn import GCNConv
from torch_geometric.datasets import Planetoid
from torch_geometric.transforms import NormalizeFeatures

# 加载并预处理Cora数据集
dataset = Planetoid(root='/tmp/Cora', name='Cora', transform=NormalizeFeatures())

# 打印数据集信息
print(f'Dataset: {dataset}')
print(f'Number of graphs: {len(dataset)}')
print(f'Number of features: {dataset.num_node_features}')
print(f'Number of classes: {dataset.num_classes}')

# 获取数据
data = dataset[0]

# 打印图结构和特征信息
print(data)
print(f'Number of nodes: {data.num_nodes}')
print(f'Number of edges: {data.num_edges}')
print(f'Average node degree: {data.num_edges / data.num_nodes:.2f}')
print(f'Training nodes: {data.train_mask.sum()}')
print(f'Validation nodes: {data.val_mask.sum()}')
print(f'Test nodes: {data.test_mask.sum()}')
print(f'Has isolated nodes: {data.has_isolated_nodes()}')
print(f'Has self-loops: {data.has_self_loops()}')
print(f'Is undirected: {data.is_undirected()}')

解释代码的每个部分

  1. 导入必要的库

    python 复制代码
    import torch
    import torch.nn.functional as F
    from torch_geometric.nn import GCNConv
    from torch_geometric.datasets import Planetoid
    from torch_geometric.transforms import NormalizeFeatures
  2. 加载并预处理 Cora 数据集

    python 复制代码
    dataset = Planetoid(root='/tmp/Cora', name='Cora', transform=NormalizeFeatures())
    • root 参数指定数据集存储的路径。
    • name 参数指定数据集的名称,这里是 Cora
    • transform 参数使用 NormalizeFeatures 对节点特征进行归一化处理。
  3. 打印数据集信息

    python 复制代码
    print(f'Dataset: {dataset}')
    print(f'Number of graphs: {len(dataset)}')
    print(f'Number of features: {dataset.num_node_features}')
    print(f'Number of classes: {dataset.num_classes}')
    • 打印数据集的基本信息,包括图的数量、节点特征的维数和类别的数量。
  4. 获取数据

    python 复制代码
    data = dataset[0]
    • 获取数据集中第一张图的数据(对于 Planetoid 数据集,只包含一张图)。
  5. 打印图结构和特征信息

    python 复制代码
    print(data)
    print(f'Number of nodes: {data.num_nodes}')
    print(f'Number of edges: {data.num_edges}')
    print(f'Average node degree: {data.num_edges / data.num_nodes:.2f}')
    print(f'Training nodes: {data.train_mask.sum()}')
    print(f'Validation nodes: {data.val_mask.sum()}')
    print(f'Test nodes: {data.test_mask.sum()}')
    print(f'Has isolated nodes: {data.has_isolated_nodes()}')
    print(f'Has self-loops: {data.has_self_loops()}')
    print(f'Is undirected: {data.is_undirected()}')
    • 打印图的数据结构,包括节点和边的数量、平均节点度、训练/验证/测试节点数量,以及是否存在孤立节点、自环和是否是无向图等信息。

数据集结构

在数据对象 data 中,有以下关键属性:

  • x:节点特征矩阵,形状为 [num_nodes, num_node_features]。
  • edge_index:边索引矩阵,形状为 [2, num_edges]。
  • y:节点标签向量,形状为 [num_nodes]。
  • train_mask:训练集掩码,形状为 [num_nodes]。
  • val_mask:验证集掩码,形状为 [num_nodes]。
  • test_mask:测试集掩码,形状为 [num_nodes]。

这些属性允许我们方便地进行图神经网络的训练和评估。

总结

通过 Planetoid 类加载 Cora 数据集,并对特征进行归一化,可以快速方便地准备数据,进行节点分类等任务。PyTorch Geometric 提供的工具使得处理图结构数据变得简单和高效,有助于我们在实际应用中充分利用图神经网络的强大能力。

程序运行结果

Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.x
Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.tx
Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.allx
Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.y
Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.ty
Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.ally
Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.graph
Downloading https://github.com/kimiyoung/planetoid/raw/master/data/ind.cora.test.index
Processing...
Done!
Dataset: Cora()
Number of graphs: 1
Number of features: 1433
Number of classes: 7
Data(x=[2708, 1433], edge_index=[2, 10556], y=[2708], train_mask=[2708], val_mask=[2708], test_mask=[2708])
Number of nodes: 2708
Number of edges: 10556
Average node degree: 3.90
Training nodes: 140
Validation nodes: 500
Test nodes: 1000
Has isolated nodes: False
Has self-loops: False
Is undirected: True

查看文件夹下内容 tree /tmp/Cora/Cora/

/tmp/Cora/Cora/
├── processed
│   ├── data.pt
│   ├── pre_filter.pt
│   └── pre_transform.pt
└── raw
    ├── ind.cora.allx
    ├── ind.cora.ally
    ├── ind.cora.graph
    ├── ind.cora.test.index
    ├── ind.cora.tx
    ├── ind.cora.ty
    ├── ind.cora.x
    └── ind.cora.y

2 directories, 11 files
相关推荐
不会&编程12 天前
论文阅读:A Generalization of Transformer Networks to Graphs
论文阅读·深度学习·transformer·gnn
shuaixio1 个月前
【VectorNet】vectornet网络学习笔记
gnn·自注意力机制·mlp·vectornet·子图构建·全局图构建
只是有点小怂3 个月前
【TORCH】torch.normal()中的size参数
gnn
只是有点小怂3 个月前
【PYG】简单分析Planetoid()中存储Cora数据集边的数量
gnn
只是有点小怂3 个月前
【PYG】Planetoid中边存储的格式,为什么打印前十条边用edge_index[:, :10]
gnn
盼小辉丶4 个月前
图神经网络实战(12)——图同构网络(Graph Isomorphism Network, GIN)
深度学习·图神经网络·gnn
人工智能培训咨询叶梓5 个月前
秒懂图神经网络(GNN)
人工智能·深度学习·神经网络·机器学习·语言模型·gnn·人工智能培训
Karen_Yu_5 个月前
【intro】Graph Isomorphism Network(GIN)
人工智能·神经网络·gin·gnn
盼小辉丶5 个月前
图神经网络实战(9)——GraphSAGE详解与实现
pytorch·图神经网络·gnn