在引起人們對(duì)現(xiàn)代 RNN 廣泛興趣的重大突破中,有一項(xiàng)是統(tǒng)計(jì)機(jī)器翻譯應(yīng)用領(lǐng)域的重大進(jìn)展 。在這里,模型以一種語(yǔ)言的句子呈現(xiàn),并且必須預(yù)測(cè)另一種語(yǔ)言的相應(yīng)句子。請(qǐng)注意,由于兩種語(yǔ)言的語(yǔ)法結(jié)構(gòu)不同,這里的句子可能有不同的長(zhǎng)度,并且兩個(gè)句子中相應(yīng)的詞可能不會(huì)以相同的順序出現(xiàn)。
許多問(wèn)題都具有這種在兩個(gè)這樣的“未對(duì)齊”序列之間進(jìn)行映射的風(fēng)格。示例包括從對(duì)話提示到回復(fù)或從問(wèn)題到答案的映射。廣義上,此類問(wèn)題稱為 序列到序列(seq2seq) 問(wèn)題,它們是本章剩余部分和 第 11 節(jié)大部分內(nèi)容的重點(diǎn)。
在本節(jié)中,我們將介紹機(jī)器翻譯問(wèn)題和我們將在后續(xù)示例中使用的示例數(shù)據(jù)集。幾十年來(lái),語(yǔ)言間翻譯的統(tǒng)計(jì)公式一直很流行 (Brown等人,1990 年,Brown等人,1988 年),甚至在研究人員使神經(jīng)網(wǎng)絡(luò)方法起作用之前(這些方法通常被統(tǒng)稱為神經(jīng)機(jī)器翻譯)。
首先,我們需要一些新代碼來(lái)處理我們的數(shù)據(jù)。與我們?cè)?.3 節(jié)中看到的語(yǔ)言建模不同,這里的每個(gè)示例都包含兩個(gè)單獨(dú)的文本序列,一個(gè)是源語(yǔ)言,另一個(gè)(翻譯)是目標(biāo)語(yǔ)言。以下代碼片段將展示如何將預(yù)處理后的數(shù)據(jù)加載到小批量中進(jìn)行訓(xùn)練。
import os
import tensorflow as tf
from d2l import tensorflow as d2l
10.5.1。下載和預(yù)處理數(shù)據(jù)集
首先,我們 從 Tatoeba Project 下載由雙語(yǔ)句子對(duì)組成的英法數(shù)據(jù)集。數(shù)據(jù)集中的每一行都是一個(gè)制表符分隔的對(duì),由一個(gè)英文文本序列和翻譯后的法文文本序列組成。請(qǐng)注意,每個(gè)文本序列可以只是一個(gè)句子,也可以是一段多句。在這個(gè)英語(yǔ)翻譯成法語(yǔ)的機(jī)器翻譯問(wèn)題中,英語(yǔ)被稱為源語(yǔ)言,法語(yǔ)被稱為目標(biāo)語(yǔ)言。
class MTFraEng(d2l.DataModule): #@save
"""The English-French dataset."""
def _download(self):
d2l.extract(d2l.download(
d2l.DATA_URL+'fra-eng.zip', self.root,
'94646ad1522d915e7b0f9296181140edcf86a4f5'))
with open(self.root + '/fra-eng/fra.txt', encoding='utf-8') as f:
return f.read()
data = MTFraEng()
raw_text = data._download()
print(raw_text[:75])
class MTFraEng(d2l.DataModule): #@save
"""The English-French dataset."""
def _download(self):
d2l.extract(d2l.download(
d2l.DATA_URL+'fra-eng.zip', self.root,
'94646ad1522d915e7b0f9296181140edcf86a4f5'))
with open(self.root + '/fra-eng/fra.txt', encoding='utf-8') as f:
return f.read()
data = MTFraEng()
raw_text = data._download()
print(raw_text[:75])
Go. Va !
Hi. Salut !
Run! Cours?!
Run! Courez?!
Who? Qui ?
Wow! ?a alors?!
class MTFraEng(d2l.DataModule): #@save
"""The English-French dataset."""
def _download(self):
d2l.extract(d2l.download(
d2l.DATA_URL+'fra-eng.zip', self.root,
'94646ad1522d915e7b0f9296181140edcf86a4f5'))
with open(self.root + '/fra-eng/fra.txt', encoding='utf-8') as f:
return f.read()
data = MTFraEng()
raw_text = data._download()
print(raw_text[:75])
Go. Va !
Hi. Salut !
Run! Cours?!
Run! Courez?!
Who? Qui ?
Wow! ?a alors?!
class MTFraEng(d2l.DataModule): #@save
"""The English-French dataset."""
def _download(self):
d2l.extract(d2l.download(
d2l.DATA_URL+'fra-eng.zip', self.root,
'94646ad1522d915e7b0f9296181140edcf86a4f5'))
with open(self.root + '/fra-eng/fra.txt', encoding='utf-8') as f:
return f.read()
data = MTFraEng()
raw_text = data._download()
print(raw_text[:75])
Go. Va !
Hi. Salut !
Run! Cours?!
Run! Courez?!
Who? Qui ?
Wow! ?a alors?!
下載數(shù)據(jù)集后,我們對(duì)原始文本數(shù)據(jù)進(jìn)行幾個(gè)預(yù)處理步驟。例如,我們將不間斷空格替換為空格,將大寫字母轉(zhuǎn)換為小寫字母,在單詞和標(biāo)點(diǎn)符號(hào)之間插入空格。
@d2l.add_to_class(MTFraEng) #@save
def _preprocess(self, text):
# Replace non-breaking space with space
text = text.replace('\u202f', ' ').replace('\xa0', ' ')
# Insert space between words and punctuation marks
no_space = lambda char, prev_char: char in ',.!?' and prev_char != ' '
out = [' ' + char if i > 0 and no_space(char, text[i - 1]) else char
for i, char in enumerate(text.lower())]
return ''.join(out)
text = data._preprocess(raw_text)
print(text[:80])
go . va !
hi . salut !
run ! cours !
run ! courez !
who ? qui ?
wow ! ?a alors !
@d2l.add_to_class(MTFraEng) #@save
def _preprocess(self, text):
# Replace non-breaking space with space
text = text.replace('\u202f', ' ').replace('\xa0', ' ')
# Insert space between words and punctuation marks
no_space = lambda char, prev_char: char in ',.!?' and prev_char != ' '
out = [' ' + char if i > 0 and no_space(char, text[i - 1]) else char
for i, char in enumerate(text.lower())]
return ''.join(out)
text = data._preprocess(raw_text)
print(text[:80])
go . va !
hi . salut !
run ! cours !
run ! courez !
who ? qui ?
wow ! ?a alors !
@d2l.add_to_class(MTFraEng) #@save
def _preprocess(self, text):
# Replace non-breaking space with space
text = text.replace('\u202f', ' ').replace('\xa0', ' ')
# Insert space between words and punctuation marks
no_space = lambda char, prev_char: char in ',.!?' and prev_char != ' '
out = [' ' + char if i > 0 and no_space(char, text[i - 1]) else char
for i, char in enumerate(text.lower())]
return ''.join(out)
text = data._preprocess(raw_text)
print(text[:80])
go . va !
hi . salut !
run ! cours !
run ! courez !
who ? qui ?
wow ! ?a alors !
@d2l.add_to_class(MTFraEng) #@save
def _preprocess(self, text):
# Replace non-breaking space with space
text = text.replace('\u202f', ' ').replace('\xa0', ' ')
# Insert space between words and punctuation marks
no_space = lambda char, prev_char: char in ',.!?' and prev_char != ' '
out = [' ' + char if i > 0 and no_space(char, text[i - 1]) else char
for i, char in enumerate(text.lower())]
return ''.join(out)
text = data._preprocess(raw_text)
print(text[:80])
go . va !
hi . salut !
run ! cours !
run ! courez !
who ? qui ?
wow ! ?a alors !
10.5.2。代幣化
與第 9.3 節(jié)中的字符級(jí)標(biāo)記化不同 ,對(duì)于機(jī)器翻譯,我們?cè)谶@里更喜歡單詞級(jí)標(biāo)記化(當(dāng)今最先進(jìn)的模型使用更復(fù)雜的標(biāo)記化技術(shù))。以下_tokenize
方法對(duì)第一個(gè)max_examples
文本序列對(duì)進(jìn)行分詞,其中每個(gè)分詞要么是一個(gè)單詞,要么是一個(gè)標(biāo)點(diǎn)符號(hào)。我們將特殊的“”標(biāo)記附加到每個(gè)序列的末尾,以指示序列的結(jié)束。當(dāng)模型通過(guò)生成一個(gè)接一個(gè)標(biāo)記的序列標(biāo)記進(jìn)行預(yù)測(cè)時(shí),“”標(biāo)記的生成表明輸出序列是完整的。最后,下面的方法返回兩個(gè)令牌列表列表:src
和tgt
。具體來(lái)說(shuō),src[i]
是來(lái)自ith源語(yǔ)言(此處為英語(yǔ))的文本序列和tgt[i]
目標(biāo)語(yǔ)言(此處為法語(yǔ))的文本序列。
評(píng)論