k2.fsa

class k2.Fsa(s, negate_scores=False)[source]
__init__(s, negate_scores=False)[source]

Create an Fsa from a string.

The given string s consists of lines with the following format:

  1. When it represents an acceptor:

    src_state dest_state label score

  2. When it represents a transducer:

    src_state dest_state label aux_label score

The line for the final state consists of only one field:

final_state

Note

Fields are separated by space(s), tab(s) or both. The score field is a float, while other fields are integers.

Caution

The first column has to be non-decreasing.

Caution

The final state has the largest state number. There is only one final state. All arcs that are connected to the final state have label -1.

Parameters
  • s (str) – The input string. Refer to the above comment for its format.

  • negate_scores (bool) – Optional. If true, the string form has the weights as costs, not scores, so we negate as we read.

classmethod _create(fsa, aux_labels=None)[source]

For internal use only.

Return type

Fsa

classmethod from_tensor(tensor, aux_labels=None)[source]

Build an Fsa from a tensor with optional aux_labels.

It is useful when loading an Fsa from file.

Parameters
  • tensor (Tensor) –

    A torch tensor of dtype torch.int32 with 4 columns. Each row represents an arc. Column 0 is the src_state, column 1 the dest_state, column 2 the label, and column 3 the score.

    Caution

    Scores are floats and their binary pattern is reinterpreted as integers and saved in a tensor of dtype torch.int32.

  • aux_labels (Optional[Tensor]) – Optional. If not None, it associates an aux_label with every arc, so it has as many rows as tensor. It is a 1-D tensor of dtype torch.int32.

Return type

Fsa

Returns

An instance of Fsa.

to_str(negate_scores=False)[source]

Convert an Fsa to a string.

Note

The returned string can be used to construct an Fsa.

Parameters

negate_scores (bool) – Optional. If true, we negate the score during the conversion.

Return type

str

Returns

A string representation of the Fsa.

property arcs[source]

Return the arcs of the Fsa.

Caution

The scores are not contained in the returned tensor. Please use the weights property if you need it.

Return type

Tensor

Returns

A tensor of dtype torch.int32 with 3 columns. Each row represents an arc. Column 0 is the src_state, column 1 the dest_state, and column 2 the label.

property weights[source]

Returns the weights of arcs in the Fsa.

Return type

Tensor

Returns

A 1-D tensor of dtype torch.float32. It has as many rows as arcs.

property aux_labels[source]

Return the aux_labels associated with arcs, if any.

Return type

Optional[Tensor]

Returns

None or a 1-D tensor of dtype torch.int32 if the Fsa is a transducer. It has as many rows as arcs.

to(device)[source]

Convert an Fsa to a new Fsa on a given device.

Caution

It is NOT an in-place operation. It returns a NEW instance.

Parameters

device (Union[str, device]) – A torch device. Currently it supports only CUDA and CPU devices.

Return type

Fsa

Returns

A new Fsa on the given device.

to_dot()[source]
Return type

Digraph