agents: Error loading DqnAgent saved model.

I am creating a tf-agent DqnAgent in the following code:

    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:

    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

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 23 (7 by maintainers)

Commits related to this issue

Most upvoted comments

@andreclaudino I have the same exact issue, were you able to make the load/save working? do you have a working example that I can take a look?

Policy saver ? I’m using modelcheckpoints :

cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path, save_weights_only=True, verbose=1)

@andreclaudino I have the same exact issue, were you able to make the load/save working? do you have a working example that I can take a look?

Unfortunatelly I have no solution to this problem yet.

We are planning to add a colab for policy saving, so I’m closing this issue in favor of #186.

I’ll add a reference to this from #186.

Only loading/saving the weights works, but it’s clearly far from ideal

Same problem here, would love to see a fix or workaround

Thanks. I m using 2.0 beta, unfortunately, tf-nightly is not compatible with the code I could reach in tf-agents documentation.

This looks a bug, and on the other side, we need better documentation on tf-agents for stable tensorflow.