用 Tokenizer 控制詞彙表
來更深入了解 Tokenizer 的運作吧。這個練習中,你會學到如何用已訓練好的 Tokenizer 將任意句子轉換成序列。你也會學到如何控制 Tokenizer 的詞彙表大小,並探索當你限制 Tokenizer 的詞彙表大小時,超出詞彙(OOV)字詞會發生什麼事。
本練習已提供你先前實作的 en_tok Tokenizer,且已為你匯入。
本練習屬於課程
使用 Keras 進行機器翻譯
練習說明
- 使用先前的
en_tokTokenizer,將下列句子轉換成序列:she likes grapefruit , peaches , and lemons . - 建立一個新的
Tokenizer,命名為en_tok_new,詞彙表大小為 50,並將超出詞彙字詞的替代記號設為UNK。 - 將新的 tokenizer 擬合在
en_text資料上。 - 使用
en_tok_new將句子she likes grapefruit , peaches , and lemons .轉換成序列。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Convert the sentence to a word ID sequence
seq = ____.____(['she likes grapefruit , peaches , and lemons .'])
print('Word ID sequence: ', seq)
# Define a tokenizer with vocabulary size 50 and oov_token 'UNK'
en_tok_new = ____(num_words=____, ____=____)
# Fit the tokenizer on en_text
en_tok_new.____(____)
# Convert the sentence to a word ID sequence
seq_new = en_tok_new.____(['she likes grapefruit , peaches , and lemons .'])
print('Word ID sequence (with UNK): ', seq_new)
print('The ID 1 represents the word: ', en_tok_new.index_word[1])