paddlespeech.s2t.modules.mask 模块
- paddlespeech.s2t.modules.mask.add_optional_chunk_mask(xs: Tensor, masks: Tensor, use_dynamic_chunk: bool, use_dynamic_left_chunk: bool, decoding_chunk_size: int, static_chunk_size: int, num_decoding_left_chunks: int)[来源]
为编码器应用可选的掩码。
参数:
xs (paddle.Tensor): 填充的输入,(B, L, D), L 为最大长度
mask (paddle.Tensor): xs 的掩码,(B, 1, L)
use_dynamic_chunk (bool): 是否使用动态块
use_dynamic_left_chunk (bool): 是否使用动态左块培训。
- decoding_chunk_size (int): decoding chunk size for dynamic chunk, it's
0: 训练的默认值,使用随机动态块。 <0: 解码时,使用完整块。 >0: 解码时,使用设定的固定块大小。
- static_chunk_size (int): chunk size for static chunk training/decoding
如果它大于0,并且use_dynamic_chunk为true, 这个参数将被忽略
- num_decoding_left_chunks (int): number of left chunks, this is for decoding,
块大小是 decoding_chunk_size。
>=0:使用 num_decoding_left_chunks
<0:使用所有剩余块
- Returns:
paddle.Tensor: 输入 xs 的块掩码。
- paddlespeech.s2t.modules.mask.make_non_pad_mask(lengths: Tensor) Tensor[来源]
创建包含非填充部分索引的掩码张量。 一个批次中的序列可能具有不同的长度。为了启用 批量计算,需要填充以使所有序列具有相同的 大小。为了避免将填充部分传递给上下文相关的 模块,如注意力或卷积,这部分填充被屏蔽。 该 pad_mask 在编码器和解码器中都使用。 非填充部分为 1,填充部分为 0。 参数:
长度 (paddle.Tensor): 长度的批次 (B,)。
- Returns:
paddle.Tensor:掩码张量,包含填充部分的索引。 (B, T)
- Examples:
>>> lengths = [5, 3, 2] >>> make_non_pad_mask(lengths) masks = [[1, 1, 1, 1 ,1], [1, 1, 1, 0, 0], [1, 1, 0, 0, 0]]
- paddlespeech.s2t.modules.mask.make_pad_mask(lengths: Tensor) Tensor[来源]
创建包含填充部分索引的掩码张量。
请参见make_non_pad_mask的描述。
参数:长度 (paddle.Tensor): 长度的批次 (B,)。
- Returns:
paddle.Tensor: 掩码张量,包含填充部分的索引。 (B, T)
- Examples:
>>> lengths = [5, 3, 2] >>> make_pad_mask(lengths) masks = [[0, 0, 0, 0 ,0], [0, 0, 0, 1, 1], [0, 0, 1, 1, 1]]
- paddlespeech.s2t.modules.mask.make_xs_mask(xs: Tensor, pad_value=0.0) Tensor[来源]
生成包含非填充部分索引的掩码张量。
参数:xs (paddle.Tensor): (B, T, D),用于填充的零。
- Returns:
paddle.Tensor: 掩码张量非填充部分的索引。(B, T)
- paddlespeech.s2t.modules.mask.mask_finished_preds(pred: Tensor, flag: Tensor, eos: int) Tensor[来源]
如果一个序列完成了,所有的分支应该是
Args: - pred (paddle.Tensor): A int array with shape
(batch_size * beam_size, beam_size).
- flag (paddle.Tensor): A bool array with shape
(batch_size * beam_size, 1).
- Returns:
paddle.Tensor: (batch_size * beam_size).
- paddlespeech.s2t.modules.mask.mask_finished_scores(score: Tensor, flag: Tensor) Tensor[来源]
如果一个序列结束了,我们只允许一个活跃的分支。这个函数旨在给一个分支一个零分,其余分支给-无穷分。
参数:- score (paddle.Tensor): A real value array with shape
(batch_size * beam_size, beam_size).
- flag (paddle.Tensor): A bool array with shape
(batch_size * beam_size, 1).
- Returns:
paddle.Tensor: (batch_size * beam_size, beam_size).
- Examples:
- flag: tensor([[ True],
[错误]])
- score: tensor([[-0.3666, -0.6664, 0.6019],
[-1.1490, -0.2948, 0.7460]])
- unfinished: tensor([[False, True, True],
[假,假,假]])
- finished: tensor([[ True, False, False],
[假,假,假]])
- return: tensor([[ 0.0000, -inf, -inf],
[-1.1490, -0.2948, 0.7460]])
- paddlespeech.s2t.modules.mask.subsequent_chunk_mask(size: int, chunk_size: int, num_left_chunks: int = -1) Tensor[来源]
- Create mask for subsequent steps (size, size) with chunk size,
这是用于流编码器
- Args:
size (int): 面具的大小
chunk_size (int): 块的大小
num_left_chunks (int): 剩余块的数量<0: 使用完整块 >=0: 使用 num_left_chunks
- Returns:
paddle.Tensor: 掩码, [大小, 大小]
- Examples:
>>> subsequent_chunk_mask(4, 2) [[1, 1, 0, 0], [1, 1, 0, 0], [1, 1, 1, 1], [1, 1, 1, 1]]
- paddlespeech.s2t.modules.mask.subsequent_mask(size: int) Tensor[来源]
为后续步骤创建掩码(大小,大小)。 该掩码仅在以自回归模式工作的解码器中使用。 这意味着当前步骤只能与其左侧的步骤进行注意力计算。 在编码器中,当不需要流式处理且序列不长时使用完全注意力。 在这种情况下,不需要注意力掩码。 当需要流式处理时,编码器使用基于块的注意力。参见 subsequent_chunk_mask 以获取基于块的注意力掩码。 参数:
size (int): 遮罩的大小
- Returns:
paddle.Tensor: mask, [size, size]
- Examples:
>>> subsequent_mask(3) [[1, 0, 0], [1, 1, 0], [1, 1, 1]]