Forward pass for training. Args: speech: Speech audio tensor, shape (batch, time). speech_lengths: Length of each speech sample. lid: TODO. lid_lengths: Lengths of lid.
(
self,
speech: torch.Tensor, # may be padding
speech_lengths: torch.Tensor, # actual length
lid: torch.Tensor, # lid label, (batch_size, 1)
lid_lengths: torch.Tensor,
)
| 585 | self.encoder.interctc_use_conditioning = False |
| 586 | |
| 587 | def forward( |
| 588 | self, |
| 589 | speech: torch.Tensor, # may be padding |
| 590 | speech_lengths: torch.Tensor, # actual length |
| 591 | lid: torch.Tensor, # lid label, (batch_size, 1) |
| 592 | lid_lengths: torch.Tensor, |
| 593 | ): |
| 594 | """Forward pass for training. |
| 595 | |
| 596 | Args: |
| 597 | speech: Speech audio tensor, shape (batch, time). |
| 598 | speech_lengths: Length of each speech sample. |
| 599 | lid: TODO. |
| 600 | lid_lengths: Lengths of lid. |
| 601 | """ |
| 602 | assert lid.shape[1] == 1 |
| 603 | batch_size = speech.shape[0] |
| 604 | encoder_out, encoder_out_lens = self.encode(speech, speech_lengths) |
| 605 | |
| 606 | # re-generate encoder_out |
| 607 | if self.clip_frames is None: |
| 608 | reduced_encoder_out = ( |
| 609 | torch.zeros(batch_size, encoder_out_lens.max(), encoder_out.shape[-1]) |
| 610 | .to(encoder_out.dtype) |
| 611 | .to(encoder_out.device) |
| 612 | ) |
| 613 | for i, enc_length in enumerate(encoder_out_lens): |
| 614 | reduced_encoder_out[i, :enc_length] = encoder_out[i, :enc_length] |
| 615 | else: |
| 616 | reduced_encoder_out = ( |
| 617 | torch.zeros(batch_size, self.clip_frames, encoder_out.shape[-1]) |
| 618 | .to(encoder_out.dtype) |
| 619 | .to(encoder_out.device) |
| 620 | ) |
| 621 | if self.random_clip: |
| 622 | for i, enc_length in enumerate(encoder_out_lens): |
| 623 | if enc_length <= self.clip_frames: |
| 624 | reduced_encoder_out[i, :enc_length] = encoder_out[i, :enc_length] |
| 625 | encoder_out_lens[i] = enc_length |
| 626 | else: |
| 627 | max_start_index = enc_length.item() - self.clip_frames |
| 628 | start_index = np.random.randint(0, max_start_index + 1) |
| 629 | reduced_encoder_out[i, : self.clip_frames] = encoder_out[ |
| 630 | i, start_index : start_index + self.clip_frames |
| 631 | ] |
| 632 | encoder_out_lens[i] = self.clip_frames |
| 633 | else: |
| 634 | for i, enc_length in enumerate(encoder_out_lens): |
| 635 | enc_length = self.clip_frames if enc_length >= self.clip_frames else enc_length |
| 636 | reduced_encoder_out[i, :enc_length] = encoder_out[i, :enc_length] |
| 637 | encoder_out_lens[i] = enc_length |
| 638 | if self.proj_layer is not None: |
| 639 | reduced_encoder_out = self.proj_layer(reduced_encoder_out) |
| 640 | lid_output = self.lid_predictor(reduced_encoder_out, encoder_out_lens) # (B, D) |
| 641 | lid_logits = self.output_layer(lid_output) # (B, num_classes) |
| 642 | loss = self.criterion_lid(lid_logits[:, None, :], lid) |
| 643 | with torch.no_grad(): |
| 644 | _, predicted_lid = torch.max(lid_logits, 1) |
nothing calls this directly
no test coverage detected