无法使用RectifiedAdam优化器加载Keras模型

时间:2020-03-06 10:33:10

标签: tensorflow keras tensorflow2.0 keras-layer tf.keras

尝试使用tf.keras.models.load_model加载Keras模型时出现以下错误:

import tensorflow as tf
from tensorflow_addons.optimizers import RectifiedAdam
model = tf.keras.models.load_model('model', custom_objects = {'RectifiedAdam' : RectifiedAdam})

错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/saving/save.py", line 150, in load_model
    return saved_model_load.load(filepath, compile)
  File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/load.py", line 99, in load
    training_config))
  File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/saving/saving_utils.py", line 229, in compile_args_from_training_config
    optimizer_config, custom_objects=custom_objects)
  File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/optimizers.py", line 819, in deserialize
    printable_module_name='optimizer')
  File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/utils/generic_utils.py", line 292, in deserialize_keras_object
    config, module_objects, custom_objects, printable_module_name)
  File "/mylib/python3.7/site-packages/tensorflow_core/python/keras/utils/generic_utils.py", line 250, in class_and_config_for_serialized_keras_object
    raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
ValueError: Unknown optimizer: RectifiedAdam

我可以通过编译为False tf.keras.models.load_model('model', compile=False)加载模型,然后使用RectifiedAdam优化器(如此处建议的https://stackoverflow.com/a/56565801)再次对其进行编译-但这并不理想...

所以有什么想法,我在做什么错了?

2 个答案:

答案 0 :(得分:0)

对此的一种快速破解方法是手动将RectifiedAdam分配给Tensorflow命名空间中的对象:

import tensorflow as tf
from tensorflow_addons.optimizers import RectifiedAdam

tf.keras.optimizers.RectifiedAdam = RectifiedAdam
...

答案 1 :(得分:0)

或者做这样的事情:

Failure/Error: require "api-blueprint"

ArgumentError:
  wrong number of arguments (given 2, expected 0)