我设法将梯度合并到损失函数中,但是问题是我似乎无法选择所需的特定梯度。该模型有多个输入和一个输出,我目前不确定使用哪个。该模型定义如下:
input_shape = (5,)
input_tensor = Input(shape = input_shape)
l1 = Dense(400, activation = 'relu')(input_tensor)
l2 = Dense(400, activation = 'relu')(l1)
l3 = Dense(400, activation = 'relu')(l2)
l4 = Dense(400, activation = 'relu')(l3)
output_tensor = Dense(1, activation = 'relu')(l4)
model = Model(input_tensor, output_tensor)
def custom_loss(input_tensor, output_tensor):
def newloss(y_true, y_pred):
mse = K.mean(K.square(y_true - y_pred))
gradients = K.gradients(output_tensor, input_tensor)[0][:,1]
return mse + K.maximum(-1*gradients, 0)
return newloss
sgd = keras.optimizers.Adam(lr = 0.001, beta_1 = 0.9, beta_2 = 0.999)
model.compile(loss = custom_loss(input_tensor, output_tensor),
optimizer = 'sgd',
metrics = ['mae'])
epochs = 10
batch_size = 100
# Fit the model weights.
history = model.fit(x_train_bs, y_train_bs,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test_bs, y_test_bs))
基于我的理解,K.gradients函数应该产生5个导数(由于有5个输入),但是我无法对其进行索引以选择我具体需要的一个。我对Keras完全没有经验,所以任何帮助/直觉都将不胜感激。