Пример #1
0
Файл: fsa.py Проект: ts0923/k2
 def arcs_as_tensor(self) -> torch.Tensor:
     '''Return the core part of the Fsa (the arcs) serialized to a Tensor.
        This can be passed to the constructor, along with the aux_labels if
        present, to reconstruct this object.
        A more convenient way to serialize a Tensor is to use `as_dict`
        and `from_dict`
     '''
     return _fsa_to_tensor(self.arcs)
Пример #2
0
 def as_dict(self) -> Dict[str, Any]:
     '''Convert this Fsa to a dict (probably for purposes of serialization
       with, e.g., torch.save).
     '''
     ans = dict()
     ans['arcs'] = _fsa_to_tensor(self.arcs)
     if hasattr(self, 'aux_labels'):
         ans['aux_labels'] = self.aux_labels
     # TODO(dan): add other properties, e.g. from _tensor_attr and
     # _non_tensor_attr.
     return ans
Пример #3
0
def to_tensor(fsa: Fsa) -> torch.Tensor:
    '''Convert an Fsa to a Tensor.

    You can save the tensor to disk and read it later
    to construct an Fsa.

    Note:
      The returned Tensor contains only the transition rules, e.g.,
      arcs. You may want to save its aux_labels separately if any.

    Args:
      fsa:
        The input Fsa.
    Returns:
      A ``torch.Tensor`` of dtype ``torch.int32``. It is a 2-D tensor
      if the input is a single FSA. It is a 1-D tensor if the input
      is a vector of FSAs.
    '''
    return _fsa_to_tensor(fsa.arcs)
Пример #4
0
Файл: fsa.py Проект: ts0923/k2
    def as_dict(self) -> Dict[str, Any]:
        '''Convert this Fsa to a dict (probably for purposes of serialization
          with, e.g., torch.save).

        Caution:
          `self.requires_grad` attribute is not saved.
        Returns:
          A `dict` that can be used to reconstruct this FSA by using
          `Fsa.from_dict`.
        '''
        ans = dict()
        ans['arcs'] = _fsa_to_tensor(self.arcs)

        for name, value in self.named_tensor_attr(include_scores=False):
            ans[name] = value

        for name, value in self.named_non_tensor_attr():
            ans[name] = value

        return ans