AI Wiki中文/EN

知识图谱补全:过滤排名究竟衡量什么?

Knowledge graph completion

为缺失链接评分,计算原始与过滤排名,检查假负例和并列分数。

本页目录直觉理解数学与方法代码实现局限与误解实验与练习研究脉络

模型没变,排名却提高了

模型把两座城市排在查询答案的前两位。测试文件标注第二座城市为正确答案,于是原始评估给它第2名。如果第一座城市也是已知正确答案,把这个竞争者移除后,目标的过滤排名变为第1名,模型参数一个都没改。

知识图谱补全预测缺失关系或缺失端点,不只是数据库查询,因为要找的部分边根本不在输入图中。TransE描述了原始与过滤端点排名;Toutanova和Chen则研究了这项任务中的观察特征和潜在特征。TransE,PDF第5页 任务与评估,PDF第2、5页

最需要分清的是:未知与假、训练输入与评估元数据,以及预测分数与计算排名的协议。下面用一个能逐项检查的小图把它们分开。

已知答案过滤改变排名和MRR,评分未变;并列排名采用明确约定。
已知答案过滤改变排名和MRR,评分未变;并列排名采用明确约定。

先规定哪些信息允许被观察

设E是固定实体名单,R是关系标签。这个传导式设置中,训练边和测试边的实体都来自名单,但两组边不相交:

Gtrain,Gtest⊆E×R×E,Gtrain∩Gtest=∅.G_{\rm train},G_{\rm test}\subseteq E\times R\times E,\qquad G_{\rm train}\cap G_{\rm test}=\varnothing.

边不相交还不足以防住所有捷径。近重复关系或逆关系可能用另一个标签暴露同一配对。Toutanova和Chen在构造更难的评测数据时讨论了移除这类关系。报告模型比较时,应同时交代准确的数据构造和划分。PDF第8页选读逆关系段

当观察主要为正例时,训练仍常需要竞争三元组。替换一个端点,并排除已知训练正例,可构造损坏候选池:

N(h,r,t)=({(e,r,t):e∈E}∪{(h,r,e):e∈E})∖(Gtrain∪{(h,r,t)}).\mathcal N(h,r,t)=\big(\{(e,r,t):e\in E\}\cup\{(h,r,e):e\in E\}\big)\setminus\big(G_{\rm train}\cup\{(h,r,t)\}\big).

里面的三元组是训练竞争者,不是经过证实的假陈述。未观察到的正确边也可能进去,本例会显示一条留出正例确实进入了候选池。这个构造借鉴TransE的单端点替换,但明确去掉训练正例是本文声明的选择。PDF第3页

为了便于检查,基线计算同一关系的两跳路径数,再加上较小的尾实体频次项,两者都只来自训练边:

f(h,r,t)=∑z∈E1[(h,r,z)∈Gtrain]1[(z,r,t)∈Gtrain]+0.1∑x∈E1[(x,r,t)∈Gtrain].f(h,r,t)=\sum_{z\in E}\mathbf1[(h,r,z)\in G_{\rm train}]\mathbf1[(z,r,t)\in G_{\rm train}]+0.1\sum_{x\in E}\mathbf1[(x,r,t)\in G_{\rm train}].

这是原创评分启发式,偏向类似传递的模式和热门尾实体,不是学到的规则,也没有断言所有关系都具传递性。Toutanova和Chen的观察特征方法使用直接链接与学习权重,因此本程序不复现他们的模型。PDF第2页

对目标t和候选集合C,分数越高越好。令B为分数严格更高的候选数,T为与目标同分的候选数,包含目标自身。这里明确定义三种并列约定:

ropt=1+B,rpess=B+T,ravg=1+B+T−12.r_{\rm opt}=1+B,\qquad r_{\rm pess}=B+T,\qquad r_{\rm avg}=1+B+\frac{T-1}{2}.

六个候选全同分时,三种排名分别为1、6、3.5。先取平均排名再取倒数,不等于对各个可能位置的倒数求平均;需要报告采用哪种约定。

设K包含评估协议允许使用的已知正例。移除其他已知正确答案,但始终保留当前目标:

Cfilt(h,r,t)={e∈E:(h,r,e)∉K}∪{t}.C_{\rm filt}(h,r,t)=\{e\in E:(h,r,e)\notin K\}\cup\{t\}.

TransE在这一步使用训练、验证和测试集的已知正例。它们是评估元数据,不代表可以把测试边加入评分所用的图。尚未知晓的正确替代答案仍可能留在候选里。PDF第5页对M个目标查询,定义:

MRR⁡=1M∑i=1M1ri,Hits@⁡k=1M∑i=1M1[ri≤k].\operatorname{MRR}=\frac1M\sum_{i=1}^{M}\frac1{r_i},\qquad\operatorname{Hits@}k=\frac1M\sum_{i=1}^{M}\mathbf1[r_i\le k].

MRR奖励靠前排名,Hits@k检查是否进入截断位置。两者都不能直接衡量事实确定性,也不等于实际部署时批量新增边的精确率。

不要让测试事实进入评分

Python3.12程序只用标准库,六个实体ID和五条训练边均为合成数据。测试要求预测从a出发的两个缺失尾实体。同一个评分函数分别接受两种评估,已知正例只影响候选过滤。

"""Original link-prediction protocol with a declared two-hop/count baseline.

Known positives filter evaluation only; no embedding or benchmark reproduction.
"""
import json
import math
from collections import Counter


def scores(train, head, relation, entities):
    edges = set(train)
    tails = Counter(t for _, r, t in edges if r == relation)
    forward = {}
    for h, r, t in edges:
        if r == relation:
            forward.setdefault(h, set()).add(t)
    paths = Counter(t for middle in forward.get(head, ()) for t in forward.get(middle, ()))
    return {e: paths[e] + 0.1*tails[e] for e in entities}


def rank(values, target, exclude=frozenset(), tie="realistic"):
    if target not in values or any(not math.isfinite(x) for x in values.values()):
        raise ValueError("Target and finite scores required")
    if tie not in {"optimistic", "pessimistic", "realistic"}:
        raise ValueError("Unknown tie convention")
    kept = {e: s for e, s in values.items() if e == target or e not in exclude}
    best = 1 + sum(s > kept[target] for s in kept.values())
    worst = sum(s >= kept[target] for s in kept.values())
    return best if tie == "optimistic" else worst if tie == "pessimistic" else (best+worst)/2


def evaluate(train, test, entities, known=None):
    train, test = set(train), list(test)
    if not test or set(test) & train:
        raise ValueError("Need nonempty test disjoint from training")
    if len(set(entities)) != len(entities) or any(h not in entities or t not in entities for h, _, t in train | set(test)):
        raise ValueError("Entity roster must be unique and cover all triples")
    known = set(known) if known is not None else train | set(test)
    raw, filtered = [], []
    for h, r, t in test:
        v = scores(train, h, r, entities)
        excluded = {tail for head, rel, tail in known if (head, rel) == (h, r)}
        raw.append(rank(v, t))
        filtered.append(rank(v, t, excluded))
    return dict(raw_ranks=raw, filtered_ranks=filtered,
                raw_mrr=sum(1/r for r in raw)/len(raw),
                filtered_mrr=sum(1/r for r in filtered)/len(filtered))


def corruptions(triple, entities, training):
    h, r, t = triple
    return sorted(({(e, r, t) for e in entities} | {(h, r, e) for e in entities}) - set(training) - {triple})


def demo():
    entities = list("abcdex")
    train = {("a", "r", "b"), ("b", "r", "d"), ("a", "r", "c"),
             ("c", "r", "e"), ("x", "r", "d")}
    test = [("a", "r", "e"), ("a", "r", "d")]
    out = evaluate(train, test, entities)
    out.update(scores=scores(train, "a", "r", entities),
               withheld_positive_can_be_negative=test[0] in corruptions(("a", "r", "b"), entities, train),
               all_tied_ranks={m: rank(dict.fromkeys(entities, 0), "a", tie=m)
                               for m in ["optimistic", "pessimistic", "realistic"]})
    return out


if __name__ == "__main__":
    print(json.dumps(demo(), indent=2))

d与e的分数分别为1.2和1.1。按e、d的顺序,两条留出目标的原始排名为2、1,过滤后为1、1。MRR从0.75变为1.0,完全因为移除了另一个已知答案。程序还验证留出正例可能进入训练损坏候选池,并打印三种全并列排名约定。

每个查询构造关系索引与尾频次时,需要扫描m条训练边;枚举两跳的成本取决于访问的路径组合数p;评分和排名扫描n个候选实体。因此期望时间为O(m+p+n),索引与计数器空间为O(m+n)。当前实现为每个查询重复这些工作。实际基线可以缓存关系索引,但不能借此混入留出事实。

排名好坏取决于候选范围

按实体类型限制候选会让任务变简单,改为抽样负例又会改变任务。演示只评尾实体,不能据此断言头实体预测也好。实际报告应明确实体覆盖、适用时的两个端点任务、负例构造、类型限制、并列处理和已知正例过滤。

基线可能给错误的头实体推荐热门尾实体;面对非传递关系,两跳模式也会误导。学习嵌入是另一种选择,但同样需要严谨评估协议。下一篇TransE提供小型训练例子;关系嵌入讨论评分对称性怎样限制预测。

改了协议,再解释数字

  1. 一个损坏三元组没有出现在训练集中,就一定是假事实吗?答案:不是。它可能缺失但为真。损坏过程是在不完整观察下生成竞争者。
  2. 两个查询的排名为2和1,MRR是多少?答案:(1/2+1)/2=0.75。如果过滤把两者都变为第1名,MRR就是1,即使所有分数都没改变。
  3. 六个候选同分,本例平均排名约定使用的倒数排名是多少?答案:1/3.5=2/7。如果平均1、1/2、…、1/6,会得到另一种并列处理策略。
  4. 过滤评估知道测试正例,是否就能先把测试图加入训练图,再计算两跳评分?答案:不能。过滤可以使用声明的已知正例集,评分特征却必须遵守预测时可用信息的限制。混合两者会泄漏目标事实。

让模型回答同一个问题,再比较

TransE提供了具体的端点排名协议,Toutanova—Chen说明除了嵌入容量,还应关注观察特征与关系冗余。本文数值例子为原创,没有复现两篇论文的基准。可以将同一个基线分别放在全名单与类型限制候选上评估,同时报告候选召回上限和排名指标。这样能先看清协议改变的作用,再判断改进是否来自更复杂的模型。

先判断,再看解释

模型没变,过滤排名为什么还能提高?

选择一个答案

学习目标

  • 让留出事实远离特征构造,同时按明示协议用已知正例过滤评估候选。
  • 计算三种并列约定和MRR,指出为什么训练损坏池可能包含缺失真边。

先修与延伸

查看知识关联图 →

参考文献

  1. Translating Embeddings for Modeling Multi-relational Data
    Antoine Bordes; Nicolas Usunier; Alberto Garcia-Durán; Jason Weston; Oksana Yakhnenko · 2013
  2. Observed versus latent features for knowledge base and text inference
    Kristina Toutanova; Danqi Chen · 2015

版本1 · 编写状态: 已完成记录中的技术复核

查看技术检查记录(AI)

Codex · 2026-09-23 · Single AI author. Scoped reading: Read PDF1 identity and PDF2–3/5 text; viewed PDF3. Translation, L1/L2 norm, margin loss, head-or-tail corruption, unit entity constraint and raw/filtered evaluation. Original full-pair deterministic program differs from paper sampling/validation; no benchmark reproduction. Read PDF1 identity, PDF2/4–5 text and selected inverse-relation paragraphs on PDF1/3/8. Distinguishes observed features, training inputs, candidate domains and filtered metrics. Teaching baseline has two-hop counts, not the paper’s trained one-link model. No reported benchmark replicated. 6 paired equations and four explained exercises. Checked training-only two-hop/count features, known-positive filtering, raw/filtered MRR, target retention, three tie conventions and false negatives. Synthetic tail-only baseline; no original-paper model or benchmark reproduction.

  • 核仅训练的两跳/频次特征、已知正例过滤、原始/过滤MRR、目标保留、三种并列规约及假负例;合成尾预测基线,非原论文模型或基准复现。
  • 每题四张计算SVG,14数值/几何与36响应式组合通过;12张当前选定图实看,其中7张与此前所看字节相同、5张最终图重新实看,TransE箭头修订已保全,完整页面验收另记。

核对的来源位置:

  • Translating Embeddings for Modeling Multi-relational Data · Read PDF1 identity and PDF2–3/5 text; viewed PDF3. Translation, L1/L2 norm, margin loss, head-or-tail corruption, unit entity constraint and raw/filtered evaluation. Original full-pair deterministic program differs from paper sampling/validation; no benchmark reproduction.
  • Observed versus latent features for knowledge base and text inference · Read PDF1 identity, PDF2/4–5 text and selected inverse-relation paragraphs on PDF1/3/8. Distinguishes observed features, training inputs, candidate domains and filtered metrics. Teaching baseline has two-hop counts, not the paper’s trained one-link model. No reported benchmark replicated.

相关主题

没有匹配结果,试试英文名或缩写。

已显示 0 条结果

将整个查询放入双引号,可按短语匹配。