首页 > 解决方案 > Python,Keras基于预测的自定义损失函数,不是输入

问题描述

我正在尝试在 keras 中编写自定义损失函数,该函数基于训练期间模型的多次预测。我有一个以前的列表/字典,例如 dict = {X1: [X2,X3,X4], X2:[X1, X6, X7]} 等。

给定输入 X1,我需要在训练期间预测 X2、X3、X4。

model.predict 不工作,给出错误 ValueError: When feed symbolic tensors to a model, we expect thetensors to have a static batch size。得到具有形状的张量:(无,36)

from keras.layers import * 
from keras.models import Model
import keras.backend as K

input_tensor = Input(shape=(36,))
hidden       = Dense(100, activation='relu')(input_tensor)
out          = Dense(1, activation='linear')(hidden)

def custom_loss(input_tensor, dict):
    def inner(y_true, y_pred):
        X2 = dict[input_tensor][0]
        X3 = dict[input_tensor][1]
        X4 = dict[input_tensor][2]
        X2_pred = model.predict(X2)
        X3_pred = model.predict(X3)
        X4_pred = model.predict(X4)
        return K.mean(max(X2_pred, X3_pred, X4_pred)-y_true)

    return inner

custom_loss_final = custom_loss(input_tensor = input_tensor, dict = dict)

model        = Model(input_tensor, out)
model.compile(loss = custom_loss_final, optimizer='adam')

model.fit(x = Train_X, y = Train_y, batch_size= 100)

根据Anakin 的解决方案进行编辑:我尝试了您的代码,实际上在 np.append 我需要使用axis = 0。

现在我有: INPUT_X.shape (100, 36) INPUT_Y.shape (100,3,36)

INPUT_X:我有 100 个训练样本,每个都是 36 len 数组。
INPUT_Y:这些是 X2、X3 等。

实际上我的代码中甚至不需要 Y_true,因为我将使用模型(X2)等。

我按照你的建议将它们传递给 model.fit,在损失函数中我打印了一些类型/形状:
(input_tensor)
(?, 36)
("y_true")
(?, ?)
(pred_y)
(?, 1)

我不知道为什么 y_true 形状是 (?,?) 而不是 (3,36)。我不能放模型(pred_y[:,0]),因为我得到:

ValueError:形状必须为 2 级,但对于 'loss_56/dense_114_loss/model_57/dense_113/MatMul'(操作:'MatMul')为 1 级,输入形状为:[?]、[36,300]。
##为了清楚起见,现在300是隐藏层的大小。

标签: python-3.xkeras

解决方案


您可以使用输入中的额外数据列填充标签并编写自定义损失。您可以像这样将输入的额外预测信息作为 numpy 数组传递

    def custom_loss(data, y_pred):

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


    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, extra, axis =1), batch_size = batch_size, epochs=90, shuffle=True, verbose=1)

该示例来自我的代码的一部分,但我希望您能理解。


推荐阅读