【问题标题】:Keras custom loss coupling different samplesKeras 自定义损失耦合不同样本
【发布时间】:2019-07-05 17:22:32
【问题描述】:

我试图了解 Keras 如何在一般设置中实际计算自定义损失的梯度。 通常,损失被定义为独立贡献样本的总和。这最终允许在梯度计算中进行适当的并行化。 但是,如果我在其上添加全局非线性,从而耦合各个样本的贡献,Keras 是否能够正确处理差异化?

实际上,它实际上是最小化 f(sum_i(x_i)) 还是一次计算一个样本,从而减少到 sum_i(f(x_i))?

下面是一个日志函数的例子。

def custom_loss(y_true,y_pred): return K.log(1+K.mean((y_pred-y_true)*(y_pred-y_true)))

我检查了文档,但找不到任何准确的答案。

【问题讨论】:

    标签: python tensorflow keras sample loss


    【解决方案1】:

    它会最小化你告诉它最小化的任何东西。

    • 如果要最小化整个总和的对数,则应用总和后的对数。
    • 如果您想最小化每个样本的对数并在以后求和,则在求和之前应用对数
    def log_of_sum(y_true, y_pred):
        return K.log(1 + K.mean(K.square(y_true-y_pred)))
    
    def sum_of_logs(y_true, y_ored):
        return K.mean(K.log(1 + K.square(y_true-y_pred))) 
        #mean is optional here - you can return all the samples and Keras will handle it
        #returning all the samples allows other functions to work, like sample_weights   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-22
      • 2021-12-15
      • 1970-01-01
      • 2020-12-19
      • 2017-12-18
      • 2020-03-27
      • 2020-12-21
      相关资源
      最近更新 更多