Nasty.Semantic.Coreference.Neural.MentionEncoder (Nasty v0.3.0)
View SourceNeural mention encoder using BiLSTM with attention.
Encodes mentions into fixed-size vector representations by processing the mention tokens and surrounding context through a bidirectional LSTM with attention mechanism.
Architecture
- Token embeddings (GloVe or trainable)
- BiLSTM over context tokens
- Attention over mention span
- Concatenate: [mention_repr, head_word, context_repr]
Example
# Build model
model = MentionEncoder.build_model(
vocab_size: 50_000,
embedding_dim: 100,
hidden_dim: 128
)
# Encode mention
encoding = MentionEncoder.encode_mention(
model,
params,
mention_tokens,
context_tokens,
mention_span
)
Summary
Functions
Batch encode multiple mentions.
Build the mention encoder model.
Build vocabulary from training data.
Encode a mention with its context.
Load pre-trained GloVe embeddings.
Types
Functions
@spec batch_encode_mentions( model(), params(), [{Nasty.AST.Semantic.Mention.t(), [Nasty.AST.Token.t()]}], map() ) :: Nx.Tensor.t()
Batch encode multiple mentions.
More efficient than encoding one at a time.
Parameters
model- Trained Axon modelparams- Model parametersmentions- List of mentions with contextsvocab- Token to ID mapping
Returns
Tensor of shape [batch_size, hidden_dim * 2]
Build the mention encoder model.
Options
:vocab_size- Vocabulary size (required):embedding_dim- Embedding dimension (default: 100):hidden_dim- LSTM hidden dimension (default: 128):context_window- Context window size (default: 10):dropout- Dropout rate (default: 0.3):use_pretrained- Use pre-trained embeddings (default: false)
Returns
Axon model that takes token IDs and returns mention encodings
Build vocabulary from training data.
Parameters
documents- OntoNotes documentsmin_count- Minimum token frequency (default: 2)max_vocab_size- Maximum vocabulary size (default: 50_000)
Returns
Map from token text to ID
@spec encode_mention( model(), params(), Nasty.AST.Semantic.Mention.t(), [Nasty.AST.Token.t()], map() ) :: encoding()
Encode a mention with its context.
Parameters
model- Trained Axon modelparams- Model parametersmention- Mention structcontext_tokens- List of context tokensvocab- Token to ID mapping
Returns
Tensor encoding of the mention [hidden_dim * 2]
@spec load_glove_embeddings(Path.t(), map(), pos_integer()) :: {:ok, Nx.Tensor.t()} | {:error, term()}
Load pre-trained GloVe embeddings.
Parameters
path- Path to GloVe file (e.g., "glove.6B.100d.txt")vocab- Vocabulary mapembedding_dim- Embedding dimension
Returns
Tensor of shape [vocab_size, embedding_dim] with pre-trained embeddings