python - BERT 编码器层不可训练
问题描述
我正在尝试从 TensorFlow hub 微调 BERT 模型。我加载了预处理层和编码器,如下所示:
bert_preprocess_model = hub.KerasLayer('https://tfhub.dev/tensorflow/bert_multi_cased_preprocess/3')
bert_model = hub.KerasLayer('https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-512_A-8/1')
这是我的模型定义:
def build_classifier_model():
text_input = tf.keras.layers.Input(shape=(), dtype=tf.string, name='text')
preprocessing_layer = hub.KerasLayer(bert_preprocess_model, name='preprocessing')
encoder_inputs = preprocessing_layer(text_input)
encoder = hub.KerasLayer(bert_model, trainable=True, name='BERT_encoder')
outputs = encoder(encoder_inputs)
net = outputs['pooled_output']
net = tf.keras.layers.Dropout(0.1)(net)
net = tf.keras.layers.Dense(3, activation='softmax', name='classifier')(net)
return tf.keras.Model(text_input, net)
classifier_model = build_classifier_model()
但我收到以下错误: ERROR:absl:hub.KerasLayer 是可训练的,但可训练的权重为零。在官网上,模型是微调的。
解决方案
我找到了解决方案,只需添加 trainable = True :
bert_model = hub.KerasLayer('https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-4_H-512_A-8/1',trainable=True)
推荐阅读
- php - MySQL - 按星期几比较几个月的数据 - 例如每个月的第一个星期六的会话
- swift - 如何在 Xcode 13 中使用图像文字
- wordpress - 在 Wordpress 标签中保存自定义帖子类型时出错
- sql - Grafana:如何在 Grafana 查询变量中使用 $__timeFilter
- android-studio - Android Studio 中的亮绿色突出显示
- mysql - 哦哦 :( 无法通过套接字 '/run/mysqld/mysqld.sock' 连接到本地 MySQL 服务器 (2)
- php - 工作区状态仪表板 php
- json - jq 脚本文件未获取键/值对
- c# - MUXC:Navigation 中是否不支持 WebView
- discord.js - discord bot 如何获取用户当前正在玩的游戏的缩略图?