简体   繁体   English

如何理解张量流中的'viterbi_decode'

[英]How to understand the 'viterbi_decode' in tensorflow

The traditional viterbi algorithm using in HMM has a start probability matrix( viterbi algorithm wiki ), but the params of viterbi_decode in tensorflow only need transition probability matrix and emission probability matrix . HMM中使用的传统维特比算法具有起始概率矩阵( 维特比算法wiki ),但是张量中的维特比 解码参数仅需要转移概率矩阵发射概率矩阵 How to understand it? 怎么理解呢?

def viterbi_decode(score, transition_params):
  """Decode the highest scoring sequence of tags outside of 
  TensorFlow.

  This should only be used at test time.

  Args:
    score: A [seq_len, num_tags] matrix of unary potentials.
    transition_params: A [num_tags, num_tags] matrix of binary potentials.

  Returns:
    viterbi: A [seq_len] list of integers containing the highest scoring tag
    indicies.
    viterbi_score: A float containing the score for the Viterbi 
    sequence.
  """

The viterbi algorithm in Tensorflow doesn't need an initial probability matrix because it starts decoding by giving zero probability to all states. Tensorflow中的维特比算法不需要初始概率矩阵,因为它通过为所有状态赋予零概率来开始解码。

This means that it starts at state 0. 这意味着它从状态0开始。

You can check out the implementation here . 您可以在此处检查实现。

I have created full detailed tutorial with example about viterbi algorithm with tensorflow , You can take a look here : 我已经创建了完整的详细教程,并带有带有tensorflow的viterbi算法的示例,您可以在这里查看:

Suppose if your data looks like: 假设您的数据如下所示:

# logits :       A [batch_size, max_seq_len, num_tags] tensor of unary potentials to use as input to the CRF layer.

# labels_a :     A [batch_size, max_seq_len] matrix of tag indices for which we compute the log-likelihood.

# sequence_len : A [batch_size] vector of true sequence lengths.

Then 然后

log_likelihood , transition_params = tf.contrib.crf.crf_log_likelihood(logits,labels_a,sequence_len)

#return of crf log_likelihood function

# log_likelihood: A scalar containing the log-likelihood of the given sequence of tag indices.

# transition_params: A [num_tags, num_tags] transition matrix. 

# This is either provided by the caller or created in this function.

Now we can calculate viterbi score: 现在我们可以计算维特比分数:

# score: A [seq_len, num_tags] matrix of unary potentials.
# transition_params: A [num_tags, num_tags] matrix of binary potentials.

Notebook Link 笔记本链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM