paddleaudio.utils.tensor_utils模块

转换器的工具函数。

paddleaudio.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]])
paddleaudio.utils.tensor_utils.has_tensor(val)[来源]
paddleaudio.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_firstFalse,则大小为 T x B x * 的张量。否则为大小 B x T x * 的张量。

paddleaudio.utils.tensor_utils.th_accuracy(pad_outputs: Tensor, pad_targets: Tensor, ignore_label: int) float[来源]

计算准确率。

pad_outputs (Tensor): 预测张量 (B * Lmax, D)。
pad_targets (LongTensor): 目标标签张量 (B, Lmax, D)。
ignore_label (int): 忽略标签 ID。

Returns:

浮动:准确度值(0.0 - 1.0)。