PyG下载、处理、探索Cora、Citeseer、Pubmed数据集【PyTorch geometric】

PyG图数据集加载

发现PyG已经有了封装好的数据加载、预处理模块了。感觉自己之前处理Cora、Citeseer、Pubmed都白搞了。所以现在我决定站在巨人的肩膀上😂,PyG大法好啊!

参考资料:https://pytorch-geometric.readthedocs.io/en/latest/notes/introduction.html
需要的第三方库

  • torch
  • torch_geometric

我的代码:https://github.com/ytchx1999/GNN-Dataset/blob/main/Citation.ipynb


from torch_geometric.datasets import Planetoid
import torch

1.Cora数据集的处理

1.1 下载数据集

# 下载并保存预处理的数据集
dataset_cora = Planetoid(root='./cora/', name='Cora')
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!
# 打印数据集
print(dataset_cora)
Cora()

1.2 法一:使用[0]方式从dataset中提取data

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)
cpu
# 提取data,并转换为device格式
data_cora = dataset_cora[0].to(device)
# 打印dataset的属性
print(dataset_cora.num_classes)  # 标签的类别数量
print(dataset_cora.num_node_features)  # 节点特征的维度
print(len(dataset_cora))  # 数据集图的个数
# 打印data
print(data_cora)
7
1433
1
Data(edge_index=[2, 10556], test_mask=[2708], train_mask=[2708], val_mask=[2708], x=[2708, 1433], y=[2708])

1.3 查看data的各项属性

# 提取各项属性
x = data_cora.x  # 节点的特征矩阵[N,input_dim]
edge_index = data_cora
<think> 我们正在回答关于PyTorch Geometric经典数据集的问题。PyTorch GeometricPyG)是一个基于PyTorch图神经网络库,提供了许多常用的图数据集。 经典数据集包括: 1. Cora:引文网络数据集,包含2708篇科学论文,分为7类,引用关系作为边。 2. Citeseer:类似Cora,包含3327篇论文,分为6类。 3. Pubmed:生物医学文献数据集,包含19717篇论文,分为3类。 4. PPI(蛋白质相互作用):包含24张图,每张图对应一个人体组织,用于多任务节点分类。 5. Reddit:大规模的帖子-评论图,用于社区检测。 6. QM9:小分子数据集,包含133,885个有机分子的几何结构和性质,用于图回归任务。 7. ModelNet10/40:3D点云数据集,用于3D物体分类。 8. ShapeNet:大型3D模型数据集,用于语义分割。 这些数据集可以通过PyG的`torch_geometric.datasets`模块直接加载。 注意:回答后必须按照要求提出3-5个相关问题,且第一行是“§§相关问题§§”,然后列出问题。 </think> PyTorch GeometricPyG)是一个基于PyTorch图神经网络库,提供了多种经典图结构数据集,可直接通过`torch_geometric.datasets`模块加载。以下是主要经典数据集及其特点: 1. **Cora** - **类型**:引文网络 - **内容**:2,708篇机器学习论文节点,5,429条引用关系边,节点特征为词袋向量 - **任务**:节点分类(7个类别) - **加载代码**: ```python from torch_geometric.datasets import Planetoid dataset = Planetoid(root='/tmp/Cora', name='Cora') ``` 2. **Citeseer** - **类型**:引文网络 - **内容**:3,327篇论文节点,4,732条边,节点特征为词频向量 - **任务**:节点分类(6个类别) 3. **Pubmed** - **类型**:生物医学文献网络 - **内容**:19,717篇论文节点,44,338条边,节点特征为TF-IDF向量 - **任务**:节点分类(3个疾病类别) 4. **PPI (Protein-Protein Interaction)** - **类型**:蛋白质相互作用网络 - **内容**:24个子图(对应人体组织),共56,944个蛋白质节点,818,716条边 - **任务**:多任务节点分类(121个标签) 5. **Reddit** - **类型**:社交网络 - **内容**:1个社区大图(232,965个帖子节点,11,606,919条边) - **任务**:社区分类(41个板块) - **加载代码**: ```python from torch_geometric.datasets import Reddit dataset = Reddit(root='/tmp/Reddit') ``` 6. **QM9** - **类型**:分子图 - **内容**:133,885个有机小分子,包含原子类型(节点)和化学键(边) - **任务**:分子性质回归(19个目标值如能量、极化率) 7. **ModelNet10/40** - **类型**:3D点云 - **内容**:4,899个(ModelNet10)或12,311个(ModelNet40)CAD模型 - **任务**:3D物体分类(10或40个类别) 这些数据集覆盖了节点分类、图分类、链接预测、3D处理图神经网络核心任务,是测试GNN模型的基准工具。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值