当前位置:网站首页>Transformers Roberta如何添加tokens
Transformers Roberta如何添加tokens
2022-06-24 23:04:00 【Vincy_King】
1. 前提
最近用roberta模型需要添加special tokens,但每次运行在GPU上会报错(上面还有一堆的block)
而在CPU上则报错
网上搜了很多资料,说是如果增加了special tokens或是修改了vocab.txt,则需要加上model.resize_token_embeddings(len(tokenizer))
,不然维度会不对,但一直不太清楚加在哪里,刚开始加在了dataset处理的地方,但仍然报错。
2. 具体操作
先展示一下roberta文件夹
added_tokens.json放需要添加的tokens
{
"[CH-2]": 21133, "[CH-0]": 21131, "[CH-3]": 21134, "[CH-6]": 21137, "[CH-9]": 21140, "[CH-4]": 21135, "[CH-1]": 21132, "[CH-8]": 21139, "”": 21129, "</s>": 21130, "“": 21128, "[CH-5]": 21136, "[CH-7]": 21138}
special_tokens_map.json放特殊tokens
{
"unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]"}
tokenizer_config.json放tokenizer的一些的配置
{
"do_lower_case": true, "do_basic_tokenize": true, "never_split": null, "unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]", "tokenize_chinese_chars": true, "strip_accents": null, "special_tokens_map_file": "special_tokens_map.json", "name_or_path": "chinese-roberta-wwm-ext", "use_fast": true, "tokenizer_file": "tokenizer.json", "tokenizer_class": "BertTokenizer"}
在bert模型代码处添上self.bert.resize_token_embeddings(len(self.tokenizer))
class Model(nn.Module):
def __init__(self, config):
super(Model, self).__init__()
self.bert = BertModel.from_pretrained(config['bert_path'])
self.tokenizer = BertTokenizer.from_pretrained(config['bert_path'])
# self.tokenizer.add_tokens(self.new_tokens, special_tokens=True)
self.bert.resize_token_embeddings(len(self.tokenizer))
for param in self.bert.parameters():
param.requires_grad = True
这样就大功告成啦~
边栏推荐
- Convert string array to list collection
- Test / development programmers, 30, do you feel confused? And where to go
- NPM package publishing tutorial
- PSQL column to row
- LINQ 查询(3)
- 转行软件测试2年了,给还在犹豫的女生一点建议
- yarn : 无法加载文件 C:\Users\xxx\AppData\Roaming\npm\yarn.ps1,因为在此系统上禁止运行脚本
- Viewing MySQL password on Linux_ MySQL forgets password "suggestions collection" under Linux
- 1-6搭建Win7虚拟机环境
- Jetson nano from introduction to practice (cases: opencv configuration, face detection, QR code detection)
猜你喜欢
Test / development programmers, 30, do you feel confused? And where to go
Pit entry machine learning: I. Introduction
How to quickly familiarize yourself with the code when you join a new company?
华为、阿里等大厂程序员真的好找对象吗?
Use of hashcat
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source
记一次beego通过go get命令后找不到bee.exe的坑
I've been doing software testing for two years. I'd like to give some advice to girls who are still hesitating
[STL source code analysis] configurator (to be supplemented)
Application of TSDB in civil aircraft industry
随机推荐
leecode学习笔记-机器人走到终点的最短路径
【Proteus仿真】Arduino UNO+数码管显示4x4键盘矩阵按键
ProcessOn制作ER过程(自定义)
The Oracle 11g RAC cluster database cannot be started due to directory permission errors
Centos7.3 modifying MySQL default password_ Explain centos7 modifying the password of the specified user in MySQL
Intranet learning notes (7)
E - Average and Median(二分)
会自动化—10K,能做自动化—20K,你搞懂自动化测试没有?
Application of TSDB in civil aircraft industry
|遇到bug怎么分析,专业总结分析来了
计算机三级(数据库)备考题目知识点总结
[i.mx6ul] u-boot migration (VI) network driver modification lan8720a
Pit entry machine learning: I. Introduction
Folding screen will become an important weapon for domestic mobile phones to share the apple market
Talking about the advantages of flying book in development work | community essay solicitation
Software testing salary in first tier cities - are you dragging your feet
商城项目 pc----商品详情页
产业互联网的概念里有「互联网」字眼,但却是一个和互联网并不关联的存在
Please run IDA with elevated permissons for local debugging.
探索C语言程序奥秘——C语言程序编译与预处理