linear.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 typeguard import check_argument_types
  7. from typing import Tuple
  8. import torch
  9. class LinearProjection(AbsPreEncoder):
  10. """Linear Projection Preencoder."""
  11. def __init__(
  12. self,
  13. input_size: int,
  14. output_size: int,
  15. ):
  16. """Initialize the module."""
  17. assert check_argument_types()
  18. super().__init__()
  19. self.output_dim = output_size
  20. self.linear_out = torch.nn.Linear(input_size, output_size)
  21. def forward(
  22. self, input: torch.Tensor, input_lengths: torch.Tensor
  23. ) -> Tuple[torch.Tensor, torch.Tensor]:
  24. """Forward."""
  25. output = self.linear_out(input)
  26. return output, input_lengths # no state in this layer
  27. def output_size(self) -> int:
  28. """Get the output size."""
  29. return self.output_dim