确保 BERT 模型不加载预训练权重?

Make sure BERT model does not load pretrained weights?

我想确保我的 BertModel 不会加载预训练的权重。我正在使用自动加载模型的 auto class(拥抱脸)。

我的问题是如何在没有预训练权重的情况下加载 bert 模型?

也许您可以使用预训练的权重加载模型,迭代模型参数并使用您喜欢的任何初始化技术随机设置模型参数。

使用 AutoConfig 而不是 AutoModel:

from transformers import AutoConfig
config = AutoConfig.from_pretrained('bert-base-uncased')
model =  AutoModel.from_config(config)

这应该在不加载权重的情况下设置模型。

Documentation here and here