学习周报
姓名 | 孙琪 | 时间范围 | 2025.04.01-2025.04.13 | |
周次 | 第7周 | 研究方向 | 数据集、调用模型训练 | |
本周完成工作 | 1. 通过Planetoid下载Cora、Citeseer、PubMed数据集,并展示其相关统计学信息。其中Cora还尝试了手动下载数据集,并对该数据集进行简单处理。全部代码详见下方笔记。 2. 分别用MLP和GCN跑了Cora和Citeseer。 3. 写了毕设论文的文献综述部分。 | |||
本周 问题汇报 | 1. 通过Planetoid下载Cora、Citeseer、PubMed数据集报错:应该修改Planetoid 源码,具体操作详见下方链接: https://blog.csdn.net/Daomiyo/article/details/136432295 2. 测试函数的最初命名为test(),导致在进行迭代训练的时候print(f"Epoch:{epoch}, loss:{loss:4f}, test_acc:{test_acc}")无法正确输出。原因是该文件并不是以普通的 Python 脚本方式运行,而是被当作pytest的测试用例执行了。改为evaluate()可解决。 3. MLP训练结果:Epoch:400, loss:0.315931, test_acc:0.592(左图) 4. GCN训练结果:Epoch:100, loss:0.579887, test_acc:0.815(右图) | |||
下周工作计划 | 1. 完成论文第一章的内容书写及润色。 2. 完成LLM相关模块的初步搭建。 | |||
学习笔记
【GNN数据集】
数据集 | Nodes | Edges | Features | Classes | Edge Type |
Cora | 2708 | 5429 | 1433 | 7 | 无向图 |
Citeseer | 3327 | 4732 | 3703 | 6 | 无向图 |
PubMed | 19717 | 44338 | 500 | 3 | 无向图 |
Ogbn-Arxiv | 169343 | 1166243 | 128 | 40 | 有向图 |
Ogbn-Products | 2449029 | 61859140 | 100 | 47 | 无向图 |
232965 | 11606919 | 602 | 50 | 无向图 |
数据集 | Nodes | Edges | Features | Classes | Edge Type |
Cora | 2708 | 5429 | 1433 | 7 | 无向图 |
Citeseer | 3327 | 4732 | 3703 | 6 | 无向图 |
PubMed | 近2万 | 4万多 | 500 | 3 | 无向图 |
Ogbn-Arxiv | 近16万 | 近117万 | 128 | 40 | 有向图 |
Ogbn-Products | 近245万 | 6千万 | 100 | 47 | 无向图 |
23万多 | 1千万 | 602 | 50 | 无向图 |
Cora数据集
·该数据集为2708个机器学习论文,被分成7类。每个出版物为一个点,每个点为1433维向量,每个类别只有20个点有标注,最终要对每个点进行分类。
(1).content文件是论文描述:<paper_id> <word_attributes>+ <class_label>
每行(其实就是图的一个节点)的第一个字段是论文的唯一字符串标识,后跟1433个字段(取值为二进制值),表示1433个词汇中的每个单词在文章中是存在(由1表示)还是不存在(由0表示)。最后,该行的最后一个字段表示论文的类别标签(7个)。因此该数据的特征应该有1433个维度,另外加上第一个字段idx,最后一个字段label, 一共有1433 + 2个维度。
(2).cites文件包含语料库的引用关系图,每行(图的一条边)描述一个引用关系:<被引论文编号> <引论文编号>
每行包含两个paper id。第一个字段是被引用论文的标识,第二个字段代表引用的论文。引用关系的方向是从右向左。如果一行由“论文1 论文2”表示,则“论文2 引用 论文1”,即链接是“论文2 - >论文1”。可以通过论文之间的链接(引用)关系建立邻接矩阵adj。
# 方法一:手动下载Cora并进行print相关信息。
import pandas as pd import numpy as np from torch_geometric.datasets import Planetoid from torch_geometric.transforms import NormalizeFeatures # 读取 cora.content 文件,该文件包含节点的特征和标签 raw_data = pd.read_csv('cora/cora.content', sep='\t', header=None) print("cora.content shape: ", raw_data.shape) # 读取 cora.cites 文件,该文件包含节点之间的引用关系 raw_data_cites = pd.read_csv('cora/cora.cites', sep='\t', header=None) print("cora.cites shape: ", raw_data_cites.shape) # 提取特征部分,选择从第二列到倒数第二列的所有列 features = raw_data.iloc[:,1:-1] print("features shape: ", features.shape) # 对标签进行独热编码(One-Hot Encoding) labels = pd.get_dummies(raw_data[1434]) print("-----前三个样本的独热编码-----") print("label: ", labels.head(3)) # 重新读取 cora.content 文件以获取节点数量 raw_data = pd.read_csv('cora/cora.content', sep='\t', header=None) num_nodes = raw_data.shape[0] # 将节点编号重新映射为连续的整数 [0, num_nodes-1] new_id = list(raw_data.index) # DataFrame 的索引 id = list(raw_data.iloc[:, 0]) # 第一列的原始节点编号 mapping = dict(zip(id, new_id)) # 创建原始ID到新ID的映射字典 # 再次读取 cora.cites 文件 raw_data_cites = pd.read_csv('cora/cora.cites', sep='\t', header=None) # 根据节点数量定义邻接矩阵的维度 matrix = np.zeros((num_nodes, num_nodes)) # 根据引用关系构建邻接矩阵 for i, j in zip(raw_data_cites.iloc[:, 0], raw_data_cites.iloc[:, 1]): x = mapping[i] # 原始ID i 映射到新ID x y = mapping[j] # 原始ID j 映射到新ID y matrix[x][y] = matrix[y][x] = 1 # 无向图,设置对称位置为1 # 查看邻接矩阵的部分元素 print("-----matrix-----") print(matrix)
# 方法二:通过Planetoid下载到指定目录并print相关信息。
import pandas as pd import numpy as np from torch_geometric.datasets import Planetoid from torch_geometric.transforms import NormalizeFeatures # 下载数据集到指定目录,同时对节点特征进行归一化 cora = Planetoid(root='', name='Cora', transform=NormalizeFeatures()) citeseer = Planetoid(root='', name='Citeseer', transform=NormalizeFeatures()) pubmed = Planetoid(root='', name='Pubmed', transform=NormalizeFeatures()) for i in [cora, citeseer, pubmed]: print() print() print(f'Dataset: {i}:') print('===========================================================================================================') print(f'Number of graphs: {len(i)}') print(f'Number of features: {i.num_features}') print(f'Number of classes: {i.num_classes}') data = i[0] # 第一个图 print(f"The first graph: {data}") print() # 第一个图的统计信息 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'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'Has isolated nodes: {data.has_isolated_nodes()}') print(f'Has self-loops: {data.has_self_loops()}') print(f'Is undirected: {data.is_undirected()}') print('===========================================================================================================')
# 输出结果:
转载本文请联系原作者获取授权,同时请注明本文来自孙琪科学网博客。
链接地址:https://wap.sciencenet.cn/blog-3623012-1481699.html?mobile=1
收藏