BLEU和SPICE:機器翻譯與圖像描述的自動評估方法詳解
在機器翻譯和圖像描述領域,評估模型性能是一個重要且復雜的問題。為了有效地評估機器生成的文本質量,我們需要一種自動、快速且可靠的方法。BLEU(Bilingual Evaluation Understudy)和SPICE(Semantic Propositional Image Caption Evaluation)是其中兩種廣泛使用的方法,本文將詳細介紹它們的工作原理及其優缺點。
BLEU:機器翻譯評估方法
1. BLEU的基本原理
BLEU是一種自動化的機器翻譯評估方法,通過計算候選翻譯(candidate)和參考翻譯(reference)之間的n-gram匹配來評估翻譯質量。n-gram是指將句子中相鄰的n個單詞組合在一起的方式,例如:
- 1-gram:{it, is, a, nice, day, today}
- 2-gram:{it is, is a, a nice, nice day, day today}
- 3-gram:{it is a, is a nice, a nice day, nice day today}
2. n-gram相似度計算
候選翻譯和參考翻譯的n-gram匹配度通過以下方式計算:
假設候選句子為 “It is a nice day today”,參考句子為 “Today is a nice day”。我們可以得到以下匹配情況:
- 1-gram相似度:{it, is, a, nice, day, today} vs {today, is, a, nice, day},匹配項為 {is, a, nice, day},相似度為 p1 = 5/6 = 0.83。
- 2-gram相似度:{it is, is a, a nice, nice day, day today} vs {today is, is a, a nice, nice day},匹配項為 {is a, a nice, nice day},相似度為 p2 = 3/5 = 0.6。
- 3-gram相似度:{it is a, is a nice, a nice day, nice day today} vs {today is a, is a nice, a nice day},匹配項為 {is a nice, a nice day},相似度為 p3 = 2/4 = 0.5。
3. 特殊情況處理
情況1:重復單詞
對于候選句子 “the the the the” 和參考句子 “The cat is standing on the ground”。如果不加以限制,1-gram相似度 p1 會等于 1。為了解決這個問題,需要對單詞計數進行截斷,計數取候選句子和參考句子中的最小值。這樣計算得到的 p1 = 1/4。
情況2:長度懲罰因子
候選句子 “a nice day” 和參考句子 “Today is a nice day” 的匹配度可能會因為句子長度較短而偏高。為了避免這種現象,BLEU 引入了長度懲罰因子(Brevity Penalty,BP)。計算公式如下:
B P = { 1 if? c > r e ( 1 ? r / c ) if? c ≤ r BP = \begin{cases} 1 & \text{if } c > r \\ e^{(1 - r/c)} & \text{if } c \leq r \end{cases} BP={1e(1?r/c)?if?c>rif?c≤r?
其中,c 表示候選翻譯的長度,r 表示參考翻譯的長度。
4. BLEU最終公式
為了平衡各階n-gram統計量的作用,通常取 N = 4,并對各階統計量進行加權求和,權重 w n w_n wn? 取 1 / n 1/n 1/n。BLEU最終計算公式如下:
B L E U = B P ? exp ? ( ∑ n = 1 N w n log ? p n ) BLEU = BP \cdot \exp \left( \sum_{n=1}^{N} w_n \log p_n \right) BLEU=BP?exp(n=1∑N?wn?logpn?)
5. BLEU代碼示例
以下是一個計算BLEU得分的Python代碼示例:
from nltk.translate.bleu_score import sentence_bleureference = [['today', 'is', 'a', 'nice', 'day']]
candidate = ['it', 'is', 'a', 'nice', 'day', 'today']
score = sentence_bleu(reference, candidate)
print('BLEU score:', score)
SPICE:圖像描述評估方法
1. SPICE的基本原理
BLEU在某些情況下表現不佳,例如“A shiny metal pot filled with some diced veggies.”和“The pan on the stove has chopped vegetables in it.”意思相近,但BLEU得分可能較低。為了彌補這一不足,SPICE通過比較語義命題內容(semantic propositional content)來提升圖像描述的評估性能。
2. Scene Graphs
SPICE通過對比候選描述和參考描述的scene graphs來進行評估。scene graph 表征了對象(object)、對象的屬性(attribute)以及對象之間的關系(relation)。如下圖所示:
3. F-score計算
定義候選描述為c,參考描述集合為S={s1, s2, …, sn},G()表示scene graph,T()表示從scene graph中得到的logical tuples。例如,圖1的logical tuple為:
{(girl), (court), (girl, young), (girl, standing), (court, tennis), (girl, on-top-of, court)}
定義精度P和召回R,SPICE的相似度計算如下:
P = ∣ T ( G ( c ) ) ∩ T ( G ( S ) ) ∣ ∣ T ( G ( c ) ) ∣ P = \frac{|T(G(c)) \cap T(G(S))|}{|T(G(c))|} P=∣T(G(c))∣∣T(G(c))∩T(G(S))∣?
R = ∣ T ( G ( c ) ) ∩ T ( G ( S ) ) ∣ ∣ T ( G ( S ) ) ∣ R = \frac{|T(G(c)) \cap T(G(S))|}{|T(G(S))|} R=∣T(G(S))∣∣T(G(c))∩T(G(S))∣?
F 1 = 2 P R P + R F1 = \frac{2PR}{P + R} F1=P+R2PR?
其中 ( T x T ) (T x T) (TxT) 方法表示從兩個scene graph中返回matching tuples,通過wordnet數據同義詞查找得到。
4. SPICE代碼示例
以下是一個計算SPICE得分的Python代碼示例:
from spice import spice_scorereference = "The pan on the stove has chopped vegetables in it."
candidate = "A shiny metal pot filled with some diced veggies."
score = spice_score(reference, candidate)
print('SPICE score:', score)
參考文獻
- BLEU: a method for automatic evaluation of machine translation.
- SPICE: Semantic Propositional Image Caption Evaluation.
- Meteor Universal: Language Specific Translation Evaluation for Any Target Language.
總結
BLEU和SPICE是兩種常用的自動評估方法,各有優缺點。BLEU通過計算n-gram匹配來評估翻譯的流暢性和保真度,適用于機器翻譯的自動評估。SPICE通過比較語義內容和scene graphs來提升圖像描述的評估性能,更適合評估圖像描述的語義一致性。選擇適合的方法可以提高評估的準確性和有效性。