resize_token_embeddings在具有不同嵌入尺寸的pretrain模型上
我想询问改变训练有素模型的嵌入尺寸的方法。
我有一个训练有素的模型型号/BERT-PRETRAIN-1-Step-5000.pkl
。 现在,我将新的令牌[tra]
添加到令牌,并尝试将resize_token_embeddings
添加到有关的resize_token_embeddings
。
from pytorch_pretrained_bert_inset import BertModel #BertTokenizer
from transformers import AutoTokenizer
from torch.nn.utils.rnn import pad_sequence
import tqdm
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
model_bert = BertModel.from_pretrained('bert-base-uncased', state_dict=torch.load('models/BERT-pretrain-1-step-5000.pkl', map_location=torch.device('cpu')))
#print(tokenizer.all_special_tokens) #--> ['[UNK]', '[SEP]', '[PAD]', '[CLS]', '[MASK]']
#print(tokenizer.all_special_ids) #--> [100, 102, 0, 101, 103]
num_added_toks = tokenizer.add_tokens(['[TRA]'], special_tokens=True)
model_bert.resize_token_embeddings(len(tokenizer)) # --> Embedding(30523, 768)
print('[TRA] token id: ', tokenizer.convert_tokens_to_ids('[TRA]')) # --> 30522
但是我遇到了这个错误:
AttributeError: 'BertModel' object has no attribute 'resize_token_embeddings'
我认为这是因为model_bert(bert-pretrain-1-step-5000.pkl)
我的嵌入式大小不同。 我想知道是否有任何方法可以适应我的修改后令牌的嵌入尺寸以及我想用作初始权重的型号。
多谢!!
I would like to ask about the way to change the embedding size of the trained model.
I have a trained model models/BERT-pretrain-1-step-5000.pkl
.
Now I am adding a new token [TRA]
to the tokeniser and try to use the resize_token_embeddings
to the pertained one.
from pytorch_pretrained_bert_inset import BertModel #BertTokenizer
from transformers import AutoTokenizer
from torch.nn.utils.rnn import pad_sequence
import tqdm
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
model_bert = BertModel.from_pretrained('bert-base-uncased', state_dict=torch.load('models/BERT-pretrain-1-step-5000.pkl', map_location=torch.device('cpu')))
#print(tokenizer.all_special_tokens) #--> ['[UNK]', '[SEP]', '[PAD]', '[CLS]', '[MASK]']
#print(tokenizer.all_special_ids) #--> [100, 102, 0, 101, 103]
num_added_toks = tokenizer.add_tokens(['[TRA]'], special_tokens=True)
model_bert.resize_token_embeddings(len(tokenizer)) # --> Embedding(30523, 768)
print('[TRA] token id: ', tokenizer.convert_tokens_to_ids('[TRA]')) # --> 30522
But I encountered the error:
AttributeError: 'BertModel' object has no attribute 'resize_token_embeddings'
I assume that it is because the model_bert(BERT-pretrain-1-step-5000.pkl)
I had has the different embedding size.
I would like to know if there is any way to fit the embedding size of my modified tokeniser and the model I would like to use as the initial weights.
Thanks a lot!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
resize_token_embegrer“您正在使用
pytorch_pretrataining_bert_inset
的BertModel类,该类别不提供这种方法。查看您可以等待插图的更新(也许创建GitHub问题),也可以编写自己的代码以扩展Word_embedding层:
resize_token_embeddings is a huggingface transformer method. You are using the BERTModel class from
pytorch_pretrained_bert_inset
which does not provide such a method. Looking at the code, it seems like they have copied the BERT code from huggingface some time ago.You can either wait for an update from INSET (maybe create a github issue) or write your own code to extend the word_embedding layer: