如何将假阳性率作为TF指标

2024-04-29 05:32:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图向BERT风格的模型中添加一些度量,但在tf.metrics方面遇到了困难。对于大多数指标,您可以使用tf.metrics.mean非常简单,但对于假阳性率这样的指标,则不是。我知道有tf.metrics.false_阳性和tf.metrics.true_阴性,但是由于tf.metrics也有一个相关的op,所以不能只做fpr = fp / (fp + tn)。这件事怎么办


Tags: 模型falsetrue度量风格tfmean阳性
1条回答
网友
1楼 · 发布于 2024-04-29 05:32:18

代码如下:

from tensorflow.python.eager import context
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import variable_scope
from tensorflow.python.ops.metrics_impl import _aggregate_across_towers
from tensorflow.python.ops.metrics_impl import true_negatives
from tensorflow.python.ops.metrics_impl import false_positives
from tensorflow.python.ops.metrics_impl import _remove_squeezable_dimensions

def false_positive_rate(labels,                                                             
               predictions,                                                     
               weights=None,                                                       
               metrics_collections=None,                                           
               updates_collections=None,                                           
               name=None):                                                         
  if context.executing_eagerly():                                                  
    raise RuntimeError('tf.metrics.recall is not supported is not '                
                       'supported when eager execution is enabled.')               
                                                                                   
  with variable_scope.variable_scope(name, 'false_alarm',                          
                                     (predictions, labels, weights)):           
    predictions, labels, weights = _remove_squeezable_dimensions(                  
        predictions=math_ops.cast(predictions, dtype=dtypes.bool),                 
        labels=math_ops.cast(labels, dtype=dtypes.bool),                           
        weights=weights)                                                           
                                                                                   
    false_p, false_positives_update_op = false_positives(                          
        labels,                                                                    
        predictions,                                                            
        weights,                                                                   
        metrics_collections=None,                                                  
        updates_collections=None,                                                  
        name=None)                                                                 
    true_n, true_negatives_update_op = true_negatives(                          
        labels,                                                                    
        predictions,                                                               
        weights,                                                                   
        metrics_collections=None,                                                  
        updates_collections=None,                                                  
        name=None)                                                              
                                                                                   
    def compute_false_positive_rate(true_n, false_p, name):                                        
      return array_ops.where(                                                      
          math_ops.greater(true_n + false_p, 0),                                   
          math_ops.div(false_p, true_n + false_p), 0, name)                        
                                                                                   
    def once_across_towers(_, true_n, false_p):                                 
      return compute_false_positive_rate(true_n, false_p, 'value')                              
                                                                                   
    false_positive_rate = _aggregate_across_towers(                                                
        metrics_collections, once_across_towers, true_n, false_p)                  
                                                                                   
    update_op = compute_false_positive_rate(true_negatives_update_op,                              
                               false_positives_update_op, 'update_op')             
    if updates_collections:                                                        
      ops.add_to_collections(updates_collections, update_op)                       
                                                                                   
    return false_positive_rate, update_op

相关问题 更多 >