前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >知识图谱推理(论文复现)

知识图谱推理(论文复现)

作者头像
Srlua
发布2024-11-13 08:38:32
530
发布2024-11-13 08:38:32
举报
文章被收录于专栏:CSDN社区搬运

论文概述

本研究专注于基于图神经网络(GNN)的知识图谱推理,特别关注了传播路径的应用与优化。在智能问答和推荐系统等领域,知识图谱推理具有关键作用,但传统GNN方法在效率和准确度方面存在局限。为了改进这些问题,本研究引入了创新的自适应传播策略AdaProp,并与传统的Red-GNN方法进行了对比实验。通过实际运行AdaProp和Red-GNN两种方法,并在多个数据集上进行实验验证,结果显示AdaProp在多项性能指标上取得了显著的提升。这一发现不仅突显了AdaProp在知识图谱推理中的潜力,也为该领域的未来研究和应用提供了新的方向。AdaProp的成功实现在理论和实践层面上都为知识图谱推理开辟了新的可能性,强调了自适应传播策略的重要性。

论文方法

通过有效的采样技术来动态调整传播路径,既考虑到查询实体和查询关系的依赖性,又避免在传播过程中涉及过多无关实体,从而提高推理效率并减少计算成本。这将涉及到开发新的采样策略,以确保在扩展传播路径时能够保持对目标答案实体的精确预测。为此,提出了一种名为AdaProp的基于GNN的方法,该算法可以根据给定的查询动态调整传播路径。

在这里插入图片描述
在这里插入图片描述

与传统方法的比较

在知识图谱推理领域,传统的方法如全传播、渐进式传播和受限传播都各自有优势和局限。提出的AdaProp方法在效率和性能上对这些传统方法进行了显著的优化。

实验部分

1 实验条件

使用Python环境和PyTorch框架,在单个NVIDIA RTX 3070 GPU上进行,该GPU具有8GB的内存。实验的主要目的是验证AdaProp算法在传导(transductive)和归纳(inductive)设置下的有效性,并分析其各个组成部分在模型性能中的作用。

2 .数据集

family数据集,存放在./transductive/data文件夹下

3 .实验步骤

step1:安装环境依赖

  • torch == 1.12.1
  • torch_scatter == 2.0.9
  • numpy == 1.21.6
  • scipy == 1.10.1

step2:进入项目目录,进行训练

Description
Description

step3:输入tensorboard指令,可视化结果

4.实验结果
Description
Description

核心代码

代码语言:javascript
复制
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#555555"># start</span>
 <span style="color:#000000">check</span> <span style="color:#000000">all</span> <span style="color:#000000">output</span> <span style="color:#000000">paths</span>
    <span style="color:#0000ff">checkPath</span>(<span style="color:#aa1111">'./results/'</span>)
    <span style="color:#000000">checkPath</span>(<span style="color:#000000">f</span><span style="color:#aa1111">'./results/{dataset}/'</span>)
    <span style="color:#000000">checkPath</span>(<span style="color:#000000">f</span><span style="color:#aa1111">'{loader.task_dir}/saveModel/'</span>)
​
    <span style="color:#000000">model</span> <span style="color:#981a1a">=</span> <span style="color:#000000">BaseModel</span>(<span style="color:#000000">opts</span>, <span style="color:#000000">loader</span>)
    <span style="color:#000000">opts</span>.<span style="color:#000000">perf_file</span> <span style="color:#981a1a">=</span> <span style="color:#000000">f</span><span style="color:#aa1111">'results/{dataset}/{model.modelName}_perf.txt'</span>
    <span style="color:#000000">print</span>(<span style="color:#000000">f</span><span style="color:#aa1111">'==> perf_file: {opts.perf_file}'</span>)
​
    <span style="color:#000000">config_str</span> <span style="color:#981a1a">=</span> <span style="color:#aa1111">'%.4f, %.4f, %.6f,  %d, %d, %d, %d, %.4f,%s\n'</span> <span style="color:#981a1a">%</span> (
    <span style="color:#000000">opts</span>.<span style="color:#000000">lr</span>, <span style="color:#000000">opts</span>.<span style="color:#000000">decay_rate</span>, <span style="color:#000000">opts</span>.<span style="color:#000000">lamb</span>, <span style="color:#000000">opts</span>.<span style="color:#000000">hidden_dim</span>, <span style="color:#000000">opts</span>.<span style="color:#000000">attn_dim</span>, <span style="color:#000000">opts</span>.<span style="color:#000000">n_layer</span>, <span style="color:#000000">opts</span>.<span style="color:#000000">n_batch</span>, <span style="color:#000000">opts</span>.<span style="color:#000000">dropout</span>,
    <span style="color:#000000">opts</span>.<span style="color:#000000">act</span>)
    <span style="color:#000000">print</span>(<span style="color:#000000">config_str</span>)
    <span style="color:#000000">with</span> <span style="color:#0000ff">open</span>(<span style="color:#000000">opts</span>.<span style="color:#000000">perf_file</span>, <span style="color:#aa1111">'a+'</span>) <span style="color:#000000">as</span> <span style="color:#000000">f</span>:
        <span style="color:#000000">f</span>.<span style="color:#000000">write</span>(<span style="color:#000000">config_str</span>)
​
    <span style="color:#770088">if</span> <span style="color:#000000">args</span>.<span style="color:#000000">weight</span> <span style="color:#981a1a">!=</span> <span style="color:#000000">None</span>:
        <span style="color:#000000">model</span>.<span style="color:#000000">loadModel</span>(<span style="color:#000000">args</span>.<span style="color:#000000">weight</span>)
        <span style="color:#000000">model</span>.<span style="color:#000000">_update</span>()
        <span style="color:#000000">model</span>.<span style="color:#000000">model</span>.<span style="color:#000000">updateTopkNums</span>(<span style="color:#000000">opts</span>.<span style="color:#000000">n_node_topk</span>)
​
    <span style="color:#770088">if</span> <span style="color:#000000">opts</span>.<span style="color:#000000">train</span>:
        <span style="color:#000000">writer</span> <span style="color:#981a1a">=</span> <span style="color:#000000">SummaryWriter</span>(<span style="color:#000000">log_dir</span><span style="color:#981a1a">=</span><span style="color:#000000">f</span><span style="color:#aa1111">'./tensorboard_logs/{dataset}'</span>)
        <span style="color:#555555"># training mode</span>
        <span style="color:#000000">best_v_mrr</span> <span style="color:#981a1a">=</span> <span style="color:#116644">0</span>
        <span style="color:#770088">for</span> <span style="color:#000000">epoch</span> <span style="color:#000000">in</span> <span style="color:#0000ff">range</span>(<span style="color:#000000">opts</span>.<span style="color:#000000">epoch</span>):
            <span style="color:#000000">epoch_loss</span> <span style="color:#981a1a">=</span> <span style="color:#000000">model</span>.<span style="color:#000000">train_batch</span>()
            <span style="color:#770088">if</span> <span style="color:#000000">epoch_loss</span> <span style="color:#000000">is</span> <span style="color:#000000">not</span> <span style="color:#000000">None</span>:
                <span style="color:#000000">writer</span>.<span style="color:#000000">add_scalar</span>(<span style="color:#aa1111">'Training Loss'</span>, <span style="color:#000000">epoch_loss</span>, <span style="color:#000000">epoch</span>)
            <span style="color:#770088">else</span>:
                <span style="color:#000000">print</span>(<span style="color:#aa1111">"Warning: Skipping logging of Training Loss due to NoneType."</span>)
            <span style="color:#000000">model</span>.<span style="color:#000000">train_batch</span>()
            <span style="color:#555555"># eval on val/test set</span>
            <span style="color:#770088">if</span> (<span style="color:#000000">epoch</span> <span style="color:#981a1a">+</span> <span style="color:#116644">1</span>) <span style="color:#981a1a">%</span> <span style="color:#000000">args</span>.<span style="color:#000000">eval_interval</span> <span style="color:#981a1a">==</span> <span style="color:#116644">0</span>:
                <span style="color:#000000">result_dict</span>, <span style="color:#000000">out_str</span> <span style="color:#981a1a">=</span> <span style="color:#000000">model</span>.<span style="color:#000000">evaluate</span>(<span style="color:#000000">eval_val</span><span style="color:#981a1a">=</span><span style="color:#000000">True</span>, <span style="color:#000000">eval_test</span><span style="color:#981a1a">=</span><span style="color:#000000">True</span>)
                <span style="color:#000000">v_mrr</span>, <span style="color:#000000">t_mrr</span> <span style="color:#981a1a">=</span> <span style="color:#000000">result_dict</span>[<span style="color:#aa1111">'v_mrr'</span>], <span style="color:#000000">result_dict</span>[<span style="color:#aa1111">'t_mrr'</span>]
                <span style="color:#000000">writer</span>.<span style="color:#000000">add_scalar</span>(<span style="color:#aa1111">'Validation MRR'</span>, <span style="color:#000000">result_dict</span>[<span style="color:#aa1111">'v_mrr'</span>], <span style="color:#000000">epoch</span>)
                <span style="color:#000000">writer</span>.<span style="color:#000000">add_scalar</span>(<span style="color:#aa1111">'Validation Hits@1'</span>, <span style="color:#000000">result_dict</span>[<span style="color:#aa1111">'v_h1'</span>], <span style="color:#000000">epoch</span>)
                <span style="color:#000000">writer</span>.<span style="color:#000000">add_scalar</span>(<span style="color:#aa1111">'Validation Hits@10'</span>, <span style="color:#000000">result_dict</span>[<span style="color:#aa1111">'v_h10'</span>], <span style="color:#000000">epoch</span>)
                <span style="color:#000000">writer</span>.<span style="color:#000000">add_scalar</span>(<span style="color:#aa1111">'Test MRR'</span>, <span style="color:#000000">result_dict</span>[<span style="color:#aa1111">'t_mrr'</span>], <span style="color:#000000">epoch</span>)
                <span style="color:#000000">writer</span>.<span style="color:#000000">add_scalar</span>(<span style="color:#aa1111">'Test Hits@1'</span>, <span style="color:#000000">result_dict</span>[<span style="color:#aa1111">'t_h1'</span>], <span style="color:#000000">epoch</span>)
                <span style="color:#000000">writer</span>.<span style="color:#000000">add_scalar</span>(<span style="color:#aa1111">'Test Hits@10'</span>, <span style="color:#000000">result_dict</span>[<span style="color:#aa1111">'t_h10'</span>], <span style="color:#000000">epoch</span>)
                <span style="color:#000000">print</span>(<span style="color:#000000">out_str</span>)
                <span style="color:#000000">with</span> <span style="color:#0000ff">open</span>(<span style="color:#000000">opts</span>.<span style="color:#000000">perf_file</span>, <span style="color:#aa1111">'a+'</span>) <span style="color:#000000">as</span> <span style="color:#000000">f</span>:
                    <span style="color:#000000">f</span>.<span style="color:#000000">write</span>(<span style="color:#000000">out_str</span>)
                <span style="color:#770088">if</span> <span style="color:#000000">v_mrr</span> <span style="color:#981a1a">></span> <span style="color:#000000">best_v_mrr</span>:
                    <span style="color:#000000">best_v_mrr</span> <span style="color:#981a1a">=</span> <span style="color:#000000">v_mrr</span>
                    <span style="color:#000000">best_str</span> <span style="color:#981a1a">=</span> <span style="color:#000000">out_str</span>
                    <span style="color:#0000ff">print</span>(<span style="color:#000000">str</span>(<span style="color:#000000">epoch</span>) <span style="color:#981a1a">+</span> <span style="color:#aa1111">'\t'</span> <span style="color:#981a1a">+</span> <span style="color:#000000">best_str</span>)
                    <span style="color:#000000">BestMetricStr</span> <span style="color:#981a1a">=</span> <span style="color:#000000">f</span><span style="color:#aa1111">'ValMRR_{str(v_mrr)[:5]}_TestMRR_{str(t_mrr)[:5]}'</span>
                    <span style="color:#000000">model</span>.<span style="color:#000000">saveModelToFiles</span>(<span style="color:#000000">BestMetricStr</span>, <span style="color:#000000">deleteLastFile</span><span style="color:#981a1a">=</span><span style="color:#000000">False</span>)
​
        <span style="color:#555555"># show the final result</span>
        <span style="color:#000000">print</span>(<span style="color:#000000">best_str</span>)
        <span style="color:#000000">writer</span>.<span style="color:#000000">close</span>()
        <span style="color:#000000">model</span>.<span style="color:#000000">writer</span>.<span style="color:#000000">close</span>()</span></span>

希望对你有帮助!加油!

若您认为本文内容有益,请不吝赐予赞同并订阅,以便持续接收有价值的信息。衷心感谢您的关注和支持!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-11-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 论文概述
  • 论文方法
  • 实验部分
    • 1 实验条件
      • 2 .数据集
        • 3 .实验步骤
          • 4.实验结果
          • 核心代码
          相关产品与服务
          灰盒安全测试
          腾讯知识图谱(Tencent Knowledge Graph,TKG)是一个集成图数据库、图计算引擎和图可视化分析的一站式平台。支持抽取和融合异构数据,支持千亿级节点关系的存储和计算,支持规则匹配、机器学习、图嵌入等图数据挖掘算法,拥有丰富的图数据渲染和展现的可视化方案。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档