在 Keras 自定义损失函数中,我经常看到轴参数设置为 -1。
def custom_loss(y_true, y_pred):
intersection = K.sum(K.abs(y_true * y_pred), axis=-1)
我一直认为它应该是0,因为0轴代表批次。那么为什么设置为-1?
在 Keras 自定义损失函数中,我经常看到轴参数设置为 -1。
def custom_loss(y_true, y_pred):
intersection = K.sum(K.abs(y_true * y_pred), axis=-1)
我一直认为它应该是0,因为0轴代表批次。那么为什么设置为-1?
因为 y_true 和 y_pred 有形状 (N,)。其中 N 是示例的数量。通过将轴设置为 -1,您正在计算最后一个维度的总和,这恰好超过 N(这是批次)