Encoder + Decoder + Calc loss Args: speech: (Batch, Length, ...) speech_lengths: (Batch, ) text: (Batch, Length) text_lengths: (Batch,)
(
self,
speech: torch.Tensor,
speech_lengths: torch.Tensor,
text: torch.Tensor,
text_lengths: torch.Tensor,
**kwargs,
)
| 206 | self.beam_search = None |
| 207 | |
| 208 | def forward( |
| 209 | self, |
| 210 | speech: torch.Tensor, |
| 211 | speech_lengths: torch.Tensor, |
| 212 | text: torch.Tensor, |
| 213 | text_lengths: torch.Tensor, |
| 214 | **kwargs, |
| 215 | ) -> Tuple[torch.Tensor, Dict[str, torch.Tensor], torch.Tensor]: |
| 216 | """Encoder + Decoder + Calc loss |
| 217 | Args: |
| 218 | speech: (Batch, Length, ...) |
| 219 | speech_lengths: (Batch, ) |
| 220 | text: (Batch, Length) |
| 221 | text_lengths: (Batch,) |
| 222 | """ |
| 223 | |
| 224 | if len(text_lengths.size()) > 1: |
| 225 | text_lengths = text_lengths[:, 0] |
| 226 | if len(speech_lengths.size()) > 1: |
| 227 | speech_lengths = speech_lengths[:, 0] |
| 228 | |
| 229 | batch_size = speech.shape[0] |
| 230 | |
| 231 | # 1. Encoder |
| 232 | encoder_out, encoder_out_lens = self.encode(speech, speech_lengths) |
| 233 | intermediate_outs = None |
| 234 | if isinstance(encoder_out, tuple): |
| 235 | intermediate_outs = encoder_out[1] |
| 236 | encoder_out = encoder_out[0] |
| 237 | |
| 238 | loss_att, acc_att, cer_att, wer_att = None, None, None, None |
| 239 | loss_ctc, cer_ctc = None, None |
| 240 | stats = dict() |
| 241 | |
| 242 | # decoder: CTC branch |
| 243 | if self.ctc_weight != 0.0: |
| 244 | loss_ctc, cer_ctc = self._calc_ctc_loss( |
| 245 | encoder_out, encoder_out_lens, text, text_lengths |
| 246 | ) |
| 247 | |
| 248 | # Collect CTC branch stats |
| 249 | stats["loss_ctc"] = loss_ctc.detach() if loss_ctc is not None else None |
| 250 | stats["cer_ctc"] = cer_ctc |
| 251 | |
| 252 | # Intermediate CTC (optional) |
| 253 | loss_interctc = 0.0 |
| 254 | if self.interctc_weight != 0.0 and intermediate_outs is not None: |
| 255 | for layer_idx, intermediate_out in intermediate_outs: |
| 256 | # we assume intermediate_out has the same length & padding |
| 257 | # as those of encoder_out |
| 258 | loss_ic, cer_ic = self._calc_ctc_loss( |
| 259 | intermediate_out, encoder_out_lens, text, text_lengths |
| 260 | ) |
| 261 | loss_interctc = loss_interctc + loss_ic |
| 262 | |
| 263 | # Collect Intermedaite CTC stats |
| 264 | stats["loss_interctc_layer{}".format(layer_idx)] = ( |
| 265 | loss_ic.detach() if loss_ic is not None else None |
nothing calls this directly
no test coverage detected