在Keras中,如果您需要使用其他参数进行自定义丢失,我们可以像https://datascience.stackexchange.com/questions/25029/custom-loss-function-with-additional-parameter-in-中所述使用它.keras
def penalized_loss(noise):
def loss(y_true, y_pred):
return K.mean(K.square(y_pred - y_true) - K.square(y_true - noise), axis=-1)
return loss
Run Code Online (Sandbox Code Playgroud)
当我训练模型时,上述方法有效.但是,一旦训练模型,我就难以加载模型.当我尝试在load_model中使用custom_objects参数时,如下所示
model = load_model(modelFile, custom_objects={'penalized_loss': penalized_loss} )
Run Code Online (Sandbox Code Playgroud)
它抱怨 ValueError: Unknown loss function:loss
有没有办法将损失函数作为自定义损失之一传递custom_objects?从我可以收集的内容来看,在load_model调用期间,内部函数不在命名空间中.是否有更简单的方法来加载模型或使用附加参数的自定义损失