Keras:具有自定义损失功能的错误加载模型

问题描述:

我定义了一个自定义损失函数,如下所示:

I define a custom loss function as follows:

weight_for_hierarchical_error = K.variable(np.ones(16))

def mse_weighted(y_true, y_pred):
    return K.mean(weight_for_hierarchical_error * K.square(y_pred - y_true), axis=-1)

当我将模型保存到hdf5文件中,然后尝试用load_model加载模型时,出现以下错误信息:

When I save the model in a hdf5 file and then try to load it with load_model, I got following error information:

ValueError: Unknown loss function:mse_weighted

ValueError: Unknown loss function:mse_weighted

有人知道如何解决此问题吗?

Does anyone know how to fix this problem?

谢谢!

在此可以通过将自定义词典对象传递给load_model来解决:

This is solved here by passing a custom dictionary object to load_model:

https://github.com/fchollet/keras/issues/5916 https://github.com/fchollet/keras/issues/3977