TextEncoder

class TextEncoder(*args, **kwargs)[source]

Bases: Module

An encoder for text.

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Methods Summary

encode_all(labels[, batch_size])

Encode all labels (inference mode & batched).

forward(labels)

Encode a batch of text.

forward_normalized(texts)

Encode a batch of text.

Methods Documentation

encode_all(labels: Sequence[str], batch_size: int | None = None) Tensor[source]

Encode all labels (inference mode & batched).

Parameters:
  • labels (Sequence[str]) – a sequence of strings to encode

  • batch_size (int | None) – the batch size to use for encoding the labels. batch_size=1 means that the labels are encoded one-by-one, while batch_size=len(labels) would correspond to encoding all at once. Larger batch sizes increase memory requirements, but may be computationally more efficient. batch_size can also be set to None to enable automatic batch size maximization for the employed hardware.

Returns:

shape: (len(labels), dim) a tensor representing the encodings for all labels

Return type:

Tensor

forward(labels: str | Sequence[str]) Tensor[source]

Encode a batch of text.

Parameters:

labels (str | Sequence[str]) – length: b the texts

Returns:

shape: (b, dim) an encoding of the texts

Return type:

Tensor

abstract forward_normalized(texts: Sequence[str]) Tensor[source]

Encode a batch of text.

Parameters:

texts (Sequence[str]) – length: b the texts

Returns:

shape: (b, dim) an encoding of the texts

Return type:

Tensor