【问题标题】:Tensorflow 2.2.0 and Keras save model / load model problemsTensorflow 2.2.0 和 Keras 保存模型/加载模型问题
【发布时间】:2021-03-08 21:32:54
【问题描述】:

将自定义损失函数作为@tf.function 添加到我的keras DQN 后,keras 模型停止加载(似乎保存模型,但无法重新加载模型)。文档表明这很简单,但是...

各种 SO 答案表明,使用一个 Keras 版本训练的模型无法加载到其他 Keras 版本中。所以我卸载了 Keras 2.4.3(来自 Anaconda env),以避免任何混淆,并尝试使用 Tensorflow-keras 单独建模和保存/加载。

所以,现在尝试保存一个 Tensorflow-keras 模型,然后再次加载该模型,但不会重新加载,出现各种错误(如下)。环境是Anaconda3 python3.8(用Keras 2.4.3,然后卸载了这个)和Tensorflow 2.2.0(含Keras 2.3.0-tf)。

是否有一些解决方案可以简单地保存模型,然后在 tf 2.2.0(使用 keras 2.3.0-tf)中重新加载模型?

import tensorflow as tf
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Dense,  LSTM, Masking, Input
from tensorflow.keras.optimizers import Adam

那么所有的 tf.keras 建模和保存/加载都应该由 Keras-2.3.0-tf 从 Tensorflow 中完成。模型保存完成:

agent.model.save(os.path.join(pathOUT, PAIR, 'models' + modelNum, modelFolder), 
save_format='tf')

但在保存过程中会产生弃用警告:

2020-11-26 00:19:03.388858: W tensorflow/python/util/util.cc:329] Sets are not currently 
considered sequences, but this may change in the future, so consider avoiding using them.
WARNING:tensorflow:From C:\..mypath......\lib\site- 
packages\tensorflow\python\ops\resource_variable_ops.py:1813: calling 
BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with 
constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
Saved an Intermediate model...

然后尝试加载模型:

model = load_model(LOAD_MODEL)

但在加载过程中产生错误:

TypeError: __init__() got an unexpected keyword argument 'reduction'

再次,是否有一些解决方案可以简单地保存模型,然后在 tf 2.2.0(使用 keras 2.3.0-tf)中重新加载模型?

完全错误:

Traceback (most recent call last):
File mypath, line 851, in <module>
  agent = DQNAgent()   
File mypath, line 266, in __init__
  self.model = self.create_model()
File mypath, line 336, in create_model
  model = load_model(LOAD_MODEL)
File mypath\lib\site-packages\tensorflow\python\keras\saving\save.py", line 190, in load_model
  return saved_model_load.load(filepath, compile)
File mypath\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 116, 
  in load
  model = tf_load.load_internal(path, loader_cls=KerasObjectLoader)
File mypath\lib\site-packages\tensorflow\python\saved_model\load.py", line 602, in 
  load_internal
  loader = loader_cls(object_graph_proto,
File mypath\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 188, 
  in __init__
  super(KerasObjectLoader, self).__init__(*args, **kwargs)
File mypath\lib\site-packages\tensorflow\python\saved_model\load.py", line 123, in __init__
  self._load_all()
File mypath\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 209, 
   in _load_all
 self._layer_nodes = self._load_layers()
File mypath\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 312, 
  in _load_layers
  layers[node_id] = self._load_layer(proto.user_object, node_id)
File mypath\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 335, 
  in _load_layer
  obj, setter = self._revive_from_config(proto.identifier, metadata, node_id)
File mypath\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 349, 
  in _revive_from_config
  obj = self._revive_metric_from_config(metadata, node_id)
File mypath\lib\site-packages\tensorflow\python\keras\saving\saved_model\load.py", line 441, 
  in _revive_metric_from_config
  obj = metrics.deserialize(
File mypath\lib\site-packages\tensorflow\python\keras\metrics.py", line 3345, in deserialize
  return deserialize_keras_object(
File mypath\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 361, in 
  deserialize_keras_object
  (cls, cls_config) = class_and_config_for_serialized_keras_object(
File mypath\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 327, in 
  class_and_config_for_serialized_keras_object
  deserialized_objects[key] = deserialize_keras_object(
File mypath\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 375, in 
  deserialize_keras_object
  return cls.from_config(cls_config)
File mypath\lib\site-packages\tensorflow\python\keras\metrics.py", line 628, in from_config
  return super(MeanMetricWrapper, cls).from_config(config)
File mypath\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 655, in 
  from_config
  return cls(**config)
TypeError: __init__() got an unexpected keyword argument 'reduction'

附加代码: 自定义损失函数(尝试通过“翻转”误差梯度来实现梯度上升)已在不同位置(与模型相同的代理类、外部代理类等)进行了尝试。

@tf.function
def positive_mse(y_true, y_pred):
    return -1 * tf.keras.losses.MSE(y_true, y_pred)

我使用的顺序模型是here

【问题讨论】:

  • 您要保存的"tf"format 是什么?我不确定您的错误,但我正在运行 tf 2.1.0 和 tf.keras 2.2.4-tf 并使用 tensorflow.keras.models.Sequential.save("modelname.h5") 保存并使用 tensorflow.keras.models.load_model("modelname.h5") 加载模型。
  • 为了解决保存/加载问题,我试图尽可能明确。虽然在 tf2.2.* save_format 默认为 tf 格式(来自 keras 文档:'save_format: 'tf' 或 'h5',表示是将模型保存到 Tensorflow SavedModel 还是 HDF5。在 TF 2 中默认为 'tf'。 X 和 TF 1.X. 中的“h5”)。如果默认是 tf 并且你没有指定 h5,你确定你的保存格式是 h5 吗?已尝试您的保存/加载格式,但我的代码没有解决方案。thx
  • @MarkD,this 是否回答您的问题?如果不能,请分享完整的代码以复制您的问题。因为我已经按照 colab 中的上述指定版本成功保存和加载示例模型,并带有自定义损失。谢谢!
  • 谢谢。以前(不幸的是)删除的帖子指向了这个解决方案。但它不能解决重新加载时的关键字参数“reduction”错误。使用 tf.keras (2.3.0-tf) 保存和加载时,在我的模型中使用修饰的自定义损失函数似乎有一些问题。我目前已恢复使用 keras 2.4.0 和未修饰的函数,重新加载模型时使用 compile=False。将在上面添加其他代码。

标签: tensorflow keras deep-learning save load


【解决方案1】:

在原始模型的模型编译过程中,通过删除 MeanSquareError() 度量已解决/绕过原始关键字参数“reduction”错误。原型号:

model.compile(loss=positive_mse,
              optimizer=Adam(lr=LEARNING_RATE, decay=DECAY),
              metrics=[tf.keras.losses.MeanSquaredError()])

来自 Keras 文档:“请注意,这是 tf.keras.losses.mean_squared_error 等损失函数与 tf.keras.losses.MeanSquaredError 等默认损失类实例之间的重要区别:函数版本不执行减少,但默认情况下类实例会这样做。"

MeanSquaaredError 损失类函数在评估小批量损失期间传递了一个“reduction”关键字。删除此指标允许重新加载模型而不会出错。

【讨论】:

  • 但是如果你指的是这个指标呢?你的意思是用 MeanSquaredError 代替 mean_squared_error?
  • 没有。已删除,未替换。我只使用损失和优化器关键字参数,不使用指标。我在数据文件中实时记录模型中的指标,并在 matplotlib 中简单地可视化。
  • 为什么不使用tf.keras.metrics
  • Tensorflow 指标和 Tensorboard 一直存在问题(从来没有稳定过,尤其是 Tensorboard,在我的安装中)......所以一直在使用自定义损失输出
猜你喜欢
  • 2019-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-24
  • 1970-01-01
  • 2020-10-28
相关资源
最近更新 更多