对transformers库不常用记录,方便回溯。
知识点
1. fast的含义
比如BertTokenizerFast
,use_fast
, 示例如下:
1
| AutoTokenizer.from_pretrained('hfl/chinese-electra-180g-small-discriminator', use_fast=True)
|
它的含义是使用rust加速速度。
嘿嘿,rust现在要进入linux内核了,恭喜恭喜。
2. tokenizer
比如常见的convert_ids_to_tokens
,encode
, encode_plus
等等,下面记录一种对句子对的使用方式.
完整例子可参考ne_bert_mrc.py。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
from transformers import AutoTokenizer, BertTokenizerFast
tokenizer = AutoTokenizer.from_pretrained('hfl/chinese-electra-180g-small-discriminator', use_fast=True) question = '南京天气怎么样' context = '我今天早上站在阳台看天空,今天南京天气很好!'
tokenized_examples = tokenizer( question, context, truncation="only_second", max_length=20, return_overflowing_tokens=True, return_offsets_mapping=True, padding="max_length", )
print(tokenized_examples) input_ids = tokenized_examples['input_ids'] token_type_ids = tokenized_examples['token_type_ids'] attention_masks = tokenized_examples['attention_mask'] offset_mappings = tokenized_examples['offset_mapping'] overflow_to_sample_mapping = tokenized_examples['overflow_to_sample_mapping'] for index, _input_ids in enumerate(input_ids): print('input_ids -> ', tokenizer.convert_ids_to_tokens(_input_ids)) print('token_type_ids -> ', token_type_ids[index]) print('attention_masks -> ', attention_masks[index]) print('offset_mappings -> ', offset_mappings[index]) print('overflow_to_sample_mapping -> ', overflow_to_sample_mapping[index])
|
可以自行改动这个例子,其中stride默认注释掉了,默认为0。