【问题标题】:Custom loss function and fit data for multiple inputs and outputs in tf.Kerastf.Keras 中多个输入和输出的自定义损失函数和拟合数据
【发布时间】:2021-08-20 15:34:27
【问题描述】:

我正在使用 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)。我瞄准的损失函数是:c0 * (x0 - x1**2)**2 + c1 * (x1 - c0 * x0)**2,它包括两个输入和两个输出。以下是我的问题:

  • 如何编写考虑所有c0, c1, x0, x1 的损失函数?我曾尝试使用 Keras 中的自定义损失函数,但从 y_pred (应该是损失函数的一部分)中切片和提取 x0x1 似乎是不正确的。
  • 如何拟合训练数据?在这种情况下,我们有一个用于c0 和另一个用于c1 的数组训练数据。
  • 如果用 Keras 很难做到这一点,是否有任何其他更容易处理的包的建议?

非常感谢您阅读并回答我的问题。我尝试过自定义减肥和减肥,但到目前为止似乎没有帮助。

【问题讨论】:

    标签: python tf.keras loss-function multiple-input


    【解决方案1】:

    您可以使用tf.keras.layers.Concatenate 类并设置axis=-1trainable=False 将解决您的问题。 我也有为多输入和多输出编写自定义损失函数的相同经验。

    这是我对您现有代码所做的更改

    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')
    

    自定义代码功能

    #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
    

    拟合模型:

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

    预测代码:

    DNN_model.predict([c0_inp,c1_inp])
    

    测试代码..

    【讨论】:

      猜你喜欢
      • 2019-05-19
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 2019-01-11
      • 1970-01-01
      • 2020-01-21
      相关资源
      最近更新 更多