加载模型Raise ValueError未知损失函数 [英] Loading a model Raise ValueError Unknown loss function

查看:872
本文介绍了加载模型Raise ValueError未知损失函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我尝试保存并加载模型后的代码:

this is the code after i try to save and load my model:

model.save('path_to_my_model.h5')
del model
model = tf.keras.models.load_model('path_to_my_model.h5', custom_objects={'Wraparound2D': Wraparound2D})

import tensorflow.keras.backend as K

inp = model.input                                           # input placeholder
outputs = [layer.output for layer in model.layers]          # all layer outputs
functor = K.function(inp, outputs)   # evaluation function

layer_outs = functor([X_test, 1.])



# Plot activations of different neurons in different layers 
all_layer_activations = list()

min_max_scaler = lambda x : (x - np.min(x))/(np.max(x) - np.min(x))
# min_max_scaler = lambda x : (x - np.mean(x))
for j in range(1, 5):
    if j==1:
        layer_im = np.hstack([min_max_scaler(layer_outs[1][0][..., i]) for i in range(10)])
    else:
        pattern = np.reshape(layer_outs[j][0], (wspan, hspan, -1))
        layer_im = np.hstack([min_max_scaler(pattern[..., i]) for i in range(10)])
    all_layer_activations.append(layer_im)

但是出现以下错误:

ValueError                                Traceback (most recent call last)
<ipython-input-9-75d24275ae64> in <module>()
     92 model.save('path_to_my_model.h5')
     93 del model
---> 94 model = tf.keras.models.load_model('path_to_my_model.h5', custom_objects={'Wraparound2D': Wraparound2D})
     95 
     96 import tensorflow.keras.backend as K

5 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    390       obj = module_objects.get(object_name)
    391       if obj is None:
--> 392         raise ValueError('Unknown ' + printable_module_name + ':' + object_name)
    393     # Classes passed by name are instantiated with no args, functions are
    394     # returned as-is.

ValueError: Unknown loss function: <lambda>

我找不到我为什么得到它的感谢,感谢您的帮助.在一切正常之前,我尝试加载模型后就会出现此错误

I can't find why I get it thanks for help. this error comes right after i try to load the model before that everything is fine

推荐答案

TL/DR:当保存的模型中有custom_objects时,则需要提供compile = False作为load_model的参数.加载模型后,需要使用custom_objects进行编译.请在此处查看示例.

TL/DR: When you have custom_objects in the saved model, then you need to provide compile = False as an argument to the load_model. After loading the model, you need to compile with the custom_objects. Please check the example here.

在保存带有custom_objects的模型时,这些custom_objects无法正确序列化.因此,在加载模型时,您需要传递compile=False并加载模型.加载模型后,您需要通过传递自定义对象来编译模型.

When you save a model with custom_objects, those custom_objects cannot be serialized properly. So, when you load the model you need to pass compile=False and load the model. After loading the model, you need to compile the model by passing the custom objects.

这篇关于加载模型Raise ValueError未知损失函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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