ElegantRL icon indicating copy to clipboard operation
ElegantRL copied to clipboard

:children_crossing: How to save and load policy network for testing.

Open Yonv1943 opened this issue 2 years ago • 0 comments

After training the agent, many people are not sure how to save and load the policy network after training and see how the agent actually performs in a simulation environment. 很多人在完成agent 的训练之后,不清楚要如何保存并加载训练完成后的 policy network,并在仿真环境中看看这个agent的实际表现。

Here is the code to (take Pendulum env for example):

  • train the agent and save the policy network
  • load the policy network and use it to map the state to get the action.

下面是两个代码例子(举Pendulum 环境为例):

  • 训练agent并保存policy network
  • 加载policy network 并使用它 对 state 映射得到 action

train the agent and save the policy network

训练agent并保存policy network

https://github.com/AI4Finance-Foundation/ElegantRL/blob/68bf0ea4ef3fb461026ece8897deabb92aeead32/examples/demo_A2C_PPO.py#L14-L18

https://github.com/AI4Finance-Foundation/ElegantRL/blob/68bf0ea4ef3fb461026ece8897deabb92aeead32/elegantrl/train/run.py#L99

The process will keep saving policy network (actor) in cwd="./Pendulum_PPO_0/act.pt" (current working directory) during training. 程序会在训练中,持续保存 saving policy network (actor) 在当前的工作目录下 cwd="./Pendulum_PPO_0/act.pt" (current working directory)

https://github.com/AI4Finance-Foundation/ElegantRL/blob/68bf0ea4ef3fb461026ece8897deabb92aeead32/elegantrl/train/run.py#L92

load the policy network and use it to map the state to get the action.

加载policy network 并使用它 对 state 映射得到 action

https://github.com/AI4Finance-Foundation/ElegantRL/blob/68bf0ea4ef3fb461026ece8897deabb92aeead32/examples/demo_A2C_PPO.py#L662

The following code load the policy netowrk (actor) from disk: 下面的代码从硬盘里 加载了 policy netowrk (actor):

https://github.com/AI4Finance-Foundation/ElegantRL/blob/68bf0ea4ef3fb461026ece8897deabb92aeead32/examples/demo_A2C_PPO.py#L679-L682

The following code map state to action using policy netowrk (actor): 下面的代码使用 policy netowrk (actor) 将 state 映射到 action:

https://github.com/AI4Finance-Foundation/ElegantRL/blob/68bf0ea4ef3fb461026ece8897deabb92aeead32/examples/demo_A2C_PPO.py#L699-L705

Yonv1943 avatar Mar 06 '23 02:03 Yonv1943