共现过滤模型
- class CooccurrenceFilteredModel(*, triples_factory: CoreTriplesFactory, additional_triples: None | Tensor | list[Tensor] = None, apply_in_training: bool = False, base: str | Model | type[Model] | None = 'rotate', training_fill_value: float = -1000.0, inference_fill_value: float = -inf, conjunctive: bool = False, **kwargs)[source]
基础类:
Model一个通过共现过滤预测的模型。
初始化模型。
- Parameters:
triples_factory (CoreTriplesFactory) – (训练)三元组工厂;用于创建共现计数以及实例化基础模型。
additional_triples (None | Tensor | list[Tensor]) – 用于创建共现统计的额外三元组
apply_in_training (bool) – 是否在训练期间也应用掩码
training_fill_value (float) – 训练填充值;对于大多数损失函数,这必须是一个有限值,即不能是无穷大
inference_fill_value (float) – 推理填充值
conjunctive (bool) – 是否使用合取或析取来组合非过滤掩码
kwargs – 在实例化时传递给基础模型的额外基于关键字的参数
属性摘要
优化模型超参数的默认策略
方法总结
获取损失函数的正则化项。
predict_h(rt_batch, **kwargs)使用左侧(头部)预测进行前向传递,以获取所有可能头部的分数。
predict_r(ht_batch, **kwargs)使用中间(关系)预测进行前向传递,以获取所有可能关系的分数。
predict_t(hr_batch, **kwargs)使用右侧(尾部)预测进行前向传递,以获取所有可能尾部的分数。
score_h(rt_batch, **kwargs)使用左侧(头部)预测进行前向传递。
score_hrt(hrt_batch, **kwargs)前向传播。
score_r(ht_batch, **kwargs)使用中间(关系)预测进行前向传递。
score_t(hr_batch, **kwargs)使用右侧(尾部)预测进行前向传递。
属性文档
- hpo_default: ClassVar[Mapping[str, Any]] = {'base': {'choices': ['distmult', 'mure', 'rescal', 'rotate', 'transe'], 'type': 'categorical'}, 'conjunctive': {'type': <class 'bool'>}}
优化模型超参数的默认策略
方法文档
- predict_h(rt_batch: Tensor, **kwargs) Tensor[来源]
使用左侧(头部)预测进行前向传递,以获取所有可能头部的分数。
此方法为每个(关系,尾部)对计算所有可能头部的分数。
注意
如果模型已经用逆关系进行了训练,预测头实体的任务就变成了预测逆三元组的尾实体的任务,即\(f(*,r,t)\)通过\(f(t,r_{inv},*)\)来预测。
此外,模型被设置为评估模式。
- Parameters:
rt_batch (Tensor) – 形状: (batch_size, 2), 数据类型: long (关系, 尾部) 对的索引。
kwargs – 传递给
Model.score_h()的额外基于关键字的参数
- Returns:
形状: (batch_size, num_heads), 数据类型: float 对于每个r-t对,所有可能头部的分数。
- Return type:
- predict_r(ht_batch: Tensor, **kwargs) Tensor[来源]
使用中间(关系)预测进行前向传递,以获取所有可能关系的分数。
此方法为每个(头,尾)对计算所有可能关系的分数。
此外,模型被设置为评估模式。
- Parameters:
ht_batch (Tensor) – 形状: (batch_size, 2), 数据类型: long (head, tail) 对的索引。
kwargs – 传递给
Model.score_r()的额外基于关键字的参数
- Returns:
形状: (batch_size, num_relations), 数据类型: float 对于每个h-t对,所有可能关系的分数。
- Return type:
- predict_t(hr_batch: Tensor, **kwargs) Tensor[来源]
使用右侧(尾部)预测进行前向传递,以获取所有可能尾部的分数。
此方法为每个(头,关系)对计算所有可能尾部的分数。
此外,模型被设置为评估模式。
- Parameters:
hr_batch (Tensor) – 形状: (batch_size, 2), 数据类型: long (head, relation) 对的索引。
kwargs – 传递给
Model.score_t()的额外基于关键字的参数
- Returns:
形状: (batch_size, num_tails), 数据类型: float 对于每个h-r对,所有可能尾部的分数。
- Return type:
注意
我们只期望右侧预测,即 \((h,r,*)\) 在模型使用逆关系训练时改变其默认行为(主要是因为LCWA训练方法的行为)。这就是为什么
predict_h()的行为会根据训练中是否使用逆三元组而有所不同,以及为什么此函数的行为不受逆三元组使用的影响。
- score_h(rt_batch: Tensor, **kwargs) Tensor[source]
使用左侧(头部)预测进行前向传递。
此方法为每个(关系,尾部)对计算所有可能头部的分数。
- Parameters:
rt_batch (Tensor) – 形状: (batch_size, 2), 数据类型: long (关系, 尾部) 对的索引。
slice_size – >0 在使用切片时,评分函数的除数。
mode – 传递模式,在传导设置中为None,在归纳设置中为“training”、“validation”或“testing”之一。
heads – 形状: (num_heads,) | (batch_size, num_heads) 要评分的头实体索引。如果为None,则对所有实体进行评分(从给定模式中)。
- Returns:
形状: (batch_size, num_heads), 数据类型: float 对于每个r-t对,所有可能头部的分数。
- Return type:
- score_r(ht_batch: Tensor, **kwargs) Tensor[来源]
使用中间(关系)预测进行前向传递。
此方法为每个(头,尾)对计算所有可能关系的分数。
- Parameters:
ht_batch (Tensor) – 形状: (batch_size, 2), 数据类型: long (head, tail) 对的索引。
slice_size – >0 使用切片时,评分函数的除数。
mode – 传递模式,在传导设置中为None,在归纳设置中为“training”、“validation”或“testing”之一。
relations – 形状: (num_relations,) | (batch_size, num_relations) 要评分的relation索引。如果为None,则对所有relations进行评分(从给定模式中)。
- Returns:
形状: (batch_size, num_real_relations), 数据类型: float 对于每个h-t对,所有可能关系的分数。
- Return type:
- score_t(hr_batch: Tensor, **kwargs) Tensor[来源]
使用右侧(尾部)预测进行前向传递。
此方法为每个(头,关系)对计算所有可能尾部的分数。
- Parameters:
hr_batch (Tensor) – 形状: (batch_size, 2), 数据类型: long (head, relation) 对的索引。
slice_size – >0 使用切片时,评分函数的除数。
mode – 传递模式,在传导设置中为None,在归纳设置中为“training”、“validation”或“testing”之一。
tails – 形状: (num_tails,) | (batch_size, num_tails) 要评分的尾实体索引。如果为None,则对所有实体进行评分(从给定模式中)。
- Returns:
形状: (batch_size, num_tails), 数据类型: float 对于每个h-r对,所有可能尾部的分数。
- Return type: