paddlespeech.s2t.modules.decoder模块
解码器定义。
- class paddlespeech.s2t.modules.decoder.TransformerDecoder(vocab_size: int, encoder_output_size: int, attention_heads: int = 4, linear_units: int = 2048, num_blocks: int = 6, dropout_rate: float = 0.1, positional_dropout_rate: float = 0.1, self_attention_dropout_rate: float = 0.0, src_attention_dropout_rate: float = 0.0, input_layer: str = 'embed', use_output_layer: bool = True, normalize_before: bool = True, concat_after: bool = False, max_len: int = 5000)[来源]
基础:
BatchScorerInterface,Layer变换器解码器模块的基类。
参数:词汇大小: 输出维度 编码器输出大小: 注意力的维度 注意力头数: 多头注意力的头数 线性单元: 逐位置前馈的隐藏单元数量 块数: 解码器块的数量 丢弃率: 丢弃率 自注意力丢弃率: 注意力的丢弃率 输入层: 输入层类型, embed 使用输出层: 是否使用输出层 位置编码类: PositionalEncoding 模块 归一化前:
正确:在层的每个子块之前使用 layer_norm。
错误:在层的每个子块之后使用 layer_norm。- concat_after: whether to concat attention layer's input and output
真:x -> x + linear(concat(x, att(x)))
假:x -> x + att(x)
方法
__call__(*inputs, **kwargs)将self作为一个函数调用。
add_parameter(name, parameter)添加一个参数实例。
add_sublayer(name, sublayer)添加一个子层实例。
apply(fn)递归地将
fn应用到每个子层(由.sublayers()返回)以及自身。batch_init_state(x)获取解码的初始状态(可选).
batch_score(ys, states, xs)评分新标记批次(必需)。
buffers([include_sublayers])返回当前层及其子层中的所有缓冲区的列表。
children()返回一个迭代器,遍历直接子层。
clear_gradients()清除此层所有参数的梯度。
create_parameter(shape[, attr, dtype, ...])为该层创建参数。
create_tensor([name, persistable, dtype])为该层创建张量。
create_variable([name, persistable, dtype])为该层创建张量。
eval()将该层及其所有子层设置为评估模式。
extra_repr()该层的额外表示,您可以自定义实现自己的层。
final_score(state)得分 eos(可选)。
forward(memory, memory_mask, ys_in_pad, ...)前向解码器。 参数: memory: 编码后的记忆,float32 (batch, maxlen_in, feat) memory_mask: 编码器记忆掩码,(batch, 1, maxlen_in) ys_in_pad: 填充的输入标记ID,int64 (batch, maxlen_out) ys_in_lens: 此批次的输入长度 (batch) r_ys_in_pad: 在变压器解码器中未使用,为了与双向解码器统一API reverse_weight: 在变压器解码器中未使用,为了与双向解码器统一API 返回: (tuple): 包含以下内容的元组: x: softmax 之前解码的标记分数 (batch, maxlen_out, vocab_size) 如果 use_output_layer 为 True, olens: (batch, ).
forward_one_step(memory, memory_mask, tgt, ...)前进一步。
full_name()此层的完整名称,由 name_scope + "/" + MyLayer.__class__.__name__ 组成
init_state(x)获取用于解码的初始状态(可选)。
load_dict(state_dict[, use_structured_name])从 state_dict 设置参数和可持久化缓存。
named_buffers([prefix, include_sublayers])返回一个迭代器,遍历层中的所有缓冲区,生成名称和张量的元组。
named_children()返回一个直接子层的迭代器,同时提供层的名称和层本身。
named_parameters([prefix, include_sublayers])返回一个迭代器,遍历层中的所有参数,生成名称和参数的元组。
named_sublayers([prefix, include_self, ...])返回Layer中所有子层的迭代器,生成名称和子层的元组。
parameters([include_sublayers])返回当前层及其子层的所有参数的列表。
register_buffer(name, tensor[, persistable])将一个张量注册为该层的缓冲区。
register_forward_post_hook(hook)为层注册一个前向后钩子。
register_forward_pre_hook(hook)为层注册一个前向预钩子。
score(ys, state, x)得分。
select_state(state, i[, new_id])在主光束搜索中选择具有相对ID的状态。
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()将此层及其所有子层设置为训练模式。
向后
注册状态字典钩子
- batch_score(ys: Tensor, states: List[Any], xs: Tensor) Tuple[Tensor, List[Any]][来源]
评分新令牌批次(必需)。
- Args:
ys (paddle.Tensor): paddle.int64 前缀标记 (n_batch, ylen). states (List[Any]): 前缀标记的计分器状态。 xs (paddle.Tensor):
生成ys的编码器特征(n_batch,xlen,n_feat)。
- Returns:
- tuple[paddle.Tensor, List[Any]]: Tuple of
下一个标记的批处理分数,形状为(n_batch, n_vocab),以及 ys 的下一个状态列表。
- forward(memory: ~paddle.Tensor, memory_mask: ~paddle.Tensor, ys_in_pad: ~paddle.Tensor, ys_in_lens: ~paddle.Tensor, r_ys_in_pad: ~paddle.Tensor = Tensor(shape=[0], dtype=float32, place=Place(cpu), stop_gradient=True, []), reverse_weight: float = 0.0) Tuple[Tensor, Tensor][来源]
前向解码器。 参数:
内存:编码内存,float32 (batch, maxlen_in, feat) 内存掩码:编码器内存掩码,(batch, 1, maxlen_in) ys_in_pad:填充的输入令牌ID,int64 (batch, maxlen_out) ys_in_lens:该批次的输入长度 (batch) r_ys_in_pad:在转换器解码器中未使用,为了统一api
具有双向解码器
- reverse_weight: not used in transformer decoder, in order to unify
具有双向解码的api
- Returns:
- (tuple): tuple containing:
- x: decoded token score before softmax (batch, maxlen_out, vocab_size)
如果 use_output_layer 为真,
olens: (batch, )
- forward_one_step(memory: Tensor, memory_mask: Tensor, tgt: Tensor, tgt_mask: Tensor, cache: Optional[List[Tensor]] = None) Tuple[Tensor, List[Tensor]][来源]
- Forward one step.
这仅用于解码。
- Args:
内存: 编码内存, float32 (批次, 最大长度输入, 特征)
内存掩码: 编码内存掩码, (批次, 1, 最大长度输入)
目标: 输入标记ID, int64 (批次, 最大长度输出)
目标掩码: 输入标记掩码, (批次, 最大长度输出, 最大长度输出)dtype=paddle.bool
缓存:已缓存的输出列表(批次,最大超时-1,大小)
- Returns:
- y, cache: NN output value and cache per self.decoders.
y.shape` 的形状是 (batch, token)