openai / baselines

OpenAI Baselines: high-quality implementations of reinforcement learning algorithms
MIT License
15.63k stars 4.86k forks source link

AssertionError: Do not use tf.reset_default_graph() to clear nested graphs. If you need a cleared graph, exit the nesting and create a new graph. #1122

Open SrAlexBay opened 4 years ago

SrAlexBay commented 4 years ago

Hello, I try to write a loop code to test the training effect of DQN agent, which needs to load the model multiple times and reset the environment and tensorflow state. So I inserted tf.reset_default_graph() at the beginning of the loop to reset the graph. But when the loop runs to the second pass, the program will report an error: AssertionError: Do not use tf.reset_default_graph() to clear nested graphs. If you need a cleared graph, exit the nesting and create a new graph. I checked the information and found :

"This error message is displayed when you call tf.reset_default_graph() in one of the following scenarios:

  1. Inside a with graph.as_default(): block.
  2. Inside a with tf.Session(): block.
  3. Between creating a tf.InteractiveSession and calling sess.close()."

But my code does not seem to include these cases. And I added sess = tf.get_default_session() sess.close() at the end of the loop, but this does not seem to work. Is there any error in my code that needs to be modified? Or does baselines provide an API that can exit nested graph? The body of the loop code is as follows:

for i in range(X):
    tf.reset_default_graph()

    act = deepq.load_act(path)
    obs, done = env.validate(0, 0, 1.0, faultdurtime), False
    episode_rew = 0
    actions = list()
    while not done:
        action = act(obs[None])[0]
        obs, rew, done, _ = env.step(action)
        episode_rew += rew
        actions.append(action)
    sess = tf.get_default_session()
    sess.close()

Thank you very much!

vajrag commented 3 years ago

Hi @SrAlexBay, did you solve this error? I am also facing the same error

SaraRezaei commented 7 months ago

@SrAlexBay and @vajrag, same problem