为什么稳定基线 3 中的多处理速度较慢?

问题描述 投票:0回答:1

我采用了 Stable Baselines 3 的多处理示例,一切都很好。 https://colab.research.google.com/github/Stable-Baselines-Team/rl-colab-notebooks/blob/sb3/multiprocessing_rl.ipynb#scrollTo=pUWGZp3i9wyf

在 num_cpu=4 的情况下,多处理训练比单处理训练花费的时间大约少 3.6 倍。

但是当我尝试使用 PPO 代替 A3C,使用 BipedalWalker-v3 代替 CartPole-v1 时,我发现多处理模式下的性能更差。我的问题是:我做错了什么?为什么速度慢了?

我的代码是:

import gym
import time

from stable_baselines3 import PPO
from stable_baselines3 import A2C
from stable_baselines3.common.env_util import make_vec_env
from stable_baselines3.common.evaluation import evaluate_policy

env_name = "BipedalWalker-v3"
num_cpu = 4
n_timesteps = 10000

env = make_vec_env(env_name, n_envs=num_cpu)

model = PPO('MlpPolicy', env, verbose=0)

start_time = time.time()
model.learn(n_timesteps)
total_time_multi = time.time() - start_time
print(f"Took {total_time_multi:.2f}s for multiprocessed version - {n_timesteps / total_time_multi:.2f} FPS")


single_process_model = PPO('MlpPolicy', env_name, verbose=0)
start_time = time.time()
single_process_model.learn(n_timesteps)
total_time_single = time.time() - start_time


print(f"Took {total_time_single:.2f}s for single process version - {n_timesteps / total_time_single:.2f} FPS")
print("Multiprocessed training is {:.2f}x faster!".format(total_time_single / total_time_multi))

输出为:

Took 16.39s for multiprocessed version - 610.18 FPS
Took 14.19s for single process version - 704.80 FPS
Multiprocessed training is 0.87x faster!
python multiprocessing reinforcement-learning openai-gym stable-baselines
1个回答
0
投票

您可以尝试将 SubprocVecEnv 类作为

make_vec_env
vec_env_cls 参数传递。

默认情况下 make_vec_env 使用 DummyVecEnv 包装器对环境进行矢量化。这实际上并没有创建子流程,而是按顺序调用每个环境。 当多处理的开销超过环境计算时间时,它适用于简单环境(例如 CartPole),但对于计算量更大的环境,SubprocVecEnv 更好(因为它创建实际的子进程)。

© www.soinside.com 2019 - 2024. All rights reserved.