【问题标题】:Keras metrics with TF backend vs tensorflow metrics带有 TF 后端的 Keras 指标与张量流指标
【发布时间】:2018-08-07 20:31:12
【问题描述】:
当 Keras 2.x 删除某些指标时,变更日志表示这样做是因为它们是“基于批处理的”,因此并不总是准确的。这是什么意思? tensorflow 中包含的相应指标是否存在相同的缺点?例如:精度和召回指标。
【问题讨论】:
标签:
python
python-2.7
tensorflow
keras
keras-2
【解决方案1】:
我们以精度为例。 stateless version which was removed 是这样实现的:
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 具有与所有这些标签相对应的模型预测,这很好。
问题在于人们经常将他们的数据集分成批次,例如通过对 1000 张图像运行 10 次评估来评估 10000 张图像。这可能是适应内存限制所必需的。在这种情况下,您将获得 10 个不同的精度分数,而无法将它们组合起来。
有状态度量通过将中间值保留在变量中并持续整个评估来解决这个问题。因此,在precision 的情况下,有状态度量可能具有true_positives 和predicted_positives 的持久计数器。 TensorFlow 指标是有状态的,例如tf.metrics.precision.