基于输入数据的Keras中的自定义损失函数

use*_*033 0 keras

我正在尝试使用Keras创建自定义损失函数。我想根据输入来计算损失函数并预测神经网络的输出。

我尝试在Keras中使用customloss函数。我认为y_true是我们为训练提供的输出,而y_pred是神经网络的预测输出。以下损失函数与Keras中的“ mean_squared_error”损失相同。

def customloss(y_true, y_pred):
    return K.mean(K.square(y_pred - y_true), axis=-1)
Run Code Online (Sandbox Code Playgroud)

除了mean_squared_error损失,我还想使用神经网络的输入来计算自定义损失函数。有没有一种方法可以将输入作为自定义函数的参数发送到神经网络。

谢谢。

rvi*_*nas 5

您可以使用另一个将输入张量作为参数的函数包装您的自定义损失:

def customloss(x):
    def loss(y_true, y_pred):
        # Use x here as you wish
        err = K.mean(K.square(y_pred - y_true), axis=-1)
        return err

    return loss
Run Code Online (Sandbox Code Playgroud)

然后按如下方式编译模型:

model.compile('sgd', customloss(x))
Run Code Online (Sandbox Code Playgroud)

x你的输入张量在哪里。

注意:未经测试。


Ana*_*kin 5

对于您提出的问题,我遇到了2种解决方案。

  1. 您可以将输入张量作为参数传递给自定义损失包装函数。
    def custom_loss(i):

        def loss(y_true, y_pred):
            return K.mean(K.square(y_pred - y_true), axis=-1) + something with i...
        return loss

    def baseline_model():
        # create model
        i = Input(shape=(5,))
        x = Dense(5, kernel_initializer='glorot_uniform', activation='linear')(i)
        o = Dense(1, kernel_initializer='normal', activation='linear')(x)
        model = Model(i, o)
        model.compile(loss=custom_loss(i), optimizer=Adam(lr=0.0005))
        return model
Run Code Online (Sandbox Code Playgroud)

此解决方案也在此处接受的答案中提到

  1. 您可以在输入中用额外的数据列填充标签,并编写自定义损失。如果您只想从输入中选择一个或几个功能列,这将很有帮助。
    def custom_loss(data, y_pred):

        y_true = data[:, 0]
        i = data[:, 1]
        return K.mean(K.square(y_pred - y_true), axis=-1) + something with i...


    def baseline_model():
        # create model
        i = Input(shape=(5,))
        x = Dense(5, kernel_initializer='glorot_uniform', activation='linear')(i)
        o = Dense(1, kernel_initializer='normal', activation='linear')(x)
        model = Model(i, o)
        model.compile(loss=custom_loss, optimizer=Adam(lr=0.0005))
        return model


    model.fit(X, np.append(Y_true, X[:, 0], axis =1), batch_size = batch_size, epochs=90, shuffle=True, verbose=1)
Run Code Online (Sandbox Code Playgroud)

此解决方案也可以在此线程中找到

我只在损失中不得不使用输入要素列时才使用第二种方法。我将第一种方法与标量参数一起使用;但我相信张量输入也可以。

  • 我参加聚会很晚了,但你的第二个解决方案真是天才!非常非常实用,棒棒哒。 (2认同)
  • 这样做我得到: `tensorflow.python.eager.core._SymbolicException:急切执行函数的输入不能是 Keras 符号张量,但找到 [<tf.Tensor 'mel_specs:0' shape=(None, None, 512) dtype =float32>]`。并不是说我将“tf.data.Dataset”传递给“fit()”函数。知道这里有什么问题吗? (2认同)