【发布时间】:2021-08-13 18:30:25
【问题描述】:
我有一个预训练的模型,我可以这样加载:
from transformers import BertForSequenceClassification, AdamW, BertConfig, BertModel
model = BertForSequenceClassification.from_pretrained(
"bert-base-uncased", # Use the 12-layer BERT model, with an uncased vocab.
num_labels = 2, # The number of output labels--2 for binary classification.
# You can increase this for multi-class tasks.
output_attentions = False, # Whether the model returns attentions weights.
output_hidden_states = False, # Whether the model returns all hidden-states.
)
我想创建一个具有相同架构和随机初始权重的新模型,除了嵌入层:
==== Embedding Layer ====
bert.embeddings.word_embeddings.weight (30522, 768)
bert.embeddings.position_embeddings.weight (512, 768)
bert.embeddings.token_type_embeddings.weight (2, 768)
bert.embeddings.LayerNorm.weight (768,)
bert.embeddings.LayerNorm.bias (768,)
看来我可以这样做来创建一个具有相同架构的新模型,但是所有权重都是随机的:
configuration = model.config
untrained_model = BertForSequenceClassification(configuration)
那么如何将model 的嵌入层权重复制到新的untrained_model?
【问题讨论】:
标签: python bert-language-model huggingface-transformers