做的网站怎么转成网址链接,WordPress修改网站背景,松江网站建设品划网络,wordpress 关联表实验复现来源
https://zhuanlan.zhihu.com/p/603486955
该文章主要解决问题#xff1a;
1.加深对图神经网络数据集的理解
2.加深对图神经网络模型中喂数据中维度变化的理解
原理问题在另一篇文章分析#xff1a; 介绍数据集#xff1a;cora数据集
其中的主要内容表示为…实验复现来源
https://zhuanlan.zhihu.com/p/603486955
该文章主要解决问题
1.加深对图神经网络数据集的理解
2.加深对图神经网络模型中喂数据中维度变化的理解
原理问题在另一篇文章分析 介绍数据集cora数据集
其中的主要内容表示为一堆文章有自己的特征内容有自己的编号有自己的类别标签相互引用的关系构成了图。 cora.content包含特征编号特征内容特征类别标签
31336 0 0 0 0 0 0 ....0 Neural_Networks
1061127 0 0 0 0 0 0 ....0 Rule_Learning
1106406 0 0 0 0 0 0 ....0 Reinforcement_Learning
13195 0 0 0 0 0 0 ....0 Reinforcement_Learning
37879 0 0 0 0 0 0 ....0 Probabilistic_Methods
1.其中左面第一列表示特征编号
2.中间的内容表示特征内容1433维
3.右面的最后一列表示标签 cora.cite:引用关系也称作边
35 1033
35 103482
35 103515
35 1050679
35 1103960
35 1103985
35 1109199
35 1112911
左面第一列表示起始点序号右面表示终止点序号其中一行表示一个边表示两个点的连接 以点作为主要特征进行分类
首先先看一下GCN网络的参数部分 self.conv1 GCNConv(in_channels16, out_channels32, add_self_loopsTrue, normalizeTrue)主要参数就是输入的维度输出的维度 # 前向传播时调用
output self.conv1(x, edge_index, edge_weight)主要的参数为结点的特征矩阵与图的连接关系 也就是说数据需要预处理成结点的特征矩阵然后单独的标签再预处理出图的连接关系
分为三个部分。 1.数据预处理
from plistlib import Data
from torch_geometric.data import Data
import torch
#print(torch.__version__)
import torch.nn.functional as F
# import sys
# print(sys.executable)
# import torch_geometric
# print(torch_geometric.__version__)
datasetPath E:/pytorch/pytorch exercise/Graph neural network/Cora dataset/cora
node_feature_file E:/pytorch/pytorch exercise/Graph neural network/Cora dataset/cora/Cora.content
edge_file E:/pytorch/pytorch exercise/Graph neural network/Cora dataset/cora/Cora.cites
label_mapping {}
node_features []
node_labels []
node_ids {} #特征数
# 定义一个计数器遍历所有可能的标签
current_label 0with open(node_feature_file,r) as f:for line in f:parts line.strip().split(\t)node_id int(parts[0])features list(map(float, parts[1:-1])) # 特征label_str parts[-1]if label_str not in label_mapping:label_mapping[label_str] current_labelcurrent_label 1# 将标签转换为整数label label_mapping[label_str]node_ids[node_id] len(node_features) #补充结点索引node_features.append(features) #将节点特征依次按照数量拼接在一起node_labels.append(label)
#print(node_ids)
# 将节点特征和标签转换为 tensor
node_features torch.tensor(node_features, dtypetorch.float)
# 输出张量的形状
print(node_features.shape)
# 或者使用 .size() 也能得到相同的结果
print(node_features.size())node_labels torch.tensor(node_labels, dtypetorch.long)
print(node_labels size ,node_labels.size())
edge_index []
with open(edge_file, r) as f:for line in f:parts line.strip().split(\t)source int(parts[0]) # 源节点target int(parts[1]) # 目标节点source_idx node_ids[source] # 获取节点ID的索引target_idx node_ids[target]edge_index.append([source_idx, target_idx])#引用边的信息生成边的索引集合
# print(source_idx)
# print(target_idx)
edge_index torch.tensor(edge_index, dtypetorch.long).t().contiguous()
print(edge_index size ,edge_index.size())
#print(edge_index.shape())
data Data(xnode_features, edge_indexedge_index, ynode_labels)
# 输出数据的一些信息
print(f节点特征矩阵 shape: {data.x.shape})
print(f边的连接关系 (edge_index) shape: {data.edge_index.shape})
print(f节点标签 shape: {data.y.shape})# 输出第一个节点的特征和标签
print(f节点 0 的特征: {data.x[0]})
print(f节点 0 的标签: {data.y[0]})
其中
node_features表示所有点的特征结合在一起
node_labels表示所有标签集中在一起
node_ids表示特征点的个数 首先是从数据集中抽取特征矩阵的过程
with open(node_feature_file,r) as f: #打开文件for line in f: #按照行为单位开始进行遍历parts line.strip().split(\t)#删除其他空格与回车node_id int(parts[0]) #将第一个元素放入node_idfeatures list(map(float, parts[1:-1])) # 将第二个到倒数第二个元素一并放入featureslabel_str parts[-1] #最后一个元素放入标签if label_str not in label_mapping: #处理标签为null的情况label_mapping[label_str] current_labelcurrent_label 1# 将标签转换为整数label label_mapping[label_str] node_ids[node_id] len(node_features) #补充结点索引
#为新的node_id分配一个新的整数索引比如第一个元素node-id35422那么就是node_ids[35422] 1
#也就是为第一个名字为35422的节点编辑了一个序号1表示第一个元素node_features.append(features) #将节点特征依次按照数量拼接在一起node_labels.append(label) #拼接标签到一个集合中
从数据集中提取边的集合
edge_index []
with open(edge_file, r) as f:for line in f:parts line.strip().split(\t)source int(parts[0]) # 源节点target int(parts[1]) # 目标节点source_idx node_ids[source] # 获取节点ID的索引target_idx node_ids[target]edge_index.append([source_idx, target_idx])#引用边的信息生成边的索引集合 转换成data对象
edge_index torch.tensor(edge_index, dtypetorch.long).t().contiguous()
data Data(xnode_features, edge_indexedge_index, ynode_labels) 简易的模型
class Net(torch.nn.Module):def __init__(self):super(Net, self).__init__()self.conv1 GCNConv(data.x.size(1), 16) # 输入特征维度是 data.x.size(1)输出 16 个特征# 计算类别数假设 data.y 是节点标签num_classes data.y.max().item() 1 # 获取类别数# 第二层卷积层输出类别数个特征self.conv2 GCNConv(16, num_classes)def forward(self,x,edge_index):x self.conv1(x, edge_index) #输入特征矩阵与边的索引集合x F.relu(x) #卷积后激活x self.conv2(x, edge_index)return F.log_softmax(x, dim1)