笔记
Transformers==4.0.0
由于每次调用bert等模型都需要使用模型的tokenizer,所以写个笔记,方便自己以及后人查阅,(其实看官方文档也可以,但是英文的看着头疼。有错误也请留言吧。
base : PreTrainedTokenizerBase
作为基类,该类有着所有tokenizer都具有的方法还有属性。
PreTrainedTokenizer
使用multiprocess 加速tokenize
仿照这个就完事了。partial
可以固定函数中的参数,简直是专门为多进程准备的。
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
| def squad_convert_example_to_features_init(tokenizer_for_convert: PreTrainedTokenizerBase): global tokenizer tokenizer = tokenizer_for_convert
features = []
threads = min(threads, cpu_count()) with Pool(threads, initializer=squad_convert_example_to_features_init, initargs= (tokenizer,)) as p: annotate_ = partial( tokenzier.encode_plus, max_seq_length=max_seq_length, doc_stride=doc_stride, max_query_length=max_query_length, padding_strategy=padding_strategy, is_training=is_training, ) features = list( tqdm( p.imap(annotate_, examples, chunksize=32), total=len(examples), desc="convert squad examples to features", disable=not tqdm_enabled, ) )
|