besskge.scoring.BaseScoreFunction

class besskge.scoring.BaseScoreFunction(*args, **kwargs)[source]

Base class for scoring functions.

Initializes internal Module state, shared by both nn.Module and ScriptModule.

entity_embedding: Parameter

Entity embedding table

forward(head_emb, relation_id, tail_emb)[source]

see BaseScoreFunction.score_triple()

Return type:

Tensor

Parameters:
negative_sample_sharing: bool

Share negative entities to construct negative samples

relation_embedding: Parameter

Relation embedding table

abstract score_heads(head_emb, relation_id, tail_emb)[source]

Score sets of head entities against fixed (r,t) queries.

Parameters:
  • head_emb (Tensor) – shape: (B, n_heads, embedding_size) with B = 1, batch_size Embeddings of head entities.

  • relation_id (Tensor) – shape: (batch_size,) IDs of relation types in batch.

  • tail_emb (Tensor) – shape: (batch_size, embedding_size) Embedding of tail entities in batch.

Return type:

Tensor

Returns:

shape: (batch_size, B * n_heads) if BaseScoreFunction.negative_sample_sharing else (batch_size, n_heads). Scores of broadcasted triples.

abstract score_tails(head_emb, relation_id, tail_emb)[source]

Score sets of tail entities against fixed (h,r) queries.

Parameters:
  • head_emb (Tensor) – shape: (batch_size, embedding_size) Embeddings of head entities in batch.

  • relation_id (Tensor) – shape: (batch_size,) IDs of relation types in batch.

  • tail_emb (Tensor) – shape: (B, n_tails, embedding_size) with B = 1, batch_size Embedding of tail entities.

Return type:

Tensor

Returns:

shape: (batch_size, B * n_tails) if BaseScoreFunction.negative_sample_sharing else (batch_size, n_tails) Scores of broadcasted triples.

abstract score_triple(head_emb, relation_id, tail_emb)[source]

Score a batch of (h,r,t) triples.

Parameters:
  • head_emb (Tensor) – shape: (batch_size, embedding_size) Embeddings of head entities in batch.

  • relation_id (Tensor) – shape: (batch_size,) IDs of relation types in batch.

  • tail_emb (Tensor) – shape: (batch_size, embedding_size) Embeddings of tail entities in batch.

Return type:

Tensor

Returns:

shape: (batch_size,) Scores of a batch of triples.

sharding: Sharding

Sharding of entities

update_sharding(new_sharding)[source]

Change the sharding of the entity embedding table.

Parameters:

new_sharding (Sharding) – The new entity sharding.

Return type:

None