(自定义)百分比MSE损失功能

时间:2020-03-16 14:27:55

标签: python keras loss-function quantile risk-analysis

我有一个Keras模型,它具有输入x_1,...,x_n和d维输出f(x_1),...,f(x_n)。我正在处理d维目标y_1,...,y_n的回归问题。

我想最小化损失函数: 对于介于0和1之间的固定元参数a,返回|f(x_i)-y_i|^2的第a个(经验)分位数。

这是到目前为止我编写的代码:

def keras_custom_loss(y_true,y_predicted):
    SEs = K.square(y_true-y_predicted)
    out_custom = tfp.stats.percentile(SEs, 50.0, interpolation='midpoint')
    return out_custom

一个问题是我想避免使用tensorflow_probability,而是希望在Keras上完成整个实现。

但是,我不知道如何。

2 个答案:

答案 0 :(得分:2)

对于您的特定用例,您可以使用以下功能,该功能是tfp.stats.percentile的简化版本(它们使用Apache License 2.0):

import tensorflow as tf

def percentile(x, p):
    with tf.name_scope('percentile'):
        y = tf.transpose(x)  # take percentile over batch dimension
        sorted_y = tf.sort(y)
        frac_idx = tf.cast(p, tf.float64) / 100. * (tf.cast(tf.shape(y)[-1], tf.float64) - 1.)
        return 0.5 * (  # using midpoint rule
            tf.gather(sorted_y, tf.math.ceil(frac_idx), axis=-1)
            + tf.gather(sorted_y, tf.math.floor(frac_idx), axis=-1))

答案 1 :(得分:2)

要使“所有元素”高于该百分比,您将需要一个不同的答案:

import keras.backend as K
from keras.layers import *
from keras.models import Model
import numpy as np
import tensorflow as tf

def above_percentile(x, p): #assuming the input is flattened: (n,)

    samples = K.cast(K.shape(x)[0], K.floatx()) #batch size
    p =  (100. - p)/100.  #100% will return 0 elements, 0% will return all elements

    #samples to get:
    samples = K.cast(tf.math.floor(p * samples), 'int32')
        #you can choose tf.math.ceil above, it depends on whether you want to
        #include or exclude one element. Suppose you you want 33% top,
        #but it's only possible to get exactly 30% or 40% top:
        #floor will get 30% top and ceil will get 40% top.
        #(exact matches included in both cases)

    #selected samples
    values, indices = tf.math.top_k(x, samples)

    return values

def custom_loss(p):
    def loss(y_true, y_predicted):
        ses = K.square(y_true-y_predicted)
        above = above_percentile(K.flatten(ses), p)
        return K.mean(above)
    return loss

测试:

dataX = np.array([2,3,1,4,7,10,8,5,6]).reshape((-1,1))
dataY = np.ones((9,1))


ins = Input((1,))
outs = Lambda(lambda x: x)(ins)
model = Model(ins, outs)

model.compile(optimizer='adam', loss = custom_loss(70.))
model.fit(dataX, dataY)

亏损为65,即130/2(平均)。并且130 = (10-1)² + (8-1)²108在输入中的前两个k。