我正在尝试在 keras 中编写一个自定义损失函数,该函数基于训练期间模型的多次预测。我有一个以前的列表/字典,例如 dict = {X1: [X2,X3,X4], X2:[X1, X6, X7]} 等。
给定输入 X1,我需要在训练期间预测 X2、X3、X4。
model.predict 不起作用,给出错误 ValueError: 当将符号张量提供给模型时,我们期望张量具有静态批量大小。得到具有形状的张量:(无,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)
Run Code Online (Sandbox Code Playgroud)
根据阿纳金的解决方案进行编辑:我尝试了你的代码,实际上在 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)。我不能放置 model(pred_y[:,0]),因为我得到:
ValueError:形状必须为等级 2,但对于“loss_56/dense_114_loss/model_57/dense_113/MatMul”(操作:“MatMul”),其等级为 1,输入形状为:[?]、[36,300]。
##为了清楚起见,300 是现在隐藏层的大小。
小智 5
您可以使用输入中的额外数据列填充标签并编写自定义损失。您可以将额外的预测信息作为 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)
Run Code Online (Sandbox Code Playgroud)
该示例来自我的代码的一部分,但我希望您能明白。
归档时间: |
|
查看次数: |
1408 次 |
最近记录: |