首页 > 解决方案 > 如何冻结 TFBertForSequenceClassification 预训练模型?

问题描述

如果我使用的是tensorflow版本的拥抱脸转换器,我如何冻结预训练编码器的权重,以便只优化头部层的权重?

对于 PyTorch 实现,它是通过

for param in model.base_model.parameters():
    param.requires_grad = False

想对 tensorflow 实现做同样的事情。

标签: tensorflowhuggingface-transformers

解决方案


找到了一种方法来做到这一点。在编译之前冻结基础模型。

model = TFBertForSequenceClassification.from_pretrained("bert-base-uncased")
model.layers[0].trainable = False
model.compile(...)

推荐阅读