简体   繁体   English

将一层的权重从一个 Huggingface BERT model 复制到另一个

[英]Copy one layer's weights from one Huggingface BERT model to another

I have a pre-trained model which I load like so:我有一个预训练的 model ,我像这样加载:

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.
)

I want to create a new model with the same architecture, and random initial weights, except for the embedding layer:我想创建一个具有相同架构和随机初始权重的新 model,但嵌入层除外

==== 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,)

It seems I can do this to create a new model with the same architecture, but then all the weights are random:看来我可以这样做来创建一个具有相同架构的新 model,但是所有的权重都是随机的:

configuration   = model.config
untrained_model = BertForSequenceClassification(configuration)

So how do I copy over model 's embedding layer weights to the new untrained_model ?那么如何将model的嵌入层权重复制到新的untrained_model

Weights and bias are just tensor and you can simply copy them withcopy_ :权重和偏差只是张量,你可以简单地用copy_复制它们:

from transformers import BertForSequenceClassification, BertConfig
jetfire = BertForSequenceClassification.from_pretrained('bert-base-cased')
config = BertConfig.from_pretrained('bert-base-cased')

optimus = BertForSequenceClassification(config)

parts = ['bert.embeddings.word_embeddings.weight'
,'bert.embeddings.position_embeddings.weight'              
,'bert.embeddings.token_type_embeddings.weight'    
,'bert.embeddings.LayerNorm.weight'
,'bert.embeddings.LayerNorm.bias']

def joltElectrify (jetfire, optimus, parts):
  target = dict(optimus.named_parameters())
  source = dict(jetfire.named_parameters())

  for part in parts:
    target[part].data.copy_(source[part].data)  

joltElectrify(jetfire, optimus, parts)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Huggingface BERT 模型之上添加密集层 - Add dense layer on top of Huggingface BERT model 将权重从一个Conv2D层复制到另一个 - Copying weights from one Conv2D layer to another 有没有办法使用python将过滤器从一层复制到另一层 - is there a way to copy a filter from one layer to another layer using python 如何在 Huggingface model 中将 PyTorch model 层的张量替换为另一层相同形状的张量? - How to replace PyTorch model layer's tensor with another layer of same shape in Huggingface model? 将权重设置为 model 从另一个带有附加层的 - Setting weights to model from another one with additional layers 将文件从一个模型的FileField复制到另一个模型的FileField而不读取它 - Copy a File from one Model's FileField to another Model's FileField without reading it 在 Keras 上将两个模型的权重加载到一个中 - Load two model's weights into one on Keras 如何使用 Huggingface BERT 模型来馈送二元分类器 CNN? - How to use a Huggingface BERT model from to feed a binary classifier CNN? Photoshop:使用 python 将图层从一个文件复制到另一个文件 - Photoshop: copy a layer from one file to another with python 将图层从一个CNN模型复制到另一个模型。 (layer_from_config在第二版中不起作用) - Copy a layer from one CNN model to other. (layer_from_config not working in ver 2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM