"openai/openai_test.go" did not exist on "b0135f4b9b176eab9155b660d04c9ca2a1ec2341"
metric_functions.py 1.83 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Copyright 2020 The TensorFlow Authors All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
# Lint as: python3
"""Metric functions."""
import tensorflow.compat.v1 as tf


def classification_metric(per_example_loss, label_ids, logits):
  """Compute eval metrics."""
  return {
      "accuracy":
          tf.metrics.accuracy(label_ids, tf.math.argmax(logits, axis=-1)),
      "eval_loss":
          tf.metrics.mean(per_example_loss)
  }


30
31
32
THRESHOLDS = [0.5]


33
34
def labeling_metric(per_example_loss, label_ids, logits):
  """Compute eval metrics."""
35
  scores = tf.math.sigmoid(logits)
36
37
38
  num_classes = label_ids.get_shape().as_list()[-1]
  return_dict = {"eval_loss": tf.metrics.mean(per_example_loss)}
  for idx in range(num_classes):
39
40
    return_dict["auc/" + str(idx)] = tf.metrics.auc(label_ids[:, idx],
                                                    scores[:, idx])
41
42
43
44
45
46
    return_dict["precision@" + str(THRESHOLDS) + "/" +
                str(idx)] = tf.metrics.precision_at_thresholds(
                    label_ids[:, idx], scores[:, idx], thresholds=THRESHOLDS)
    return_dict["recall@" + str(THRESHOLDS) + "/" +
                str(idx)] = tf.metrics.recall_at_thresholds(
                    label_ids[:, idx], scores[:, idx], thresholds=THRESHOLDS)
47
  return return_dict