Keras load_model raise ValueError: Unknown layer: TokenEmbedding问题

当尝试使用Keras的load_model加载包含自定义层的模型时,会遇到ValueError。解决方法是在load_model函数中提供custom_objects参数,传入一个包含自定义层的字典。可以使用keras_bert的get_custom_objects()或者手动添加缺失的自定义对象。

问题复现: 

训练的模型存储方式: 

model.save_weights(model_path)

模型加载方式

from keras.models import load_model
model = load_model(model_path)

异常:

ValueError: Unknown layer: TokenEmbedding

出现该错误是因为要保存的model中包含了自定义的层(Custom Layer),导致加载模型的时候无法解析该Layer
解决该问题的方法是在load_model函数中添加custom_objects参数,该参数接受一个字典,键值为自定义的层,当然也可以偷懒,直接使用custom_objects=get_custom_objects()

解决方案:

from keras.models import load_model
from keras_bert import get_custom_objects
model = load_model(model_path, custom_objects=get_custom_objects())

keras_bert中提供的get_custom_objects() 中包含如下元素: 

{'LayerNormalization': <class 'keras_layer_normalization.layer_normalization.LayerNormalization'>, 'MultiHeadAtten
(base) root@autodl-container-811348b9ea-7832b743:~/autodl-tmp/SatIQ/training# python fgsm_attack_satiq-Copy15.py 2025-07-03 15:33:24.952232: I tensorflow/core/util/port.cc:110] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`. 2025-07-03 15:33:24.993549: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. 2025-07-03 15:33:25.612835: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT /root/miniconda3/lib/python3.8/site-packages/keras/src/layers/core/lambda_layer.py:327: UserWarning: util.models is not loaded, but a Lambda layer uses it. It may cause errors. function = cls._parse_function_from_config( Traceback (most recent call last): File "fgsm_attack_satiq-Copy15.py", line 45, in <module> model = tf.keras.models.load_model(model_path) File "/root/miniconda3/lib/python3.8/site-packages/keras/src/saving/saving_api.py", line 238, in load_model return legacy_sm_saving_lib.load_model( File "/root/miniconda3/lib/python3.8/site-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler raise e.with_traceback(filtered_tb) from None File "/root/miniconda3/lib/python3.8/site-packages/keras/src/saving/legacy/serialization.py", line 365, in class_and_config_for_serialized_keras_object raise ValueError( ValueError: Unknown layer: 'UpscalingLayer'. Please ensure you are using a `keras.utils.custom_object_scope` and that this object is included in the scope. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
07-04
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值