简体   繁体   English

无法加载 tensorflow(tf-agent)保存的模型

[英]Can't load tensorflow (tf-agent) saved model

I am creating a tf-agent DqnAgent in the following code:我正在以下代码中创建一个 tf-agent DqnAgent:

tf_agent = dqn_agent.DqnAgent(
    train_env.time_step_spec(),
    train_env.action_spec(),
    q_network=q_net,
    optimizer=optimizer,
    td_errors_loss_fn=dqn_agent.element_wise_squared_loss,
    train_step_counter=train_step_counter

) )

During the training loop I am saving this model with在训练循环中,我将这个模型保存为

tf.saved_model.save(tf_agent, saved_models_path)

Once trained, I want to load saved model with训练后,我想加载保存的模型

if tf.saved_model.contains_saved_model(saved_models_path):
    tf_agent = tf.saved_model.load(saved_models_path)

This code will load the saved model only if the folder in saved_path contains one, the functions contains_saved_model(saved_models_path) returns True , so the model is loaded, but there is an excetion and the program crashes:只有当saved_path中的文件夹包含一个时,此代码才会加载保存的模型,函数contains_saved_model(saved_models_path)返回True ,因此加载了模型,但是有一个异常并且程序崩溃:

Traceback (most recent call last):
    File "/home/claudino/Projetos/dino-tf-agents/dino_ia/model/agent.py", line 50, in <module>
        tf_agent = tf.saved_model.load(saved_models_path)
    File "/home/claudino/Projetos/dino-tf-agents/venv/lib/python3.6/site-packages/tensorflow/python/saved_model/load.py", line 408, in load
        return load_internal(export_dir, tags)
    File "/home/claudino/Projetos/dino-tf-agents/venv/lib/python3.6/site-packages/tensorflow/python/saved_model/load.py", line 432, in load_internal
        export_dir)
    File "/home/claudino/Projetos/dino-tf-agents/venv/lib/python3.6/site-packages/tensorflow/python/saved_model/load.py", line 58, in __init__
        self._load_all()
    File "/home/claudino/Projetos/dino-tf-agents/venv/lib/python3.6/site-packages/tensorflow/python/saved_model/load.py", line 168, in _load_all
        slot_variable = optimizer_object.add_slot(
    AttributeError: '_UserObject' object has no attribute 'add_slot'

    Process finished with exit code 1

I navigated tensorflow code but couldn't find the problem.我浏览了 tensorflow 代码,但找不到问题。 Anyone can help me?任何人都可以帮助我吗?

I am using tf-agents-nightly because google's colaboratory source code don't work on tf-agents "stable" version (I am not sure tf-agents is really stable), and tryed the code with tensorflow 1.3 and 2.0.0-beta0 , same problem occurs.我正在使用tf-agents-nightly ,因为 google 的协作源代码不适用于tf-agents “稳定”版本(我不确定 tf-agents 是否真的稳定),并尝试使用tensorflow 1.3 和2.0.0-beta0 ,同样的问题发生。

Have you tried TensorFlow 2.7?你试过 TensorFlow 2.7 吗? That usually helps with this issue.这通常有助于解决这个问题。

Something else that worked for me was to load the model this way (This assumes the model is a keras / tf.keras model):对我有用的其他方法是以这种方式加载模型(假设模型是keras / tf.keras模型):

try:
    model = tf.keras.models.load_model(model_dir)
except:
  load_options = tf.saved_model.LoadOptions(experimental_io_device= '/job:localhost')
  model = tf.saved_model.load(model_dir, options= load_options)

The try clause will lead to an exception, because load_model() requires a keras_metadata.pb file, which is absent when you save models with saved_model.save() . try 子句将导致异常,因为load_model()需要一个keras_metadata.pb文件,当您使用saved_model.save()保存模型时,该文件不存在。

However, running that clause will somehow make tf.saved_model.load() run without any problems.但是,运行该子句将以某种方式使tf.saved_model.load()运行没有任何问题。 There's probably some sort of interaction happening in the background that I don't really understand, but it works for me and the ' no attribute add_slot ' error doesn't come up.可能在后台发生了某种我不太了解的交互,但它对我有用,并且不会出现“ no attribute add_slot ”错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM