2017-04-01 314 views
2

我想定義一個在Keras(用Tensorflow作爲後端)實現神經網絡中的「分位數迴歸」的pinbal損失函數。用張量流後端定義keras中的彈球損失函數

的定義是在這裏:pinball loss

很難實現傳統K.means()等函數,因爲他們應對整批y_pred,y_true的,但我要考慮y_pred,y_true的每個組件,這是我的原代碼:

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) 

我試圖轉移y_pred,y_true是量化數據結構,所以我可以用指數引用他們,並處理各個部件,但似乎出現由於缺乏的問題知識在單獨治療y_pred,y_true。

我試圖潛入由錯誤指示的線條,但我幾乎迷路了。

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"]()]] 

我該如何解決?謝謝!

+0

刪除此行'y_pin = tf.placeholder(tf.float32,[無,1])'也整個會話基於塊。 – lejlot

+0

謝謝!我已經明白了這一點。 –

回答

2

我已經與Keras後端這出由自己:

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