回填表示

class BackfillRepresentation(max_id: int, base_ids: Iterable[int], base: str | Representation | type[Representation] | None = None, base_kwargs: Mapping[str, Any] | None = None, backfill: str | Representation | type[Representation] | None = None, backfill_kwargs: Mapping[str, Any] | None = None, **kwargs)[源代码]

基础:PartitionRepresentation

一种分区表示的变体,易于应用于单一基础表示。

"""Example for backfill representations."""

import torch

from pykeen.datasets import get_dataset
from pykeen.nn import BackfillRepresentation, Embedding, init
from pykeen.pipeline import pipeline

dataset = get_dataset(dataset="nations")

# we start by creating the representation for those entities where we have pre-trained features
# here we simulate this for a set of Asian countries
embedding_dim = 32
known_ids = dataset.training.entities_to_ids(["burma", "china", "india", "indonesia"])
pre_trained_embeddings = torch.rand(len(known_ids), embedding_dim)
initializer = init.PretrainedInitializer(tensor=pre_trained_embeddings)
base_repr = Embedding(max_id=len(known_ids), shape=(embedding_dim,), initializer=initializer, trainable=False)

# Next, we directly create representations for the remaining ones using the backfill representation.
# To do this, we need to create an iterable (e.g., a set) of all of the entity IDs that are in the base
# representation. Then, the assignments to the base representation and an auxillary representation are
# automatically generated for the base class.
entity_repr = BackfillRepresentation(base_ids=known_ids, max_id=dataset.num_entities, base=base_repr)

# We assume that we do not have any pre-trained information for relations here for simplicity and train
# them from scratch.
relation_repr = Embedding(max_id=dataset.num_relations, shape=(embedding_dim,))

# The combined representation can now be used as any other representation, e.g., to train a model with
# distmult interaction:
result = pipeline(
    dataset=dataset,
    interaction="distmult",
    dimensions=dict(d=embedding_dim),
    model_kwargs=dict(
        entity_representations=entity_repr,
        relation_representations=relation_repr,
    ),
)

初始化表示。

Parameters:
  • max_id (int) – 需要嵌入的实体总数

  • base_ids (Iterable[int]) – 一个整数实体索引的可迭代对象,这些索引通过基础表示提供

  • base (HintOrType[Representation]) – 基础表示,或其提示。

  • base_kwargs (OptionalKwargs) – 用于实例化基础表示的关键字参数

  • backfill (HintOrType[Representation]) – 回填表示或其提示。

  • backfill_kwargs (OptionalKwargs) – 用于实例化回填表示的关键字参数

  • kwargs – 传递给 Representation.__init__() 的额外基于关键字的参数。不能包含 max_idshape,这些是从基础表示中推断出来的。