簡體   English   中英

自定義損失 function 並擬合 tf.Keras 中多個輸入和輸出的數據

[英]Custom loss function and fit data for multiple inputs and outputs in tf.Keras

我正在使用 tf.Keras 中的 DNN,如下所示:

# Construct the DNN model with 2 inputs, 2 outputs and 3 hidden layers
c0_input = Input(shape=(1,), name="c0")
c1_input = Input(shape=(1,), name="c1")

# Concatenate the input into one layer
tensor_input = Concatenate(axis=-1)([c0_input, c1_input])
hidden_1 = Dense(100)(tensor_input)
activation_1 = LeakyReLU(alpha=0.1)(hidden_1)
hidden_2 = Dense(100)(activation_1)
activation_2 = LeakyReLU(alpha=0.1)(hidden_2)
hidden_3 = Dense(100)(activation_2)
activation_3 = LeakyReLU(alpha=0.1)(hidden_3)

# 2 outputs are named as x0 and x1
x0_output = Dense(1, name="x0")(activation_3)
x1_output = Dense(1, name="x1")(activation_3)

# The model
DNN_model = Model(inputs=[c0_input, c1_input], outputs=[x0_output, x1_output])

如您所見,這個 DNN 有 2 個輸入(c0, c1)和 2 個輸出(x0, x1) 我針對的損失 function 是: c0 * (x0 - x1**2)**2 + c1 * (x1 - c0 * x0)**2 ,其中包括兩個輸入和兩個輸出。 以下是我的問題:

  • 我如何寫一個損失 function,它考慮到所有c0, c1, x0, x1 我試圖解決 Keras 中的自定義損失 function,但是從y_pred切片和提取x0x1看起來是不正確的(這應該是損失函數的一部分)。
  • 如何擬合訓練數據? 在這種情況下,我們有一個用於c0的數組訓練數據和另一個用於c1的訓練數據。
  • 如果用 Keras 很難做到這一點,有沒有其他更容易處理的包的推薦?

非常感謝您閱讀並回答我的問題。 我曾嘗試過自定義減肥和減肥,但到目前為止似乎沒有幫助。

您可以使用tf.keras.layers.Concatenate class 並設置axis=-1和 trainable trainable=False將解決您的問題。 我也有為多輸入和多輸出編寫自定義損失 function 的相同經驗。

這是我對您現有代碼所做的更改

c0_input = tf.keras.layers.Input(shape=(1,), name="c0")
c1_input = tf.keras.layers.Input(shape=(1,), name="c1")

# Concatenate the input into one layer
tensor_input = tf.keras.layers.Concatenate(axis=-1)([c0_input, c1_input])
hidden_1 = tf.keras.layers.Dense(100)(tensor_input)
activation_1 =  tf.keras.layers.LeakyReLU(alpha=0.1)(hidden_1)
hidden_2 = tf.keras.layers.Dense(100)(activation_1)
activation_2 =  tf.keras.layers.LeakyReLU(alpha=0.1)(hidden_2)
hidden_3 = tf.keras.layers.Dense(100)(activation_2)
activation_3 =  tf.keras.layers.LeakyReLU(alpha=0.1)(hidden_3)

# 2 outputs are named as x0 and x1
x0_output = tf.keras.layers.Dense(1, name="x0")(activation_3)
x1_output = tf.keras.layers.Dense(1, name="x1")(activation_3)
#Here is the main code part
concat_output = tf.keras.layers.Concatenate(axis=-1,trainable=False)([c0_input,c1_input,x0_output,x1_output])
# The model
DNN_model = tf.keras.Model(inputs=[c0_input, c1_input], outputs=[concat_output])
DNN_model.compile(loss=custom_multi_loss,optimizer='adam')

自定義代碼 function

#c0 * (x0 - x1**2)**2 + c1 * (x1 - c0 * x0)**2
def custom_multi_loss(y_true, y_pred):
    c0 = y_pred[0][0]
    c1 = y_pred[0][1]
    x0 = y_pred[0][2]
    x1 = y_pred[0][3]
    x0_true = y_true[0][0] #Not defined in your request
    x1_true = y_true[0][1] #Not defined in your request
    return  c0 * (x0 - x1**2)**2 + c1 * (x1 - c0 * x0)**2

生成虛擬數據進行測試

c0_inp = tf.random.uniform(minval=0.0,maxval=1.0,shape=(4,1))
c1_inp = tf.random.uniform(minval=0.0,maxval=1.0,shape=(4,1))
x0_x1_output = tf.random.uniform(minval=0.0,maxval=1.0,shape=(4,2))
c0_inp,c1_input,x0_x1_output

配件 model:

DNN_model.fit([c0_inp,c1_inp],x0_x1_output,epochs=2)

預測代碼:

DNN_model.predict([c0_inp,c1_inp])

測試代碼..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM