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:
ModuleBase 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:
NormBasedInteractionUnstructured 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
Moduleinstance 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:
NormBasedInteractionStructure 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
Moduleinstance 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:
NormBasedInteractionTransE β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
Moduleinstance 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:
NormBasedInteractionTransH β 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
Moduleinstance 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:
NormBasedInteractionTransF β 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
Moduleinstance 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:
NormBasedInteractionPairRE β 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
Moduleinstance 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:
NormBasedInteractionTripleRE β 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
Moduleinstance 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:
ModuleDistMult β Ξ£_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
Moduleinstance 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:
ModuleSimplE β 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
Moduleinstance 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:
ModuleTuckER β 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
Moduleinstance 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:
ModuleDistMA β 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
Moduleinstance 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:
ModuleComplEx β 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
Moduleinstance 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:
ModuleQuatE β 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
Moduleinstance 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:
ModuleHolE β 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
Moduleinstance 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:
ModuleER-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
Moduleinstance 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:
ModuleConvKB: Convolutional Knowledge Base interaction (Conv1d version).
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- 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:
ModuleConvE: 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.
- class gliner.modeling.multitask.triples_layers.TriplesScoreLayer(interaction_mode, dim=768, **kwargs)[source]ΒΆ
Bases:
ModuleWrapper 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