当前位置:网站首页>How transformers Roberta adds tokens
How transformers Roberta adds tokens
2022-06-25 02:33:00 【Vincy_ King】
1. Premise
Recently, with roberta The model needs to be added special tokens, But every time it runs GPU There will be a mistake ( There is also a pile of block)
And in the CPU An error will be reported if the error occurs 
I searched a lot of information on the Internet , It is said that if special tokens Or modified vocab.txt, You need to add model.resize_token_embeddings(len(tokenizer)), Otherwise, the dimension will be wrong , But it's not clear where to add it , It was just added to dataset Where to deal with , But it's still wrong .
2. Specific operation
Let's show it first roberta Folder 
added_tokens.json Put what needs to be added 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 Special tokens
{
"unk_token": "[UNK]", "sep_token": "[SEP]", "pad_token": "[PAD]", "cls_token": "[CLS]", "mask_token": "[MASK]"}
tokenizer_config.json discharge tokenizer Some configurations of
{
"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"}
stay bert Add... To the model code 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
So it's done ~
边栏推荐
- 产业互联网的概念里有「互联网」字眼,但却是一个和互联网并不关联的存在
- qt打包exe文件,解决“无法定位程序输入点_ZdaPvj于动态链接库Qt5Cored.dll”
- ERROR日志格式与注意点
- 做软件安全测试的作用,如何寻找软件安全测试公司出具报告?
- Network planning | [four network layers] knowledge points and examples
- [analysis of STL source code] functions and applications of six STL components (directory)
- [live review] battle code pioneer phase 7: how third-party application developers contribute to open source
- Computing service network: a systematic revolution of multi integration
- Left hand dreams right hand responsibilities GAC Honda not only pays attention to sales but also children's safety
- When they are in private, they have a sense of propriety
猜你喜欢

DDD concept is complex and difficult to understand. How to design code implementation model in practice?

【STL源码剖析】配置器(待补充)

It is said that Yijia will soon update the product line of TWS earplugs, smart watches and bracelets

背了八股文,六月赢麻了……

After reciting the eight part essay, I won the hemp in June

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制作ER过程(自定义)

How to quickly familiarize yourself with the code when you join a new company?
![Planification du réseau | [quatre couches de réseau] points de connaissance et exemples](/img/c3/d7f382409e99eeee4dcf4f50f1a259.png)
Planification du réseau | [quatre couches de réseau] points de connaissance et exemples
随机推荐
Qt中使用QDomDocument操作XML文件
psql 列转行
折叠屏将成国产手机分食苹果市场的重要武器
Resolution of cross reference in IDA
3 years of testing experience. I don't even understand what I really need on my resume. I need 20K to open my mouth?
Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro
E - Average and Median(二分)
centos7.3修改mysql默认密码_详解Centos7 修改mysql指定用户的密码
Distributed transaction solutions and code implementation
[STL source code analysis] configurator (to be supplemented)
Cusdis - lightweight, privacy first open source comment system | chain of the city
Test / development programmers, 30, do you feel confused? And where to go
leecode学习笔记-机器人走到终点的最短路径
Experience of epidemic prevention and control, home office and online teaching | community essay solicitation
LeetCode 210:课程表 II (拓扑排序)
How to monitor the log through the easycvr interface to observe the platform streaming?
Charles 抓包工具
Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro
算力服务网络:一场多元融合的系统革命
ARM汇编中的栈桢小结