tensorflow - 如何冻结 TFBertForSequenceClassification 预训练模型?
问题描述
如果我使用的是tensorflow版本的拥抱脸转换器,我如何冻结预训练编码器的权重,以便只优化头部层的权重?
对于 PyTorch 实现,它是通过
for param in model.base_model.parameters():
param.requires_grad = False
想对 tensorflow 实现做同样的事情。
解决方案
找到了一种方法来做到这一点。在编译之前冻结基础模型。
model = TFBertForSequenceClassification.from_pretrained("bert-base-uncased")
model.layers[0].trainable = False
model.compile(...)
推荐阅读
- php - php datetime diff完全关闭
- ruby - 使用 Sequel 生成迁移
- c# - 如何从 PlayCommand 中删除目标?
- apache-spark - YarnAllocator 请求的容器比我请求的多
- javascript - Azure 移动应用 Nodejs 后端提供的数据类型无效
- python - Python中的峰值检测算法
- php - Woocommerce 结构消失了
- python-3.x - Python打印具有给定位数的浮点数
- function - 如何防止 JS 错误“列中的预期‘函数’...”?
- symfony - Symfony 4 - Encore - 将文件夹复制到 dist