tensorflow - 如何理解 tensorflow 中的“viterbi_decode”
问题描述
HMM中使用的传统维特比算法有一个起始概率矩阵(维特比算法维基),而tensorflow中的维特比解码参数只需要转移概率矩阵和发射概率矩阵。怎么理解?
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.
"""
解决方案
我已经创建了完整的详细教程,其中包含有关使用 tensorflow 的维特比算法的示例,您可以在这里查看:
假设您的数据如下所示:
# 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.
然后
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.
现在我们可以计算维特比分数:
# score: A [seq_len, num_tags] matrix of unary potentials.
# transition_params: A [num_tags, num_tags] matrix of binary potentials.
推荐阅读
- c++ - 重载的小于运算符返回相反的布尔值
- python-3.x - 如何计算熊猫列中非空元素的数量?
- c++ - 在 C/C++ 中打开位于“/sys/”下的文件时出错
- spring-boot - SpringBootApplication 与 Spring Starter Web 错误
- node.js - 无法在 mongoDb 集合中保存数据:“ReferenceError:模型未定义”
- image - python图像显示
- swiftui - 在 SwiftUI 中返回泛型 View 类型的方法
- elasticsearch - ElasticSearch 查询字符串在嵌套对象中按范围搜索日期
- reactjs - 如何声明 SVG 组件的 props 类型?[React、TypeScript 和 Webpack]
- javascript - 当用户一段时间没有点击图片时自动显示文本