GCN(二)GCN模型介绍
上一节介绍了处理cora
数据集,以及返回的结果:
- features:论文的属性特征,维度2708×14332708 \times 14332708×1433,并且做了归一化,即每一篇论文属性值的和为1.
- labels:每一篇论文对应的分类编号:0-6
- adj:邻接矩阵,维度2708×27082708 \times 27082708×2708
- idx_train:0-139
- idx_val:200-499
- idx_test:500-1499
这一节介绍GCN的模型。
GCN 模型
model:
import torch.nn as nn
import torch.nn.functional as F
from pygcn.layers import GraphConvolutionclass GCN(nn.Module):def __init__(self, nfeat, nhid, nclass, dropout):super(GCN, self).__init__()self.gc1 = GraphConvolution(nfeat, nhid) # 构建第一层 GCNself.gc2 = GraphConvolution(nhid, nclass) # 构建第二层 GCNself.dropout = dropoutdef forward(self, x, adj):x = F.relu(self.gc1(x, adj))x = F.dropout(x, self.dropout, training=self.training)x = self.gc2(x, adj)return F.log_softmax(x, dim=1)
layers:
import mathimport torchfrom torch.nn.parameter import Parameter
from torch.nn.modules.module import Moduleclass GraphConvolution(Module):"""Simple GCN layer, similar to https://arxiv.org/abs/1609.02907"""def __init__(self, in_features, out_features, bias=True):super(GraphConvolution, self).__init__()self.in_features = in_featuresself.out_features = out_featuresself.weight = Parameter(torch.FloatTensor(in_features, out_features)) # input_features, out_featuresif bias:self.bias = Parameter(torch.FloatTensor(out_features))else:self.register_parameter('bias', None)self.reset_parameters()def reset_parameters(self):stdv = 1. / math.sqrt(self.weight.size(1))self.weight.data.uniform_(-stdv, stdv) # 随机化参数if self.bias is not None:self.bias.data.uniform_(-stdv, stdv)def forward(self, input, adj):support = torch.mm(input, self.weight) # GraphConvolution forward。input*weightoutput = torch.spmm(adj, support) # 稀疏矩阵的相乘,和mm一样的效果if self.bias is not None:return output + self.biaselse:return outputdef __repr__(self):return self.__class__.__name__ + ' (' \+ str(self.in_features) + ' -> ' \+ str(self.out_features) + ')'
初始化模型
调用模型:
model = GCN(nfeat=features.shape[1],nhid=args.hidden,nclass=labels.max().item() + 1,dropout=args.dropout)
具体参数:
model = GCN(nfeat=1433,nhid=16,nclass=7,dropout=0.5)
初始化模型两层GCN
:
self.gc1 = GraphConvolution(nfeat=1433, nhid=16) # 构建第一层 GCN
self.gc2 = GraphConvolution(nhid=16, nclass=7) # 构建第二层 GCN
self.dropout = 0.5
初始化具体layer:
第一层:gc1
def __init__(self, in_features, out_features, bias=True):super(GraphConvolution, self).__init__()self.in_features = 1433self.out_features = 16self.weight = Parameter(torch.FloatTensor(1433, 16)) # input_features, out_featuresself.bias = Parameter(torch.FloatTensor(16))self.reset_parameters() # 初始化w和b
参数www的维度:W1433×16W_{1433 \times 16}W1433×16
参数bbb的维度:b1×16b_{1 \times 16}b1×16
第二层:gc2
def __init__(self, in_features, out_features, bias=True):super(GraphConvolution, self).__init__()self.in_features = 16self.out_features = 7self.weight = Parameter(torch.FloatTensor(16, 7)) # input_features, out_featuresself.bias = Parameter(torch.FloatTensor(7))self.reset_parameters() # 初始化w和b
参数www的维度:W1433×16W_{1433 \times 16}W1433×16
参数bbb的维度: b1×7b_{1 \times 7}b1×7
forward执行模型
- 首先执行model:
def forward(self, x, adj):x = F.relu(self.gc1(x, adj))x = F.dropout(x, self.dropout, training=self.training)x = self.gc2(x, adj)return F.log_softmax(x, dim=1)
执行
self.gc1(x, adj)
,x
表示输入特征,维度2708×14332708 \times 14332708×1433,adj
表示邻接矩阵,维度2708×27082708 \times 27082708×2708执行GCN layer gc1层,
support = torch.mm(input, self.weight) # GraphConvolution forward。input*weightoutput = torch.spmm(adj, support)
计算output,output2708×16=adj2708×2708×input2708×1433×W1433×16output_{2708 \times 16} = adj_{2708 \times 2708} \times input_{2708 \times 1433} \times W_{1433 \times 16}output2708×16=adj2708×2708×input2708×1433×W1433×16,然后返回output=output2708×16+bias1×16output = output_{2708 \times 16} + bias_{1 \times 16}output=output2708×16+bias1×16
output[0]=
tensor([ 0.0201, -0.0242, 0.0608, 0.0272, 0.0133, 0.0085, 0.0084, -0.0265,0.0149, -0.0100, 0.0077, 0.0029, 0.0145, -0.0181, -0.0021, -0.0183],grad_fn=<SelectBackward>)
self.bias=
Parameter containing:
tensor([-0.2232, -0.0295, -0.1387, 0.2170, -0.1749, -0.1551, 0.1056, -0.1860,-0.0666, -0.1327, 0.0212, 0.1587, 0.2496, -0.0154, -0.1683, 0.0151],requires_grad=True)
(output + self.bias)[0]=
tensor([-0.2030, -0.0537, -0.0779, 0.2442, -0.1616, -0.1466, 0.1140, -0.2125,-0.0516, -0.1427, 0.0289, 0.1615, 0.2641, -0.0336, -0.1704, -0.0032],grad_fn=<SelectBackward>)
- 使用ReluReluRelu激活函数,
x = F.relu(self.gc1(x, adj))
x[0]=
tensor([0.0000, 0.0000, 0.0000, 0.2442, 0.0000, 0.0000, 0.1140, 0.0000, 0.0000,0.0000, 0.0289, 0.1615, 0.2641, 0.0000, 0.0000, 0.0000],grad_fn=<SelectBackward>)
- 在training阶段,使用dropoutdropoutdropout, 执行x=x1−0.5x=\frac{x}{1-0.5}x=1−0.5x,并以0.5的概率去除:
x = F.dropout(x, self.dropout, training=self.training)
x[0]=
tensor([0.0000, 0.0000, 0.0000, 0.4884, 0.0000, 0.0000, 0.2280, 0.0000, 0.0000,0.0000, 0.0000, 0.3230, 0.5282, 0.0000, 0.0000, 0.0000],grad_fn=<SelectBackward>)
- 执行第二层 gc2
support = torch.mm(input, self.weight) # GraphConvolution forward。input*weightoutput = torch.spmm(adj, support)
计算output,output2708×7=adj2708×2708×input2708×16×W16×7output_{2708 \times 7} = adj_{2708 \times 2708} \times input_{2708 \times 16} \times W_{16 \times 7}output2708×7=adj2708×2708×input2708×16×W16×7,然后返回output=output2708×7+bias1×7output = output_{2708 \times 7} + bias_{1 \times 7}output=output2708×7+bias1×7
output[0]=
tensor([-0.1928, 0.1723, 0.1689, -0.0516, 0.0387, -0.0276, -0.1027],grad_fn=<SelectBackward>)
- 将返回结果x,直接吐给F.log_softmax(x,dim=1)F.log\_softmax(x, dim=1)F.log_softmax(x,dim=1),dim=1dim=1dim=1表示对7维度进行log_softmax
x[0]=
tensor([-2.1474, -1.7823, -1.7856, -2.0062, -1.9158, -1.9822, -2.0573],grad_fn=<SelectBackward>)
- 将output与label进行计算loss 与 acc_train
loss=tensor(1.9186, grad_fn=<NllLossBackward>)
acc_train=tensor(0.1357, dtype=torch.float64)
- 最后进行反向传播,更新梯度W和b
- 完成一次train的过程
GCN(二)GCN模型介绍相关推荐
- 网络安全能力成熟度模型介绍
一.概述 经过多年网络安全工作,一直缺乏网络安全的整体视角,网络安全的全貌到底是什么,一直挺迷惑的.目前网络安全的分类和厂家非常多,而且每年还会冒出来不少新的产品.但这些产品感觉还是像盲人摸象,只看到 ...
- DL之GCN:GCN算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略
DL之GCN:GCN算法的简介(论文介绍).架构详解.案例应用等配图集合之详细攻略 目录 GCN算法的简介(论文介绍) 0.实验结果 GCN算法的架构详解 GCN算法的案例应用 相关文章 DL之GCN ...
- GCN图卷积网络 | 介绍
目录 0 前言 1 基于空间域的GCN[2] 2 基于谱域的GCN 2.1拉普拉斯矩阵 2.2为什么GCN要用拉普拉斯矩阵? 2.3 拉普拉斯矩阵的谱分解(特征分解) 2.4卷积的定义 2.5傅里叶变 ...
- 经典Wide Deep模型介绍及tensorflow 2代码实现
Wide & Deep模型介绍 目标: 内容: 一. 模型介绍 二. 推荐系统架构 三. Wide部分 四. Deep部分 五. Wide和Deep一起训练 六. 系统实现 (1)数据生成阶段 ...
- [机器学习] 二分类模型评估指标---精确率Precision、召回率Recall、ROC|AUC
一 为什么要评估模型? 一句话,想找到最有效的模型.模型的应用是循环迭代的过程,只有通过持续调整和调优才能适应在线数据和业务目标. 选定模型时一开始都是假设数据的分布是一定的,然而数据的分布会随着时间 ...
- Qt样式表之一:Qt样式表和盒子模型介绍
一.Qt样式表介绍 Qt样式表是一个可以自定义部件外观的十分强大的机制,可以用来美化部件.Qt样式表的概念.术语和语法都受到了HTML的层叠样式表(Cascading Style Sheets, CS ...
- PyTorch框架学习二十——模型微调(Finetune)
PyTorch框架学习二十--模型微调(Finetune) 一.Transfer Learning:迁移学习 二.Model Finetune:模型的迁移学习 三.看个例子:用ResNet18预训练模 ...
- [深度学习-总结]Deep learning中8大模型介绍与比较(LeNet5,AlexNet,VGG,Inception,MobileNets,ResNet,DenseNet,Senet)
深度学习 9中模型介绍与比较 0. CNN 结构演化 1. LeNet5 2. AlexNet 3. VGG 为什么使用2个3x3卷积核可以来代替5*5卷积核 4. 1*1卷积 5. Inceptio ...
- PGM学习之二 PGM模型的分类与简介
废话:和上一次的文章确实隔了太久,希望趁暑期打酱油的时间,将之前学习的东西深入理解一下,同时尝试用Python写相关的机器学习代码. 一 PGM模型的分类 通过上一篇文章的介绍,相信大家对PGM的定义 ...
- Netty入门笔记-Linux网络I/O模型介绍
在之前的博客中并没有将关于Netty的知识系统的总结起来.从这篇博客开始就将关于Netty的有关知识点总结起来顺便提升自己的分析问题的能力,通过博客分享的形式将学习的知识点形成体系,希望也可以帮助大家 ...
最新文章
- 微信公众号数据2019_公众号年度数据报告怎么写?2019年公众号年报可一键生成啦!...
- ifcfg系列命令配置网络属性
- CVPR 2022 57 篇论文分方向整理 + 打包下载|涵盖目标检测、语义分割、人群计数、异常检测等方向
- 在某公司用到一些shell
- swift5表情键盘项目封装
- 当maven引用的jar在maven库中下载不到源代码
- cesium米转换经纬度_cesium 笛卡尔坐标(单位:米) 与 经纬度(单位:弧度/度) 之间的转换。...
- 介绍Linux中cp直接覆盖不提示的方法
- 小汤学编程之JAVA番外篇——Properties工具类
- ACM题目————A simple problem
- mysql 赋权笔记
- Spark-Serialization序列化的2种方式解释对比使用场景
- java 查找链表中间元素,如何找到链表的中间节点?
- 右下角弹窗代码_vueamap使用步骤和代码示例
- 极狐gitlib的安装和使用
- 打工就是 “演戏”,你可以认真,但别太当真
- 【第4天】尊重是最有力的征服
- 程序员晒工资,工作 3 年被应届生倒挂!网友:工作 8 年被你倒挂!
- 鸡啄米之VS2010/MFC编程入门之二十四(常用控件:列表框控件ListBox)
- CIKM 2020 | 一文详解美团6篇精选论文
热门文章
- HttpClient, 使用C#操作Web
- Java Zip压缩实现(亲测)
- postman发送post请求,服务器日志确保GET请求错误“ PageNotFound:208 Request method ‘GET‘ not supported”
- 程序员面试时这样介绍自己的项目经验,成功率能达到98.99%
- gitlab常规维护命令
- 程序员怎样才能写出一篇好的技术文章
- PHP返回数据json数据样式要求是对象{},而不是[]
- 钢铁苍穹html5,自定义网站搜索教程
- sql limit不接具体数字_SQL别再秀操作了,这么写吧还是。
- mysql自增id前端安全显示_mysql使用自增id遇到的坑