espnet2.asr.decoder.rnn_decoder.build_attention_list
espnet2.asr.decoder.rnn_decoder.build_attention_list
espnet2.asr.decoder.rnn_decoder.build_attention_list(eprojs: int, dunits: int, atype: str = 'location', num_att: int = 1, num_encs: int = 1, aheads: int = 4, adim: int = 320, awin: int = 5, aconv_chans: int = 10, aconv_filts: int = 100, han_mode: bool = False, han_type=None, han_heads: int = 4, han_dim: int = 320, han_conv_chans: int = -1, han_conv_filts: int = 100, han_win: int = 5)
Builds a list of attention mechanisms for a neural network decoder.
This function creates a list of attention modules based on the specified parameters, including types and configurations for both single and multi-encoder setups. It initializes the attention mechanisms according to the input projection size and decoder units.
- Parameters:
- eprojs (int) – The number of input projection dimensions.
- dunits (int) – The number of decoder units.
- atype (str , optional) – Type of attention mechanism to use. Defaults to “location”.
- num_att (int , optional) – Number of attention mechanisms to create. Defaults to 1.
- num_encs (int , optional) – Number of encoders. Defaults to 1.
- aheads (int , optional) – Number of attention heads. Defaults to 4.
- adim (int , optional) – Dimension of the attention layer. Defaults to 320.
- awin (int , optional) – Size of the attention window. Defaults to 5.
- aconv_chans (int , optional) – Number of channels in the attention convolution. Defaults to 10.
- aconv_filts (int , optional) – Size of filters in the attention convolution. Defaults to 100.
- han_mode (bool , optional) – Flag to indicate if hierarchical attention mode is enabled. Defaults to False.
- han_type (optional) – Type of hierarchical attention if han_mode is True. Defaults to None.
- han_heads (int , optional) – Number of heads in hierarchical attention. Defaults to 4.
- han_dim (int , optional) – Dimension of the hierarchical attention layer. Defaults to 320.
- han_conv_chans (int , optional) – Number of channels in the hierarchical attention convolution. Defaults to -1.
- han_conv_filts (int , optional) – Size of filters in the hierarchical attention convolution. Defaults to 100.
- han_win (int , optional) – Size of the hierarchical attention window. Defaults to 5.
- Returns: A list of initialized attention modules.
- Return type: torch.nn.ModuleList
- Raises:ValueError – If num_encs is less than or equal to 0.
Examples
>>> att_list = build_attention_list(256, 128)
>>> len(att_list)
1
>>> att_list = build_attention_list(256, 128, num_att=2)
>>> len(att_list)
2
>>> att_list = build_attention_list(256, 128, num_encs=2)
>>> len(att_list)
2
NOTE
This function is typically used in the context of sequence-to-sequence models where attention mechanisms are critical for aligning inputs and outputs.