paddlespeech.s2t.decoders.beam_search.beam_search 模块
束搜索模块。
- class paddlespeech.s2t.decoders.beam_search.beam_search.BeamSearch(scorers: Dict[str, 得分接口], weights: Dict[str, float], beam_size: int, vocab_size: int, sos: int, eos: int, token_list: Optional[List[str]] = None, pre_beam_ratio: float = 1.5, pre_beam_score_key: Optional[str] = None)[来源]
基础:
Layer束搜索实现。
方法
__call__(*inputs, **kwargs)将self作为一个函数调用。
add_parameter(name, parameter)添加一个参数实例。
add_sublayer(name, sublayer)添加一个子层实例。
append_token(xs, x)将新令牌附加到前缀令牌。
apply(fn)递归地将
fn应用到每个子层(由.sublayers()返回)以及自身。beam(weighted_scores, ids)计算前k个完整的令牌ID和部分令牌ID。
buffers([include_sublayers])返回当前层及其子层中的所有缓冲区的列表。
children()返回一个迭代器,遍历直接子层。
clear_gradients()清除此层所有参数的梯度。
create_parameter(shape[, attr, dtype, ...])为该层创建参数。
create_tensor([name, persistable, dtype])为该层创建张量。
create_variable([name, persistable, dtype])为该层创建张量。
eval()将该层及其所有子层设置为评估模式。
extra_repr()该层的额外表示,您可以自定义实现自己的层。
forward(x[, maxlenratio, minlenratio])执行束搜索。
full_name()此层的完整名称,由 name_scope + "/" + MyLayer.__class__.__name__ 组成
init_hyp(x)获取初始假设数据。
load_dict(state_dict[, use_structured_name])从 state_dict 设置参数和可持久化缓存。
merge_scores(prev_scores, next_full_scores, ...)合并新假设的评分。
merge_states(states, part_states, part_idx)合并新假设的状态。
named_buffers([prefix, include_sublayers])返回一个迭代器,遍历层中的所有缓冲区,生成名称和张量的元组。
named_children()返回一个直接子层的迭代器,同时提供层的名称和层本身。
named_parameters([prefix, include_sublayers])返回一个迭代器,遍历层中的所有参数,生成名称和参数的元组。
named_sublayers([prefix, include_self, ...])返回Layer中所有子层的迭代器,生成名称和子层的元组。
parameters([include_sublayers])返回当前层及其子层的所有参数的列表。
post_process(i, maxlen, maxlenratio, ...)执行束搜索迭代的后处理。
register_buffer(name, tensor[, persistable])将一个张量注册为该层的缓冲区。
register_forward_post_hook(hook)为层注册一个前向后钩子。
register_forward_pre_hook(hook)为层注册一个前向预钩子。
score_full(hyp, x)通过 self.full_scorers 评分新假设。
score_partial(hyp, ids, x)通过 self.part_scorers 评分新的假设。
search(running_hyps, x)为正在运行的假设和编码的语音 x 搜索新的标记。
set_dict(state_dict[, use_structured_name])从 state_dict 设置参数和可持久化的缓冲区。
set_state_dict(state_dict[, use_structured_name])从state_dict设置参数和持久化缓冲区。
state_dict([destination, include_sublayers, ...])获取当前层及其子层的所有参数和可持久化缓冲区。
sublayers([include_self])返回子层的列表。
to([device, dtype, blocking])通过给定的设备、数据类型和阻塞方式转换层的参数和缓冲区。
to_static_state_dict([destination, ...])获取当前层及其子层的所有参数和缓冲区。
train()将此层及其所有子层设置为训练模式。
向后
注册状态字典钩子
- static append_token(xs: Tensor, x: Union[int, Tensor]) Tensor[来源]
将新令牌附加到前缀令牌。
- Args:
xs (paddle.Tensor): 前缀标记,(T,)
x (int): 要附加的新标记- Returns:
paddle.Tensor: (T+1,), 新张量包含: xs + [x],其 xs.dtype 和 xs.device
- beam(weighted_scores: Tensor, ids: Tensor) Tuple[Tensor, Tensor][来源]
计算 topk 完整令牌 ID 和部分令牌 ID。
- Args:
- weighted_scores (paddle.Tensor): The weighted sum scores for each tokens.
它的形状是 (self.n_vocab,)。
ids (paddle.Tensor): 用于计算 topk 的部分令牌 ids(全局)。
- Returns:
- Tuple[paddle.Tensor, paddle.Tensor]:
topk 的完整令牌 ID 和部分令牌 ID。
它们的形状为 (self.beam_size,)。
即(全球 ID,全球相对本地 ID)。
- forward(x: Tensor, maxlenratio: float = 0.0, minlenratio: float = 0.0) List[假设][来源]
执行束搜索。
- Args:
x (paddle.Tensor): 编码的语音特征 (T, D) maxlenratio (float): 输入长度与获取最大输出长度的比率。
- If maxlenratio=0.0 (default), it uses a end-detect function
自动查找最大假设长度
- If maxlenratio<0.0, its absolute value is interpreted
作为一个常量最大输出长度。
minlenratio (float): 输入长度比例以获得最小输出长度。
- Returns:
list[Hypothesis]: N-best 解码结果
- static merge_scores(prev_scores: Dict[str, float], next_full_scores: Dict[str, Tensor], full_idx: int, next_part_scores: Dict[str, Tensor], part_idx: int) Dict[str, Tensor][来源]
合并新假设的分数。
- Args:
- prev_scores (Dict[str, float]):
之前的假设得分由 self.scorers 提供
next_full_scores (Dict[str, paddle.Tensor]): 由 self.full_scorers 提供的分数 full_idx (int): 下一个 token id 对应 next_full_scores next_part_scores (Dict[str, paddle.Tensor]):
部分标记的分数由 self.part_scorers 提供
part_idx (int): next_part_scores的新令牌ID
- Returns:
- Dict[str, paddle.Tensor]: The new score dict.
它的键是 self.full_scorers 和 self.part_scorers 的名称。它的值是由评分者提供的标量张量。
- merge_states(states: Any, part_states: Any, part_idx: int) Any[来源]
合并新假设的状态。
- Args:
状态:self.full_scorers 的状态
部分状态:self.part_scorers 的状态
part_idx (int):part_scores 的新令牌ID- Returns:
- Dict[str, paddle.Tensor]: The new score dict.
它的键是 self.full_scorers 和 self.part_scorers 的名称。它的值是得分者的状态。
- post_process(i: int, maxlen: int, maxlenratio: float, running_hyps: List[假设], ended_hyps: List[假设]) List[假设][来源]
执行束搜索迭代的后处理。
- Args:
i (int): 假设令牌的长度。
maxlen (int): 在层叠搜索中令牌的最大长度。
maxlenratio (int): 在层叠搜索中的最大长度比率。
running_hyps (List[Hypothesis]): 在层叠搜索中正在进行的假设。
ended_hyps (List[Hypothesis]): 在层叠搜索中结束的假设。- Returns:
List[Hypothesis]: 新的运行假设。
- score_full(hyp: 假设, x: Tensor) Tuple[Dict[str, Tensor], Dict[str, Any]][来源]
通过 self.full_scorers 来评分新假设。
- Args:
hyp (假设): 带前缀标记的假设以进行评分
x (paddle.Tensor): 相应的输入特征,(T, D)- Returns:
- Tuple[Dict[str, paddle.Tensor], Dict[str, Any]]: Tuple of
包含字符串键的hyp的分数字典self.full_scorers和形状为(self.n_vocab,)的张量分值,以及包含字符串键和状态值的状态字典self.full_scorers
- score_partial(hyp: 假设, ids: Tensor, x: Tensor) Tuple[Dict[str, Tensor], Dict[str, Any]][来源]
通过 self.part_scorers 评分新假设。
- Args:
hyp (假设): 带前缀标记的假设以进行评分
ids (paddle.Tensor): 一维张量的新部分标记以进行评分,len(ids) < n_vocab
x (paddle.Tensor): 对应的输入特征, (T, D)
- Returns:
- Tuple[Dict[str, paddle.Tensor], Dict[str, Any]]: Tuple of
具有 self.part_scorers 字符串键的 hyp 的分数字典,值为形状为 (len(ids),) 的张量分数,以及具有字符串键和 self.part_scorers 的状态值的状态字典
- class paddlespeech.s2t.decoders.beam_search.beam_search.Hypothesis(yseq: Tensor, score: Union[float, Tensor] = 0, scores: Dict[str, Union[float, Tensor]] = {}, states: Dict[str, Any] = {})[来源]
基础:
tuple假设数据类型。
方法
asdict()将数据转换为适合JSON的字典。
count(value, /)返回值出现的次数。
index(value[, start, stop])返回值的第一个索引。
- property score
字段编号 1 的别名
- property scores
字段编号 2 的别名
- property states
字段编号 3 的别名
- property yseq
字段编号 0 的别名
- paddlespeech.s2t.decoders.beam_search.beam_search.beam_search(x: Tensor, sos: int, eos: int, beam_size: int, vocab_size: int, scorers: Dict[str, 得分接口], weights: Dict[str, float], token_list: Optional[List[str]] = None, maxlenratio: float = 0.0, minlenratio: float = 0.0, pre_beam_ratio: float = 1.5, pre_beam_score_key: str = 'full') list[来源]
执行带评分器的束搜索。
- Args:
x (paddle.Tensor): 编码的语音特征 (T, D) sos (int): 序列开始id eos (int): 序列结束id beam_size (int): 搜索过程中保持的假设数量 vocab_size (int): 词汇表的数量 scorers (dict[str, ScorerInterface]): 解码模块的字典
例如,Decoder,CTCPrefixScorer,LM
如果得分器为None,将被忽略- weights (dict[str, float]): Dict of weights for each scorers
如果得分器的权重为0,则将被忽略
token_list (list[str]): 用于调试日志的令牌列表
maxlenratio (float): 输入长度与最大输出长度的比率。如果 maxlenratio=0.0(默认),它使用一个结束检测函数来自动找到最大假设长度
minlenratio (float): 输入长度比以获得最小输出长度。
pre_beam_score_key (str): 用于执行预先光束搜索的分数键
pre_beam_ratio (float): 预先光束搜索中的光束大小将会是 int(pre_beam_ratio * beam_size)
- Returns:
List[Dict]: N-best 解码结果