torch_geometric.llm.models.SentenceTransformer

class SentenceTransformer(model_name: str, pooling_strategy: Union[PoolingStrategy, str] = 'mean')[source]

Bases: Module

A wrapper around a Sentence-Transformer from HuggingFace.

Parameters:
  • model_name (str) – The HuggingFace model name, e.g., "BERT".

  • pooling_strategy (str, optional) – The pooling strategy to use for generating node embeddings. (default: "mean")

max_seq_length

Some models define a max sequence length in their configuration. Others only in the tokenizer. This is a hacky heuristic to find the max sequence length that works for the model.

forward(input_ids: Tensor, attention_mask: Tensor) Tensor[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 Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

encode(text: List[str], batch_size: Optional[int] = None, output_device: Optional[Union[device, str]] = None, verbose=False) Tensor[source]

Main function for users. Converts strings to embeddings.

Parameters:
  • text (List[str]) – List of strings to embed.

  • batch_size (int, optional) – How many strings to process. Defaults to processing all at once, but this may lead to OOM errors. (default: obj:None)

  • output_device (Union[torch.device, str], optional) – By default outputs cpu pytorch tensor, but can choose to output to specific cuda devices. (default: obj:None)

  • verbose (bool, optional) – Controls the verbosity of outputs. (default: obj:False)

Return type:

Tensor