tokenizers in Transformers:BPE、WordPiece,SentencePiece
目錄
1. space、punctuation、rule-based tokenization
2. Subword tokenization
2.1 Byte-Pair Encoding (BPE)
Byte-level BPE
2.2 WordPiece
2.3 Unigram
2.4 SentencePiece:ALBERT,XLNet,Marian和T5
tokenizing a text是將文本分為words或subwords,然后通過look-up table將其轉換為ID。我們將研究Transformers中使用的三種主要分詞器類型:字節對編碼(BPE),WordPiece和SentencePiece,并顯示每種模型使用哪種分詞器類型的示例。
鏈接:Summary of the tokenizers
1. space、punctuation、rule-based tokenization
space、punctuation、rule-based tokenization:?word tokenization,?即splitting sentences into words.
優點:將文本分成較小chunks的最直觀的方法;
缺點:使用所有唯一的words和tokens,通常會使vocabulary很大。Such a big vocabulary size forces the model to have an enormous embedding matrix as the input and output layer, which causes both an increased memory and time complexity. In general, transformers models rarely have a vocabulary size greater than 50,000, especially if they are pretrained only on a single language.
例如,Transformer XL使用空格和標點符號化,詞匯量為267,735!
2. Subword tokenization
Subword tokenization是word-level 和 character-level tokenization的混合體。
Subword tokenization算法基于以下原則:不應將常用詞分解為較小的子詞,而應將稀有詞分解為有意義的子詞,即frequently used words should not be split into smaller subwords, but rare words should be decomposed into meaningful subwords.
from transformers import BertTokenizer tokenizer = BertTokenizer.from_pretrained("bert-base-uncased") tokenizer.tokenize("I have a new GPU!")[“i”, “have”, “a”, “new”, “gp”, “##u”, “!”]
The tokenizer splits?“gpu”?into known subwords:?[“gp”?and?“##u”].?
"##":that the rest of the token should be attached to the previous one, without space (for decoding or reversal of the tokenization)
SentencePiece:?
from transformers import XLNetTokenizer tokenizer = XLNetTokenizer.from_pretrained("xlnet-base-cased") tokenizer.tokenize("Don't you love Transformers? We sure do.")[“▁Don”, “’”, “t”, “▁you”, “▁love”, “▁”, “”, “▁”, “Transform”, “ers”, “?”, “▁We”, “▁sure”, “▁do”, “.”]
2.1 Byte-Pair Encoding (BPE)
學習資料:BPE論文
BPE依賴于pre-tokenizer,該pre-tokenizer將訓練數據分成單詞, Pretokenization可以像空格分詞器一樣簡單。
After pre-tokenization, a set of unique words has been created and the frequency of each word it occurred in the training data has been determined. Next, BPE creates a base vocabulary consisting of all symbols that occur in the set of unique words and learns merge rules to form a new symbol from two symbols of the base vocabulary. It does so until the vocabulary has attained the desired vocabulary size. Note that the desired vocabulary size is a hyperparameter to define before training the tokenizer.
Step1:pre-tokenization:創建一組唯一的單詞和在訓練數據中出現的每個單詞的頻率。如:
("hug", 10), ("pug", 5), ("pun", 12), ("bun", 4), ("hugs", 5)Step2:BPE創建一個base vocabulary(該詞匯表由一組出現在唯一詞中的所有符號組成);并學習合并規則(從基礎詞匯表的兩個符號中形成一個新符號), 這樣做直到詞匯表達到所需的詞匯量vocabulary size 為止。 注意,vocabulary size 是在訓練tokenizer之前定義的超參數。如:
Base vocabulary:["b",?"g",?"h",?"n",?"p",?"s",?"u"]
Splitting all words into symbols of the base vocabulary:("h" "u" "g", 10), ("p" "u" "g", 5), ("p" "u" "n", 12), ("b" "u" "n", 4), ("h" "u" "g" "s", 5)
然后,BPE計算每個可能的符號對的頻率,并選擇最頻繁出現的符號對。
假設BPE訓練將在此處停止,則將學習的合并規則應用于新單詞(只要這些新單詞不包括基本詞匯表中沒有的符號)。
the vocabulary size = the base vocabulary size + the number of merges, is a hyperparameter to choose.
For instance?GPT?has a vocabulary size of 40,478 since they have 478 base characters and chose to stop training after 40,000 merges.
Byte-level BPE
包含所有可能的基本字符的基本詞匯表可能非常大,例如 所有unicode字符都被視為基本字符。 為了擁有更好的基礎詞匯表,GPT-2使用字節作為基礎詞匯表,這是一個巧妙的技巧,可以強制基礎詞匯表的大小為256,同時確保詞匯表中包含每個基本字符。隨著一些額外的rules 來處理標點符號,GPT-2的標記生成器可以記號化每個文本,而不需要<UNK>符號。
GPT-2的詞匯量為50257,它對應于256個字節的基本標記,一個特殊的文本結尾標記以及通過50,000次合并學習的符號。
2.2 WordPiece
WordPiece本質還是用的BPE思想,是用于BERT,DistilBERT和Electra的subword分詞算法。
與BPE非常相似,WordPiece首先初始化一個詞匯表,包含訓練數據中存在的每個字符,然后逐步學習給定數量的合并規則。
與BPE相比,WordPiece不會選擇最頻繁的符號對,而是會選擇一種將訓練數據添加到詞匯表中的可能性最大化的符號對。最大化訓練數據的可能性等同于找到符號對,在所有符號對中,第一個符號后緊跟第二個符號的概率最大。例如。僅當“ ug”由“ u”,“ g”分割的概率大于任何其他符號對時,“ u” followed by“ g”才可以合并。直觀上,WordPiece與BPE稍有不同,它通過合并兩個符號來評估丟失的內容
2.3 Unigram
與BPE或WordPiece相比,Unigram將其基本詞匯表初始化為大量符號,并逐步修整每個符號以獲得較小的詞匯表。基本詞匯可以例如對應于所有pre-tokenized的單詞和最常見的substrings。?
 Unigram不能直接用于Transformers中的任何模型,但可以與SentencePiece結合使用。
2.4 SentencePiece
參考:https://github.com/google/sentencepiece
使用SentencePiece的模型示例包括ALBERT,XLNet,Marian和T5。
到目前為止描述的所有tokenization算法都具有相同的問題:假定輸入文本使用空格分隔單詞。 但是,并非所有語言都使用空格來分隔單詞。 一種可能的解決方案是使用特定于語言的pre-tokenizers,例如 XLM使用特定的中文、日文和泰文pre-tokenizer。 為了更廣泛地解決此問題:
SentencePiece將輸入視為原始輸入流,因此在要使用的字符集中包含空格 ;
然后,它使用BPE或unigram算法來構建適當的詞匯表。
XLNetTokenizer使用SentencePiece。 使用SentencePiece進行解碼非常容易,因為所有tokens都可以串聯在一起,并且用空格替換"▁"。 transformers庫中所有使用SentencePiece的模型都將它與unigram結合使用。
?
?
?
總結
以上是生活随笔為你收集整理的tokenizers in Transformers:BPE、WordPiece,SentencePiece的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: mp3lame linux 编译,uba
 - 下一篇: Android如何关闭硬件加速