bert 如何获取微调模型的输出概率?

bvjxkvbb  于 6个月前  发布在  其他
关注(0)|答案(4)|浏览(53)

你好,我有一个问题。当我完成bert微调模型的run_classifier.py后,我将其用于我的任务,但我不知道如何从微调模型中获取概率。当我使用以下代码时,当我输入相同的text_a和text_b时,我得到的结果不同。请帮助我!谢谢!

pkln4tw6

pkln4tw61#

def predict(self, text_a, text_b):
    """
    bert model predict
    :param text_a: text a
    :param text_b: text b
    :return: label, similarity
    """
    input_ids, input_mask, segment_ids = self.convert_single_example(text_a, text_b)
    input_ids = np.reshape(input_ids, [-1, len(input_ids)])
    input_mask = np.reshape(input_mask, [-1, len(input_mask)])
    segment_ids = np.reshape(segment_ids, [-1, len(segment_ids)])

    output_layer = self.sess.run(self.model.get_pooled_output(), feed_dict={
        self.input_ids: input_ids,
        self.input_mask: input_mask,
        self.input_type_ids: segment_ids
    })

    logits = tf.matmul(output_layer, self.output_weights, transpose_b=True)
    logits = tf.nn.bias_add(logits, self.output_bias)
    probabilities = tf.nn.softmax(logits, axis=-1)

    print(self.sess.run(probabilities))
uqdfh47h

uqdfh47h2#

请有人能帮助我,谢谢@jacobdevlin-google

rnmwe5a2

rnmwe5a23#

也许可以尝试这样做:

def getPrediction(in_sentences):
    labels = ["Negative", "Positive"]
    input_examples = [run_classifier.InputExample(guid="", text_a = x, text_b = None, label = 0) 
                      for x in in_sentences] # here, "" is just a dummy label
    input_features = run_classifier.convert_examples_to_features(input_examples, label_list, MAX_SEQ_LENGTH, tokenizer)
    predict_input_fn = run_classifier.input_fn_builder(features=input_features, 
                                                       seq_length=MAX_SEQ_LENGTH,
                                                       is_training=False, drop_remainder=False)
    predictions = estimator.predict(predict_input_fn)
    return [(sentence, prediction['probabilities'], labels[prediction['labels']]) for sentence, prediction in zip(in_sentences, predictions)]

然后你可以传递一个示例列表:

pred_sentences = [
  "That movie was absolutely awful",
  "The acting was a bit lacking",
  "The film was creative and surprising",
  "Absolutely fantastic!"
]
predictions = getPrediction(pred_sentences)

这个示例来自示例笔记本: https://colab.research.google.com/github/google-research/bert/blob/master/predicting_movie_reviews_with_bert_on_tf_hub.ipynb

zd287kbt

zd287kbt4#

你好,这个给我的负概率像在例子中。有人能告诉我如何从这个得到正概率吗?这样阈值处理就可以应用到这个上。
这个的一个用例场景是用于双类分类,并且有一个输入不属于任何类别

相关问题