GCN项目实战1-SimGNN

news2025/2/25 21:25:31

文章目录

  • SimGNN:快速图相似度计算的神经网络方法
    • 1. 数据
    • 2. 模型
      • 2.1 python文件功能介绍
      • 2.2 重要函数和类的实现

SimGNN:快速图相似度计算的神经网络方法

原论文名称:SimGNN: A Neural Network Approach to Fast Graph Similarity Computation
代码链接:https://github.com/benedekrozemberczki/SimGNN

1. 数据

例子:

{"graph_1": [[0, 8], [0, 9], [0, 2], [0, 3], [0, 11], [1, 2], [1, 3], [1, 5], [1, 6], [1, 7], [2, 3], [2, 5], [2, 6], [2, 7], [2, 8], [2, 10], [2, 11], [3, 5], [3, 7], [3, 8], [3, 10], [3, 11], [4, 9], [4, 10], [4, 5], [4, 6], [4, 7], [5, 7], [5, 8], [5, 11], [6, 7], [6, 8], [6, 11], [7, 8], [7, 10], [7, 11], [8, 9], [10, 11]], "ged": 32, "graph_2": [[0, 1], [0, 2], [0, 4], [1, 8], [1, 10], [1, 2], [1, 7], [2, 4], [2, 7], [2, 9], [2, 11], [3, 10], [3, 11], [3, 5], [3, 6], [3, 7], [4, 9], [4, 11], [5, 8], [5, 9], [5, 6], [6, 9], [7, 9], [7, 10], [7, 11], [8, 9], [8, 10], [9, 10], [9, 11], [10, 11]], "labels_2": ["3", "5", "6", "5", "4", "4", "3", "6", "4", "8", "6", "6"], "labels_1": ["5", "5", "9", "8", "5", "7", "6", "9", "7", "3", "5", "7"]}

一个样本包含五个属性:
graph_1:graph1的邻接矩阵
graph_2:graph2的邻接矩阵
labels_1:graph1的特征矩阵
labels_2:graph2的特征矩阵
ged:图相似度

2. 模型

在这里插入图片描述

2.1 python文件功能介绍

layers.py	-- 包含注意力机制层和Neural Tensor Network层的实现
simgnn.py   	-- 模型实现
utils.py  -- 一些辅助函数的实现
param_parser.py   -- 参数
main.py       -- 主函数


--filters-1             INT         Number of filter in 1st GCN layer.       Default is 128.
--filters-2             INT         Number of filter in 2nd GCN layer.       Default is 64. 
--filters-3             INT         Number of filter in 3rd GCN layer.       Default is 32.
--tensor-neurons        INT         Neurons in tensor network layer.         Default is 16.
--bottle-neck-neurons   INT         Bottle neck layer neurons.               Default is 16.
--bins                  INT         Number of histogram bins.                Default is 16.
--batch-size            INT         Number of pairs processed per batch.     Default is 128. 
--epochs                INT         Number of SimGNN training epochs.        Default is 5.
--dropout               FLOAT       Dropout rate.                            Default is 0.5.
--learning-rate         FLOAT       Learning rate.                           Default is 0.001.
--weight-decay          FLOAT       Weight decay.                            Default is 10^-5.
--histogram             BOOL        Include histogram features.              Default is False.

2.2 重要函数和类的实现

  1. 注意力机制层
  • __init__初始化函数

功能
导入需要的参数:args;
设置参数矩阵,形状为[self.args.filters_3,self.args.filters_3]:调用setup_weights(),
初始化参数矩阵的值:调用init_parameters()

def __init__(self, args):
	"""
	:param args: Arguments object.
	"""
	super(AttentionModule, self).__init__()
	self.args = args
	self.setup_weights()
	self.init_parameters()
  • setup_weights函数
def setup_weights(self):
    """
    Defining weights.
    """
    self.weight_matrix = torch.nn.Parameter(torch.Tensor(self.args.filters_3,
                                                         self.args.filters_3))
  • init_parameters函数
def init_parameters(self):
    """
    Initializing weights.
    """
    torch.nn.init.xavier_uniform_(self.weight_matrix)
  • forward函数
'''
key: embedding
value: embedding
query: embedding_i
'''
def forward(self, embedding): # embedding形状:[graph_node_num, self.args.filters_3]
    """
    Making a forward propagation pass to create a graph level representation.
    :param embedding: Result of the GCN.
    :return representation: A graph level representation vector.
    """
    global_context = torch.mean(torch.matmul(embedding, self.weight_matrix), dim=0) # [1,self.args.filters_3]
    transformed_global = torch.tanh(global_context) # [1, self.args.filters_3]
    sigmoid_scores = torch.sigmoid(torch.mm(embedding, transformed_global.view(-1, 1))) # [graph_node_num, 1]
    representation = torch.mm(torch.t(embedding), sigmoid_scores) # [self.args.filters_3, 1]
    return representation

整体代码:

class AttentionModule(torch.nn.Module):
    """
    SimGNN Attention Module to make a pass on graph.
    """
    def __init__(self, args):
        """
        :param args: Arguments object.
        """
        super(AttentionModule, self).__init__()
        self.args = args
        self.setup_weights()
        self.init_parameters()

    def setup_weights(self):
        """
        Defining weights.
        """
        self.weight_matrix = torch.nn.Parameter(torch.Tensor(self.args.filters_3,
                                                             self.args.filters_3))

    def init_parameters(self):
        """
        Initializing weights.
        """
        torch.nn.init.xavier_uniform_(self.weight_matrix)

    def forward(self, embedding): 
        """
        Making a forward propagation pass to create a graph level representation.
        :param embedding: Result of the GCN.
        :return representation: A graph level representation vector.
        """
        global_context = torch.mean(torch.matmul(embedding, self.weight_matrix), dim=0)
        transformed_global = torch.tanh(global_context)
        sigmoid_scores = torch.sigmoid(torch.mm(embedding, transformed_global.view(-1, 1)))
        representation = torch.mm(torch.t(embedding), sigmoid_scores)
        return representation
  1. Neural Tensor Network层
class TenorNetworkModule(torch.nn.Module):
    """
    SimGNN Tensor Network module to calculate similarity vector.
    """
    def __init__(self, args):
        """
        :param args: Arguments object.
        """
        super(TenorNetworkModule, self).__init__()
        self.args = args
        self.setup_weights()
        self.init_parameters()

    def setup_weights(self):
        """
        Defining weights.
        """
        self.weight_matrix = torch.nn.Parameter(torch.Tensor(self.args.filters_3,
                                                             self.args.filters_3,
                                                             self.args.tensor_neurons))

        self.weight_matrix_block = torch.nn.Parameter(torch.Tensor(self.args.tensor_neurons,
                                                                   2*self.args.filters_3))
        self.bias = torch.nn.Parameter(torch.Tensor(self.args.tensor_neurons, 1))

    def init_parameters(self):
        """
        Initializing weights.
        """
        torch.nn.init.xavier_uniform_(self.weight_matrix)
        torch.nn.init.xavier_uniform_(self.weight_matrix_block)
        torch.nn.init.xavier_uniform_(self.bias)

    def forward(self, embedding_1, embedding_2):
    	# embedding_1:[self.args.filters_3, 1]
    	# embedding_2:[self.args.filters_3, 1]
        """
        Making a forward propagation pass to create a similarity vector.
        :param embedding_1: Result of the 1st embedding after attention.
        :param embedding_2: Result of the 2nd embedding after attention.
        :return scores: A similarity score vector.
        """
        scoring = torch.mm(torch.t(embedding_1), self.weight_matrix.view(self.args.filters_3, -1)) # [1, self.args.filters_3*self.args.tensor_neurons]
        scoring = scoring.view(self.args.filters_3, self.args.tensor_neurons) # [self.args.filters_3, self.args.tensor_neurons]
        scoring = torch.mm(torch.t(scoring), embedding_2) # [self.args.tensor_neurons, 1]
        combined_representation = torch.cat((embedding_1, embedding_2)) # [2*self.args.filters_3, 1]
        block_scoring = torch.mm(self.weight_matrix_block, combined_representation) # [self.args.tensor_neurons, 1]
        scores = torch.nn.functional.relu(scoring + block_scoring + self.bias) # [self.args.tensor_neurons, 1]
        return scores

  1. SimGNN模型
  • __init__初始化函数:

功能:导入需要用到的参数:args,label数量:number_of_labels,构建模型:调用setup_layers函数

def __init__(self, args, number_of_labels):
    """
    :param args: Arguments object.
    :param number_of_labels: Number of node labels.
    """
    super(SimGNN, self).__init__()
    self.args = args
    self.number_labels = number_of_labels # 存放label种类数量
    self.setup_layers()
  • calculate_bottleneck_features函数:

功能:是否要加上histogram层(下半部分)提取的embedding

def calculate_bottleneck_features(self):
    """
    Deciding the shape of the bottleneck layer.
    """
    if self.args.histogram == True:
        self.feature_count = self.args.tensor_neurons + self.args.bins
    else:
        self.feature_count = self.args.tensor_neurons
  • setup_layers函数:

功能:构建模型,包括三个图卷积层,自注意力机制层,Neural Tensor Network层,两个线性层,最后输出一个预测的值。

def setup_layers(self):
    """
    Creating the layers.
    """
    self.calculate_bottleneck_features()
    self.convolution_1 = GCNConv(self.number_labels, self.args.filters_1)
    self.convolution_2 = GCNConv(self.args.filters_1, self.args.filters_2)
    self.convolution_3 = GCNConv(self.args.filters_2, self.args.filters_3)
    self.attention = AttentionModule(self.args)
    self.tensor_network = TenorNetworkModule(self.args)
    self.fully_connected_first = torch.nn.Linear(self.feature_count,
                                                 self.args.bottle_neck_neurons)
    self.scoring_layer = torch.nn.Linear(self.args.bottle_neck_neurons, 1)
  • calculate_histogram函数:
def calculate_histogram(self, abstract_features_1, abstract_features_2):
    """
    Calculate histogram from similarity matrix.
    :param abstract_features_1: Feature matrix for graph 1.
    :param abstract_features_2: Feature matrix for graph 2.
    :return hist: Histsogram of similarity scores.
    """
    scores = torch.mm(abstract_features_1, abstract_features_2).detach()
    scores = scores.view(-1, 1)
    hist = torch.histc(scores, bins=self.args.bins)
    hist = hist/torch.sum(hist)
    hist = hist.view(1, -1)
    return hist
  • convolutional_pass函数:
def convolutional_pass(self, edge_index, features):
    """
    Making convolutional pass.
    :param edge_index: Edge indices.
    :param features: Feature matrix.
    :return features: Absstract feature matrix.
    """
    features = self.convolution_1(features, edge_index)
    features = torch.nn.functional.relu(features)
    features = torch.nn.functional.dropout(features,
                                           p=self.args.dropout,
                                           training=self.training)

    features = self.convolution_2(features, edge_index)
    features = torch.nn.functional.relu(features)
    features = torch.nn.functional.dropout(features,
                                           p=self.args.dropout,
                                           training=self.training)

    features = self.convolution_3(features, edge_index)
    return features
  • forward函数:

功能:运行神经网络,预测结果

def forward(self, data):
    """
    Forward pass with graphs.
    :param data: Data dictiyonary.
    :return score: Similarity score.
    """
    edge_index_1 = data["edge_index_1"]
    edge_index_2 = data["edge_index_2"]
    features_1 = data["features_1"]
    features_2 = data["features_2"]

	# 图卷积的计算
    abstract_features_1 = self.convolutional_pass(edge_index_1, features_1) # [graph1_num_node,self.args.filters_3]
    abstract_features_2 = self.convolutional_pass(edge_index_2, features_2) # [graph2_num_node,self.args.filters_3]

	# 计算histogram
    if self.args.histogram == True:
        hist = self.calculate_histogram(abstract_features_1,
                                        torch.t(abstract_features_2))

	# 使用注意力机制层
    pooled_features_1 = self.attention(abstract_features_1)
    pooled_features_2 = self.attention(abstract_features_2)
    scores = self.tensor_network(pooled_features_1, pooled_features_2) # [self.args.tensor_neurons, 1]
    scores = torch.t(scores) # [1,self.args.tensor_neurons]

	# 合并注意力机制层和Neural Tensor Network层提取的特征
    if self.args.histogram == True:
        scores = torch.cat((scores, hist), dim=1).view(1, -1) # [1, self.feature_count]

	# 获得预测分数,由于标准答案使用了归一化,所以最后要过一下sigmoid层
    scores = torch.nn.functional.relu(self.fully_connected_first(scores)) # [1, self.args.bottle_neck_neurons]
    score = torch.sigmoid(self.scoring_layer(scores)) # self.scoring_layer(scores): [1,1]
    return score

整体代码:

class SimGNN(torch.nn.Module):
    """
    SimGNN: A Neural Network Approach to Fast Graph Similarity Computation
    https://arxiv.org/abs/1808.05689
    """
    def __init__(self, args, number_of_labels):
        """
        :param args: Arguments object.
        :param number_of_labels: Number of node labels.
        """
        super(SimGNN, self).__init__()
        self.args = args
        self.number_labels = number_of_labels
        self.setup_layers()

    def calculate_bottleneck_features(self):
        """
        Deciding the shape of the bottleneck layer.
        """
        if self.args.histogram == True:
            self.feature_count = self.args.tensor_neurons + self.args.bins
        else:
            self.feature_count = self.args.tensor_neurons

    def setup_layers(self):
        """
        Creating the layers.
        """
        self.calculate_bottleneck_features()
        self.convolution_1 = GCNConv(self.number_labels, self.args.filters_1)
        self.convolution_2 = GCNConv(self.args.filters_1, self.args.filters_2)
        self.convolution_3 = GCNConv(self.args.filters_2, self.args.filters_3)
        self.attention = AttentionModule(self.args)
        self.tensor_network = TenorNetworkModule(self.args)
        self.fully_connected_first = torch.nn.Linear(self.feature_count,
                                                     self.args.bottle_neck_neurons)
        self.scoring_layer = torch.nn.Linear(self.args.bottle_neck_neurons, 1)

    def calculate_histogram(self, abstract_features_1, abstract_features_2):
        """
        Calculate histogram from similarity matrix.
        :param abstract_features_1: Feature matrix for graph 1.
        :param abstract_features_2: Feature matrix for graph 2.
        :return hist: Histsogram of similarity scores.
        """
        scores = torch.mm(abstract_features_1, abstract_features_2).detach()
        scores = scores.view(-1, 1)
        hist = torch.histc(scores, bins=self.args.bins)
        hist = hist/torch.sum(hist)
        hist = hist.view(1, -1)
        return hist

    def convolutional_pass(self, edge_index, features):
        """
        Making convolutional pass.
        :param edge_index: Edge indices.
        :param features: Feature matrix.
        :return features: Absstract feature matrix.
        """
        features = self.convolution_1(features, edge_index)
        features = torch.nn.functional.relu(features)
        features = torch.nn.functional.dropout(features,
                                               p=self.args.dropout,
                                               training=self.training)

        features = self.convolution_2(features, edge_index)
        features = torch.nn.functional.relu(features)
        features = torch.nn.functional.dropout(features,
                                               p=self.args.dropout,
                                               training=self.training)

        features = self.convolution_3(features, edge_index)
        return features

    def forward(self, data):
        """
        Forward pass with graphs.
        :param data: Data dictiyonary.
        :return score: Similarity score.
        """
        edge_index_1 = data["edge_index_1"]
        edge_index_2 = data["edge_index_2"]
        features_1 = data["features_1"]
        features_2 = data["features_2"]
		
		# 图卷积的计算
        abstract_features_1 = self.convolutional_pass(edge_index_1, features_1) # [graph1_num_node,self.args.filters_3]
        abstract_features_2 = self.convolutional_pass(edge_index_2, features_2) # [graph2_num_node,self.args.filters_3]
		
		# 计算histogram
        if self.args.histogram == True:
            hist = self.calculate_histogram(abstract_features_1,
                                            torch.t(abstract_features_2))
		
		# 使用注意力机制层
        pooled_features_1 = self.attention(abstract_features_1)
        pooled_features_2 = self.attention(abstract_features_2)
        scores = self.tensor_network(pooled_features_1, pooled_features_2) # [self.args.tensor_neurons, 1]
        scores = torch.t(scores) # [1,self.args.tensor_neurons]
		
		# 合并注意力机制层和Neural Tensor Network层提取的特征
        if self.args.histogram == True:
            scores = torch.cat((scores, hist), dim=1).view(1, -1) # [1, self.feature_count]
		
		# 获得预测分数,由于标准答案使用了归一化,所以最后要过一下sigmoid层
        scores = torch.nn.functional.relu(self.fully_connected_first(scores)) # [1, self.args.bottle_neck_neurons]
        score = torch.sigmoid(self.scoring_layer(scores)) # self.scoring_layer(scores): [1,1]
        return score

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/361725.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

2023年,即时配送迎来黄金年,其他玩家该如何“弯道超车”?

我们知道,面对如此宏达的快递行业,它的市场一直被许多人所看好。尤其近几年,随着电商、物流的发展,各互联网公司纷纷跻身这一领域。现在市面上为大家所熟知的三通一达、极兔以及顺丰等等。 除此之外,在一些细分领域&a…

AWS攻略——Peering连接VPC

文章目录创建IP/CIDR不覆盖的VPC创建VPC创建子网创建密钥对创建EC2创建Peering接受Peering邀请修改各个VPC的路由表修改美东us-east-1 pulic subnet的路由修改悉尼ap-southeast-2路由测试知识点我们回顾下《AWS攻略——VPC初识》中的知识: 一个VPC只能设置在一个Re…

劳特巴赫仿真测试工具Trace32的基本使用(cmm文件)

劳特巴赫 Trace32 调试使用教程 使用PRACTICE 脚本(.cmm) 在TRACE32 中使用PRACTICE 脚本(*.cmm)将帮助你: 在调试器启动时立即执行命令根据您的项目需求自定义TRACE32PowerView用户界面加载应用程序或符号使调试操作具有可重复性, 并可用于验证目的和回归测试 自动启动脚本…

Spring Boot系列--创建第一个Spring Boot项目

1.项目搭建 在IDEA中新建项目,选择Spring Initializr。 填写项目信息: 选择版本和Spring Web依赖: Spring Web插件能为项目集成Tomcat、配置dispatcherServlet和xml文件。此处选择的版本若为3.0.2的话会出现如下错误: java: …

【C++】文件IO流

一起来康康C中的文件IO操作吧 文章目录1.operator bool2.C文件IO流3.文件操作3.0 关于按位与的说明3.1 ifstream3.2 ofstream流插入文本3.3 ostringstream/istringstream3.4 stringstream3.5使用stringstream的注意事项结语1.operator bool 之前写OJ的时候,就已经用…

Python学习-----排序问题1.0(冒泡排序、选择排序、插入排序)

目录 前言: 1.冒泡排序 2.选择排序 3.插入排序 前言: 学过C语言肯定接触过排序问题,我们最常用的也就是冒泡排序、选择排序、插入排序……等等,同样在Python中也有排序问题,这里我也会讲解Python中冒泡排序、选择排…

MvvmLight框架入门

MvvmLight MvvmLight主要程序库 ViewModelLocator 在.netFramework环境下nuget安装MvvmLight会自动安装依赖项MvvmLightLibs库,如果在.net core环境下需要手动安装MvvmLightLibs库。 .netFramework环境下安装完成后,在ViewModel目录下具有一个 ViewMo…

page cache设计及实现

你好,我是安然无虞。 page cache的设计及实现 page cache 本质上也是一个哈希桶, 它是按照页的数量进行映射的. 当 central cache 向 page cache 申请内存时, page cache 先检查对应位置是否有span, 如果没有则向更大页去寻找一个span, 如果找到则分裂成两个. 比如…

Java基础常见面试题(五)

异常 Java 异常类层次结构图概览 : Exception 和 Error 有什么区别? Java 中,所有的异常都有一个共同的祖先 java.lang 包中的 Throwable 类。Throwable 类有两个重要的子类 Exception(异常)和 Error(错误…

DAMA-CDGA/CDGP数据治理认证考试如何报考?

DAMA认证为数据管理专业人士提供职业目标晋升规划,彰显了职业发展里程碑及发展阶梯定义,帮助数据管理从业人士获得企业数字化转型战略下的必备职业能力,促进开展工作实践应用及实际问题解决,形成企业所需的新数字经济下的核心职业…

LocalDNS

目录 文章目录目录本节实战DNS优化1、dns 5s 超时问题解决办法2、NodeLocal DNSCache实验软件关于我最后本节实战 实战名称💘 实战:NodeLocal DNSCache-2022.7.30(测试成功)💘 实战:NodeLocal DNSCache-2023.2.21(测试成功) DNS优…

强大的内置数据结构_元组(6,)

如约而至,紧接着上一期文章,小编将会陆续把全套的Python笔记将依次发放给大家,便于大家学习Python、期末备考、巩固基础等(这几期是公众号小插曲,后期发放编程技术的话主要还是会围绕Java来展开~感谢大家支持)元组python内置数据结…

Vulnhub靶场----2、DC-2

文章目录一、环境搭建二、渗透流程三、思路总结一、环境搭建 DC-2下载地址:https://download.vulnhub.com/dc/DC-2.zip kali:192.168.144.148 DC-2:192.168.144.150 添加hosts文件:192.168.144.150 DC-2 二、渗透流程 nmap -A -…

FLV-初学总结

FLV-初学总结 从零开始仅学习了一下午的总结,本文非常稚嫩… 本文为纯初学者的学习记录,为了方便理解,内容未必严谨,可以用作纯新手的入门了解篇。本文主要的参考链接如下⬇️ 详细了解FLV:FLV官方文档(Ve…

面向对象之三大特性(二)

5. 面向对象三大特性 继承、多肽、封装 5.1 继承 类的继承与现实生活中的父、子继承关系一样。被继承的类称为父类、基类或超类,继承者称为子类。Python 继承分为:单继承和多继承(继承多个类)。 class Parent1:pass class Par…

分析称勒索攻击在非洲、中东与中国增长最快

Orange Cyberdefense(OCD)于 2022 年 12 月 1 日发布了最新的网络威胁年度报告。报告中指出,网络勒索仍然是头号威胁 ,也逐渐泛滥到世界各地。 报告中的网络威胁指的是企业网络中的某些资产被包括勒索软件在内的攻击进行勒索&…

Qt中修改界面类的类名时需要注意的几个修改点

有些时候因为一些原因,需要修改Qt中创建的界面类,需要特别注意几个修改点。 比如将test类修改为test2类 修改test.h名称为test2.h文件;修改test.cpp名称为test2.cpp文件;修改test.ui名称为test2.ui文件;修改pro文件中…

前端Docker部署方案

一、Docker容器和镜像概念 首先明确镜像和容器的概念。我们可以用 docker 构建一个镜像,这个镜像可以导入导出,用于传输,重复利用。然后如果把他 run 起来,则称为一个容器。容器是运行时,会包括运行时上下文&#xff…

Vue基础14之TodoList组件自定义事件、全局事件总线、TodoList全局事件总线

Vue基础14TodoList-组件自定义事件先改Header和Footer子组件,List先不考虑App.vueMyHeader.vueMyFooter.vue全局事件总线实现思路正规写法main.jsApp.vueStudent.vueSchool.vue总结:全局事件总线(GlobalEventBus)TodoList案例&…

【Java闭关修炼】MyBatis-接口代理的方式实现Dao层

【Java闭关修炼】MyBatis-接口代理的方式实现Dao层实现规则代码实现代理对象分析接口代理方式小结实现规则 映射配置文件中的名称空间必须和Dao层接口的全类名相同映射配置文件的增删改查标签的id属性必须和Dao层接口方法的参数相同映射配置文件中的增删改查标签的parameterTyp…