# 【算法】BILSTM+CRF中的条件随机场

## BILSTM+CRF中的条件随机场

tensorflow中crf关键的两个函数是训练函数tf.contrib.crf.crf_log_likelihood和解码函数tf.contrib.crf.viterbi_decode

crf_log_likelihood(inputs, tag_indices, sequence_lengths, transition_params=None)
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 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_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.



## crf细节

$P(y|x)=\frac{e^s}{e^{s_1}+e^{s_2}+e^{s_3}+...+e^{s_n}}$

$e^s$是当前序列的分数，分母是所有序列分数的和。

$EmissionScore$: bilstm输出的每个位置是各个标签的概率。是一个$seq\_len\times num\_tags$的矩阵。如上图黄色矩形部分。
$TransitionScore$: 标签间的转移概率。是一个$num\_tags\times num\_tags$的矩阵

$s_i=EmissionScore+TransitionScore$

$EmissionScore=x_{0,START}+x_{1,B-Person}+x_{2,I-Person}+x_{3,O}+x_{4,B-Organization}+x_{5,O}+x_{6,END}$

$TransitionScore=t_{START->B-Person} + t_{B-Person->I-Person} + t_{I-Person->O} + t_{0->B-Organization} + t_{B-Organization->O} + t_{O->END}$

$e^s=e^{EmissionScore+TransitionScore}$

crf_log_likelihood函数输出的transition_params，就是要求解的状态转移矩阵。
viterbi_decode(score, transition_params)，就是通过bilstm的输出score和求解的状态转移矩阵transition_params来解码最终结果。

## 参考资料

posted @ 2018-12-03 20:55  匡子语  阅读(...)  评论(...编辑  收藏