【transformers】tokenizer用法(encode、encode_plus、batch_encode_plus等等)

2023-11-06

tranformers中的模型在使用之前需要进行分词和编码,每个模型都会自带分词器(tokenizer),熟悉分词器的使用将会提高模型构建的效率。
string tokens ids 三者转换

  • string → tokens tokenize(text: str, **kwargs)
  • tokens → string convert_tokens_to_string(tokens: List[token])
  • tokens → ids convert_tokens_to_ids(tokens: List[token])
  • ids → tokens convert_ids_to_tokens(ids: int or List[int], skip_special_tokens=False)
  • string → ids encode(text, text_pair=None, add_special_tokens=True, padding=False, truncation=False, max_length=None, return_tensors=None)
    text:str, List[str], List[int].
    text_pair: str, List[str], List[int]
    add_special_tokens: bool.是否添加特殊token([CLS]、[SEP])
    max_length: int, None.
    padding: bool. padding取True or 'longest"时, padding至batch中最长的句子长度; padding取’max_length’时, padding至max_length; padding取False or ‘do_not_pad’ (default), 不padding.
    truncation: bool, str。只对输入为 sequence pair 有效。truncation取True or 'longest_first’时, token by token 的截断,哪一句长,截断哪一句的最后一个 token,相同长度就第二句。截至总token数等于 max_length; truncation取’only_first’时只截第一句,至总token数等于 max_length; truncation取’only_second’时只截第二句,至总token数等于 max_length;truncation取False or ‘do_not_truncate’ (default)。
    return_tensors: str, None。‘tf’, ‘pt’ or ‘np’ 分布表示不同的tensor type.
  • ids → string decode(token_ids: List[int], skip_special_tokens=False, clean_up_tokenization_spaces=True)
  • encode_plus:
    encode_plus(text, text_pair=None, add_special_tokens=True, padding=False, truncation=False, max_length=None, stride=0, is_pretokenized=False, pad_to_multiple_of=None, return_tensors=None, return_token_type_ids=None, return_attention_mask=None, return_overflowing_tokens=False, return_special_tokens_mask=False, return_offsets_mapping=False, return_length=False)
  • batch_encode_plus:
    输入为 encode 输入的 batch,其它参数相同。注意,plus 是返回一个字典。
  • batch_decode:
    输入是batch.
#这里以bert模型为例,使用上述提到的函数

from transformers import BertTokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
text = "It's a nice day today!"

#tokenize,#仅用于分词
seg_words = tokenizer.tokenize(text)  
print("tokenize分词结果:\n",seg_words)

#convert_tokens_to_ids,将token转化成id,在分词之后。
#convert_ids_to_tokens,将id转化成token,通常用于模型预测出结果,查看时使用。
seg_word_id = tokenizer.convert_tokens_to_ids(seg_words)  
print("tokenize Id:\n",seg_word_id)

#encode,进行分词和token转换,encode=tokenize+convert_tokens_to_ids
encode_text = tokenizer.encode(text)
print("encode结果:\n",encode_text)

#encode_plus,在encode的基础之上生成input_ids、token_type_ids、attention_mask
encode_plus_text = tokenizer.encode_plus(text)
print("encode_plus结果:\n",encode_plus_text)

#batch_encode_plus,在encode_plus的基础之上,能够批量梳理文本。
batch_encode_plus_text = tokenizer.batch_encode_plus([text,text])
print("batch_encode_plus结果:\n",batch_encode_plus_text)

在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【transformers】tokenizer用法(encode、encode_plus、batch_encode_plus等等) 的相关文章

随机推荐

  • PHP底层工作原理

    原文地址 http www cnblogs com phphuaibei archive 2011 09 13 2174927 html 最近搭建服务器 突然感觉lamp之间到底是怎么工作的 或者是怎么联系起来 平时只是写程序 重来没有思考
  • Java中数据类型详解

    文章目录 一 数据类型的作用 二 两种数据类型 1 基本数据类型 1 第一类 整数型 2 第二类 浮点型 3 第三类 布尔型 4 第四类 字符型 2 引用数据类型 1 类 2 数组 3 接口 三 基本数据类型之间的转化 一 数据类型的作用
  • Vue 项目如何实现一个全局菜单搜索框

    个人主页 山山而川 xyj 作者简介 前端领域新星创作者 专注于前端各领域技术 共同学习共同进步 一起加油 系列专栏 Vue 系列 学习格言 与其临渊羡鱼 不如退而结网 目录 前言 一 过滤路由 二 搜索框展示路由 三 雏形出现但有缺陷 四
  • 九宫格选择照片

    一 Adapter public class RecyclerImageAdapter extends RecyclerView Adapter
  • 使用IDEA创建JavaWeb项目

    由于看的视频教程比较老 且开发工具为Eclipse 本人已习惯使用IDEA 在此记录一下创建JavaWeb项目过程 创建一个项目 左侧选择 Java Enterprise 右侧选择 Web Application 这里我输入的项目名字为 F
  • 公开课精华

    本文章总结于大疆前技术总监 目前在卡内基梅隆大学读博的杨硕博士在深蓝学院的关于机器人的带约束轨迹规划的公开课演讲内容 全文约5000字 笔者不是机器人领域的 因此特地去了解了一下杨硕博士 深感佩服 不仅是他的履历 更多的是他关于学术上的至臻
  • 2021-09-15 C++ 继承和多态(虚函数,纯虚函数,虚继承)

    C 继承和多态 虚函数 纯虚函数 虚继承 一 继承 继承的概念 为了代码的复用 保留基类的原始结构 并添加派生类的新成员 继承的本质 代码复用 我们用下图解释下 那么我们这里就可以提出几个问题了 进程的方式有哪些呢 这里有三种继承方式 pu
  • Vue3 实现背景水印功能

    在 web 的世界里 对于图片文档等增加水印处理是十分有必要的 水印的添加根据环境可以分为两大类 前端浏览器环境添加和后端服务环境添加 通过 canvas 创建一张含有水印信息的背景图片 通过 hooks 函数插入到页面中 对外暴露方法 设
  • pnpm安装与卸载

    文章目录 1 安装 1 1 Linux CentOS 1 1 1 使用脚本直接安装 1 2 2 通过npm安装 2 卸载 2 1 移除全局安装的包 2 2 移除pnpm cli 2 2 1 脚本直接安装 2 2 2 使用npm安装 官方文档
  • Mysql主从库不同步1236错误:could not find first log file name in binary....

    Mysql主从库不同步1236错误 could not find first log file name in binary log index file错误是主从的一个日志问题 我们只要简单的配置一下即可解决 最近造成Mysql主从库不同
  • 二进制运算基础

    本篇随笔仅作记录 文中有引用的一篇博客 博客地址为 https www cnblogs com joahyau p 6420619 html 首先谈一下为何会写这个知识点 这是由一道题引出这个知识点的 题目如下 System out pri
  • 在zotero-better-notes新版本里的笔记中插入模板的方法

    在zotero better notes新版本里的笔记中插入模板的方法 一 失效的方法 二 新的方法 三 模板分享 四 模板导入方法 一 失效的方法 上一篇文章Zotero笔记插件zotero better notes的基本使用方法与下载
  • MySQL主从复制原理、半同步操作步骤及原理

    原文地址 http blog csdn net linuxlsq article details 52606292 1 1 企业Linux运维场景数据同步方案 1 1 1 文件级别的异机同步方案 1 scp sftp nc 命令可以实现远程
  • iOS是伪多任务?

    iOS是伪多任务 Android系统原生就支持多任务 而iOS系统是从4开始的 它的多任务并不是传统意义上的多任务 不是任何应用进程都可以在后台自由运行 真正做到支持多任务的只有苹果Safari和Mail 很多苹果亲生的应用也都并不支持 对
  • TensorRT学习笔记3 - 运行sampleMNIST

    目录 步骤 遇到的问题 步骤 cd your tensorrt path samples sampleMNIST make cd your tensorrt path bin sudo sample mnist h datadir your
  • 多分支git合并流程

    阅读摘要 推荐一个git合并步骤 开发分支可能会多次提交合并到dev master主干分支也会显示很多个提交点 这样不方便代码分支管理和回溯发布记录 所以推荐如下方法 不出意外 这也是个新手教程 git 合并步骤 本地开发分支建立格式建议
  • 2023年电工杯

    电工数学建模竞赛 中国电机工程学会杯 全国大学生电工数学建模竞赛是全国性大学生学科竞赛活动 目的在于按照紧密结合教学实际 着重基础 注重前沿的原则 促进电气类专业建设 引导学生注重动手能力 创新能力和协作精神的培养 提高学生针对实际问题进行
  • 怎么解决idea版本不兼容?

    怎么解决2020 3 1版本的idea的jdk不兼容 希望好心的大哥哥回答 在线等 挺急的 谢谢
  • springboot 框架学习 thymeleaf静态模板引擎实现页面跳转

    最近在深啃jvm虚拟机 主要方法是阅读 深入java虚拟机 这本书 写的很形象深入 简单易懂 今天上web课程老师讲到servlet时突然想到boot的页面跳转与数据响应 之前的springmvc在boot中也有集成 但是boot的常用页面
  • 【transformers】tokenizer用法(encode、encode_plus、batch_encode_plus等等)

    tranformers中的模型在使用之前需要进行分词和编码 每个模型都会自带分词器 tokenizer 熟悉分词器的使用将会提高模型构建的效率 string tokens ids 三者转换 string tokens tokenize te