Spec-Zone.ru › PyTorch 2

torch.nn.utils.rnn.pad_sequence

torch.nn.utils.rnn.pad_sequence(sequences, batch_first=False, padding_value=0.0) [source]

Pad a list of variable length Tensors with padding_value

This function stacks a list of Tensors along a new dimension, and pads them to equal length. For example, if the input is a list of sequences with size L x * and batch_first is False, the output is of size T x B x *. The output size is determined by the number of sequences and the length of the longest sequence in the input list.

N is batch size. It is equal to the number of elements in sequences. T is length of the longest sequence. D is length of the sequence. * is any number of trailing dimensions, including none.

Пример

>>> from torch.nn.utils.rnn import pad_sequence
>>> a = torch.ones(25, 300)
>>> b = torch.ones(22, 300)
>>> c = torch.ones(15, 300)
>>> pad_sequence([a, b, c]).size()
torch.Size([25, 3, 300])

Примечание

This function returns a Tensor of size (T, N, *), where T is the length of the longest sequence. This function assumes trailing dimensions and type of all the Tensors in sequences are same.

Параметры
  • sequences (list[Tensor]) – список последовательностей переменной длины.
  • batch_first (bool, optional) – output will be in (N, T, *) if True, or in (T, N, *) otherwise. Значение по умолчанию: False.
  • padding_value (float, optional) – значение для заполнения пропущенных элементов. Значение по умолчанию: 0.
Возвращает

Tensor of size (T, N, *) if batch_first is False. Tensor of size (N, T, *) otherwise

Тип возвращаемого значения

Tensor

© 2024, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://pytorch.org/docs/2.1/generated/torch.nn.utils.rnn.pad_sequence.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API