赞
踩
import numpy as np # 指定bert预训练模型文件夹路径 bert_path='your_bert_model_path' #加载BertTokenizer tokenizer = BertTokenizer.from_pretrained(bert_path) ''' 方式一,直接使用tokenizer进行序列化 ''' out_puts= tokenizer(['我','爱我'], ['的祖国','这是一种'], padding='max_length', return_tensors="pt", max_length=6) ''' { 'input_ids': tensor([[ 101, 2769, 102, 4638, 4862, 1744, 102, 0, 0, 0], [ 101, 4263, 2769, 102, 6821, 3221, 671, 4905, 102, 0]]), 'token_type_ids': tensor([[0, 0, 0, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 1, 0]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 0]])} ''' print(out_puts.input_ids) #方式二,使用encode进行序列化
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。