Mat*_*tti 6 metrics machine-learning neural-network conv-neural-network tensorflow
我正在为高度不平衡的分类问题实施 CNN,我想在 tensorflow 中实施 custum 指标以使用 Select Best Model 回调。具体来说,我想实现平衡准确度分数,即每个类的召回率的平均值(请参阅此处的sklearn 实现),有人知道怎么做吗?
我遇到了同样的问题,所以我实现了一个基于 off 的自定义类SparseCategoricalAccuracy:
class BalancedSparseCategoricalAccuracy(keras.metrics.SparseCategoricalAccuracy):
def __init__(self, name='balanced_sparse_categorical_accuracy', dtype=None):
super().__init__(name, dtype=dtype)
def update_state(self, y_true, y_pred, sample_weight=None):
y_flat = y_true
if y_true.shape.ndims == y_pred.shape.ndims:
y_flat = tf.squeeze(y_flat, axis=[-1])
y_true_int = tf.cast(y_flat, tf.int32)
cls_counts = tf.math.bincount(y_true_int)
cls_counts = tf.math.reciprocal_no_nan(tf.cast(cls_counts, self.dtype))
weight = tf.gather(cls_counts, y_true_int)
return super().update_state(y_true, y_pred, sample_weight=weight)
Run Code Online (Sandbox Code Playgroud)
这个想法是设置每个类的权重与其大小成反比。
这段代码从 Autograph 产生了一些警告,但我相信这些是 Autograph 的错误,并且该指标似乎工作正常。
| 归档时间: |
|
| 查看次数: |
2803 次 |
| 最近记录: |