首页
人工智能
网络安全
手机
搜索
登录
搜索
golden81
累计撰写
154
篇文章
累计收到
0
条评论
首页
栏目
首页
人工智能
网络安全
手机
包含标签 【特征向量】 的文章
2025-4-27
基于Pytorch Gemotric在昇腾上实现GAT图神经网络
本实验主要介绍了如何在昇腾上,使用pytorch对经典的图神经网络GAT在论文引用数据集Pubmed上进行分类训练的实战讲解。内容包括GAT网络创新点分析、图注意力机制原理与架构剖析、多头注意力机制分析与GAT网络模型代码实战分析等等。 本实验的目录结构安排如下所示: GAT网络创新点分析 图注意力机制原理与架构分析 多头注意力机制分析 GAT网络用于Pubmed数据集分类实战 GAT网络创新点分析 注意机制已成功用于许多基于序列的任务,例如机器翻译,机器阅读等等。与GCN平等对待节点的所有邻居相比,注意力机制可以为每个邻居分配不同的注意力得分,从而识别出更重要的邻居。将注意力机制纳入图谱神经网络的传播步骤是很直观的。图注意力网络也可以看作是图卷积网络家族中的一种方法。其创新点如下: GAT是一种图结构化数据上操作的新型神经网络架构,利用掩码自注意力层来解决基于图卷积或其近似值的现有方法的缺点。 GAT网络对不同的相邻节点分配相应的权重,既不需要矩阵运算,也不需要事先知道图结构。 GAT网络克服了基于谱神经网络的几个关键挑战,使得模型更加适用于归纳问题以及转导问题。 在Cora、Citeseer和Pubmed引文网络数据集上取得了非常好的效果。 图注意力机制原理与架构分析 图中输入由N个节点组成,每个节点feature数为F(也就是feature vector长度),下述两个公式分别表示输入的节点向量通过GAT网络进行注意力计算后得到的输出。 Input: $h = { \\vec{h_1}, \\vec{h_2}, . . . , \\vec{h_N} },\\vec h_i \\in R\^{F} $ Output: $h = { \\vec{h_1\^{'}}, \\vec{h_2\^{'}}, . . . , \\vec{h_N\^{'}} },\\vec h_i\^{'} \\in R\{F\{'}} $ 为了将输入的特征转换为高维的特征,这里至少需要一个科学系的线性转换。在 (Velickovic et al.,2017)中,作者对于每一个点使用了一个共享的线性转换方式,同时介绍了一个权重矩阵来参数化线性转换。对于节点中的每两个node间都有了相互关注机制(用来做加权平均,卷积时每个node的更新是其他的加权平均)。 计算节点与节点的相关性系数(注意力值): 先初步计算节点i与节点j的相关性:其中a是一个共享的权重系数,执行的是 计算节点i与节点j的相关性(归一化)$ \\alpha_{i, j} = softmax_j(e_{ij}) = \\frac {exp(e_{ij})} {\\sum_{k \\in N_i}exp(e_{ik})} $ 论文中采取取的计算attention coefficient的函数a是一个单层的前馈网络,经LeakyReLU处理得最终的: $\\alpha_{i, j} = \\frac {exp(LeakyReLU({\\vec a\^T}\[W\\vec{h_i} \|\| W \\vec{h_j}\]))} {\\sum_{k \\in N_i}exp(LeakyReLU(\\vec a\^T\[W \\vec{h_i} ,W \\vec{h_k}\])} $ 式中 \|\| 表示串联/连接,一旦获得,归一化的相互注意系数用来计算对应特征的线性组合,以用作每个节点的最终输出特征。 左图表示在模型中应用注意机制a(W \* h_i ,W \* h_j) 通过权重向量参数化,应用LeakyReLU 激活输出。 右图表示在邻域中具有多端连接,不同的箭头样式表示独立的注意力计算,通过直连concat或平均avg获取。 多头注意力机制分析 对于图中多头注意力情况,对应上图中右图情景,不只用一个函数进行attention coefficient的计算,而是设置K个函数,每一个函数都能计算出一组attention coefficient,并能计算出一组加权求和用的系数,每一个卷积层中,K个attention机制独立的工作,分别计算出自己的结果后连接在一起,得到卷积的结果。 我们知道对于单个注意力层输出计算如下: 对于有k个独立的相互注意机制同时计算,则集中其特征,可得到特征表示如下: 分别遍历1\~k个头,每一个上按照下述公式计算: 对于网络的最后一层卷积层,如果还是使用multi-head attention机制,那么就不采取连接的方式合并不同的attention机制的结果了,而是采用求平均的方式进行处理。 GAT网络用于Pubmed数据集分类实战 #导入torch相关库 import torch import torch.nn.functional as F 该实验需要跑在npu上,因此需要导入Npu相关库使得模型快速迁移到npu上运行 import torch_npu from torch_npu.contrib import transfer_to_npu /home/pengyongrong/miniconda3/envs/AscendCExperiments/lib/python3.9/site-packages/torch_npu/dynamo/__init__.py:18: UserWarning: Register eager implementation for the 'npu' backend of dynamo, as torch_npu was not compiled with torchair. warnings.warn( /home/pengyongrong/miniconda3/envs/AscendCExperiments/lib/python3.9/site-packages/torch_npu/contrib/transfer_to_npu.py:164: ImportWarning: ************************************************************************************************************* The torch.Tensor.cuda and torch.nn.Module.cuda are replaced with torch.Tensor.npu and torch.nn.Module.npu now.. The torch.cuda.DoubleTensor is replaced with torch.npu.FloatTensor cause the double type is not supported now.. The backend in torch.distributed.init_process_group set to hccl now.. The torch.cuda.* and torch.cuda.amp.* are replaced with torch.npu.* and torch.npu.amp.* now.. The device parameters have been replaced with npu in the function below: torch.logspace, torch.randint, torch.hann_window, torch.rand, torch.full_like, torch.ones_like, torch.rand_like, torch.randperm, torch.arange, torch.frombuffer, torch.normal, torch._empty_per_channel_affine_quantized, torch.empty_strided, torch.empty_like, torch.scalar_tensor, torch.tril_indices, torch.bartlett_window, torch.ones, torch.sparse_coo_tensor, torch.randn, torch.kaiser_window, torch.tensor, torch.triu_indices, torch.as_tensor, torch.zeros, torch.randint_like, torch.full, torch.eye, torch._sparse_csr_tensor_unsafe, torch.empty, torch._sparse_coo_tensor_unsafe, torch.blackman_window, torch.zeros_like, torch.range, torch.sparse_csr_tensor, torch.randn_like, torch.from_file, torch._cudnn_init_dropout_state, torch._empty_affine_quantized, torch.linspace, torch.hamming_window, torch.empty_quantized, torch._pin_memory, torch.Tensor.new_empty, torch.Tensor.new_empty_strided, torch.Tensor.new_full, torch.Tensor.new_ones, torch.Tensor.new_tensor, torch.Tensor.new_zeros, torch.Tensor.to, torch.nn.Module.to, torch.nn.Module.to_empty ************************************************************************************************************* warnings.warn(msg, ImportWarning) 由于torch_geometric中集成了单层的GATConv模块,这里直接进行导入,若有兴趣可以自行实现该类,注意输入与输出对齐即可。此外,数据集用的是Pubmed,该数据集也直接集成在Planetoid模块中,这里也需要将其import进来。 from torch_geometric.nn import GATConv from torch_geometric.datasets import Planetoid /home/pengyongrong/miniconda3/envs/AscendCExperiments/lib/python3.9/site-packages/torch_npu/contrib/transfer_to_npu.py:124: RuntimeWarning: torch.jit.script will be disabled by transfer_to_npu, which currently does not support it. warnings.warn(msg, RuntimeWarning) Pubmed数据集介绍 PubMed数据集是一个广泛用于图神经网络(GNN)研究的基准数据集,主要用于节点分类任务。其由生物医学文献组成,每篇文献被视为一个节点,引用关系被视为边。该数据集包含三类糖尿病相关的论文,每个节点都带有特征向量和标签。 数据集一共有19717个节点 ,每一个节点代表一篇生物医学文献,每个节点有一个500 维的特征向量,用来表示该医学文献的内容。总共含44338条边 ,每条边表示一篇文献对另一篇文献的引用关系,边与边之间是无向的,因此可以看做是对称的。总类别数包含Diabetes Mellitus Experiment、Diabetes Mellitus Type 1与Diabetes Mellitus Type2三类。 # 加载数据 print("===== begin Download Dadasat=====\n") dataset = Planetoid(root='/home/pengyongrong/workspace/data', name='PubMed') print("===== Download Dadasat finished=====\n") print("dataset num_features is: ", dataset.num_features) print("dataset.num_classes is: ", dataset.num_classes) print("dataset.edge_index is: ", dataset.edge_index) print("train data is: ", dataset.data) print("dataset0 is: ", dataset[0]) print("train data mask is: ", dataset.train_mask, "num train is: ", (dataset.train_mask ==True).sum().item()) print("val data mask is: ",dataset.val_mask, "num val is: ", (dataset.val_mask ==True).sum().item()) print("test data mask is: ",dataset.test_mask, "num test is: ", (dataset.test_mask ==True).sum().item()) ===== begin Download Dadasat===== ===== Download Dadasat finished===== dataset num_features is: 500 dataset.num_classes is: 3 dataset.edge_index is: tensor([[ 1378, 1544, 6092, ..., 12278, 4284, 16030], [ 0, 0, 0, ..., 19714, 19715, 19716]]) train data is: Data(x=[19717, 500], edge_index=[2, 88648], y=[19717], train_mask=[19717], val_mask=[19717], test_mask=[19717]) dataset0 is: Data(x=[19717, 500], edge_index=[2, 88648], y=[19717], train_mask=[19717], val_mask=[19717], test_mask=[19717]) train data mask is: tensor([ True, True, True, ..., False, False, False]) num train is: 60 val data mask is: tensor([False, False, False, ..., False, False, False]) num val is: 500 test data mask is: tensor([False, False, False, ..., True, True, True]) num test is: 1000 /home/pengyongrong/miniconda3/envs/AscendCExperiments/lib/python3.9/site-packages/torch_geometric/data/in_memory_dataset.py:300: UserWarning: It is not recommended to directly access the internal storage format `data` of an 'InMemoryDataset'. If you are absolutely certain what you are doing, access the internal storage via `InMemoryDataset._data` instead to suppress this warning. Alternatively, you can access stacked individual attributes of every graph via `dataset.{attr_name}`. warnings.warn(msg) 下载后的pubmed数据集总共包含8个文件,分别是ind.pubmed.x、ind.pubmed.tx、ind.pubmed.all、ind.pubmed.y、ind.pubmed.ty、ind.pubmed.ally、ind.pubmed.graph与ind.pubmed.test.index。每个文件的作用说明如下: ind.pubmed.x:训练集节点特征向量,大小(140,1433) ind.pubmed.tx:测试集节点特征向量,实际展开后大小为(1000,1433) ind.pubmed.allx:包含标签核无标签的训练节点特征向量(1708,1433) ind.pubmed.y:one-hot表示的训练节点的标签 ind.pubmed.ty:one-hot表示的测试节点的标签 ind.pubmed.ally:one-hot表示的ind.cora.allx对应的标签 ind.pubmed.graph:保存节点之间边的信息 ind.pubmed.test.index:保存测试集节点的索引,用于后面的归纳学习设置 从打印结果可以看出,数据集的特点与上述描述的相对应,GAT_NET网络定义了一个两层的GAT网络,heads的数量设置成4。 开启Pubmed数据训练过程 class GAT_NET(torch.nn.Module): def __init__(self, features, hidden, classes, heads=4): super(GAT_NET, self).__init__() # 定义GAT层,使用多头注意力机制 self.gat1 = GATConv(features, hidden, heads=4) # 因为多头注意力是将向量拼接,所以维度乘以头数。 self.gat2 = GATConv(hidden*heads, classes) def forward(self, data): # 从输入数据集中获取x与边集相关信息 x, edge_index = data.x, data.edge_index # 将输入传入GAT层中,获得第一层Gat层的输出 x = self.gat1(x, edge_index) # 经过非线性激活与dropout,减少过拟合现象,增加模型的泛化能力 x = F.relu(x) x = F.dropout(x, training=self.training) # 第二层GAT层,得到整个网络的输出送给分类器进行分类 x = self.gat2(x, edge_index) return F.log_softmax(x, dim=1) 定义设备跑在Npu上,这里如果需要替换成Gpu或Cpu,则替换成'cuda'或'cpu'即可。 device = 'npu' 定义GAT_NET网络,中间隐藏层节点个数定义为16,'dataset.num_classes'为先前数据集中总的类别数,这里是7类。'to()'的作用是将该加载到指定模型设备上。优化器用的是'optim'中的'Adam'。 model = GAT_NET(dataset.num_node_features, 16, dataset.num_classes).to(device) # 定义GraphSAGE data = dataset[0].to(device) optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4) 开始训练模型,指定训练次数200次,训练后采用极大似然用作损失函数计算损失,然后进行反向传播更新模型的参数,训练完成后,用验证集中的数据对模型效果进行验证,最后打印模型的准确率。 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() # 模型验证过程,对训练得到的模型效果进行评估,并打印准确率。 model.eval() _, pred = model(data).max(dim=1) correct = int(pred[data.test_mask].eq(data.y[data.test_mask]).sum().item()) acc = correct / int(data.test_mask.sum()) print('GAT Accuracy: {:.4f}'.format(acc)) [W VariableFallbackKernel.cpp:51] Warning: CAUTION: The operator 'aten::scatter_reduce.two_out' is not currently supported on the NPU backend and will fall back to run on the CPU. This may have performance implications. (function npu_cpu_fallback) GAT Accuracy: 0.3660 内存使用情况: 整个训练过程的内存使用情况可以通过"npu-smi info"命令在终端查看,因此本文实验只用到了单个npu卡(也就是chip 0),内存占用约573M,对内存、精度或性能优化有兴趣的可以自行尝试进行优化。 Reference ========= \[1\] Velikovi, Petar , et al. "Graph Attention Networks." (2017).
2025年-4月-27日
12 阅读
0 评论
人工智能
2025-4-27
基于Pytorch 在昇腾上实现GCN图神经网络
本文主要介绍了如何在昇腾上,使用pytorch对经典的图神经网络GCN在论文引用Cora数据集上进行分类训练的实战讲解。内容包括GCN背景介绍、模型特点介绍、GCN网络架构剖析与GCN网络模型代码实战分析等等。 本文的目录结构安排如下所示: GCN网络背景介绍 模型特点介绍 GCN网络架构剖析 GCN网络用于Cora数据集分类实战 GCN网络背景介绍 多层感知机、卷积神经网络、循环神经网络和自编码器等深度学习模型通过对输入的数据进行逐层的特征提取和筛选,可以完成分类和预测等任务,在计算机视觉和语音识别等领域已被广泛应用。但上述模型只能处理具有固定排列规则和顺序的欧氏结构数据,对于一些非规则排布的非欧式数据显得有些则力不从心。随着非欧式数据结构被越来越多的实际问题应用所需要,针对处理非欧氏结构数据的深度学习模型图神经网络(Graph Neural Networks, GNN)应运而生。 图神经网络的本质就是:图中的任何一个节点,都受到和它相连的其他节点的影响,距离越近影响则越大。一个图中的所有节点间的互动关系和每个节点本身的信息,就构成了这整张图的全部信息。 由于CNN已经是个相当成熟的技术了,聚合"邻居"的信息并不是什么少见的思路。显然,从GNN出现开始,就必然会有人尝试在GNN上进行类似CNN的"聚合节点信息"操作。事实上在GCN之前,就已经有一些关于类似的研究了。但不外乎存在计算量大、聚合效果差、卷积核复杂的问题。 模型特点介绍 GCN是GNN的一个分支,全称为图卷积神经网络,顾名思义,GCN是在图上进行"卷积"操作的GNN,这里用引号是因为,GCN的操作并不是卷积神经网络里的那个卷积,这里的卷积,是因为GCN的运算是在聚合节点周围其他节点的信息,与卷积神经网络(CNN)的行为类似。不过话说回来,CNN里的"卷积",也并不是不是数学意义上的卷积。 GCN的创新之处在于,提出了一种简化到在计算量上可行且效果好的"卷积"计算方案。GCN利用拉普拉斯变换变化,利用邻接矩阵算出了这个滤波矩阵,然后利用这个滤波矩阵进行层间传播。 其迭代间节点核心更新计算公式如下: 其中表示邻接矩阵的度矩阵,表示整张图的邻接矩阵(含自回环,也就是加了单位矩阵),X表示节点在k-1层的特征向量,是k-1层的卷积参数。 GCN的上述公式表达的是从整个图的角度来考虑和描述的。从单个节点来说,每个节点的特征向量可以表示为的变换 (前向传播) 的向量形式可以表示为如下: 其中是权重矩阵 (即模型学习过程中要更新的参数),表示节点i在第k次迭代的特征向量,deg(i)表示节点i的度,N(i)表示节点i所有邻接节点的集合。 GCN网络架构剖析 GCN定义了一个两层的模型,中间隐藏的节点个数可以自设,后面输出层可以结合具体数据集类别设置使用,当然也可以跟训练类别设置不一致,只需要在后面接一个分类器即可(后再接一个全连接层)。 MessagePassing模块是图神经网络(Graph Neural Networks,GNNs)的一个基础组件,它被设计用来处理图形数据的问题。在图形数据中,数据点(节点)之间的关系(边)是非常重要的信息。MessagePassing通过在节点之间传递和聚合信息,使得每个节点都能获取其邻居节点的信息,从而更好地理解图形的结构和特性。里面'propagate'函数与'aggregate'函数用于实现节点之间的传播与聚合功能。 # 导入torch及相关库,便于后续搭建网络调用基础算子模块 import torch import torch.nn.functional as F from torch_geometric.nn import MessagePassing 在图神经网络(GNN)的实现中,对图结构的调整和优化是提升模型性能的关键手段之一。'add_self_loops'函数在PyTorch的图处理库中用于向图中添加自环(self-loops),即连接节点自身的边。'degree'用来计算一个无权图的入度矩阵。 from torch_geometric.utils import add\_self\_loops, degree GCN实现继承了'MessagePassing'类,线性变换功能在'init'函数中通过'self.lin'为线性变换函数定义,具体特征维度的逻辑在'forward()'中实现,'init'函数中入参'in_channel'是每个节点输入特征的维度,'out_channels'是每个节点输出特征的维度,这一部分对应上述公式中的X。输入的特征矩阵维度是(N, in_channels),输出的特征矩阵维度是(N, out_channels),其中 N 是节点个数。 在'forward()'函数中'edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0))'是给邻接矩阵加上self loops,也即构造出矩阵 ,在torch geometric中,无权图的邻接矩阵表示为2维数组(COO存储格式),第1行表示边的起始节点(source 节点),第2行表示边的目标节点(target节点)。 对于'message()'函数而言,函数入参'x_j' 的形状是\[E, out_channels\],其中E表示边的数量。由上面可知,特征矩阵经过线性变换后的输出形状是(N, out_channels),边的矩阵的形状为 \[2, E\]。'row, col = edge_index'表示取出所有边的起始节点和目标节点,row表示边的起始节点的结合,col表示边的目标节点的集合。在无向图中,这两者是等价的。以target节点作为索引,从线性变换后的特征矩阵中索引得到target节点的特征矩阵。 class GCNConv(MessagePassing): def __init__(self, in_channels, out_channels): # "Add" aggregation. super(GCNConv, self).__init__(aggr='add') self.lin = torch.nn.Linear(in\_channels, out\_channels) def forward(self, x, edge_index): # x has shape \[N, in_channels\] # edge_index has shape \[2, E\] # Step 1: Add self-loops to the adjacency matrix. edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0)) # Step 2: Linearly transform node feature matrix. x = self.lin(x) # Step 3-5: Start propagating messages. return self.propagate(edge_index, size=(x.size(0), x.size(0)), x=x) def message(self, x_j, edge_index, size): # x_j has shape [E, out_channels] # edge_index has shape [2, E] # Step 3: Normalize node features. row, col = edge_index # [N, ] deg = degree(row, size[0], dtype=x_j.dtype) # [N, ] deg_inv_sqrt = deg.pow(-0.5) norm = deg_inv_sqrt[row] * deg_inv_sqrt[col] return norm.view(-1, 1) * x_j def update(self, aggr_out): # aggr_out has shape [N, out_channels] # Step 5: Return new node embeddings. return aggr_out 上述类目前已经集成在torch_geometric.nn模块中,也可以使用下述一行代码替换 'from torch_geometric.nn import GCNConv' 导入GCN层替换GCNConv类定义。 Planetoid集成了论文引用中Cora,CiteSeer,PubMed三个数据集,由于本实验需要用到Cora数据集,因此此处需要导入该模块用于加载数据集。 from torch_geometric.datasets import Planetoid 定义GCN_NET图网络,中间构造一个隐藏层用来辅助实现线性转换过程。 class GCN_NET(torch.nn.Module): def __init__(self, features, hidden, classes): super(GCN_NET, self).__init__() # shape(输入的节点特征维度 * 中间隐藏层的维度) self.conv1 = GCNConv(features, hidden) # shaape(中间隐藏层的维度 * 节点类别) self.conv2 = GCNConv(hidden, classes) def forward(self, data): # 加载节点特征和邻接关系 x, edge_index = data.x, data.edge_index # 传入卷积层 x = self.conv1(x, edge_index) # 激活函数 x = F.relu(x) # dropout层,防止过拟合 x = F.dropout(x, training=self.training) # 第二层卷积层 x = self.conv2(x, edge_index) # 将经过两层卷积得到的特征输入log_softmax函数得到概率分布 return F.log_softmax(x, dim=1) GCN网络用于Cora数据集分类实战 本实验需要跑在npu上,因此需要导入Npu相关库,以便于模型能够跑在Npu上。 import torch_npu from torch_npu.contrib import transfer_to_npu /home/pengyongrong/miniconda3/envs/AscendCExperiments/lib/python3.9/site-packages/torch_npu/dynamo/init.py:18: UserWarning: Register eager implementation for the 'npu' backend of dynamo, as torch_npu was not compiled with torchair.warnings.warn(/home/pengyongrong/miniconda3/envs/AscendCExperiments/lib/python3.9/site-packages/torch_npu/contrib/transfer_to_npu.py:164: ImportWarning: The torch.Tensor.cuda and torch.nn.Module.cuda are replaced with torch.Tensor.npu and torch.nn.Module.npu now..The torch.cuda.DoubleTensor is replaced with torch.npu.FloatTensor cause the double type is not supported now..The backend in torch.distributed.init_process_group set to hccl now.. The torch.cuda.\* and torch.cuda.amp.\* are replaced with torch.npu.\* and torch.npu.amp.\* now.. The device parameters have been replaced with npu in the function below: torch.logspace, torch.randint, torch.hann_window, torch.rand, torch.full_like, torch.ones_like, torch.rand_like, torch.randperm, torch.arange, torch.frombuffer, torch.normal, torch._empty_per_channel_affine_quantized, torch.empty_strided, torch.empty_like, torch.scalar_tensor, torch.tril_indices, torch.bartlett_window, torch.ones, torch.sparse_coo_tensor, torch.randn, torch.kaiser_window, torch.tensor, torch.triu_indices, torch.as_tensor, torch.zeros, torch.randint_like, torch.full, torch.eye, torch._sparse_csr_tensor_unsafe, torch.empty, torch._sparse_coo_tensor_unsafe, torch.blackman_window, torch.zeros_like, torch.range, torch.sparse_csr_tensor, torch.randn_like, torch.from_file, torch._cudnn_init_dropout_state, torch._empty_affine_quantized, torch.linspace, torch.hamming_window, torch.empty_quantized, torch._pin_memory, torch.Tensor.new_empty, torch.Tensor.new_empty_strided, torch.Tensor.new_full, torch.Tensor.new_ones, torch.Tensor.new_tensor, torch.Tensor.new_zeros, torch.Tensor.to, torch.nn.Module.to, torch.nn.Module.to_empty\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*warnings.warn(msg, ImportWarning) /home/pengyongrong/miniconda3/envs/AscendCExperiments/lib/python3.9/site-packages/torch_npu/dynamo/init.py:18: UserWarning: Register eager implementation for the 'npu' backend of dynamo, as torch_npu was not compiled with torchair.warnings.warn(/home/pengyongrong/miniconda3/envs/AscendCExperiments/lib/python3.9/site-packages/torch_npu/contrib/transfer_to_npu.py:164: ImportWarning: The torch.Tensor.cuda and torch.nn.Module.cuda are replaced with torch.Tensor.npu and torch.nn.Module.npu now..The torch.cuda.DoubleTensor is replaced with torch.npu.FloatTensor cause the double type is not supported now..The backend in torch.distributed.init_process_group set to hccl now.. The torch.cuda.\* and torch.cuda.amp.\* are replaced with torch.npu.\* and torch.npu.amp.\* now.. The device parameters have been replaced with npu in the function below: torch.logspace, torch.randint, torch.hann_window, torch.rand, torch.full_like, torch.ones_like, torch.rand_like, torch.randperm, torch.arange, torch.frombuffer, torch.normal, torch._empty_per_channel_affine_quantized, torch.empty_strided, torch.empty_like, torch.scalar_tensor, torch.tril_indices, torch.bartlett_window, torch.ones, torch.sparse_coo_tensor, torch.randn, torch.kaiser_window, torch.tensor, torch.triu_indices, torch.as_tensor, torch.zeros, torch.randint_like, torch.full, torch.eye, torch._sparse_csr_tensor_unsafe, torch.empty, torch._sparse_coo_tensor_unsafe, torch.blackman_window, torch.zeros_like, torch.range, torch.sparse_csr_tensor, torch.randn_like, torch.from_file, torch._cudnn_init_dropout_state, torch._empty_affine_quantized, torch.linspace, torch.hamming_window, torch.empty_quantized, torch._pin_memory, torch.Tensor.new_empty, torch.Tensor.new_empty_strided, torch.Tensor.new_full, torch.Tensor.new_ones, torch.Tensor.new_tensor, torch.Tensor.new_zeros, torch.Tensor.to, torch.nn.Module.to, torch.nn.Module.to_empty warnings.warn(msg, ImportWarning) Cora数据集介绍与加载 Cora数据集包含2708篇科学出版物,10556条边,总共7种类别,数据集中的每个出版物都由一个 0/1 值的词向量描述,表示字典中相应词的缺失/存在。该词典由1433个独特的词组成,意思就是说每一个出版物都由1433个特征构成,每个特征仅由0/1表示。 如图,节点大小对应进出边的数量,节点越大表示进出边的数量越多,边的粗细反映对应两节点之间的相似或关联程度越高,也就是对彼此的影响力权重越大。 由于cora数据集处理的是无向图,所以'in degree count'与'out degree count'分布图一致,底部的'nodes for a given out-degree'与'node degree'图统计的是数据集中出边的分布情况,可以看到峰值点出现在\[2, 4\]范围内,说明大多数的节点之间之间与少量的边进行相连,相连节点最多的边是图中绿色节点,约有169个节点相连。 下载后的数据集总共包含8个文件分别是ind.cora.x、ind.cora.tx、ind.cora.all、ind.cora.y、ind.cora.ty、ind.cora.ally、ind.cora.graph与ind.cora.test.index。 其中: ind.cora.x:训练集节点特征向量,大小(140,1433) ind.cora.tx:测试集节点特征向量,实际展开后大小为(1000,1433) ind.cora.allx:包含标签核无标签的训练节点特征向量(1708,1433) ind.cora.y:one-hot表示的训练节点的标签 ind.cora.ty:one-hot表示的测试节点的标签 ind.cora.ally:one-hot表示的ind.cora.allx对应的标签 ind.cora.graph:保存节点之间边的信息 ind.cora.test.index:保存测试集节点的索引,用于后面的归纳学习设置 上述介绍完cora数据集的基本组成情况,接下来我们通过Planetoid集成库来下载cora数据集,下载好以后对数据集中一些基本的信息进行打印。 import numpy as np # 加载数据,出错可自行下载,解决方案见下文 print("===== begin Download Dadasat=====\\n") dataset = Planetoid(root='/home/pengyongrong/workspace/data', name='Cora') print("===== Download Dadasat finished=====\\n") print("dataset num_features is: ", dataset.num_features) print("dataset.num_classes is: ", dataset.num_classes) print("dataset.edge_index is: ", dataset.edge_index) print("train data is: ", dataset.data) print("dataset0 is: ", dataset\[0\]) print("train data mask is: ", dataset.train_mask, "num train is: ", (dataset.train_mask ==True).sum().item()) print("val data mask is: ",dataset.val_mask, "num val is: ", (dataset.val_mask ==True).sum().item()) print("test data mask is: ",dataset.test_mask, "num test is: ", (dataset.test_mask ==True).sum().item()) ===== begin Download Dadasat===== ===== Download Dadasat finished===== dataset num_features is: 1433 dataset.num_classes is: 7 dataset.edge_index is: tensor(\[\[ 633, 1862, 2582, ..., 598, 1473, 2706\], \[ 0, 0, 0, ..., 2707, 2707, 2707\]\]) train data is: Data(x=\[2708, 1433\], edge_index=\[2, 10556\], y=\[2708\], train_mask=\[2708\], val_mask=\[2708\], test_mask=\[2708\]) dataset0 is: Data(x=\[2708, 1433\], edge_index=\[2, 10556\], y=\[2708\], train_mask=\[2708\], val_mask=\[2708\], test_mask=\[2708\]) train data mask is: tensor(\[ True, True, True, ..., False, False, False\]) num train is: 140 val data mask is: tensor(\[False, False, False, ..., False, False, False\]) num val is: 500 test data mask is: tensor(\[False, False, False, ..., True, True, True\]) num test is: 1000 /home/pengyongrong/miniconda3/envs/AscendCExperiments/lib/python3.9/site-packages/torch_geometric/data/in_memory_dataset.py:300: UserWarning: It is not recommended to directly access the internal storage format \data\ of an 'InMemoryDataset'. If you are absolutely certain what you are doing, access the internal storage via \InMemoryDataset._data\ instead to suppress this warning. Alternatively, you can access stacked individual attributes of every graph via \dataset.{attr_name}\. warnings.warn(msg) 本文用到的实验数据集这里已经下载好并保存在"/home/pengyongrong/workspace/data"目录下,若没有下载好启动该命令会自动下载数据集,整个下载过程可能会比较慢,也可以在https://github.com/D61-IA/FisherGCN/tree/a58c1613f1aca7077ef90af6e51a8021548cdb4c/data 自行选择下载。 从打印的信息可以看出来,每一个节点的特征维度为1433,也就是'datasat.numfeatures'的取值是1433;总的类别数是7,对应'datasat.numclasses';'dataset.edge_index'表示所有边与边之间的互联关系,采用coo存储格式,因为这里是无权边,因此只需要二维数组即可完成对应功能。 上述信息介绍完后就剩下训练集、验证集与测试集相关的信息,dataset\[0\]包含了所有信息,包括输入x,边信息、标签y及'train_mask'、'val_mask'与'test_mask'分别表示2708篇刊物中哪些用于训练,哪些用于验证及哪些用于测试。 开启cora数据训练过程 接下来就是将cora数据集送入搭建好的GCN网络模型中进行训练,训练过程中设置设备在npu上运行,并定义训练的'epoch=200',迭代次数可以根据需要自行更改,训练完成后对模型的效果进行评估并打印预测的准确率约为0.8。 #设置成npu device = 'npu' print("device is: ", device) # 构建模型,设置中间隐藏层维度为16 model = GCN_NET(dataset.num_node_features, 16, dataset.num_classes).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() # 评估模型 model.eval() # 得到模型输出的类别 _, pred = model(data).max(dim=1) # 计算正确的个数 correct = int(pred[data.test_mask].eq(data.y[data.test_mask]).sum().item()) # 得出准确率 acc = correct / int(data.test_mask.sum()) # 打印准确率及结果 print('GCN Accuracy: {:.4f}'.format(acc)) device is: npuGCN Accuracy: 0.8040 内存使用情况: 整个训练过程的内存使用情况可以通过"npu-smi info"命令在终端查看,因此本文实验只用到了单个npu卡(也就是chip 0),内存占用约167M,对内存、精度或性能优化有兴趣的可以自行尝试进行优化,这里运行过程中也有其他程序在运行,因此本实验用到的网络所需要的内存已单独框出来。 Reference========= \[1\] Heidari, Negar , L. Hedegaard , and A. Iosifidis . "Graph convolutional networks." Deep Learning for Robot Perception and Cognition (2022).
2025年-4月-27日
10 阅读
0 评论
人工智能