linear.py 961 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env python3
  2. # 2021, Carnegie Mellon University; Xuankai Chang
  3. # Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
  4. """Linear Projection."""
  5. from funasr.models.preencoder.abs_preencoder import AbsPreEncoder
  6. from typing import Tuple
  7. import torch
  8. class LinearProjection(AbsPreEncoder):
  9. """Linear Projection Preencoder."""
  10. def __init__(
  11. self,
  12. input_size: int,
  13. output_size: int,
  14. ):
  15. """Initialize the module."""
  16. super().__init__()
  17. self.output_dim = output_size
  18. self.linear_out = torch.nn.Linear(input_size, output_size)
  19. def forward(
  20. self, input: torch.Tensor, input_lengths: torch.Tensor
  21. ) -> Tuple[torch.Tensor, torch.Tensor]:
  22. """Forward."""
  23. output = self.linear_out(input)
  24. return output, input_lengths # no state in this layer
  25. def output_size(self) -> int:
  26. """Get the output size."""
  27. return self.output_dim