NLP|NLP 中subword编码方式总结

NLP subword:
1.传统方法 空格token

  • 传统词表示方法无法很好的处理未知或罕见的词汇(OOV问题)
  • 传统词tokenization方法不利于模型学习词缀之间的关系
    • E.g. 模型学到的“old”, “older”, and “oldest”之间的关系无法泛化到“smart”, “smarter”, and “smartest”
2.byte pair encoding(BPE)
BPE(字节对)编码或二元编码是一种简单的数据压缩形式,其中最常见的一对连续字节数据被替换为该数据中不存在的字节
举个简单的例子:
如句子中存在low, lower,可以把low合并看成一个字符。
编码:
# 给定单词序列 [“the”, “highest”, “mountain”]# 假设已有排好序的subword词表 [“errrr”, “tain”, “moun”, “est”, “high”, “the”, “a”]# 迭代结果 "the" -> ["the"] "highest" -> ["high", "est"] "mountain" -> ["moun", "tain"]

解码:
# 编码序列 [“the”, “high”, “est”, “moun”, “tain”]# 解码序列 “the highest mountain”

  • 优点
    • 可以有效地平衡词汇表大小和步数(编码句子所需的token数量)。
  • 缺点
    • 基于贪婪和确定的符号替换,不能提供带概率的多个分片结果。
3.WordPiece WordPiece算法可以看作是BPE的变种。不同点在于,WordPiece基于似然概率生成新的subword而不是下一最高频字节对。
【NLP|NLP 中subword编码方式总结】choose the new word unit out of all possible ones that increase the likelihood on the training data the most when added to the model(如何去理解这句话,非常有意思)
4.Unigram Language Model Unigram 的概念
不使用历史信息,单词出现的顺序只和单词本身出现的概率有关:

举个例子:
i live in osaka . i am a graduate student . my school is in nara .

P(nara) = 1/20 = 0.05 P(i) = 2/20 = 0.1 P() = 3/20 = 0.15P(W=i live in nara . ) =0.1 * 0.05 * 0.1 * 0.05 * 0.15 * 0.15 = 5.625 * 10-7

对于未知的字符怎么办
  • 概率视为0
  • 忽略
  • 给定一个很小的概率
  • 猜测一下总单词数
unigram 在subword 中使用:
1.先设置一个比较大的subword数量。
2.固定subword数,使用EM算法计算每个subword出现的概率。计算每个单词出现的loss。按照单词的loss顺序排序,只保留80%的subword.保留character来保证不会出现OOV。
5.字LEVEL编码(中文) 由于中文的常用字有只有几千,中文通常直接用字来进行编码。但也有一些字几乎不单独出现,比如匍匐,葡萄,等等。有一些比较细致的编码会把他们认为是一个词。
Reference: https://zhuanlan.zhihu.com/p/86965595
https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/37842.pdf
https://arxiv.org/pdf/1804.10959.pdf

    推荐阅读