예제 #1
0
 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 _k2.fsa_to_tensor(self.arcs)
예제 #2
0
파일: fsa.py 프로젝트: zhu-han/k2
 def arcs_as_tensor(self) -> torch.Tensor:
     '''Return the core part of the Fsa (the arcs) serialized to a Tensor
        of int32 type, with shape (num_arcs, 4); the floats are reinterpreted
        as int32 and will appear as garbage if printed.  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 :func:`as_dict` and :func:`from_dict`
     '''
     return _k2.fsa_to_tensor(self.arcs)
예제 #3
0
파일: utils.py 프로젝트: aarora8/k2
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 _k2.fsa_to_tensor(fsa.arcs)
예제 #4
0
파일: fsa.py 프로젝트: zhu-han/k2
    def as_dict(self) -> Dict[str, Any]:
        '''Convert this Fsa to a dict (probably for purposes of serialization
        , 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'] = _k2.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