【问题标题】:Keras, cost function for cyclic outputs?Keras,循环输出的成本函数?
【发布时间】:2016-05-30 14:13:35
【问题描述】:

现在我正在尝试使用神经网络对图像进行着色。我想在 HSV 色彩空间中做到这一点。问题在于色调通道是循环的。色调的归一化值介于 0 和 1 之间。例如,模型预测为 0.99,但实际色调为 0.01。对于正常的均方误差损失,这看起来很遥远。然而,距离真的更像 0.02。如何在 keras 中获得循环损失函数?

【问题讨论】:

    标签: python neural-network keras


    【解决方案1】:

    从预测色调A 到实际色调B 的真实距离实际上是 3 项中的最小值:

    1. (A - B)^2(不绕圈的距离)
    2. (A - B + 1)^2(绕到左边的距离)
    3. (A - B - 1)^2(向右转的距离)

    例如,在您的示例中,从A = 0.99B = 0.01 的最短路径是向右转,距离为(A - B - 1)^2 = (0.99 - 0.01 - 1)^2 = (-0.02)^2 = 0.02^2

    既然我们已经弄清楚了数学,我们如何实现它? Keras 的implementation 均方误差为:

    from keras import backend as K
    
    def mean_squared_error(y_true, y_pred):
        return K.mean(K.square(y_pred - y_true), axis=-1)
    

    这是使其循环的调整:

    def cyclic_mean_squared_error(y_true, y_pred):
        return K.mean(K.minimum(K.square(y_pred - y_true), 
                                K.minimum(K.square(y_pred - y_true + 1), 
                                          K.square(y_pred - y_true - 1)), axis=-1)
    

    要使用此损失函数,请在编译模型时指定loss=cyclic_mean_squared_error

    【讨论】:

      猜你喜欢
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多