tokenizer.tokenize(), tokenizer.encode() , tokenizer.encode_plus() 方法介绍及其区别

Posted GD_Wang1206

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tokenizer.tokenize(), tokenizer.encode() , tokenizer.encode_plus() 方法介绍及其区别相关的知识,希望对你有一定的参考价值。

测试代码

from transformers import BertTokenizer
# BertTokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')  # bert分词器

sentence = "i am overheat"
encode_ids = tokenizer.encode(sentence) # encode 默认为True 加[CLS][SEP]
encode_words = tokenizer.convert_ids_to_tokens(tokenizer.encode(sentence))   # encode 默认为True 加[CLS][SEP]


print(f"word_list   : sentence.split()")                 # 单词列表 (不进行分词)
print(f"tokenize    : tokenizer.tokenize(sentence) ")    # 单词列表 (进行分词)
print(f"encode_words: encode_words")                     # 单词列表 (进行分词) [CLS]+sentence+[SEP]
print(f"encode_ids  : tokenizer.encode(sentence)")       # 词id列表 进行分词   101 + ids + 102
print(f"encode_plus : tokenizer.encode_plus(sentence)")  # dict 类型 三个key:value, input_ids:词id列表(进行分词) token_type_ids:分句列表0(分句) attention_mask:掩码列表1(掩码)
print("=" * 100)

encode_words_true =  tokenizer.encode(sentence, add_special_tokens=True)    # encode 默认为True 加[CLS][SEP]
encode_words_false = tokenizer.encode(sentence, add_special_tokens=False)  # encode False    不加[CLS][SEP]
print(f"encode_words_true : encode_words_true")
print(f"encode_words_false: encode_words_false")

运行结果:

1. 总结

三个方法的输入都是字符串: "i am overheat"

1.1 tokenizer.tokenize() 方法

输入: str 字符串
输出: str_list 词列表(进行了wordpiece分词的)

['i', 'am', 'over', '##hea', '##t']  

1.2 tokenizer.encode() 方法

输入: str 字符串
输出: int_list id列表 开始和末尾分别添加了[CLS] [SEP]的词id 101, 102

[101, 1045, 2572, 2058, 20192, 2102, 102]

可以通过tokenizer.convert_ids_to_tokens转化为token列表 str_list

['[CLS]', 'i', 'am', 'over', '##hea', '##t', '[SEP]']

add_special_tokens=True 默认为True 表示加不加[CLS][SEP]这两个词id

1.3 tokenizer.encode_plus() 方法

输入: str 字符串
输出: 字典 input_ids就是encode的返回值, token_type_ids用于分句, attention_mask 用于掩码

'input_ids': [101, 1045, 2572, 2058, 20192, 2102, 102], 'token_type_ids': [0, 0, 0, 0, 0, 0, 0], 'attention_mask': [1, 1, 1, 1, 1, 1, 1]

’input_ids: 是单词在词典中的编码
‘token_type_ids’:区分两个句子的编码(上句全为0,下句全为1)
‘attention_mask’:指定对哪些词进行self-Attention操作
offset_mapping:记录了 每个拆分出来 的内容(token)都 对应着原来的句子的位置

2.区别

2.1 tokenizer.tokenize() 和 tokenizer.encode() 区别

tokenizer.tokenize() 返回词列表 默认首尾不加 [CLS] [SEP]
okenizer.encode() 返回词id列表 默认首尾加 [CLS] [SEP]对应的词id

2.2 tokenizer.encode() 和 tokenizer.encode_plus() 区别

返回类型不同
tokenizer.encode() 返回 词id列表
tokenizer.encode_plus() 返回 dict类型 其中input_ids 就是 tokenizer.encode() 的返回值, 还有用于分句和掩码的其他两个id

参考博客: https://blog.csdn.net/qq_25850819/article/details/115355858

Scikit Learn 中的 CountVectorizer

【中文标题】Scikit Learn 中的 CountVectorizer【英文标题】:CountVectorizer in Scikit Learn 【发布时间】:2020-05-05 05:28:30 【问题描述】:

我不确定在创建 CountVectorizer 类的实例时,它们之间有什么区别 vectorizer = CountVectorizer(tokenizer=word_tokenize)vectorizer = CountVectorizer

请帮我说清楚。 感谢您的时间。

【问题讨论】:

【参考方案1】:

默认情况下,CountVectorizer 不会标记输入。标记化是对输入字符串的部分进行划分和可能分类的过程。换句话说,它将像'This is the input' 这样的长字符串变成了一个序列:

['This', 'is', 'the', 'input']

如果您使用可调用参数指定 tokenizer 参数 在CountVectorizer 中,它将使用此函数来标记输入(source)。

【讨论】:

感谢您的澄清

以上是关于tokenizer.tokenize(), tokenizer.encode() , tokenizer.encode_plus() 方法介绍及其区别的主要内容,如果未能解决你的问题,请参考以下文章

在令牌 TOK_TMP_FILE 附近遇到 Hive 查询错误

C指针原理(12)-C指针基础

Python Web Scraping 从 url 保存 Tik Tok 视频

国货出海,教你如何通过Tik Tok卖爆全球

org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next tok

抖音国际版Tik Tok进入IPFS存储网络 Filecon生态进一步扩大