Skip to main content

llm.TextEncoder

llm.TextEncoder(model=T5CHEM, *, pooling=None, prompt=None, max_length=None)

A pretrained language model that turns each text into one vector.

An encoder reads the text both ways and its last hidden states are averaged over the real tokens. A decoder reads it left to right, so the last token has seen the whole text and its hidden state is taken. Dropout is off.

Parameters

Name Type Description Default
model str A Hugging Face model name or path. T5CHEM
pooling Pooling | None How the hidden states become one vector: "mean" over the real tokens, the "last" real token, or the "first" token. None takes "mean" for an encoder and "last" for a decoder. None
prompt str | Callable[[str], str] | None Puts each text into a sentence before tokenizing, either a template with {} where the text goes ("The redox potential of {} is") or a function. None tokenizes the texts as they are. None
max_length int | None Truncate longer texts to this many tokens. None keeps them whole. None

Methods

Name Description
embed The vectors of the texts, as fixed features.
forward One vector per tokenized text, [N, d].
prepare Put the texts into the prompt and tokenize them, padded on the right.

embed

llm.TextEncoder.embed(texts, batch_size=64)

The vectors of the texts, as fixed features.

Parameters

Name Type Description Default
texts Any A list of strings. required
batch_size int Texts per forward pass. 64

Returns

Name Type Description
Tensor The features, [N, d].

forward

llm.TextEncoder.forward(input_ids, attention_mask)

One vector per tokenized text, [N, d].

prepare

llm.TextEncoder.prepare(texts)

Put the texts into the prompt and tokenize them, padded on the right.

Parameters

Name Type Description Default
texts Any A list of strings. required

Returns

Name Type Description
dict[str, Tensor] {"input_ids", "attention_mask"}, each [N, L], on the model’s device.