espnet.nets.pytorch_backend.transformer.decoder.Decoder
espnet.nets.pytorch_backend.transformer.decoder.Decoder
class espnet.nets.pytorch_backend.transformer.decoder.Decoder(odim, selfattention_layer_type='selfattn', attention_dim=256, attention_heads=4, conv_wshare=4, conv_kernel_length=11, conv_usebias=False, linear_units=2048, num_blocks=6, dropout_rate=0.1, positional_dropout_rate=0.1, self_attention_dropout_rate=0.0, src_attention_dropout_rate=0.0, input_layer='embed', use_output_layer=True, pos_enc_class=<class 'espnet.nets.pytorch_backend.transformer.embedding.PositionalEncoding'>, normalize_before=True, concat_after=False)
Bases: BatchScorerInterface
, Module
Transfomer decoder module.
- Parameters:
- odim (int) – Output diminsion.
- self_attention_layer_type (str) – Self-attention layer type.
- attention_dim (int) – Dimension of attention.
- attention_heads (int) – The number of heads of multi head attention.
- conv_wshare (int) – The number of kernel of convolution. Only used in self_attention_layer_type == “lightconv*” or “dynamiconv*”.
- conv_kernel_length (Union *[*int , str ]) – Kernel size str of convolution (e.g. 71_71_71_71_71_71). Only used in self_attention_layer_type == “lightconv*” or “dynamiconv*”.
- conv_usebias (bool) – Whether to use bias in convolution. Only used in self_attention_layer_type == “lightconv*” or “dynamiconv*”.
- linear_units (int) – The number of units of position-wise feed forward.
- num_blocks (int) – The number of decoder blocks.
- dropout_rate (float) – Dropout rate.
- positional_dropout_rate (float) – Dropout rate after adding positional encoding.
- self_attention_dropout_rate (float) – Dropout rate in self-attention.
- src_attention_dropout_rate (float) – Dropout rate in source-attention.
- input_layer (Union *[*str , torch.nn.Module ]) – Input layer type.
- use_output_layer (bool) – Whether to use output layer.
- pos_enc_class (torch.nn.Module) – Positional encoding module class. PositionalEncoding `or `ScaledPositionalEncoding
- normalize_before (bool) – Whether to use layer_norm before the first block.
- concat_after (bool) – Whether to concat attention layer’s input and output. if True, additional linear will be applied. i.e. x -> x + linear(concat(x, att(x))) if False, no additional linear will be applied. i.e. x -> x + att(x)
Construct an Decoder object.
batch_score(ys: Tensor, states: List[Any], xs: Tensor) → Tuple[Tensor, List[Any]]
Score new token batch (required).
- Parameters:
- ys (torch.Tensor) – torch.int64 prefix tokens (n_batch, ylen).
- states (List *[*Any ]) – Scorer states for prefix tokens.
- xs (torch.Tensor) – The encoder feature that generates ys (n_batch, xlen, n_feat).
- Returns: Tuple of : batchfied scores for next token with shape of (n_batch, n_vocab) and next state list for ys.
- Return type: tuple[torch.Tensor, List[Any]]
forward(tgt, tgt_mask, memory, memory_mask)
Forward decoder.
Parameters:
- tgt (torch.Tensor) – Input token ids, int64 (#batch, maxlen_out) if input_layer == “embed”. In the other case, input tensor (#batch, maxlen_out, odim).
- tgt_mask (torch.Tensor) – Input token mask (#batch, maxlen_out). dtype=torch.uint8 in PyTorch 1.2- and dtype=torch.bool in PyTorch 1.2+ (include 1.2).
- memory (torch.Tensor) – Encoded memory, float32 (#batch, maxlen_in, feat).
- memory_mask (torch.Tensor) – Encoded memory mask (#batch, maxlen_in). dtype=torch.uint8 in PyTorch 1.2- and dtype=torch.bool in PyTorch 1.2+ (include 1.2).
Returns: Decoded token score before softmax (#batch, maxlen_out, odim) : if use_output_layer is True. In the other case,final block outputs (#batch, maxlen_out, attention_dim).
torch.Tensor: Score mask before softmax (#batch, maxlen_out).
Return type: torch.Tensor
forward_one_step(tgt, tgt_mask, memory, *, cache=None)
Forward one step.
- Parameters:
- tgt (torch.Tensor) – Input token ids, int64 (#batch, maxlen_out).
- tgt_mask (torch.Tensor) – Input token mask (#batch, maxlen_out). dtype=torch.uint8 in PyTorch 1.2- and dtype=torch.bool in PyTorch 1.2+ (include 1.2).
- memory (torch.Tensor) – Encoded memory, float32 (#batch, maxlen_in, feat).
- cache (List *[*torch.Tensor ]) – List of cached tensors. Each tensor shape should be (#batch, maxlen_out - 1, size).
- Returns: Output tensor (batch, maxlen_out, odim). List[torch.Tensor]: List of cache tensors of each decoder layer.
- Return type: torch.Tensor
score(ys, state, x)
Score.