add_sos_eos.py 940 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Copyright 2019 Shigeki Karita
  4. # Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
  5. """Unility functions for Transformer."""
  6. import torch
  7. from funasr.modules.nets_utils import pad_list
  8. def add_sos_eos(ys_pad, sos, eos, ignore_id):
  9. """Add <sos> and <eos> labels.
  10. :param torch.Tensor ys_pad: batch of padded target sequences (B, Lmax)
  11. :param int sos: index of <sos>
  12. :param int eos: index of <eos>
  13. :param int ignore_id: index of padding
  14. :return: padded tensor (B, Lmax)
  15. :rtype: torch.Tensor
  16. :return: padded tensor (B, Lmax)
  17. :rtype: torch.Tensor
  18. """
  19. _sos = ys_pad.new([sos])
  20. _eos = ys_pad.new([eos])
  21. ys = [y[y != ignore_id] for y in ys_pad] # parse padded ys
  22. ys_in = [torch.cat([_sos, y], dim=0) for y in ys]
  23. ys_out = [torch.cat([y, _eos], dim=0) for y in ys]
  24. return pad_list(ys_in, eos), pad_list(ys_out, ignore_id)