huggingface调用一些细节记录
huggingface调用一些细节记录
- Model Input
- Model Foward
- BertModel
写给我自己看的一些小细节,因为不是每天写代码,总是会忘
要多看文档!!!
Model Input
使用tokenizer进行分词、添加特殊token例如[cls] [sep]、添加attention mask等等操作
注意类的直接调用和类中函数调用好吗??我每次都来tokenizer()
和tokenizer.encode()
之间来回横跳不长记性
(本文都是用bert进行举例,其他预训练模型同理)
类的直接调用tokenizer()
:
encoded_dict = tokenizer("你是谁?", "我是你妈。")
print(encoded_dict)
结果:
{'input_ids': [101, 872, 3221, 6443, 8043, 102, 2769, 3221, 872, 1968, 511, 102],
'token_type_ids': [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}
注意:前传的时候无脑加入参数return_tensors="pt"
,省的还得自己转换为tensor
类中函数调用tokenizer.encode()
,直接返回input_ids
input_ids = tokenizer.encode("你是谁?", "我是你妈。")
print(input_ids)
结果:
[101, 872, 3221, 6443, 8043, 102, 2769, 3221, 872, 1968, 511, 102]
关于model input 单/双输入、对齐等细节,参考官方文档,事无巨细,生怕我不会用这个接口,谢谢宁
https://huggingface.co/docs/transformers/v4.23.1/en/glossary#feed-forward-chunking
关于tokenzier的基类说明,事无巨细了
https://huggingface.co/docs/transformers/v4.23.1/en/main_classes/tokenizer#transformers.PreTrainedTokenizer
Model Foward
BertModel
函数参数:
返回值:
一般也就用到last_hidden_state
,维度见上图
官方例子就很清楚了,copy一下:
https://huggingface.co/docs/transformers/v4.23.1/en/model_doc/bert#transformers.BertModel
from transformers import BertTokenizer, BertModel
import torchtokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
model = BertModel.from_pretrained("bert-base-uncased")inputs = tokenizer("Hello, my dog is cute", return_tensors="pt") # 自动转换为tensor
outputs = model(**inputs)last_hidden_states = outputs.last_hidden_state # 返回值取last_hidden_state
注意看这个pooler_output: 即为[cls]的表示
pooler_output (torch.FloatTensor of shape (batch_size, hidden_size)) — Last layer hidden-state of the first token of the sequence (classification token) after further processing through the layers used for the auxiliary pretraining task. E.g. for BERT-family of models, this returns the classification token after processing through a linear layer and a tanh activation function. The linear layer weights are trained from the next sentence prediction (classification) objective during pretraining.
再强调一下,仔细看文档!
huggingface调用一些细节记录相关推荐
- Java实现CRM项目过程中的细节记录(一)
CRM项目实现过程中的细节记录(一) 文章目录 CRM项目实现过程中的细节记录(一) 一.数据库相关细节 1. 表名 2. 表字段说明 3. 不使用主外键约束 4. 不使用主键自动增长 UUID 5. ...
- Liferay中使用portlet:resourceURL触发serveResource()方法调用的细节
引入: 大家在Portlet 开发中经常用到<portlet:resourceURL>,而大体上都会去调用相应的serveResource()方法,这个过程虽然大家都清楚,但是能弄明白这个 ...
- 准备离职,工作的一些细节记录
到年底了,不知道为什么招人的公司会变多,一般员工都会留到年终奖之后才走,不管怎样了,第一份工作做了刚好一年了,是时候好好朝着规划好的方向发展了,对于未来还是很看好Apple,意味着未来变成全职iOS开 ...
- linux的一些细节记录
1. malloc的实现方式 uclibc中,用户空间的malloc提供了三种实现方式: malloc malloc-simple malloc-standard 具体使用何种方式,取决于.confi ...
- python获取数据库的存储过程_python远程调用sqlserver存储过程记录
开发中需要python远程调用sqlserver存储过程.搜索了网上有两种方式,pymssql和pyodbc.开始采用第一种方式开发,不过无法拿到存储过程中的返回数据,后采用pyodbc开发,当然存储 ...
- vim的配置安装和Python安装细节记录20190109
vim: vim记得以前版本是8.0:现在最新的版本是8.1:而且是安装版本,不是已经编译好的版本:可以直接安装 需要git vundle安装 到bundle/vundle目录下: 安装插件的命令全部 ...
- 搭建基于asp.net的wcf服务,ios客户端调用的实现记录
一.写wcf 问题: 1.特定的格式 2.数据绑定 3.加密解密 二.发布到iis 问题: 1.访问权限问题,添加everyone权限 访问网站时:http://localhost/WebbUploa ...
- UWP开发细节记录:判断文件类型
StorageFile.ContentType 属性,是 string 类型,用来表示文件内容的 MIME 类型.例如,音乐文件可能有 "audio/mpeg" MIME 类型.( ...
- class类初始化之后调用赋值问题记录
class PWSTRDELL:def __init__(self, pw_str):#该方法在类实例化时会自动调用self.pw = pw_strself.strength_level = 0#通过 ...
最新文章
- css中overflow:hidden的属性 可能会导致js下拉菜单无法显示
- 三句代码调整进程优先级
- Java 在指定目录中查找文件
- LeetCode Week 4:第 31 ~ 40 题
- 输出四位完全平方数_leetcode279_go_完全平方数
- java 算法基础之三合并排序法
- SolidWorks有限元分析流程
- Hilbert变换及相关特征值
- ExpandableListQQ好友列表
- C++学习笔记4:编程练习一
- CentOS的下载与安装
- VxWorks 引导程序(BIOS/BootLoader/Bootrom)
- linux网桥--简介
- WiFi-ESP8266入门http(3-1)网页认证上网-post请求(原教程)
- 从零开始之驱动发开、linux驱动(二十五、framebuffer 子系统框架)
- 得到app文稿导出_得到app学习笔记作为知识付费者,如何把所学内容快速输出?...
- TI公司之DSP结构与芯片
- 从零搭建Spring Boot脚手架:开篇以及技术选型1
- 华为harmonyos 2.0,连接无限可能 华为HarmonyOS 2.0 正式发布
- 家庭监控系统实现(一) -- 硬件准备及picamera使用
热门文章
- RabbitMQ除开RPC的五种消模型----原生API
- 2017暑假训练国庆小假期总结
- 0001-环保局垃圾分类绿色低碳通用PPT模板免费下载
- 【NOIP2016模拟7.9】现世斩
- OpenWRT飞行计划
- 路由守卫的几种方法、全局守卫、路由独享守卫、组件内部守卫
- 时间序列:时间序列模型---随机游走过程(The Random Walk Process)
- javascript 计算两个坐标的距离 米_EXCEL VBA曲线坐标计算程序在工程测量中的应用...
- 2019即将毕业的小硕,谈谈关于找工作的经验和教训。
- 动态表头excel导出(java)