【发布时间】:2020-02-04 00:15:48
【问题描述】:
汉明损失计算我们的预测错误归一化的标签数量。
HammingLoss 作为度量的标准实现依赖于计算错误预测,大致如下:(在 TF 上)
count_non_zero = tf.math.count_nonzero(actuals - predictions)
return tf.reduce_mean(count_non_zero / actuals.get_shape()[-1])
将汉明损失实现为实际损失需要它是可微的,由于tf.math.count_nonzero 而不是这种情况。
另一种(和近似的)方法是以这种方式计算非零标签,但不幸的是,NN 似乎没有改善。
def hamming_loss(y_true, y_pred):
y_true = tf.convert_to_tensor(y_true, name="y_true")
y_pred = tf.convert_to_tensor(y_pred, name="y_pred")
diff = tf.cast(tf.math.abs(y_true - y_pred), dtype=tf.float32)
#Counting non-zeros in a differentiable way
epsilon = K.epsilon()
nonzero = tf.reduce_mean(tf.math.abs( diff / (tf.math.abs(diff) + epsilon)))
return tf.reduce_mean(nonzero / K.int_shape(y_pred)[-1])
最后,TensorFlow 的 Hamming Loss 的正确实现是什么?
【问题讨论】:
-
TF Addons 正在考虑添加这个,检查Add hamming loss for both multiclass and multilabel #305
-
@Xarvalus 不幸的是,它被实现为一个指标,而不是一个可微的损失函数
标签: python tensorflow machine-learning loss-function