# 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.modules import Linear
[docs]class RNNTransducerEncoder(OpenspeechEncoder):
r"""
Encoder of RNN-Transducer.
Args:
input_dim (int): dimension of input vector
hidden_state_dim (int, optional): hidden state dimension of encoders (default: 320)
output_dim (int, optional): output dimension of encoders and decoders (default: 512)
num_layers (int, optional): number of encoders layers (default: 4)
rnn_type (str, optional): type of rnn cell (default: lstm)
bidirectional (bool, optional): if True, becomes a bidirectional encoders (default: True)
Inputs: inputs, input_lengths
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)
* outputs (torch.FloatTensor): A output sequence of encoders. `FloatTensor` of size
``(batch, seq_length, dimension)``
* hidden_states (torch.FloatTensor): A hidden state of encoders. `FloatTensor` of size
``(batch, seq_length, dimension)``
Reference:
A Graves: Sequence Transduction with Recurrent Neural Networks
https://arxiv.org/abs/1211.3711.pdf
"""
supported_rnns = {
'lstm': nn.LSTM,
'gru': nn.GRU,
'rnn': nn.RNN,
}
def __init__(
self,
input_dim: int,
hidden_state_dim: int,
output_dim: int,
num_layers: int,
rnn_type: str = 'lstm',
dropout_p: float = 0.2,
bidirectional: bool = True,
):
super(RNNTransducerEncoder, self).__init__()
self.hidden_state_dim = hidden_state_dim
rnn_cell = self.supported_rnns[rnn_type.lower()]
self.rnn = rnn_cell(
input_size=input_dim,
hidden_size=hidden_state_dim,
num_layers=num_layers,
bias=True,
batch_first=True,
dropout=dropout_p,
bidirectional=bidirectional,
)
self.fc = Linear(hidden_state_dim << 1 if bidirectional else hidden_state_dim, output_dim)
[docs] def forward(
self,
inputs: torch.Tensor,
input_lengths: torch.Tensor,
) -> Tuple[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)
* outputs (torch.FloatTensor): A output sequence of encoders. `FloatTensor` of size
``(batch, seq_length, dimension)``
* output_lengths (torch.LongTensor): The length of output tensor. ``(batch)``
"""
inputs = nn.utils.rnn.pack_padded_sequence(inputs.transpose(0, 1), input_lengths.cpu())
outputs, hidden_states = self.rnn(inputs)
outputs, _ = nn.utils.rnn.pad_packed_sequence(outputs)
outputs = self.fc(outputs.transpose(0, 1))
return outputs, input_lengths