当前位置:网站首页>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
这样就大功告成啦~
边栏推荐
- QT package the EXE file to solve the problem that "the program input point \u zdapvj cannot be located in the dynamic link library qt5cored.dll"
- 调用系统函数安全方案
- 分布式事务解决方案和代码落地
- Processon producer process (customized)
- 如何通过EasyCVR接口监测日志观察平台拉流情况?
- Software testing salary in first tier cities - are you dragging your feet
- 同花顺是正规平台吗?同花顺开户安全吗
- How to get the picture outside the chain - Netease photo album [easy to understand]
- js正则匹配数字、大小写字母、下划线、中线和点[通俗易懂]
- I've been doing software testing for two years. I'd like to give some advice to girls who are still hesitating
猜你喜欢

The role of software security testing, how to find a software security testing company to issue a report?

文件系统 -- 磁盘基础知识和FAT32文件系统详细介绍

Experience of epidemic prevention and control, home office and online teaching | community essay solicitation

Redis

【Proteus仿真】Arduino UNO+数码管显示4x4键盘矩阵按键

ProcessOn制作ER过程(自定义)

入坑机器学习:一,绪论

random list随机生成不重复数

消息称一加将很快更新TWS耳塞、智能手表和手环产品线

Intranet learning notes (5)
随机推荐
【Proteus仿真】Arduino UNO+继电器控制照明设备
转行软件测试2年了,给还在犹豫的女生一点建议
[analysis of STL source code] functions and applications of six STL components (directory)
Explanation of FTP protocol
Computing service network: a systematic revolution of multi integration
qt打包exe文件,解决“无法定位程序输入点_ZdaPvj于动态链接库Qt5Cored.dll”
Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (1) -- migrate data to node 1
Use of hashcat
数据库系统概论必背知识
疫情防控,居家办公,网上授课之心得 | 社区征文
EasyCVR国标协议接入的通道,在线通道部分播放异常是什么原因?
【Proteus仿真】Arduino UNO+数码管显示4x4键盘矩阵按键
jwt
Summary of knowledge points of computer level III (database) test preparation topics
做软件安全测试的作用,如何寻找软件安全测试公司出具报告?
【第26天】给定 n 个元素的升序数组nums,求实现一个函数在nums中寻找target的下标 | 初识二分查找
[day 26] given the ascending array nums of n elements, find a function to find the subscript of target in nums | learn binary search
Post competition summary of kaggle patent matching competition
LINQ 查询(3)
产业互联网的概念里有「互联网」字眼,但却是一个和互联网并不关联的存在