- paddle.text. viterbi_decode ( potentials, transition_params, lengths, include_bos_eos_tag=True, name=None )
Decode the highest scoring sequence of tags computed by transitions and potentials and get the viterbi path.
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
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.
- 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
import paddle paddle.seed(102) 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) # scores: [3.37089300, 1.56825531], path: [[1, 0, 0], [1, 1, 0]]