Keras指标与TF后端vs张量流指标 [英] Keras metrics with TF backend vs tensorflow metrics

查看:101
本文介绍了Keras指标与TF后端vs张量流指标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当Keras 2.x删除某些指标时,变更日志说它这样做是因为它们是基于批处理"的,因此并不总是准确的.这是什么意思?张量流中包含的相应指标是否存在相同的缺点?例如:精度和召回率指标.

When Keras 2.x removed certain metrics, the changelog said it did so because they were "Batch-based" and therefore not always accurate. What is meant by this? Do the corresponding metrics included in tensorflow suffer from the same drawbacks? For example: precision and recall metrics.

推荐答案

让我们以精度为例. 已删除的无状态版本,如: /p>

Let's take precision for example. The stateless version which was removed was implemented like so:

def precision(y_true, y_pred):  
    """Precision metric.    
     Only computes a batch-wise average of precision.   
     Computes the precision, a metric for multi-label classification of 
    how many selected items are relevant.   
    """ 
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))  
    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))  
    precision = true_positives / (predicted_positives + K.epsilon())    
    return precision

如果y_true包含数据集中的所有标签,并且y_pred具有与所有这些标签相对应的模型预测,则可以.

Which is fine if y_true contains all of the labels in the dataset and y_pred has the model's predictions corresponding to all of those labels.

问题是人们经常将其数据集分成几批,例如通过对1000张图像进行10次评估来评估10000张图像.这对于适应内存限制可能是必需的.在这种情况下,您将无法获得10个不同的精度分数.

The issue is that people often divide their datasets into batches, for example evaluating on 10000 images by running 10 evaluations of 1000 images. This can be necessary to fit memory constraints. In this case you'd get 10 different precision scores with no way to combine them.

有状态的度量标准通过将中间值保留在变量中来解决此问题,该变量在整个评估过程中都有效.因此,在precision的情况下,有状态指标可能具有true_positivespredicted_positives的持久计数器. TensorFlow指标是有状态的,例如 tf.metrics.precision .

Stateful metrics solve this issue by keeping intermediate values in variables which last for the whole evaluation. So in the case of precision a stateful metric might have a persistent counter for true_positives and predicted_positives. TensorFlow metrics are stateful, e.g. tf.metrics.precision.

这篇关于Keras指标与TF后端vs张量流指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆