Source code for openspeech.encoders.convolutional_transformer_encoder

# MIT License
#
# Copyright (c) 2021 Soohwan Kim and Sangchun Ha and Soyoung Cho
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import torch
import torch.nn as nn
from typing import Tuple

from openspeech.encoders import OpenspeechEncoder
from openspeech.encoders.transformer_encoder import TransformerEncoderLayer
from openspeech.modules import (
    Linear,
    PositionalEncoding,
    get_attn_pad_mask, Transpose,
)


[docs]class ConvolutionalTransformerEncoder(OpenspeechEncoder): r""" The TransformerEncoder is composed of a stack of N identical layers. Each layer has two sub-layers. The first is a multi-head self-attention mechanism, and the second is a simple, position-wise fully connected feed-forward network. Args: input_dim: dimension of feature vector extractor (str): convolutional extractor d_model: dimension of model (default: 512) d_ff: dimension of feed forward network (default: 2048) num_layers: number of encoders layers (default: 6) num_heads: number of attention heads (default: 8) dropout_p (float, optional): probability of dropout (default: 0.3) conv_activation (str, optional): activation function of convolutional extractor (default: hardtanh) joint_ctc_attention (bool, optional): flag indication joint ctc attention or not (default: False) Inputs: - **inputs**: list of sequences, whose length is the batch size and within which each sequence is list of tokens - **input_lengths**: list of sequence lengths Returns: (Tensor, Tensor, Tensor): * outputs: A output sequence of encoders. `FloatTensor` of size ``(batch, seq_length, dimension)`` * encoder_logits: Log probability of encoders outputs will be passed to CTC Loss. If joint_ctc_attention is False, return None. * output_lengths: The length of encoders outputs. ``(batch)`` """ def __init__( self, num_classes: int, input_dim: int, extractor: str = 'vgg', d_model: int = 512, d_ff: int = 2048, num_layers: int = 6, num_heads: int = 8, dropout_p: float = 0.3, conv_activation: str = "relu", joint_ctc_attention: bool = False, ) -> None: super(ConvolutionalTransformerEncoder, self).__init__() extractor = self.supported_extractors[extractor.lower()] self.conv = extractor(input_dim=input_dim, activation=conv_activation) self.conv_output_dim = self.conv.get_output_dim() self.num_classes = num_classes self.joint_ctc_attention = joint_ctc_attention self.d_model = d_model self.num_layers = num_layers self.num_heads = num_heads self.input_proj = Linear(self.conv_output_dim, d_model) self.input_norm = nn.LayerNorm(d_model) self.input_dropout = nn.Dropout(p=dropout_p) self.positional_encoding = PositionalEncoding(d_model) self.layers = nn.ModuleList([ TransformerEncoderLayer( d_model=d_model, num_heads=num_heads, d_ff=d_ff, dropout_p=dropout_p, ) for _ in range(num_layers) ]) if self.joint_ctc_attention: self.fc = nn.Sequential( Transpose(shape=(1, 2)), nn.Dropout(dropout_p), Linear(d_model, num_classes, bias=False), )
[docs] def forward( self, inputs: torch.Tensor, input_lengths: torch.Tensor, ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: r""" Forward propagate a `inputs` for encoders training. Args: inputs (torch.FloatTensor): A input sequence passed to encoders. Typically for inputs this will be a padded `FloatTensor` of size ``(batch, seq_length, dimension)``. input_lengths (torch.LongTensor): The length of input tensor. ``(batch)`` Returns: (Tensor, Tensor, Tensor): * outputs: A output sequence of encoders. `FloatTensor` of size ``(batch, seq_length, dimension)`` * encoder_logits: Log probability of encoders outputs will be passed to CTC Loss. If joint_ctc_attention is False, return None. * output_lengths: The length of encoders outputs. ``(batch)`` """ encoder_logits = None conv_outputs, output_lengths = self.conv(inputs, input_lengths) self_attn_mask = get_attn_pad_mask(conv_outputs, output_lengths, conv_outputs.size(1)) outputs = self.input_norm(self.input_proj(conv_outputs)) outputs += self.positional_encoding(outputs.size(1)) outputs = self.input_dropout(outputs) for layer in self.layers: outputs, attn = layer(outputs, self_attn_mask) if self.joint_ctc_attention: encoder_logits = self.fc(outputs.transpose(1, 2)).log_softmax(dim=-1) return outputs, encoder_logits, output_lengths