gliner.modeling.multitask.triples_layers moduleΒΆ

class gliner.modeling.multitask.triples_layers.NormBasedInteraction(dim, p=2, power=1.0, clamp_norm=10.0, use_scorer=False, dropout=0.3)[source]ΒΆ

Bases: Module

Base class for norm-based KGE interactions.

Parameters:
  • dim (int) – Embedding dimension

  • p (int) – β„“_p norm (e.g. 1 or 2) used in β€–Β·β€–_p

  • power (float) – Raise norm to this power before negating

  • clamp_norm (float | None) – Optional upper bound for numerical stability

  • use_scorer (bool) – If True, use learned projection instead of norm

  • dropout (float) – Dropout rate for scorer (if used)

__init__(dim, p=2, power=1.0, clamp_norm=10.0, use_scorer=False, dropout=0.3)[source]ΒΆ

Base class for norm-based KGE interactions.

Parameters:
  • dim (int) – Embedding dimension

  • p (int) – β„“_p norm (e.g. 1 or 2) used in β€–Β·β€–_p

  • power (float) – Raise norm to this power before negating

  • clamp_norm (float | None) – Optional upper bound for numerical stability

  • use_scorer (bool) – If True, use learned projection instead of norm

  • dropout (float) – Dropout rate for scorer (if used)

class gliner.modeling.multitask.triples_layers.UMInteraction(dim=768, **kwargs)[source]ΒΆ

Bases: NormBasedInteraction

Unstructured model β€–h - tβ€–.

Base class for norm-based KGE interactions.

Parameters:
  • dim (int) – Embedding dimension

  • p – β„“_p norm (e.g. 1 or 2) used in β€–Β·β€–_p

  • power – Raise norm to this power before negating

  • clamp_norm – Optional upper bound for numerical stability

  • use_scorer – If True, use learned projection instead of norm

  • dropout – Dropout rate for scorer (if used)

__init__(dim=768, **kwargs)[source]ΒΆ

Base class for norm-based KGE interactions.

Parameters:
  • dim (int) – Embedding dimension

  • p – β„“_p norm (e.g. 1 or 2) used in β€–Β·β€–_p

  • power – Raise norm to this power before negating

  • clamp_norm – Optional upper bound for numerical stability

  • use_scorer – If True, use learned projection instead of norm

  • dropout – Dropout rate for scorer (if used)

forward(h, r, t)[source]ΒΆ

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class gliner.modeling.multitask.triples_layers.SEInteraction(dim=768, **kwargs)[source]ΒΆ

Bases: NormBasedInteraction

Structure Embedding (SE).

Uses head / tail specific diagonal matrices built from relation. h’ = diag(r) Β· h , t’ = diag(r) Β· t

Base class for norm-based KGE interactions.

Parameters:
  • dim (int) – Embedding dimension

  • p – β„“_p norm (e.g. 1 or 2) used in β€–Β·β€–_p

  • power – Raise norm to this power before negating

  • clamp_norm – Optional upper bound for numerical stability

  • use_scorer – If True, use learned projection instead of norm

  • dropout – Dropout rate for scorer (if used)

__init__(dim=768, **kwargs)[source]ΒΆ

Base class for norm-based KGE interactions.

Parameters:
  • dim (int) – Embedding dimension

  • p – β„“_p norm (e.g. 1 or 2) used in β€–Β·β€–_p

  • power – Raise norm to this power before negating

  • clamp_norm – Optional upper bound for numerical stability

  • use_scorer – If True, use learned projection instead of norm

  • dropout – Dropout rate for scorer (if used)

forward(h, r, t)[source]ΒΆ

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class gliner.modeling.multitask.triples_layers.TransEInteraction(dim=768, p=1, **kwargs)[source]ΒΆ

Bases: NormBasedInteraction

TransE β€–h + r βˆ’ tβ€–.

Base class for norm-based KGE interactions.

Parameters:
  • dim (int) – Embedding dimension

  • p (int) – β„“_p norm (e.g. 1 or 2) used in β€–Β·β€–_p

  • power – Raise norm to this power before negating

  • clamp_norm – Optional upper bound for numerical stability

  • use_scorer – If True, use learned projection instead of norm

  • dropout – Dropout rate for scorer (if used)

__init__(dim=768, p=1, **kwargs)[source]ΒΆ

Base class for norm-based KGE interactions.

Parameters:
  • dim (int) – Embedding dimension

  • p (int) – β„“_p norm (e.g. 1 or 2) used in β€–Β·β€–_p

  • power – Raise norm to this power before negating

  • clamp_norm – Optional upper bound for numerical stability

  • use_scorer – If True, use learned projection instead of norm

  • dropout – Dropout rate for scorer (if used)

forward(h, r, t)[source]ΒΆ

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class gliner.modeling.multitask.triples_layers.TransHInteraction(dim, p=2, power=1.0, **kwargs)[source]ΒΆ

Bases: NormBasedInteraction

TransH – project entities to a relation-specific hyperplane.

Learn mappings from base relation r to:

r_tr = W_tr * r + b_tr (translation) w = W_w * r + b_w (hyperplane normal)

Base class for norm-based KGE interactions.

Parameters:
  • dim (int) – Embedding dimension

  • p (int) – β„“_p norm (e.g. 1 or 2) used in β€–Β·β€–_p

  • power (float) – Raise norm to this power before negating

  • clamp_norm – Optional upper bound for numerical stability

  • use_scorer – If True, use learned projection instead of norm

  • dropout – Dropout rate for scorer (if used)

__init__(dim, p=2, power=1.0, **kwargs)[source]ΒΆ

Base class for norm-based KGE interactions.

Parameters:
  • dim (int) – Embedding dimension

  • p (int) – β„“_p norm (e.g. 1 or 2) used in β€–Β·β€–_p

  • power (float) – Raise norm to this power before negating

  • clamp_norm – Optional upper bound for numerical stability

  • use_scorer – If True, use learned projection instead of norm

  • dropout – Dropout rate for scorer (if used)

forward(h, r, t)[source]ΒΆ

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class gliner.modeling.multitask.triples_layers.TransFInteraction(dim, p=2, power=1.0, **kwargs)[source]ΒΆ

Bases: NormBasedInteraction

TransF – element-wise relation-specific scaling before translation.

Learn mappings from base relation r to:

r_vec = W_r * r + b_r alpha = W_alpha * r + b_alpha beta = W_beta * r + b_beta

Score is β€–(alpha ∘ h) + r_vec βˆ’ (beta ∘ t)β€–_p

Base class for norm-based KGE interactions.

Parameters:
  • dim (int) – Embedding dimension

  • p (int) – β„“_p norm (e.g. 1 or 2) used in β€–Β·β€–_p

  • power (float) – Raise norm to this power before negating

  • clamp_norm – Optional upper bound for numerical stability

  • use_scorer – If True, use learned projection instead of norm

  • dropout – Dropout rate for scorer (if used)

__init__(dim, p=2, power=1.0, **kwargs)[source]ΒΆ

Base class for norm-based KGE interactions.

Parameters:
  • dim (int) – Embedding dimension

  • p (int) – β„“_p norm (e.g. 1 or 2) used in β€–Β·β€–_p

  • power (float) – Raise norm to this power before negating

  • clamp_norm – Optional upper bound for numerical stability

  • use_scorer – If True, use learned projection instead of norm

  • dropout – Dropout rate for scorer (if used)

forward(h, r, t)[source]ΒΆ

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class gliner.modeling.multitask.triples_layers.PairREInteraction(dim, p=2, power=1.0, **kwargs)[source]ΒΆ

Bases: NormBasedInteraction

PairRE – per-relation element-wise scaling of h & t.

Learn mappings from base relation r to:

alpha = W_alpha * r + b_alpha beta = W_beta * r + b_beta

Base class for norm-based KGE interactions.

Parameters:
  • dim (int) – Embedding dimension

  • p (int) – β„“_p norm (e.g. 1 or 2) used in β€–Β·β€–_p

  • power (float) – Raise norm to this power before negating

  • clamp_norm – Optional upper bound for numerical stability

  • use_scorer – If True, use learned projection instead of norm

  • dropout – Dropout rate for scorer (if used)

__init__(dim, p=2, power=1.0, **kwargs)[source]ΒΆ

Base class for norm-based KGE interactions.

Parameters:
  • dim (int) – Embedding dimension

  • p (int) – β„“_p norm (e.g. 1 or 2) used in β€–Β·β€–_p

  • power (float) – Raise norm to this power before negating

  • clamp_norm – Optional upper bound for numerical stability

  • use_scorer – If True, use learned projection instead of norm

  • dropout – Dropout rate for scorer (if used)

forward(h, r, t)[source]ΒΆ

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class gliner.modeling.multitask.triples_layers.TripleREInteraction(dim, p=2, power=1.0, **kwargs)[source]ΒΆ

Bases: NormBasedInteraction

TripleRE – LineaRE + scalar Ξ³ per relation.

Learn mappings from base relation r to:

alpha = W_alpha * r + b_alpha beta = W_beta * r + b_beta delta = W_delta * r + b_delta gamma = w_gamma^T * r + b_gamma (scalar)

Base class for norm-based KGE interactions.

Parameters:
  • dim (int) – Embedding dimension

  • p (int) – β„“_p norm (e.g. 1 or 2) used in β€–Β·β€–_p

  • power (float) – Raise norm to this power before negating

  • clamp_norm – Optional upper bound for numerical stability

  • use_scorer – If True, use learned projection instead of norm

  • dropout – Dropout rate for scorer (if used)

__init__(dim, p=2, power=1.0, **kwargs)[source]ΒΆ

Base class for norm-based KGE interactions.

Parameters:
  • dim (int) – Embedding dimension

  • p (int) – β„“_p norm (e.g. 1 or 2) used in β€–Β·β€–_p

  • power (float) – Raise norm to this power before negating

  • clamp_norm – Optional upper bound for numerical stability

  • use_scorer – If True, use learned projection instead of norm

  • dropout – Dropout rate for scorer (if used)

forward(h, r, t)[source]ΒΆ

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class gliner.modeling.multitask.triples_layers.DistMultInteraction(*args, **kwargs)[source]ΒΆ

Bases: Module

DistMult – Ξ£_d h_d r_d t_d.

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

forward(h, r, t)[source]ΒΆ

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class gliner.modeling.multitask.triples_layers.SimplEInteraction(dim=768)[source]ΒΆ

Bases: Module

SimplE – split every embedding into (forward, backward) halves.

score = ½( ⟨h_f, r_f, t_b⟩ + ⟨t_f, r_b, h_b⟩ ) Requires even dimension.

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

__init__(dim=768)[source]ΒΆ

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

forward(h, r, t)[source]ΒΆ

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class gliner.modeling.multitask.triples_layers.TuckERInteraction(d_e, d_r, dropout=0.2)[source]ΒΆ

Bases: Module

TuckER – global core tensor W (D_r Γ— D_e Γ— D_e).

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

__init__(d_e, d_r, dropout=0.2)[source]ΒΆ

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

forward(h, r, t)[source]ΒΆ

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class gliner.modeling.multitask.triples_layers.DistMAInteraction(*args, **kwargs)[source]ΒΆ

Bases: Module

DistMA – sum of pairwise dot products.

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

forward(h, r, t)[source]ΒΆ

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class gliner.modeling.multitask.triples_layers.ComplExInteraction(dim=768)[source]ΒΆ

Bases: Module

ComplEx – Re(⟨h, r, conj(t)⟩) with complex embeddings.

Requires even dimension.

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

__init__(dim=768)[source]ΒΆ

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

forward(h, r, t)[source]ΒΆ

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class gliner.modeling.multitask.triples_layers.QuatEInteraction(dim=768)[source]ΒΆ

Bases: Module

QuatE – use Hamilton product (a,b,c,d)⨂(e,f,g,h).

Requires dimension divisible by 4.

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

__init__(dim=768)[source]ΒΆ

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

forward(h, r, t)[source]ΒΆ

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class gliner.modeling.multitask.triples_layers.HolEInteraction(*args, **kwargs)[source]ΒΆ

Bases: Module

HolE – circular correlation Ο•(h, t) Β· r.

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

forward(h, r, t)[source]ΒΆ

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class gliner.modeling.multitask.triples_layers.ERMLPInteraction(dim, hidden=2048)[source]ΒΆ

Bases: Module

ER-MLP: 2-layer perceptron on concatenated [h, r, t].

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

__init__(dim, hidden=2048)[source]ΒΆ

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

forward(h, r, t)[source]ΒΆ

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class gliner.modeling.multitask.triples_layers.ConvKBInteraction(dim, n_filters=32, dropout=0.3, use_bias=True)[source]ΒΆ

Bases: Module

ConvKB: Convolutional Knowledge Base interaction (Conv1d version).

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

__init__(dim, n_filters=32, dropout=0.3, use_bias=True)[source]ΒΆ

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

forward(h, r, t)[source]ΒΆ

Score triples (h, r, t).

Parameters:
  • h – Head entities (…, D)

  • r – Relations (…, D)

  • t – Tail entities (…, D)

Returns:

Triple scores (…)

Return type:

scores

class gliner.modeling.multitask.triples_layers.ConvEInteraction(dim, emb_dim1, n_filters=32, kernel_size=3, input_drop=0.2, hidden_drop=0.3, feat_drop=0.2, use_bias=True)[source]ΒΆ

Bases: Module

ConvE: Convolutional interaction matching reference implementation.

Stacks head and relation embeddings vertically and applies 2D convolution.

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

__init__(dim, emb_dim1, n_filters=32, kernel_size=3, input_drop=0.2, hidden_drop=0.3, feat_drop=0.2, use_bias=True)[source]ΒΆ

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

forward(h, r, t)[source]ΒΆ

Score triples (h, r, t).

Parameters:
  • h – Head entities (…, D)

  • r – Relations (…, D)

  • t – Tail entities (…, D)

Returns:

Triple scores (…)

Return type:

scores

class gliner.modeling.multitask.triples_layers.TriplesScoreLayer(interaction_mode, dim=768, **kwargs)[source]ΒΆ

Bases: Module

Wrapper for knowledge graph triple scoring interactions.

Optimized for relation extraction in entity recognition models.

Parameters:
  • interaction_mode (str) – The type of interaction to use. Available modes: - Translational: UM, SE, TransE, TransH, TransF, PairRE, TripleRE - Semantic: DistMult, SimplE, ComplEx, QuatE, HolE, DistMA - Neural: TuckER, ERMLP, ConvE, ConvKB

  • dim (int) – Embedding dimension (required for most interactions).

  • **kwargs – Extra parameters for specific interactions: - TuckER: requires d_e, d_r, optional dropout - ERMLP: optional hidden (default 2048) - ConvE: requires emb_dim1, optional n_filters, kernel_size, input_drop, hidden_drop, feat_drop, use_bias - ConvKB: optional n_filters, dropout, use_bias - Norm-based (TransE, TransH, etc.): optional p, power, clamp_norm, use_scorer, dropout

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

DIMENSION_REQUIREMENTS = {'ComplEx': <function TriplesScoreLayer.<lambda>>, 'QuatE': <function TriplesScoreLayer.<lambda>>, 'SimplE': <function TriplesScoreLayer.<lambda>>}ΒΆ
__init__(interaction_mode, dim=768, **kwargs)[source]ΒΆ

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

validate_dimensions(dim)[source]ΒΆ

Validate that the embedding dimension meets requirements for this interaction.

Parameters:

dim (int) – The embedding dimension to validate

Raises:

ValueError – If dimension requirements are not met

forward(h, r, t)[source]ΒΆ

Score triples (h, r, t).

Parameters:
  • h – Head entities (…, D)

  • r – Relations (…, D)

  • t – Tail entities (…, D)

Returns:

Triple scores (…)

Return type:

scores

forward_batched_relations(h, t, rel_embeddings)[source]ΒΆ

Efficiently score entity pairs against all relation types.

Parameters:
  • h – Head entities (B, N, D)

  • t – Tail entities (B, N, D)

  • rel_embeddings – Relation type embeddings (B, C, D) or (C, D)

Returns:

(B, N, C) scores for each pair against each relation type

Return type:

scores

forward_single_relation(h, t, r)[source]ΒΆ

Score entity pairs with a single relation type.

Parameters:
  • h – Head entities (B, N, D)

  • t – Tail entities (B, N, D)

  • r – Single relation embedding (B, D) or (D,)

Returns:

(B, N) scores for each pair with the given relation

Return type:

scores