sequence
Functions:
Name | Description |
---|---|
build_attention_mask |
Build an attention mask for the given sequences. |
pad_sequence |
Pack a sequence of variable length tensors into a single tensor, padding them to the same length on the specified side. |
build_attention_mask
¶
build_attention_mask(
sequences: list[Tensor] | tuple[Tensor, ...],
padding_side: Literal["left", "right"] | str = "right",
pad_to_multiple_of: int | None = None,
max_length: int | None = None,
) -> torch.Tensor
Build an attention mask for the given sequences.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
list[Tensor] | tuple[Tensor, ...]
|
A list or tuple of 1D tensors of shape |
required |
|
Literal['left', 'right'] | str
|
The side to pad the inputs. Either |
'right'
|
|
int | None
|
The multiple length to pad the sequences to. |
None
|
|
int | None
|
The maximum length after which sequences will be truncated. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If |
Returns:
Type | Description |
---|---|
torch.Tensor
|
A 2D boolean attention mask tensor of shape |
Added in version 0.84.0.
pad_sequence
¶
pad_sequence(
sequences: list[Tensor] | tuple[Tensor, ...],
padding_value: float = 0.0,
padding_side: Literal["left", "right"] | str = "right",
pad_to_multiple_of: int | None = None,
truncation_side: Literal["left", "right"]
| str = "right",
max_length: int | None = None,
) -> torch.Tensor
Pack a sequence of variable length tensors into a single tensor, padding them to the same length on the specified side.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
list[Tensor] | tuple[Tensor, ...]
|
A list or tuple 1D tensors of shape |
required |
|
float
|
The value to use for padding. |
0.0
|
|
Literal['left', 'right'] | str
|
The side on which to pad the sequences. Either |
'right'
|
|
int | None
|
The multiple length to pad the sequences to. |
None
|
|
Literal['left', 'right'] | str
|
The side to truncate the sequences if |
'right'
|
|
int | None
|
The maximum length after which sequences will be truncated. |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If |
ValueError
|
If |
ValueError
|
If |
Returns:
Type | Description |
---|---|
torch.Tensor
|
A 2D tensor of shape |
torch.Tensor
|
provided, and padded to |
Added in version 0.84.0.