且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Keras模型-在自定义损失函数中获取输入

更新时间:2023-12-02 15:44:10

我认为您可以通过如下所示在model.compile中启用run_eagerly=True来做到这一点.

I think you can do this by enabling run_eagerly=True in model.compile as shown below.

model.compile(loss=custom_loss(weight_building, weight_space),optimizer=keras.optimizers.Adam(), metrics=['accuracy'],run_eagerly=True)

我认为您还需要更新custom_loss,如下所示.

I think you also need to update custom_loss as shown below.

def custom_loss(weight_building, weight_space):
  def loss(y_true, y_pred):
    truth = backend.get_value(y_true)
    error = backend.square((y_pred - y_true))
    mse_error = backend.mean(error, axis=-1) 
    return mse_error
  return loss

我用一个简单的mnist数据演示了这个想法.请在此处查看代码.

I am demonstrating the idea with a simple mnist data. Please take a look at the code here.