Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions torchTextClassifiers/model/components/text_embedder.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ def _get_sentence_embedding(
token_embeddings: torch.Tensor,
attention_mask: torch.Tensor,
return_label_attention_matrix: bool = False,
) -> torch.Tensor:
) -> dict[str, Optional[torch.Tensor]]:
"""
Compute sentence embedding from embedded tokens - "remove" second dimension.

Args (output from dataset collate_fn):
token_embeddings (torch.Tensor[Long]), shape (batch_size, seq_len, embedding_dim): Tokenized + padded text
attention_mask (torch.Tensor[Long]), shape (batch_size, seq_len): Attention mask indicating non-pad tokens
Returns:
torch.Tensor: Sentence embeddings, shape (batch_size, embedding_dim)
dict: Dictionary with keys 'sentence_embedding' (torch.Tensor) and 'label_attention_matrix' (Optional[torch.Tensor])
"""

# average over non-pad token embeddings
Expand All @@ -217,14 +217,20 @@ def _get_sentence_embedding(
if self.attention_config is not None:
if self.attention_config.aggregation_method is not None: # default is "mean"
if self.attention_config.aggregation_method == "first":
return token_embeddings[:, 0, :]
return {
"sentence_embedding": token_embeddings[:, 0, :],
"label_attention_matrix": None,
}
elif self.attention_config.aggregation_method == "last":
lengths = attention_mask.sum(dim=1).clamp(min=1) # last non-pad token index + 1
return token_embeddings[
torch.arange(token_embeddings.size(0)),
lengths - 1,
:,
]
lengths = attention_mask.sum(dim=1).clamp(min=1).long() # last non-pad token index + 1
return {
"sentence_embedding": token_embeddings[
torch.arange(token_embeddings.size(0)),
lengths - 1,
:,
],
"label_attention_matrix": None,
}
else:
if self.attention_config.aggregation_method != "mean":
raise ValueError(
Expand Down