paddlespeech.s2t.utils.tensor_utils模块
转换器的工具函数。
- paddlespeech.s2t.utils.tensor_utils.add_sos_eos(ys_pad: Tensor, sos: int, eos: int, ignore_id: int) Tuple[Tensor, Tensor][来源]
添加 和 标签。参数:
ys_pad (paddle.Tensor): 填充目标序列的批次 (B, Lmax) sos (int): 的索引 eos (int): 的索引 ignore_id (int): 填充的索引
- Returns:
ys_in (paddle.Tensor) : (B, Lmax + 1)ys_out (paddle.Tensor) : (B, Lmax + 1)
- Examples:
>>> sos_id = 10 >>> eos_id = 11 >>> ignore_id = -1 >>> ys_pad tensor([[ 1, 2, 3, 4, 5], [ 4, 5, 6, -1, -1], [ 7, 8, 9, -1, -1]], dtype=paddle.int32) >>> ys_in,ys_out=add_sos_eos(ys_pad, sos_id , eos_id, ignore_id) >>> ys_in tensor([[10, 1, 2, 3, 4, 5], [10, 4, 5, 6, 11, 11], [10, 7, 8, 9, 11, 11]]) >>> ys_out tensor([[ 1, 2, 3, 4, 5, 11], [ 4, 5, 6, 11, -1, -1], [ 7, 8, 9, 11, -1, -1]])
- paddlespeech.s2t.utils.tensor_utils.pad_sequence(sequences: List[Tensor], batch_first: bool = False, padding_value: float = 0.0) Tensor[来源]
用
padding_value填充可变长度的张量列表pad_sequence沿着新维度堆叠一个 Tensor 列表,并将它们填充到相等的长度。例如,如果输入是大小为L x *的序列列表,并且 batch_first 为 False,则T x B x *否则。B 是批量大小。它等于
sequences中的元素数量。 T 是最长序列的长度。 L 是序列的长度。 * 是任意数量的尾部维度,包括零维。- Example:
>>> from paddle.nn.utils.rnn import pad_sequence >>> a = paddle.ones(25, 300) >>> b = paddle.ones(22, 300) >>> c = paddle.ones(15, 300) >>> pad_sequence([a, b, c]).shape paddle.Tensor([25, 3, 300])
- Note:
该函数返回一个大小为
T x B x *或B x T x *的张量,其中 T 是最长序列的长度。该函数假设序列中所有张量的后续维度和类型都是相同的。- Args:
sequences (list[Tensor]): 变长序列的列表。
batch_first (bool, optional): 如果为 True,输出将为B x T x *,否则为T x B x *否则padding_value (float, optional): 填充元素的值。默认为: 0.
- Returns:
如果
batch_first为False,则大小为T x B x *的张量。否则为大小B x T x *的张量。