写之前写吐槽一下,当时写代码的时候看了一下CRF的推导怎么都看不懂,所以当时写的时候从别人的代码直接复制出来,今天面试的时候被面试官问了,结束后研究一下,发现代码真的好简单、、。。。。。。
tf.contrib.crf.crf_log_likelihood()
代码实现细节在这里,,,我只是为了看一下[transition_params],其实他也事一个variable,所以可以存储保存下来(https://github.com/tensorflow/tensorflow/blob/r1.12/tensorflow/contrib/crf/python/ops/crf.py)
最大似然估计损失函数
tf.contrib.crf.crf_log_likelihood(
inputs,
tag_indices,
sequence_lengths,
transition_params=None
)
Defined in tensorflow/contrib/crf/python/ops/crf.py.
Computes the log-likelihood of tag sequences in a CRF.
Args:
inputs: A [batch_size, max_seq_len, num_tags] tensor of unary potentials to use as input to the CRF layer.
tag_indices: A [batch_size, max_seq_len] matrix of tag indices for which we compute the log-likelihood.
sequence_lengths: A [batch_size] vector of true sequence lengths.
transition_params: A [num_tags, num_tags] transition matrix, if available.
Returns:
log_likelihood: A [batch_size] Tensor containing the log-likelihood of each example, given the 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.
输入
inputs: 一个形状为[batch_size, max_seq_len, num_tags] 的tensor,一般使用BILSTM+加一个全连接映射层,映射成和我们和我们标签个数同宽的,处理之后输出转换为他要求的形状作为CRF层的输入.
tag_indices: 一个形状为[batch_size, max_seq_len] 的矩阵…真实标签.
sequence_lengths: 一个形状为 [batch_size] 的向量,表示每个序列的长度.
transition_params: 形状为[num_tags, num_tags] 的转移矩阵 (注意:这由调用者提供或在此函数中创建,一般都是让函数自己创建)
返回:
log_likelihood: 标量,log-likelihood
transition_params: 形状为[num_tags, num_tags] 的转移矩阵