WordPiece 和 BPE 的区别
生活随笔
收集整理的這篇文章主要介紹了
WordPiece 和 BPE 的区别
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
總結
傳統詞表示方法沒有辦法很好地處理未知或罕見的詞匯。
WordPiece 和 BPE 是兩種子詞切分算法,兩者非常相似。
WordPiece用于BERT,DistilBERT。
BPE使用RoBERTa。
BPE和WordPiece的區別在于如何選擇兩個子詞進行合并。
BPE的詞表創建過程:
反復2和3,直到詞表大小達到指定大小。
WordPiece是貪心的最長匹配搜索算法。基本流程:
首先初始化詞表,詞表包含了訓練數據中出現的所有字符。
2. 然后兩兩拼接字符,統計字符對加入詞表后對語言模型的似然值的提升程度。
3. 選擇提升語言模型似然值最大的一組字符對加入詞表中。
反復2和3,直到詞表大小達到指定大小。
- 什么是語言模型的似然值以及如何統計似然值的提升程度(Byte Pair Encoding and WordPiece Model自詞算法詳解):
概率: 一件事發生的可能性
似然性:與概率相反,一件事已經發生,反推在什么情況下,這件事發生的概率最大
似然函數可以表示為:
互信息:可以看做一個隨機變量中包含的關于另一個隨機變量的信息量,或者說,一個隨機變量由于已知另一個隨機變量而減少的不確定性。
官網闡述BPE和WordPiece的區別于聯系:
鏈接
WordPiece實現源碼
def tokenize(self, text):"""Tokenizes a piece of text into its word pieces.This uses a greedy longest-match-first algorithm to perform tokenizationusing the given vocabulary.For example:input = "unaffable"output = ["un", "##aff", "##able"]Args:text: A single token or whitespace separated tokens. This should havealready been passed through `BasicTokenizer`.Returns:A list of wordpiece tokens."""def whitespace_tokenize(text):"""Runs basic whitespace cleaning and splitting on a peice of text."""text = text.strip()if not text:return []tokens = text.split()return tokensoutput_tokens = []for token in whitespace_tokenize(text):chars = list(token)if len(chars) > self.max_input_chars_per_word: #max_input_chars_per_word 默認為 100output_tokens.append(self.unk_token) # 單詞長度大于最大長度,用[UNK]表示單詞continueis_bad = Falsestart = 0sub_tokens = []while start < len(chars):end = len(chars)cur_substr = Nonewhile start < end: # 貪心的最長匹配搜索 end從最后一位往前遍歷,每移動一位,判斷start:end是否存在于詞表中substr = "".join(chars[start:end])if start > 0: # 若子詞不是從位置0開始,前面要加“##”substr = "##" + substrif substr in self.vocab:cur_substr = substrbreakend -= 1if cur_substr is None: #沒有在詞表中出現的子詞,breakis_bad = Truebreaksub_tokens.append(cur_substr)start = end # 從上一子詞的后一位開始下一輪遍歷if is_bad: #沒有在詞表中出現的子詞(單詞中的任何區域),用[unk]表示該詞:比如“wordfi”,首先確定“word”為子詞,后發現“fi”不存在在詞表中,則最終用[UNK]表示“wordfi”output_tokens.append(self.unk_token)else:output_tokens.extend(sub_tokens)return output_tokens總結
以上是生活随笔為你收集整理的WordPiece 和 BPE 的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解析Google地图的Search接口返
- 下一篇: 计算机之父童年的故事ppt,24计算机之