【问题标题】:Custom weighted MSE loss function in Keras based on error percentileKeras中基于误差百分位的自定义加权MSE损失函数
【发布时间】:2019-07-17 12:50:12
【问题描述】:

总的来说,我是 Keras 和神经网络的新手。我正在尝试为用于异常检测的多层自动编码器实现基于均方误差的自定义损失函数。基本上我要采用的方法是从这里https://www.jstage.jst.go.jp/article/ipsjjip/27/0/27_335/_pdf

不幸的是,我没有发布图片的声誉,因为我也是 SO 新手,但公式在第 2 页,第 3 部分为 Lprop

这里的直觉是,我不希望自动编码器更新返回误差高于损失百分比的数据点的权重。通过这种方式,它学会了在与异常值作斗争的同时重建数据集中的异常值,从而将它们检测为异常。

这是我尝试过的一些代码和编译后的模型

import keras.backend as K
c = 70.0
    def mean_squared_errorx(y_true, y_pred):
        es = K.square(y_pred - y_true)
        const = np.percentile(es, c)
        w = K.cast(K.less(const, K.mean(K.square(y_pred - y_true), axis=-1)), dtype = "float32")
        return w * K.mean(K.square(y_pred - y_true), axis=-1)

    #'mean_squared_error'
    autoencoder.compile(optimizer=adam, loss=mean_squared_errorx)
    autoencoder.fit(train, train,
                    epochs=num_epochs,
                    batch_size=round(len(train)/50),
                    shuffle=True,
                    validation_data=(train, train),
                    verbose = 0)
    encoded_d = encoder.predict(train)
    decoded_pred = decoder.predict(encoded_d)

这个想法是让 K.less 为每个错误返回一个布尔值,然后将其转换为浮点数以作为返回语句中的权重。 我知道 np.percentile 部分可能不适用于张量,但不知道如何完成百分位排名。

使用该代码我收到此错误消息

InvalidArgumentError: Incompatible shapes: [37,21] vs. [37]
     [[{{node loss_25/dense_104_loss/Less}}]]

在这种情况下,批量大小为 37,特征数量为 21。感谢您对此或代码其他部分的任何反馈 - 谢谢!

【问题讨论】:

    标签: python keras autoencoder loss-function mean-square-error


    【解决方案1】:

    如果有人在做类似的事情,找到一个潜在的解决方法

    import keras.backend as K
        def mean_squared_error_w(y_true, y_pred):
            mses = K.mean(K.square(y_pred - y_true), axis = -1)
            std_of_mses = K.std(mses)
            const = K.mean(mses, axis = -1) + (std_of_mses * 0.5)
            mask = K.cast(K.less(K.mean(K.square(y_pred - y_true), axis=-1), const), dtype = "float32")
            return mask * K.mean(K.square(y_pred - y_true), axis=-1)
    

    我相信这将为所有出现错误的值创建一个布尔张量 大于阈值,由批次 MSE 的平均值加上标准偏差的一半定义(如果错误是正态分布的,这应该对应于数据的大约 70% 作为截止值)。它将布尔值转换为权重 0 或 1 作为掩码,然后将其应用于输出 MSE 损失

    【讨论】:

      猜你喜欢
      • 2018-02-24
      • 2019-06-25
      • 2020-06-27
      • 1970-01-01
      • 2021-02-24
      • 2020-12-21
      • 2018-07-24
      相关资源
      最近更新 更多