Saving a Model in OPENAI Baselines

时间:2025-01-20 13:51:45

题意:OPENAI Baselines中保存模型

问题背景:

Openai Baselines save the trained model with the following command,

OpenAI Baselines 使用以下命令保存训练好的模型:

python -m baselines.run --alg=ppo2 --env=PongNoFrameskip-v4 --num_timesteps=2e7 --save_path=~/models/pong_20M_ppo2

But the saved trained model is not in the form of,

但是保存的训练模型并不是以下形式:

  1. .
  2. .
  3. .ckpt.data
  4. checkpoint

which it was in this form in the earlier versions. How can we save the model as ., ., . and checkpoint format?

在早期版本中模型是以这种形式保存的。我们如何将模型保存为 .、.、. 和检查点格式?

问题解决:

I've encountered the same problem and I've solved the problem by making a little adjustment to the baselines code.

“我遇到了同样的问题,并通过对 baselines 代码进行一些小调整解决了这个问题。”

There are two pairs of methods in baselines for saving and loading models(the save_state&load_state pair and the save_variables&loas_variables pair) and you can see it in baselines/common/tf_util.py(line325~line372).

在 baselines 中有两对用于保存和加载模型的方法(save_stateload_state 这一对,以及 save_variablesload_variables 这一对),你可以在 baselines/common/tf_util.py 文件的第 325 行到第 372 行看到这些方法。

For the latest version of baselines, the save_state&load_state pair which saves and loads models in the ., ., . and checkpoint format has been abandoned, so you need to re-enable the save_state&load_state pair.

在最新版本的 baselines 中,save_stateload_state 这对用于以 .、.、. 和检查点格式保存和加载模型的方法已被弃用,因此你需要重新启用 save_stateload_state 这对方法。

Take ppo2 for example, in baselines/ppo2/, make the following replacement: in line 125, replace

“以 ppo2 为例,在 `baselines/ppo2/` 文件中,进行以下替换:在第 125 行,将”

  1. self.save = (save_variables, sess=sess)
  2. self.load = (load_variables, sess=sess)

with  用以下语句替换

  1. self.save = (save_state, sess=sess)
  2. self.load = (load_state, sess=sess)

and in line 4, replace        将第4行的以下语句

from baselines.common.tf_util import get_session, save_variables, load_variables

with        用以下语句替换

from baselines.common.tf_util import get_session, save_state, load_state

this will replace the save_variables&loas_variables pair with the save_state&load_state pair.

“这将把 `save_variables` 和 `load_variables` 这对方法替换为 `save_state` 和 `load_state` 这对方法。”

hope this would help you.        希望对你有帮助.