viterbi_decode

paddle.text. viterbi_decode ( potentials, transition_params, lengths, include_bos_eos_tag=True, name=None ) [source]

Decode the highest scoring sequence of tags computed by transitions and potentials and get the viterbi path.

Parameters
  • potentials (Tensor) – The input tensor of unary emission. This is a 3-D tensor with shape of [batch_size, sequence_length, num_tags]. The data type is float32 or float64.

  • transition_params (Tensor) – The input tensor of transition matrix. This is a 2-D tensor with shape of [num_tags, num_tags]. The data type is float32 or float64.

  • lengths (Tensor) – The input tensor of length of each sequence. This is a 1-D tensor with shape of [batch_size]. The data type is int64.

  • include_bos_eos_tag (bool, optional) – If set to True, the last row and the last column of transitions will be considered as start tag, the second to last row and the second to last column of transitions will be considered as stop tag. Defaults to True.

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.

Returns

The output tensor containing the score for the Viterbi sequence. The shape is [batch_size]

and the data type is float32 or float64.

paths(Tensor): The output tensor containing the highest scoring tag indices. The shape is [batch_size, sequence_length]

and the data type is int64.

Return type

scores(Tensor)

Examples

>>> import paddle
>>> paddle.seed(2023)
>>> batch_size, seq_len, num_tags = 2, 4, 3
>>> emission = paddle.rand((batch_size, seq_len, num_tags), dtype='float32')
>>> length = paddle.randint(1, seq_len + 1, [batch_size])
>>> tags = paddle.randint(0, num_tags, [batch_size, seq_len])
>>> transition = paddle.rand((num_tags, num_tags), dtype='float32')
>>> scores, path = paddle.text.viterbi_decode(emission, transition, length, False)
>>> print(scores)
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[2.57385254, 2.04533720])
>>> print(path)
Tensor(shape=[2, 2], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0, 0],
 [1, 1]])