首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何理解tensorflow中的“viterbi_decode”

如何理解tensorflow中的“viterbi_decode”
EN

Stack Overflow用户
提问于 2018-07-12 16:46:12
回答 2查看 1.3K关注 0票数 1

传统的隐马尔可夫模型( HMM )中的维特比算法有一个起始概率矩阵(viterbi algorithm wiki),而tensorflow中viterbi_decode的参数只需要转移概率矩阵发射概率矩阵。如何理解它?

代码语言:javascript
复制
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.
  """
EN

回答 2

Stack Overflow用户

发布于 2018-07-12 19:51:36

Tensorflow中的viterbi算法不需要初始概率矩阵,因为它通过为所有状态提供零概率来开始解码。

这意味着它从状态0开始。

您可以查看实现here

票数 1
EN

Stack Overflow用户

发布于 2018-08-12 02:32:52

我已经用tensorflow创建了关于维特比算法的完整详细的教程,你可以在这里看看:

假设您的数据如下所示:

代码语言:javascript
复制
# 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.

然后

代码语言:javascript
复制
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.

现在我们可以计算viterbi分数了:

代码语言:javascript
复制
# score: A [seq_len, num_tags] matrix of unary potentials.
# transition_params: A [num_tags, num_tags] matrix of binary potentials.

Notebook Link

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51301061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档