gliner.modeling.scorers module¶

class gliner.modeling.scorers.Scorer(hidden_size, dropout=0.1)[source]¶

Bases: Module

Scorer for computing token-label compatibility scores.

This scorer is designed for token-level models and computes pairwise interactions between token representations and label embeddings. For each token-label pair, it produces three scores (typically for start, end, and overall compatibility).

The scoring mechanism uses:
  1. Separate projections for tokens and labels

  2. Bilinear-style interaction between projected representations

  3. An MLP to produce final scores

proj_token¶

Linear projection for token representations, mapping from hidden_size to hidden_size * 2.

Type:

nn.Linear

proj_label¶

Linear projection for label embeddings, mapping from hidden_size to hidden_size * 2.

Type:

nn.Linear

out_mlp¶

MLP that produces final scores from concatenated features.

Type:

nn.Sequential

Initialize the Scorer.

Parameters:
  • hidden_size (int) – Dimension of the hidden representations.

  • dropout (float, optional) – Dropout rate for the output MLP. Defaults to 0.1.

__init__(hidden_size, dropout=0.1)[source]¶

Initialize the Scorer.

Parameters:
  • hidden_size (int) – Dimension of the hidden representations.

  • dropout (float, optional) – Dropout rate for the output MLP. Defaults to 0.1.

forward(token_rep, label_rep)[source]¶

Compute token-label compatibility scores.

Parameters:
  • token_rep (torch.Tensor) – Token representations of shape [batch_size, seq_len, hidden_size].

  • label_rep (torch.Tensor) – Label embeddings of shape [batch_size, num_classes, hidden_size].

Returns:

Scores of shape [batch_size, seq_len, num_classes, 3],

where the last dimension contains three scores per token-label pair (typically start, end, and overall compatibility scores).

Return type:

torch.Tensor