machine-learning - 如何在bert之上添加新层?
问题描述
你能帮我在 bert 输出上添加新的 bilstm 层吗?
output_layer = model.get_pooled_output()
# Here, we make alterations to add the extra features
output_layer_extra_features = tf.concat([output_layer, tf.convert_to_tensor(extra_features, dtype=tf.float32)],
axis=1)
hidden_size = output_layer_extra_features.shape[-1].value
output_weights = tf.get_variable(
"output_weights", [num_labels, hidden_size],
initializer=tf.truncated_normal_initializer(stddev=0.02))
output_bias = tf.get_variable(
"output_bias", [num_labels], initializer=tf.zeros_initializer())
with tf.variable_scope("loss"):
if is_training:
# I.e., 0.1 dropout
output_layer_extra_features = tf.nn.dropout(output_layer_extra_features, keep_prob=0.9)
logits = tf.matmul(output_layer_extra_features, output_weights, transpose_b=True)
logits = tf.nn.bias_add(logits, output_bias)
probabilities = tf.nn.softmax(logits, axis=-1)
log_probs = tf.nn.log_softmax(logits, axis=-1)
one_hot_labels = tf.one_hot(labels, depth=num_labels, dtype=tf.float32)
per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1)
loss = tf.reduce_mean(per_example_loss)
解决方案
推荐阅读
- android - 渐进式 webapp 可以访问 android/IOS 设备的安全存储吗?
- azure - Azure databricks:通过 API 将 maven 库安装到集群会导致错误(库解析失败。原因:java.lang.RuntimeException)
- python - 如何使用 Pypdf2 从 Pdf 中提取文本,不包括图表和表格中的文本内容
- amazon-web-services - 在创建启用了私有 dns 名称的 ECR vpc 端点后,来自公共 Fargate 服务的 CannotPullContainerError
- hazelcast - 卡拉夫酒窖群
- typescript - 从 Typescript 项目生成 OpenApi 规范
- javascript - 将数据从 Javascript 发布到 Django 时如何修复 403(禁止)错误
- python - 使用命令提示符时如何将 Python 脚本输出保存到文件中?
- xcode - 重命名 Xcode 项目后出现问题
- c++ - C++ std::atomic - 不可能基于共享原子变量同步 2 个线程