Keras:具有自定义损失功能的错误加载模型 [英] Keras: error loading model with custom loss function

查看:77
本文介绍了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

这篇关于Keras:具有自定义损失功能的错误加载模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆