当前位置: 首页 > news >正文

huggingFace学习之编码工具

1.背景

transformers框架学习

2.代码

2.1.加载编码工具

# 第2章/加载编码工具
from transformers import BertTokenizer# 加载编码工具
tokenizer = BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-base-chinese',cache_dir=None,force_download=False,
)#print(tokenizer)# 第2章/准备实验数据
sents = ['你站在桥上看风景','看风景的人在楼上看你','明月装饰了你的窗子','你装饰了别人的梦',
]# 第2章/基本的编码函数
out = tokenizer.encode(text=sents[2],text_pair=sents[3],# 当句子长度大于max_length时截断truncation=True,# 一律补PAD,直到max_length长度padding='max_length',add_special_tokens=True,max_length=25,return_tensors=None,
)
# 编码
print(out)
# 解码
print(tokenizer.decode(out))
'''
[101, 3209, 3299, 6163, 7652, 749, 872, 4638, 4970, 2094, 102, 872, 6163, 7652, 749, 1166, 782, 4638, 3457, 102, 0, 0, 0, 0, 0]
[CLS] 明 月 装 饰 了 你 的 窗 子 [SEP] 你 装 饰 了 别 人 的 梦 [SEP] [PAD] [PAD] [PAD] [PAD] [PAD]'''

2.2.进阶编码函数

# 第2章/加载编码工具
from transformers import BertTokenizer# 加载编码工具
tokenizer = BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-base-chinese',cache_dir=None,force_download=False,
)# print(tokenizer)# 第2章/准备实验数据
sents = ['你站在桥上看风景','看风景的人在楼上看你','明月装饰了你的窗子','你装饰了别人的梦',
]# 第2章/进阶的编码函数
out = tokenizer.encode_plus(text=sents[0],text_pair=sents[1],# 当句子长度大于max_length时截断truncation=True,# 一律补零,直到max_length长度padding='max_length',max_length=25,add_special_tokens=True,# 可取值tf、pt、np,默认为返回listreturn_tensors=None,# 返回token_type_idsreturn_token_type_ids=True,# 返回attention_maskreturn_attention_mask=True,# 返回special_tokens_mask 特殊符号标识return_special_tokens_mask=True,# 返回length 标识长度return_length=True,
)
# input_ids 编码后的词
# token_type_ids 第1个句子和特殊符号的位置是0,第2个句子的位置是1
# special_tokens_mask 特殊符号的位置是1,其他位置是0
# attention_mask PAD的位置是0,其他位置是1
# length 返回句子长度
'''
input_ids : [101, 872, 4991, 1762, 3441, 677, 4692, 7599, 3250, 102, 4692, 7599, 3250, 4638, 782, 1762, 3517, 677, 4692, 872, 102, 0, 0, 0, 0]
token_type_ids : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
special_tokens_mask : [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
attention_mask : [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
length : 25
'''
for k, v in out.items():print(k, ':', v)str = tokenizer.decode(out['input_ids'])
# [CLS] 你 站 在 桥 上 看 风 景 [SEP] 看 风 景 的 人 在 楼 上 看 你 [SEP] [PAD] [PAD] [PAD] [PAD]
print(str)

2.3.批量编码

# 第2章/加载编码工具
from transformers import BertTokenizer# 加载编码工具
tokenizer = BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-base-chinese',cache_dir=None,force_download=False,
)# print(tokenizer)# 第2章/准备实验数据
sents = ['你站在桥上看风景','看风景的人在楼上看你','明月装饰了你的窗子','你装饰了别人的梦',
]# 第2章/批量编码成对的句子
out = tokenizer.batch_encode_plus(# 编码成对的句子batch_text_or_text_pairs=[(sents[0], sents[1]), (sents[2], sents[3])],add_special_tokens=True,# 当句子长度大于max_length时截断truncation=True,# 一律补零,直到max_length长度padding='max_length',max_length=25,# 可取值tf、pt、np,默认为返回listreturn_tensors=None,# 返回token_type_idsreturn_token_type_ids=True,# 返回attention_maskreturn_attention_mask=True,# 返回special_tokens_mask 特殊符号标识return_special_tokens_mask=True,# 返回offsets_mapping 标识每个词的起止位置,这个参数只能BertTokenizerFast使用# return_offsets_mapping=True,# 返回length 标识长度return_length=True,
)
# input_ids 编码后的词
# token_type_ids 第1个句子和特殊符号的位置是0,第2个句子的位置是1
# special_tokens_mask 特殊符号的位置是1,其他位置是0
# attention_mask PAD的位置是0,其他位置是1
# length 返回句子长度
for k, v in out.items():print(k, ':', v)str = tokenizer.decode(out['input_ids'][0])
print(str)
'''
input_ids : [[101, 872, 4991, 1762, 3441, 677, 4692, 7599, 3250, 102, 4692, 7599, 3250, 4638, 782, 1762, 3517, 677, 4692, 872, 102, 0, 0, 0, 0], [101, 3209, 3299, 6163, 7652, 749, 872, 4638, 4970, 2094, 102, 872, 6163, 7652, 749, 1166, 782, 4638, 3457, 102, 0, 0, 0, 0, 0]]
token_type_ids : [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]]
special_tokens_mask : [[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]]
length : [21, 20]
attention_mask : [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]]
[CLS] 你 站 在 桥 上 看 风 景 [SEP] 看 风 景 的 人 在 楼 上 看 你 [SEP] [PAD] [PAD] [PAD] [PAD]
'''

3.4.自定义字典

# 第2章/加载编码工具
from transformers import BertTokenizer# 加载编码工具
tokenizer = BertTokenizer.from_pretrained(pretrained_model_name_or_path='bert-base-chinese',cache_dir=None,force_download=False,
)# print(tokenizer)
print('使用默认字典时...........')
# 第2章/编码新添加的词
out = tokenizer.encode(text='明月装饰了你的窗子[EOS]',text_pair=None,# 当句子长度大于max_length时截断truncation=True,# 一律补PAD,直到max_length长度padding='max_length',add_special_tokens=True,max_length=10,return_tensors=None,
)
print('默认字典:', out)
print('默认字典:', tokenizer.decode(out))# 第2章/获取字典
vocab = tokenizer.get_vocab()
print('默认:', type(vocab), len(vocab), '明月' in vocab)# 第2章/添加新词
tokenizer.add_tokens(new_tokens=['明月', '装饰', '窗子'])# 第2章/添加新符号
tokenizer.add_special_tokens({'eos_token': '[EOS]'})vocab = tokenizer.get_vocab()
print('自定义:', type(vocab), len(vocab), '明月' in vocab)print('使用自定义字典...........')
# 第2章/编码新添加的词
out = tokenizer.encode(text='明月装饰了你的窗子[EOS]',text_pair=None,# 当句子长度大于max_length时截断truncation=True,# 一律补PAD,直到max_length长度padding='max_length',add_special_tokens=True,max_length=10,return_tensors=None,
)
print('自定义字典:', out)
print('自定义字典:', tokenizer.decode(out))
'''
使用默认字典时...........
默认字典: [101, 3209, 3299, 6163, 7652, 749, 872, 4638, 4970, 102]
默认字典: [CLS] 明 月 装 饰 了 你 的 窗 [SEP]
默认: <class 'dict'> 21128 False
自定义: <class 'dict'> 21132 True
使用自定义字典...........
自定义字典: [101, 21128, 21129, 749, 872, 4638, 21130, 21131, 102, 0]
自定义字典: [CLS] 明月 装饰 了 你 的 窗子 [EOS] [SEP] [PAD]
'''

完美


文章转载自:

http://WxLg7Y8W.frmmp.cn
http://gc50wdES.frmmp.cn
http://0meyKHDP.frmmp.cn
http://5bl2qP93.frmmp.cn
http://48JnlpHY.frmmp.cn
http://AoaY4D9k.frmmp.cn
http://ONczqzQP.frmmp.cn
http://XvdOLGL1.frmmp.cn
http://TEx5PqvK.frmmp.cn
http://0E4JHH2H.frmmp.cn
http://rqrBJyvr.frmmp.cn
http://FVJg5VqL.frmmp.cn
http://gkA1RcfC.frmmp.cn
http://S0CdGIiW.frmmp.cn
http://7sdbcatm.frmmp.cn
http://kWW9l2BY.frmmp.cn
http://Ux1iYoF2.frmmp.cn
http://lf3gu2uF.frmmp.cn
http://8CZYAV45.frmmp.cn
http://esJGIlab.frmmp.cn
http://Z0B2ZvJP.frmmp.cn
http://lLT0y3oX.frmmp.cn
http://KsvXAlxs.frmmp.cn
http://5KsD8cVg.frmmp.cn
http://QPGRxeq9.frmmp.cn
http://g0SU3mBs.frmmp.cn
http://Jf4GDKrb.frmmp.cn
http://ny6lmaGo.frmmp.cn
http://Snu1j4YW.frmmp.cn
http://FbsgYqAs.frmmp.cn
http://www.dtcms.com/a/375919.html

相关文章:

  • 人工智能期末复习(部分)
  • 【Pytorch】2025 Pytorch基础入门教程(完整详细版)
  • Cookie 与 Session 的关系详解
  • Java微服务架构拆分:边界原则的实战破局与多场景案例解析
  • expect脚本详解
  • 交通识别摄像头以及带AI算法
  • SpringMVC通过注解实现全局异常处理
  • Linux基础知识(四)
  • 向量化与嵌入模型:RAG系统背后的隐形英雄
  • 你知道zip()和zip(*)怎么用吗?
  • 工业领域企业CRM常用的有哪些系统?
  • Git cherry-pick 与分支重置技术实现代码健全性保障下的提交记录精简
  • 【Nginx 运维实战】版本替换:强制 vs 平滑升级全解析
  • HTTPS加解密流程解析
  • Android 升级minSdkVersion 导致 包体积变大的处理
  • Linux系统 Python3.12版本连接达梦数据库dmPython和django_dmPython
  • 零知开源——ESP32驱动OV7670摄像头实现简易照相机系统
  • 前端开发工具trae的使用
  • Coze源码分析-资源库-创建插件-前端源码-核心组件
  • 数据集成平台怎么选?从ETL到CDC再到iPaaS的全景对比
  • 【Linux基础】Linux系统配置IP详解:从入门到精通
  • 2025版基于springboot的企业考勤管理系统
  • 【计算机毕业设计选题】2025-2026年计算机毕业设计选题经验与项目推荐
  • Python数据处理管道完全指南:从基础到高并发系统实战
  • VMware安装CentOS 7教程
  • SpringBoot + MinIO/S3 文件服务实现:FileService 接口与 FileServiceImpl 详解
  • 如何确定丝杆升降机的额定负载和峰值负载?
  • AI 与 Web3 技术写作大赛,瓜分 2000RMB
  • git 合并多条commit
  • 联邦学习指导、代码、实验、创新点