-
Notifications
You must be signed in to change notification settings - Fork 7
/
seq2seq.py
406 lines (317 loc) · 12.8 KB
/
seq2seq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
"""
The module contains implementations of various sequence-to-sequence (seq2seq) speech recognition models
Classes:
- BasicAttSeq2SeqRNN: A basic seq2seq model with an RNN encoder and an attention-based RNN decoder.
- LAS: A Listen, Attend and Spell (LAS) model.
- RNNWithLocationAwareAtt: An RNN-based seq2seq model with location-aware attention mechanism.
- SpeechTransformer: A transformer-based seq2seq model for speech processing.
"""
from typing import Union
from torch import Tensor, nn
from speeq.constants import ENC_OUT_KEY, HIDDEN_STATE_KEY
from speeq.utils.utils import get_mask_from_lens
from .decoders import GlobAttRNNDecoder, LocationAwareAttDecoder, TransformerDecoder
from .encoders import PyramidRNNEncoder, RNNEncoder, SpeechTransformerEncoder
class BasicAttSeq2SeqRNN(nn.Module):
"""Implements The basic RNN encoder decoder ASR.
Args:
in_features (int): The encoder's input feature speech size.
n_classes (int): The number of classes/vocabulary.
hidden_size (int): The hidden size of the RNN layers.
enc_num_layers (int): The number of layers in the encoder.
bidirectional (bool): A flag indicating if the rnn is bidirectional or not.
dec_num_layers (int): The number of the RNN layers in the decoder.
emb_dim (int): The embedding size.
p_dropout (float): The dropout rate.
pred_activation (Module): An instance of an activation function.
teacher_forcing_rate (float): The teacher forcing rate. Default 0.0
rnn_type (str): The RNN type it has to be one of rnn, gru or lstm.
Default 'rnn'.
"""
def __init__(
self,
in_features: int,
n_classes: int,
hidden_size: int,
enc_num_layers: int,
bidirectional: bool,
dec_num_layers: int,
emb_dim: int,
p_dropout: float,
pred_activation: nn.Module,
teacher_forcing_rate: float = 0.0,
rnn_type: str = "rnn",
) -> None:
super().__init__()
self.has_bnorm = False
self.encoder = RNNEncoder(
in_features=in_features,
hidden_size=hidden_size,
bidirectional=bidirectional,
n_layers=enc_num_layers,
p_dropout=p_dropout,
rnn_type=rnn_type,
)
self.decoder = GlobAttRNNDecoder(
embed_dim=emb_dim,
hidden_size=hidden_size,
n_layers=dec_num_layers,
n_classes=n_classes,
pred_activation=pred_activation,
teacher_forcing_rate=teacher_forcing_rate,
rnn_type=rnn_type,
)
self.bidirectional = bidirectional
def _process_hiddens(self, h):
batch_size = h.shape[1]
h = h.permute(1, 0, 2)
h = h.contiguous()
h = h.view(1, batch_size, -1)
return h
def forward(
self, speech: Tensor, speech_mask: Tensor, text: Tensor, *args, **kwargs
) -> Tensor:
"""Passes the input to the model
Args:
speech (Tensor): The input speech of shape [B, M, d]
mask (Union[Tensor, None]): The speech mask of shape [B, M],
which is True for the data positions and False for the padding ones.
text (Tensor): The text tensor of shape [B, M_dec]
Returns:
Tensor: The prediction tensor of shape [B, M_dec, C]
"""
out, h, lengths = self.encoder(speech, speech_mask, return_h=True)
if self.bidirectional is True:
if isinstance(h, tuple):
# if LSTM is used
h = (self._process_hiddens(h[0]), self._process_hiddens(h[1]))
else:
h = self._process_hiddens(h)
speech_mask = get_mask_from_lens(lengths=lengths, max_len=out.shape[1])
speech_mask = speech_mask.to(speech.device)
preds = self.decoder(h=h, enc_out=out, enc_mask=speech_mask, dec_inp=text)
return preds
def predict(self, x: Tensor, mask: Tensor, state: dict) -> dict:
if ENC_OUT_KEY not in state:
enc_out, h, _ = self.encoder(x, mask, return_h=True)
if self.bidirectional is True:
if isinstance(h, tuple):
# if LSTM is used
h = (self._process_hiddens(h[0]), self._process_hiddens(h[1]))
else:
h = self._process_hiddens(h)
state[HIDDEN_STATE_KEY] = h
state[ENC_OUT_KEY] = enc_out
state = self.decoder.predict(state)
return state
class LAS(BasicAttSeq2SeqRNN):
"""Implements Listen, Attend and Spell model
proposed in https://arxiv.org/abs/1508.01211
Args:
in_features (int): The encoder's input feature speech size.
n_classes (int): The number of classes/vocabulary.
hidden_size (int): The hidden size of the RNN layers.
enc_num_layers (int): The number of layers in the encoder.
reduction_factor (int): The time resolution reduction factor.
bidirectional (bool): A flag indicating if the rnn is bidirectional or not.
dec_num_layers (int): The number of the RNN layers in the decoder.
emb_dim (int): The embedding size.
p_dropout (float): The dropout rate.
pred_activation (Module): An instance of an activation function to be
applied on the last dimension of the predicted logits..
teacher_forcing_rate (float): The teacher forcing rate. Default 0.0
rnn_type (str): The RNN type it has to be one of rnn, gru or lstm.
Default 'rnn'.
"""
def __init__(
self,
in_features: int,
n_classes: int,
hidden_size: int,
enc_num_layers: int,
reduction_factor: int,
bidirectional: bool,
dec_num_layers: int,
emb_dim: int,
p_dropout: float,
pred_activation: nn.Module,
teacher_forcing_rate: float = 0.0,
rnn_type: str = "rnn",
) -> None:
super().__init__(
in_features=in_features,
n_classes=n_classes,
hidden_size=hidden_size,
enc_num_layers=enc_num_layers,
bidirectional=bidirectional,
dec_num_layers=dec_num_layers,
emb_dim=emb_dim,
p_dropout=p_dropout,
pred_activation=pred_activation,
teacher_forcing_rate=teacher_forcing_rate,
rnn_type=rnn_type,
)
self.reduction_factor = reduction_factor
self.encoder = PyramidRNNEncoder(
in_features=in_features,
hidden_size=hidden_size,
reduction_factor=reduction_factor,
bidirectional=bidirectional,
n_layers=enc_num_layers,
p_dropout=p_dropout,
rnn_type=rnn_type,
)
class RNNWithLocationAwareAtt(BasicAttSeq2SeqRNN):
"""Implements RNN seq2seq model proposed
in https://arxiv.org/abs/1506.07503
Args:
in_features (int): The encoder's input feature speech size.
n_classes (int): The number of classes/vocabulary.
hidden_size (int): The hidden size of the RNN layers.
enc_num_layers (int): The number of layers in the encoder.
bidirectional (bool): A flag indicating if the rnn is bidirectional or not.
dec_num_layers (int): The number of the RNN layers in the decoder.
emb_dim (int): The embedding size.
kernel_size (int): The attention kernel size.
activation (str): The activation function to use in the attention layer.
it can be either softmax or sigmax.
p_dropout (float): The dropout rate.
pred_activation (Module): An instance of an activation function to be
applied on the last dimension of the predicted logits..
inv_temperature (Union[float, int]): The inverse temperature value. Default 1.
teacher_forcing_rate (float): The teacher forcing rate. Default 0.0
rnn_type (str): The RNN type it has to be one of rnn, gru or lstm.
Default 'rnn'.
"""
def __init__(
self,
in_features: int,
n_classes: int,
hidden_size: int,
enc_num_layers: int,
bidirectional: bool,
dec_num_layers: int,
emb_dim: int,
kernel_size: int,
activation: str,
p_dropout: float,
pred_activation: nn.Module,
inv_temperature: Union[float, int] = 1,
teacher_forcing_rate: float = 0.0,
rnn_type: str = "rnn",
) -> None:
super().__init__(
in_features=in_features,
n_classes=n_classes,
hidden_size=hidden_size,
enc_num_layers=enc_num_layers,
bidirectional=bidirectional,
dec_num_layers=dec_num_layers,
emb_dim=emb_dim,
pred_activation=pred_activation,
p_dropout=p_dropout,
rnn_type=rnn_type,
)
self.decoder = LocationAwareAttDecoder(
embed_dim=emb_dim,
hidden_size=hidden_size,
n_layers=dec_num_layers,
n_classes=n_classes,
pred_activation=pred_activation,
kernel_size=kernel_size,
activation=activation,
inv_temperature=inv_temperature,
teacher_forcing_rate=teacher_forcing_rate,
rnn_type=rnn_type,
)
class SpeechTransformer(nn.Module):
"""Implements the Speech Transformer model proposed in
https://ieeexplore.ieee.org/document/8462506
Args:
in_features (int): The input/speech feature size.
n_classes (int): The number of classes.
n_conv_layers (int): The number of down-sampling convolutional layers.
kernel_size (int): The kernel size of the down-sampling convolutional layers.
stride (int): The stride size of the down-sampling convolutional layers.
d_model (int): The model dimensionality.
n_enc_layers (int): The number of encoder layers.
n_dec_layers (int): The number of decoder layers.
ff_size (int): The dimensionality of the inner layer of the feed-forward module.
h (int): The number of attention heads.
att_kernel_size (int): The kernel size of the attentional convolutional layers.
att_out_channels (int): The number of output channels of the attentional convolution layers.
pred_activation (Module): An activation function instance to be applied on
the last dimension of the predicted logits.
masking_value (int): The attentin masking value. Default -1e15
"""
def __init__(
self,
in_features: int,
n_classes: int,
n_conv_layers: int,
kernel_size: int,
stride: int,
d_model: int,
n_enc_layers: int,
n_dec_layers: int,
ff_size: int,
h: int,
att_kernel_size: int,
att_out_channels: int,
pred_activation: nn.Module,
masking_value: int = -1e15,
) -> None:
super().__init__()
self.has_bnorm = False
self.encoder = SpeechTransformerEncoder(
in_features=in_features,
n_conv_layers=n_conv_layers,
kernel_size=kernel_size,
stride=stride,
d_model=d_model,
n_layers=n_enc_layers,
ff_size=ff_size,
h=h,
att_kernel_size=att_kernel_size,
att_out_channels=att_out_channels,
)
self.decoder = TransformerDecoder(
n_classes=n_classes,
n_layers=n_dec_layers,
d_model=d_model,
ff_size=ff_size,
h=h,
pred_activation=pred_activation,
masking_value=masking_value,
)
def forward(
self,
speech: Tensor,
speech_mask: Tensor,
text: Tensor,
text_mask: Tensor,
*args,
**kwargs
) -> Tensor:
"""Passes the input to the model
Args:
speech (Tensor): The input speech of shape [B, M, d]
speech_mask (Union[Tensor, None]): The speech mask of shape [B, M],
which is True for the data positions and False for the padding ones.
text (Tensor): The text tensor of shape [B, M_dec]
text_mask (Union[Tensor, None]): The text mask of shape [B, M_dec],
which is True for the data positions and False for the padding ones.
Returns:
Tensor: The prediction tensor of shape [B, M_dec, C]
"""
speech, lengths = self.encoder(speech, speech_mask)
speech_mask = get_mask_from_lens(lengths, speech.shape[1])
preds = self.decoder(
enc_out=speech, enc_mask=speech_mask, dec_inp=text, dec_mask=text_mask
)
return preds
def predict(self, speech: Tensor, mask: Tensor, state: dict):
if ENC_OUT_KEY not in state:
state[ENC_OUT_KEY], _ = self.encoder(speech, mask)
state = self.decoder.predict(state)
return state