Determine pinball loss function in kera using backend

I am trying to define a pinbal loss function to implement "quantile regression" in a neural network with Keras (with Tensorflow as the backend).

The definition is here: pinball loss

It is difficult to implement the traditional K.means () function, etc. since they deal with the whole batch of y_pred, y_true, but I have to consider each component y_pred, y_true and here's my source code:

def pinball_1(y_true, y_pred):
    loss = 0.1
    with tf.Session() as sess:
        y_true = sess.run(y_true)
        y_pred = sess.run(y_pred)
    y_pin = np.zeros((len(y_true), 1))
    y_pin = tf.placeholder(tf.float32, [None, 1])
    for i in range((len(y_true))):
        if y_true[i] >= y_pred[i]:
            y_pin[i] = loss * (y_true[i] - y_pred[i])
        else:
            y_pin[i] = (1 - loss) * (y_pred[i] - y_true[i])
    pinball = tf.reduce_mean(y_pin, axis=-1)
    return K.mean(pinball, axis=-1)

sgd = SGD(lr=0.1, clipvalue=0.5)
model.compile(loss=pinball_1, optimizer=sgd)
model.fit(Train_X, Train_Y, nb_epoch=10, batch_size=20, verbose=2)

      

I tried to pass y_pred, y_true into a vector data structure, so I can cast them with an index and handle the individual components, but it seems the problem comes from a lack of knowledge about handling y_pred, y_true individually.

I tried to dive into error related lines, but I was almost lost.

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'dense_16_target' with dtype float
 [[Node: dense_16_target = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

      

How can I fix this? Thank!

+3


source to share


1 answer


I figured it out myself using the Keras backend:



def pinball(y_true, y_pred):
    global i
    tao = (i + 1) / 10
    pin = K.mean(K.maximum(y_true - y_pred, 0) * tao +
                 K.maximum(y_pred - y_true, 0) * (1 - tao))
    return pin

      

+3


source







All Articles