【機器翻譯】BLEU學習

BLEU學習

簡介

BLEU(bilingual evaluation understudy),是一種翻譯結果的評估方法,主要概念來自於這篇Bleu: a method for automatic evaluation of matchin translatrion論文,本文主要學習和總結該論文及相關材料。

1. 評估標準

BLEU的思想基於一個前提:機器翻譯的結果越接近專業人士的翻譯,結果越好。爲了評估翻譯質量,需要將翻譯結果與一個或多個人工翻譯結果的接近程度量化。因此,評估系統需要兩個維度:

  1. “接近程度”的數字度量(closeness metric)
  2. 人工翻譯的高質量語料

其中接近程度的計算參考了語音識別中的單詞錯誤率(word error rate)的度量方法,不過需要針對兩個方面在算法上做修改:1、評估可能基於多條參考語料。2、允許一定範圍的單詞或者詞序不同。算法改進的主要思想是使用不同長度短語與參考語料的匹配情況的加權平均。讓我們從最簡單的度量方法開始。

2. 基準版的BLEU度量方法

給定一個句子,會存在多個很好的翻譯。這些翻譯或選詞不同或詞序不同,但是我們還是可以很容易的區分出好的和不好的翻譯。如:

candidate 1: It is a guide to action which ensures that the military always obeys the commands of the party.

candidate 2: It is to insure the troops forever hearing the activity gudiebook that party direct.

他們表達的都是同一內容,但是區別很大。作爲對比,這裏也給出3個人工翻譯:

reference 1: It is a gudie to action that ensures that the military will forever head Party commands.

reference 2: It is the guiding principle whichi guarantees the military forces always being under the command of the Party.

reference 3: It is the practical guide for the army always to heed the directions of the Party.

對照一下,很容易看出,好的翻譯candidate1與參考語料有很多詞或者短語重合,而candidate2則沒有。對於candidate1,“It is a guide to action”與reference1重合,“which”與reference2重合,“ensures that the military”與reference1重合,“always”與reference2和3重合,“commands"與reference1重合,”of the party“與reference2重合。相反,candidate2與參考翻譯重合的非常少。

對於BLEU方法,程序主要是算出機器翻譯的n-grams和參考翻譯的n-grams的匹配情況並且取得匹配總數。這些匹配是位置無關的(position-independent)。匹配的越多,那這個翻譯結果就越好。讓我們先看下unigramd的匹配情況。

(這裏的n-grams就是指短語長度,如2-gram(bigram), 就是兩個單詞長度的短語。起初看的時候,以爲用到自然語言處理裏的n-grams概念,結果不是。)

2.1 改進版的n-gram準確率

一般的準確率方法:計算機器翻譯結果中在參考翻譯中出現的單詞的個數,然後去除機器翻譯的單詞總數。但是這種方法有缺陷,如:

candidate: the the the the the the the

reference 1: The cat is on the mat.

reference 2: There is a cat ont he mat.

在這種情況下,unigram準確率會很高7/7,但是結果肯定是不好的。改進版的n-gram準確率計算方法主要就是爲了解決這種情況。計算步驟(以unigram爲例):

  1. 計算參考語料每個單詞出現的次數, 如存在多條參考語料,取次數最多的數值。

    max(ref_cnt1,ref_cnt2…)

  2. 將機翻結果每個單詞的出現次數修正爲參考語料中對應的單詞的最大次數。min(count,max_ref_count)

  3. 計算修正後的單詞總數,去除機翻的單詞總數。

使用該方法計算上面的例子,得到unigram準確率2/7.

n-gram的計算與此類似,只是單詞換爲n長的短語。如上例中的2-gram準確率爲0.

該方法能夠捕捉翻譯結果的兩個方面:adequacy和fluency. (/(ㄒoㄒ)/~~adequacy真不知道怎麼翻譯纔好,索性都不翻了吧。。。)。1-gram能計算adequacy,更長的n-gram可以計算fluency。

2.2 批量文本翻譯的準確率計算

儘管典型的機翻評估系統是基於整篇文檔,但是我們的基本輸入單元是單個句子。因此在批量文本的情況下,我們依然基於句子來計算。

  1. 基於2.1中的算法計算每個翻譯的n-gram修正後的數量。
  2. 將所有翻譯的n-gram修正後的數量求和,去除所有n-gram未修正的數量。

pn=Cϵ{candidates}ngramϵCCountclip(n-gram)Cϵ{candidates}ngramϵCCount(n-gram) \Large p_n=\frac {\sum_{C\epsilon \{candidates\}} \sum_{n-gram \epsilon C} Count_{clip}(n\text-gram)} {\sum_{C\epsilon \{candidates\}} {\sum_{n-gram^{'} \epsilon C} {Count(n\text-gram^{'})}}}

2.3 長度問題

機器翻譯的結果,不應該太長,也不應該太短。在一定程度上,改進版的n-gram準確率計算方法能處理這種情況。如單詞(短語)沒有出現,或者單詞(短語)數量超過參考句子的對應單詞(短語)的數量;但是沒有處理單詞(短語)過少的情況。如:

candidate: of the

reference 1: It is a guide to action that ensures that the military will forever heed Party commands.

reference 2: It is the gudiing principle which guarantees the military forces always being under the command of the Party.

reference 3: It is the practical guide for the army always to heed the directions of the Party.

對於這種情況,unigram準確率爲2/2,bigram準確率爲1/1,但是該翻譯肯定是不合理的。

2.4 召回問題

通常,準確率計算會結合召回來解決類似的長度相關的問題。但是,BLEU需要考慮多個參考翻譯的情況,每個參考翻譯都可能會選擇不同的詞來翻譯同一個句子。一般情況下,一個好的翻譯通常會使用參考中的某個候選詞,而不是全部。如果使用了所有候選,肯定不是一個好的翻譯。如:

candidate 1: I always invariably perpetually do.

candidate 2: I always do.

reference 1: I always do.

reference 2: I invariably do.

reference 3: I perpetually do.

candidate1使用了比參考翻譯更多的詞,但是明顯它比candidate2更差。因此,原始的召回計算不適用於這種場景。

2.5 短句懲罰

因此,我們引入一個短句懲罰因子(brevity pentlty factor)。引入該因子後,一個高評分的機器翻譯結果,應同時在長度、詞選擇以及詞序上與參考翻譯相匹配。注意:不管是短句懲罰還是改進版n-gram都不是直接引用原句的長度做計算,而是使用在參考語句上的長度區間。

如果翻譯結果與參考語句長度相等,則懲罰因子設爲1.0。如參考語句長度分別爲12、15、17,而翻譯結果爲12,則懲罰因子爲1. 我們稱最接近的參考語句長度爲"最佳匹配長度"(best match length)

還有一點需要考慮:如果逐個語句計算懲罰因子然後計算平均值,那麼對於短句的長度偏差,懲罰會很嚴厲。因此,我們在整個語料上計算懲罰因子,從而在句子這個層面一定的冗餘空間。計算步驟:

  1. 計算整個語料的參考語句的長度r,通過求和各個機器翻譯語句的最佳匹配長度獲得;
  2. 計算所有翻譯結果的長度,求和得c
  3. 得到懲罰因子r/c
2.6 BLEU計算

根據前面討論,可以得出BLEU的計算公式:

BP={1,c>re(1r/c),crBP=\begin{cases} 1,\quad c > r \\ e^{(1-r/c)}, \quad c\leq r \end{cases}

BLEU=BPexp(n=1Nwnlog(pn))BLEU=BP*exp\Bigg(\sum_{n=1}^N w_nlog(p_n)\Bigg)
其中BP爲懲罰因子,N爲n-gram的最大長度取值,一般爲4,wnw_n一般取1/N.

3. 源碼學習

從tensorflow中找到兩種BLEU的實現,nmt/bleutensor2tensor/bleu_hook. 主要實現差不多,這裏簡要說下不同點。

其中nmt版本的compute_bleu實現中, 入參是一個translation對應多個reference,與論文中描述的算法一致。 但是計算長度的時候不是按照"best match length"來取的。不知道是代碼實現錯誤還是對算法的修改(個人傾向認爲是個錯誤,但是可能在樣本數據比較多的情況下,影響會比較小而已。瞭解的同學請給我留言),按照算法,這樣取值會對懲罰因子的計算有影響。

for (references, translation) in zip(reference_corpus,
                                       translation_corpus):
    reference_length += min(len(r) for r in references) // 一對多。但是長度不是取最佳匹配長度。
    translation_length += len(translation)

還有geo_mean的計算,這個判斷對於n-gram的情況,是很容易得到0的結果啊,同樣不太理解(懂的同學請留言)。

if min(precisions) > 0:
    p_log_sum = sum((1. / max_order) * math.log(p) for p in precisions)
    geo_mean = math.exp(p_log_sum)
  else:
    geo_mean = 0

(如:
candidate: i like apple.
reference: i like organe. 這種情況,對於3-gram,p3p_3=0, 按照上述公式bleu最終會是0)
雖然引入了smooth(這塊還沒看),爲True的情況會修正上述錯誤,但是默認爲False。可能該版本的實現更多的是針對文檔級別的計算吧,不然我真看不懂。

tensor2tensor的實現比nmt版本的正常,但是也有點疑問。從邏輯上看,compute_bleu的實現是針對translation與reference一對一的關係的,也就是說該版本的實現不支持多reference的bleu計算的。

for (references, translations) in zip(reference_corpus, translation_corpus):
    reference_length += len(references) // 這種計算方法,無論從哪個角度看都決定了是一對一關係。
    translation_length += len(translations)

而geo_mean的實現就不是nmt版本的那個寫法,這個能不能從側面證明nmt版本的實現是錯誤的?

 if max(precisions) > 0:
    p_log_sum = sum(math.log(p) for p in precisions if p)
    geo_mean = math.exp(p_log_sum/max_order)

總結

這篇論文來回看了好幾遍才理解作者的用意,學習經典的東西還是要花時間的。另外,smooth-bleu還有一個wiki上提到的HyTer評估方法,需要看下。另外,還有n-grams,雖然跟這個無關,也需要了解下。

【附錄】

  1. wiki/bleu
  2. BLEU論文
  3. nmt/bleu源碼
  4. tensor2tensor/bleu源碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章