參考:
還有一些其他的函數可以到官方文檔裏面查看和使用.
Ⅰ tf.contrib.crf.crf_log_likelihood
crf_log_likelihood(inputs,tag_indices,sequence_lengths,transition_params=None)
在一個條件隨機場裏面計算標籤序列的log-likelihood
參數:
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]
的轉移矩陣
Ⅱ tf.contrib.crf.viterbi_decode
viterbi_decode(score,transition_params)
通俗一點,作用就是返回最好的標籤序列.這個函數只能夠在測試時使用,在tensorflow外部解碼
參數:
score: 一個形狀爲
[seq_len, num_tags]
matrix of unary potentials.
transition_params: 形狀爲[num_tags, num_tags]
的轉移矩陣
返回:
viterbi: 一個形狀爲
[seq_len]
顯示了最高分的標籤索引的列表.
viterbi_score: A float containing the score for the Viterbi sequence.
Ⅲ.tf.contrib.crf.crf_decode
crf_decode(potentials,transition_params,sequence_length)
在tensorflow內解碼
參數:
potentials: 一個形狀爲
[batch_size, max_seq_len, num_tags]
的tensor,
transition_params: 一個形狀爲[num_tags, num_tags]
的轉移矩陣
sequence_length: 一個形狀爲[batch_size]
的 ,表示batch中每個序列的長度
返回:
decode_tags:一個形狀爲
[batch_size, max_seq_len]
的tensor,類型是tf.int32
.表示最好的序列標記.
best_score: 有個形狀爲[batch_size]
的tensor, 包含每個序列解碼標籤的分數.