本研究专注于基于图神经网络(GNN)的知识图谱推理,特别关注了传播路径的应用与优化。在智能问答和推荐系统等领域,知识图谱推理具有关键作用,但传统GNN方法在效率和准确度方面存在局限。为了改进这些问题,本研究引入了创新的自适应传播策略AdaProp,并与传统的Red-GNN方法进行了对比实验。通过实际运行AdaProp和Red-GNN两种方法,并在多个数据集上进行实验验证,结果显示AdaProp在多项性能指标上取得了显著的提升。这一发现不仅突显了AdaProp在知识图谱推理中的潜力,也为该领域的未来研究和应用提供了新的方向。AdaProp的成功实现在理论和实践层面上都为知识图谱推理开辟了新的可能性,强调了自适应传播策略的重要性。
通过有效的采样技术来动态调整传播路径,既考虑到查询实体和查询关系的依赖性,又避免在传播过程中涉及过多无关实体,从而提高推理效率并减少计算成本。这将涉及到开发新的采样策略,以确保在扩展传播路径时能够保持对目标答案实体的精确预测。为此,提出了一种名为AdaProp的基于GNN的方法,该算法可以根据给定的查询动态调整传播路径。
与传统方法的比较
在知识图谱推理领域,传统的方法如全传播、渐进式传播和受限传播都各自有优势和局限。提出的AdaProp方法在效率和性能上对这些传统方法进行了显著的优化。
使用Python环境和PyTorch框架,在单个NVIDIA RTX 3070 GPU上进行,该GPU具有8GB的内存。实验的主要目的是验证AdaProp算法在传导(transductive)和归纳(inductive)设置下的有效性,并分析其各个组成部分在模型性能中的作用。
family数据集,存放在./transductive/data文件夹下
step1:安装环境依赖
step2:进入项目目录,进行训练
step3:输入tensorboard指令,可视化结果
<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>
希望对你有帮助!加油!
若您认为本文内容有益,请不吝赐予赞同并订阅,以便持续接收有价值的信息。衷心感谢您的关注和支持!