MindSpore实践图神经网络之GCN

2024-05-31 15:20

本文主要是介绍MindSpore实践图神经网络之GCN,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

GCN介绍

  • 图卷积网络(GCN)于2016年提出,旨在对图结构数据进行半监督学习。它提出了一种基于卷积神经网络有效变体的可扩展方法,可直接在图上操作。该模型在图边缘的数量上线性缩放,并学习隐藏层表示,这些表示编码了局部图结构和节点特征。

  • GCN(图卷积神经网络) 类似CNN(卷积神经网络),只不过CNN用于二维数据结构,GCN用于图数据结构。GCN实际上跟CNN的作用一样,就是一个特征提取器,只不过它的对象是图数据。GCN精妙地设计了一种从图数据中提取特征的方法。

  • GCN包含两个图卷积层。每一层以节点特征和邻接矩阵为输入,通过聚合相邻特征来更新节点特征。

环境配置

  • 配置MindSpore环境
# 控制台安装mindspore 
conda create -n py39_ms18 python=3.9
conda activate py39_ms18pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/1.8.1/MindSpore/cpu/x86_64/mindspore-1.8.1-cp39-cp39-linux_x86_64.whl --trusted-host ms-release.obs.cn-north-4.myhuaweicloud.com -i https://pypi.tuna.tsinghua.edu.cn/simple# 验证是否安装成功
python -c "import mindspore;mindspore.run_check()"conda activate py39_ms18
  • 配置python环境
conda activate py39_ms18pip install numpy
pip install scipy
pip install sklearn
pip install pyyaml
# 缺包
pip  install matplotlib

算子开发

  • 算子开发:Layer、Model
# 定义算子:Layer
class GraphConvolution(nn.Cell):def __init__(self,feature_in_dim,feature_out_dim,dropout_ratio=None,activation=None):super(GraphConvolution, self).__init__()self.in_dim = feature_in_dimself.out_dim = feature_out_dimself.weight_init = glorot([self.out_dim, self.in_dim])self.fc = nn.Dense(self.in_dim,self.out_dim,weight_init=self.weight_init,has_bias=False)self.dropout_ratio = dropout_ratioif self.dropout_ratio is not None:self.dropout = nn.Dropout(keep_prob=1-self.dropout_ratio)self.dropout_flag = self.dropout_ratio is not Noneself.activation = get_activation(activation)self.activation_flag = self.activation is not Noneself.matmul = P.MatMul()def construct(self, adj, input_feature):"""GCN graph convolution layer."""dropout = input_featureif self.dropout_flag:dropout = self.dropout(dropout)fc = self.fc(dropout)output_feature = self.matmul(adj, fc)if self.activation_flag:output_feature = self.activation(output_feature)return output_feature# 定义模型:Model
class GCN(nn.Cell):def __init__(self, config, input_dim, output_dim):super(GCN, self).__init__()self.layer0 = GraphConvolution(input_dim, config.hidden1, activation="relu", dropout_ratio=config.dropout)self.layer1 = GraphConvolution(config.hidden1, output_dim, dropout_ratio=None)def construct(self, adj, feature):output0 = self.layer0(adj, feature)output1 = self.layer1(adj, output0)return output1
  • 数据处理utils
# 归一化邻接矩阵
def normalize_adj(adj):"""Symmetrically normalize adjacency matrix."""rowsum = np.array(adj.sum(1))d_inv_sqrt = np.power(rowsum, -0.5).flatten()d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.d_mat_inv_sqrt = sp.diags(d_inv_sqrt)return adj.dot(d_mat_inv_sqrt).transpose().dot(d_mat_inv_sqrt).tocoo()# 加载数据集  : Cora
def get_adj_features_labels(data_dir):"""Get adjacency matrix, node features and labels from dataset."""g = ds.GraphData(data_dir)nodes = g.get_all_nodes(0)nodes_list = nodes.tolist()row_tensor = g.get_node_feature(nodes_list, [1, 2])features = row_tensor[0]labels = row_tensor[1]nodes_num = labels.shape[0]class_num = labels.max() + 1labels_onehot = np.eye(nodes_num, class_num)[labels].astype(np.float32)neighbor = g.get_all_neighbors(nodes_list, 0)node_map = {node_id: index for index, node_id in enumerate(nodes_list)}adj = np.zeros([nodes_num, nodes_num], dtype=np.float32)for index, value in np.ndenumerate(neighbor):# The first column of neighbor is node_id, second column to last column are neighbors of the first column.# So we only care index[1] > 1.# If the node does not have that many neighbors, -1 is padded. So if value < 0, we will not deal with it.if value >= 0 and index[1] > 0:adj[node_map[neighbor[index[0], 0]], node_map[value]] = 1adj = sp.coo_matrix(adj)adj = adj + adj.T.multiply(adj.T > adj) + sp.eye(nodes_num)nor_adj = normalize_adj(adj)nor_adj = np.array(nor_adj.todense())return nor_adj, features, labels_onehot, labels# 数据集划分
def get_mask(total, begin, end):"""Generate mask."""mask = np.zeros([total]).astype(np.float32)mask[begin:end] = 1return mask

Windows环境跑脚本报错(1)

问题描述

/mnt/d/mindspore_gallery/models/gnn/gcn/data
cora
data_mr exist
scripts/run_process_data.sh: line 46: cd: ../../../utils/graph_to_mindrecord: No such file or directory

根因分析

  • 由报错信息可以看出可能是数据集存放路径不对,或者windows下脚本和Linux不一致

解决办法

  • 修改路径,改为如下路径
../../utils/graph_to_mindrecord
  • 改到Linux环境,如果没有Linux环境可以安装WSL2,创建Ubuntu环境
    image.png

Windows环境跑脚本报错(2)

问题描述

{'data_dir': 'Dataset directory', 'train_nodes_num': 'Nodes numbers for training', 'eval_nodes_num': 'Nodes numbers for evaluation', 'test_nodes_num': 'Nodes numbers for test', 'save_TSNE': 'Whether to save t-SNE graph'}
Traceback (most recent call last):File "D:\mindspore_gallery\models\gnn\gcn\train.py", line 196, in <module>run_train()File "D:\mindspore_gallery\models\gnn\gcn\model_utils\moxing_adapter.py", line 105, in wrapped_funcrun_func(*args, **kwargs)File "D:\mindspore_gallery\models\gnn\gcn\train.py", line 114, in run_traincontext.set_context(mode=context.GRAPH_MODE,File "C:\Users\sunxiaobei\.conda\envs\py39_ms18\lib\site-packages\mindspore\_checkparam.py", line 1210, in wrapperreturn func(*args, **kwargs)File "C:\Users\sunxiaobei\.conda\envs\py39_ms18\lib\site-packages\mindspore\_checkparam.py", line 1179, in wrapperreturn func(*args, **kwargs)File "C:\Users\sunxiaobei\.conda\envs\py39_ms18\lib\site-packages\mindspore\context.py", line 911, in set_contextraise ValueError(f"For 'context.set_context', package type {__package_name__} support 'device_target' "
ValueError: For 'context.set_context', package type mindspore support 'device_target' type cpu, but got Ascend.

根因分析

  • 从log上不难看出,是代码指定的设备不一致,当前设备只有CPU,但是指定的是Ascent , 需要指定和实际环境一致的设备

解决办法

  • 修改代码,指定CPU
    context.set_context(mode=context.GRAPH_MODE,device_target="CPU", save_graphs=False)  # CPU  Ascend  GPU

运行代码

python train.py --data_dir=./data_mr/citeseer --train_nodes_num=120

image.png

这篇关于MindSpore实践图神经网络之GCN的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1018306

相关文章

JDK21对虚拟线程的几种用法实践指南

《JDK21对虚拟线程的几种用法实践指南》虚拟线程是Java中的一种轻量级线程,由JVM管理,特别适合于I/O密集型任务,:本文主要介绍JDK21对虚拟线程的几种用法,文中通过代码介绍的非常详细,... 目录一、参考官方文档二、什么是虚拟线程三、几种用法1、Thread.ofVirtual().start(

从基础到高级详解Go语言中错误处理的实践指南

《从基础到高级详解Go语言中错误处理的实践指南》Go语言采用了一种独特而明确的错误处理哲学,与其他主流编程语言形成鲜明对比,本文将为大家详细介绍Go语言中错误处理详细方法,希望对大家有所帮助... 目录1 Go 错误处理哲学与核心机制1.1 错误接口设计1.2 错误与异常的区别2 错误创建与检查2.1 基础

springboot依靠security实现digest认证的实践

《springboot依靠security实现digest认证的实践》HTTP摘要认证通过加密参数(如nonce、response)验证身份,避免明文传输,但存在密码存储风险,相比基本认证更安全,却因... 目录概述参数Demopom.XML依赖Digest1Application.JavaMyPasswo

分析 Java Stream 的 peek使用实践与副作用处理方案

《分析JavaStream的peek使用实践与副作用处理方案》StreamAPI的peek操作是中间操作,用于观察元素但不终止流,其副作用风险包括线程安全、顺序混乱及性能问题,合理使用场景有限... 目录一、peek 操作的本质:有状态的中间操作二、副作用的定义与风险场景1. 并行流下的线程安全问题2. 顺

Java 结构化并发Structured Concurrency实践举例

《Java结构化并发StructuredConcurrency实践举例》Java21结构化并发通过作用域和任务句柄统一管理并发生命周期,解决线程泄漏与任务追踪问题,提升代码安全性和可观测性,其核心... 目录一、结构化并发的核心概念与设计目标二、结构化并发的核心组件(一)作用域(Scopes)(二)任务句柄

Java中的Schema校验技术与实践示例详解

《Java中的Schema校验技术与实践示例详解》本主题详细介绍了在Java环境下进行XMLSchema和JSONSchema校验的方法,包括使用JAXP、JAXB以及专门的JSON校验库等技术,本文... 目录1. XML和jsON的Schema校验概念1.1 XML和JSON校验的必要性1.2 Sche

SpringBoot集成WebService(wsdl)实践

《SpringBoot集成WebService(wsdl)实践》文章介绍了SpringBoot项目中通过缓存IWebService接口实现类的泛型入参类型,减少反射调用提升性能的实现方案,包含依赖配置... 目录pom.XML创建入口ApplicationContextUtils.JavaJacksonUt

MyCat分库分表的项目实践

《MyCat分库分表的项目实践》分库分表解决大数据量和高并发性能瓶颈,MyCat作为中间件支持分片、读写分离与事务处理,本文就来介绍一下MyCat分库分表的实践,感兴趣的可以了解一下... 目录一、为什么要分库分表?二、分库分表的常见方案三、MyCat简介四、MyCat分库分表深度解析1. 架构原理2. 分

Java 中的 equals 和 hashCode 方法关系与正确重写实践案例

《Java中的equals和hashCode方法关系与正确重写实践案例》在Java中,equals和hashCode方法是Object类的核心方法,广泛用于对象比较和哈希集合(如HashMa... 目录一、背景与需求分析1.1 equals 和 hashCode 的背景1.2 需求分析1.3 技术挑战1.4

k8s搭建nfs共享存储实践

《k8s搭建nfs共享存储实践》本文介绍NFS服务端搭建与客户端配置,涵盖安装工具、目录设置及服务启动,随后讲解K8S中NFS动态存储部署,包括创建命名空间、ServiceAccount、RBAC权限... 目录1. NFS搭建1.1 部署NFS服务端1.1.1 下载nfs-utils和rpcbind1.1