コード例 #1
0
ファイル: fsa_algo.py プロジェクト: entn-at/k2
def linear_fst(labels: Union[List[int], List[List[int]]],
               aux_labels: Union[List[int], List[List[int]]]) -> Fsa:
    '''Construct a linear FST from labels and its corresponding
    auxiliary labels.

    Note:
      The scores of arcs in the returned FST are all 0.

    Args:
      labels:
        A list of integers or a list of list of integers.
      aux_labels:
        A list of integers or a list of list of integers.

    Returns:
      An FST if the labels is a list of integers.
      A vector of FSTs (FsaVec) if the input is a list of list of integers.
    '''
    ragged_arc = _k2.linear_fsa(labels)
    if isinstance(labels[0], List):
        assert isinstance(aux_labels[0],
                          list), 'aux_labels and labels do not match.'
        buf = []
        for aux in aux_labels:
            buf.extend(aux + [-1])
        aux_labels_tmp = torch.tensor(buf, dtype=torch.int32)
    else:
        aux_labels_tmp = torch.tensor(aux_labels + [-1], dtype=torch.int32)
    fsa = Fsa(ragged_arc, aux_labels=aux_labels_tmp)
    return fsa
コード例 #2
0
ファイル: fsa_algo.py プロジェクト: aarora8/k2
def linear_fsa(labels: Union[List[int], List[List[int]], k2.RaggedTensor],
               device: Optional[Union[torch.device, str]] = None) -> Fsa:
    '''Construct an linear FSA from labels.

    Note:
      The scores of arcs in the returned FSA are all 0.

    Args:
      labels:
        It can be one of the following types:

            - A list of integers, e.g., `[1, 2, 3]`
            - A list of list-of-integers, e..g, `[ [1, 2], [1, 2, 3] ]`
            - An instance of :class:`k2.RaggedTensor`.
              Must have `num_axes == 2`.
      device:
        Optional. It can be either a string (e.g., 'cpu', 'cuda:0') or a
        torch.device.
        If it is ``None``, then the returned FSA is on CPU. It has to be None
        if ``labels`` is an instance of :class:`k2.RaggedTensor`.

    Returns:
      - If ``labels`` is a list of integers, return an FSA
      - If ``labels`` is a list of list-of-integers, return an FsaVec
      - If ``labels`` is an instance of :class:`k2.RaggedTensor`, return
        an FsaVec
    '''
    if isinstance(labels, k2.RaggedTensor):
        assert device is None
    ragged_arc = _k2.linear_fsa(labels, device)
    fsa = Fsa(ragged_arc)
    return fsa
コード例 #3
0
def linear_fsa(labels: Union[List[int], List[List[int]]],
               device: Optional[Union[torch.device, str]] = None) -> Fsa:
    '''Construct an linear FSA from labels.

    Note:
      The scores of arcs in the returned FSA are all 0.

    Args:
      labels:
        A list of integers or a list of list of integers.
      device:
        Optional. It can be either a string (e.g., 'cpu',
        'cuda:0') or a torch.device.
        If it is None, then the returned FSA is on CPU.

    Returns:
      An FSA if the labels is a list of integers.
      A vector of FSAs (i.e., FsaVec) if the input is a list of list
      of integers.
    '''
    if device is not None:
        device = torch.device(device)
        if device.type == 'cpu':
            gpu_id = -1
        else:
            assert device.type == 'cuda'
            gpu_id = getattr(device, 'index', 0)
    else:
        gpu_id = -1
    ragged_arc = _k2.linear_fsa(labels, gpu_id)
    fsa = Fsa(ragged_arc)
    return fsa
コード例 #4
0
ファイル: fsa_algo.py プロジェクト: OUC-lan/k2
def linear_fsa(symbols: Union[List[int], List[List[int]]]) -> Fsa:
    '''Construct an linear FSA from symbols.

    Note:
      The scores of arcs in the returned FSA are all 0.

    Args:
      symbols:
        A list of integers or a list of list of integers.

    Returns:
      An FSA if the input is a list of integers.
      A vector of FSAs if the input is a list of list of integers.
    '''
    ragged_arc = _k2.linear_fsa(symbols)
    fsa = Fsa(ragged_arc)
    return fsa
コード例 #5
0
ファイル: fsa_algo.py プロジェクト: entn-at/k2
def linear_fsa(labels: Union[List[int], List[List[int]], k2.RaggedInt],
               device: Optional[Union[torch.device, str]] = None) -> Fsa:
    '''Construct an linear FSA from labels.

    Note:
      The scores of arcs in the returned FSA are all 0.

    Args:
      labels:
        It can be one of the following types:

            - A list of integers, e.g., `[1, 2, 3]`
            - A list of list-of-integers, e..g, `[ [1, 2], [1, 2, 3] ]`
            - An instance of :class:`k2.RaggedInt`. Must have `num_axes() == 2`.
      device:
        Optional. It can be either a string (e.g., 'cpu',
        'cuda:0') or a torch.device.
        If it is None, then the returned FSA is on CPU. It has to be None
        if `labels` is an instance of :class:`k2.RaggedInt`.

    Returns:
      - If `labels` is a list of integers, return an FSA
      - If `labels` is a list of list-of-integers, return an FsaVec
      - If `labels` is an instance of :class:`k2.RaggedInt`, return an FsaVec
    '''
    if isinstance(labels, k2.RaggedInt):
        assert device is None
        assert labels.num_axes() == 2

    if device is not None:
        device = torch.device(device)
        if device.type == 'cpu':
            gpu_id = -1
        else:
            assert device.type == 'cuda'
            gpu_id = getattr(device, 'index', 0)
    else:
        gpu_id = -1
    ragged_arc = _k2.linear_fsa(labels, gpu_id)
    fsa = Fsa(ragged_arc)
    return fsa