gpt4 book ai didi

python - 使用 BERT 进行多标签分类

转载 作者:行者123 更新时间:2023-12-03 17:06:30 24 4
gpt4 key购买 nike

我想用BERT model使用 Tensorflow 进行多标签分类。

为此,我想修改示例 run_classifier.py来自 BERT github repository ,这是一个如何使用BERT做简单分类的例子,使用pre-trained weights given by Google Research . (例如 BERT-Base, Cased )

我有 X不同的标签,其值为 0 或 1,所以我想在原始 BERT 模型中添加一个新的 Dense 层,大小为 X并使用 sigmoid_cross_entropy_with_logits激活函数。

所以,对于理论部分,我认为我没问题。

问题是我不知道如何使用现有的 BertModel 附加一个新的输出层并仅使用我的数据集重新训练这个新层。类(class)。

这是原文create_model()函数来自 run_classifier.py我想我必须做我的修改。但我有点不知所措。

def create_model(bert_config, is_training, input_ids, input_mask, segment_ids,
labels, num_labels, use_one_hot_embeddings):
"""Creates a classification model."""
model = modeling.BertModel(
config=bert_config,
is_training=is_training,
input_ids=input_ids,
input_mask=input_mask,
token_type_ids=segment_ids,
use_one_hot_embeddings=use_one_hot_embeddings)

output_layer = model.get_pooled_output()

hidden_size = output_layer.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 = tf.nn.dropout(output_layer, keep_prob=0.9)

logits = tf.matmul(output_layer, 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)

return (loss, per_example_loss, logits, probabilities)

这是相同的功能,经过我的一些修改,但是缺少某些东西(还有错误的东西?)
def create_model(bert_config, is_training, input_ids, input_mask, segment_ids, labels, num_labels):
"""Creates a classification model."""
model = modeling.BertModel(
config=bert_config,
is_training=is_training,
input_ids=input_ids,
input_mask=input_mask,
token_type_ids=segment_ids)

output_layer = model.get_pooled_output()

hidden_size = output_layer.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 = tf.nn.dropout(output_layer, keep_prob=0.9)

logits = tf.matmul(output_layer, 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)

per_example_loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=labels, logits=logits)

loss = tf.reduce_mean(per_example_loss)

return (loss, per_example_loss, logits, probabilities)

我在代码中改编的其他事情我没有问题:
  • DataProcessor 加载和解析我的自定义数据集
  • 在任何使用它的地方将标签变量的类型从数值更改为数组

  • 所以,如果有人知道我应该怎么做来解决我的问题,或者甚至指出我可能犯了一些明显的错误,我会很高兴听到它。

    注意事项:
  • 我找到了 this article这与我想要做的非常吻合,但它使用 PyTorch,我无法将其转换为 Tensorflow。
  • 最佳答案

    您想用 sigmoid 替换对可能输出(所有分数总和为 1)的单个分布建模的 softmax,该 sigmoid 为每个类建模独立分布(每个输出有是/否分布)。

    因此,您正确地更改了损失函数,但您还需要更改计算概率的方式。它应该是:

    probabilities = tf.sigmoid(logits)

    在这种情况下,您不需要 log_probs .

    关于python - 使用 BERT 进行多标签分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56006614/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com