为什么在稳定的基线3中进行多处理较慢?

发布于 2025-01-25 03:43:11 字数 1306 浏览 5 评论 0 原文

我以稳定基线3的多处理示例,一切都很好。

但是,当我尝试使用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!

I took multiprocessing example for Stable Baselines 3 and everything was fine.
https://colab.research.google.com/github/Stable-Baselines-Team/rl-colab-notebooks/blob/sb3/multiprocessing_rl.ipynb#scrollTo=pUWGZp3i9wyf

Multiprocessed training took approximately 3.6x less time than single processing with num_cpu=4.

But when I'm trying to use PPO instead of A3C, and BipedalWalker-v3 instead of CartPole-v1, I see worse performance in multiprocessing mode. My question is: What am I doing wrong? Why is it slower?

My code is:

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))

The output is:

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑,眼淚并存 2025-02-01 03:43:11

您可以尝试通过类,为 vec_env_cls make_vec_env

违约 subprocvecenv 更好(因为它创建了实际的子程序)。

You can try to pass the SubprocVecEnv class as the vec_env_cls arguments of make_vec_env.

By default make_vec_env uses the DummyVecEnv wrapper to vectorize the environment. This does not actually create subprocesses, but it calls each environment in sequence.
It is good for simple environments (such as CartPole), when the overhead of multiprocessing outweighs the environment computation time, but for more computationally heavy environments, SubprocVecEnv is better (as it creates actual subprocesses).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文