惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
量子位
G
Google Developers Blog
J
Java Code Geeks
N
Netflix TechBlog - Medium
博客园 - 聂微东
宝玉的分享
宝玉的分享
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
雷峰网
雷峰网
M
MIT News - Artificial intelligence
T
Tailwind CSS Blog
V
Visual Studio Blog
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
Microsoft Azure Blog
Microsoft Azure Blog
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss

博客园 - 孤独的猫咪神

自定义贪吃蛇环境进行PPO模型训练 通过TensorBoard进行PPO参数优化 TensorBoard PPO log 解析 部分机型下,Flutter的flutter_inappwebview在使用evaluateJavascript调用postMessage失败的情况 HarmonyOS中,html 与 ets 桥接沟通 Flutter中,html 与 dart 桥接沟通 Flutter 环境变量 Mole,清理Mac的小工具 Flutter自定义主题Theme最佳实践 Ubuntu 20.04 开机自动添加git的ssh 米家 + arduino + 自定义服务器 Flutter 第三方库 Jenkins CLI 通过ssh方式链接时的证书 阿里云Ubuntu 14.04 + Nginx + .net core + MySql 移动开发网络接口 经验总结 阿里云Ubuntu 14.04 + Nginx + let's encrypt 搭建https访问 Pathoto项目:AWS+golang+beego搭建 osx开发,skport项目记录 使用Jekyll在Github上搭建博客 iOS搜索附近的位置(类似微博朋友圈位置) retrofit2中ssl的Trust anchor for certification path not found问题 Android中的Semaphore React Native 在现有项目中的探路 博客园 Linux客户端 2.0 正式发布! 博客园 Windows客户端 2.0 正式发布! 博客园 Mac客户端 2.0 正式发布!
macos(M4)上跑强化学习 - PyTorch LunarLander-v3 Stable-B...
孤独的猫咪神 · 2026-07-23 · via 博客园 - 孤独的猫咪神

安装python环境

  • 安装miniforge3
    正确安装后,就有了conda命令

配置python环境

# 创建环境,python3.11
conda create -n rl-env python=3.11
conda activate rl-env
# 关闭用这个
conda deactivate

报错‘CondaError: Run 'conda init' before 'conda activate'’ google 一下,init命令需要参数

使用PyTorch进行训练,所以需要安装PyTorch

pip3 install torch torchvision torchaudio

创建文件进行测试,是否安装成功,是否是M系列芯片

# test_env.py
import torch
print(torch.backends.mps.is_available()) # True = GPU可用
print(torch.backends.mps.is_built())

输出如图:
image
小模型完全没必要使用gpu,直接使用cpu可能速度更快。

You are trying to run PPO on the GPU, but it is primarily intended to run on the CPU when not using a CNN policy (you are using ActorCriticPolicy which should be a MlpPolicy). See https://github.com/DLR-RM/stable-baselines3/issues/1245 for more info. You can pass device='cpu' or export CUDA_VISIBLE_DEVICES= to force using the CPU.Note: The model will train, but the GPU utilization will be poor and the training might take longer than on CPU.

安装 gymnasium

pip install gymnasium
# 或
pip install "gymnasium[box2d]"
# 我用的这个带box2d的

创建文件进行测试,是否安装成功。参考自:https://zhuanlan.zhihu.com/p/694923486

# test_gymnasium.py

import gymnasium as gym
env = gym.make("LunarLander-v3", render_mode="human")
observation, info = env.reset()

for _ in range(100):
    action = env.action_space.sample()
    observation, reward, terminated, truncated, info = env.step(action)
    print('---')
    print("action: " + str(action))
    print("observation: " + str(observation))
    print("reward: " + str(reward))
    print("terminated: " + str(terminated))
    print("truncated: " + str(truncated))
    print("info: " + str(info))

    if terminated or truncated:
        observation, info = env.reset()

env.close()

生成如图:
not_training-ezgif.com-video-to-gif-converter

image

之后将使用LunarLander-v3 这个环境进行模型训练。

使用Stable-Baselines3进行训练

使用Stable-Baselines3先让流程跑通

pip install 'stable-baselines3[extra]'

写个脚本开始训练进行测试

# test_sb3.py
import gymnasium as gym
import torch
from stable_baselines3 import PPO

# 检测设备
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
print("device: ", device)

# 创建环境
env = gym.make("LunarLander-v3")

# 简单PPO测试训练
model = PPO(
    "MlpPolicy", 
    env, 
    verbose=1, 
    device=device, 
    tensorboard_log="./ppo_logs/"  # 日志存放目录
    )
model.learn(
    total_timesteps=10000,
    tb_log_name="test_01",    # 本次实验名称
    )

# 测试运行
obs, _ = env.reset()
for _ in range(1000):
    action, _states = model.predict(obs)
    obs, reward, terminated, truncated, info = env.step(action)
    if terminated or truncated:
        obs, _ = env.reset()
env.close()

# 保存模型
model.save("ppo_LunarLander")
print("done")

运行结果部分截图:
image
查看训练效果的方法,使用tensorboard

tensorboard --logdir ./ppo_logs/

运行后,则可以针对日志文件进行查看,打开 http://localhost:6006/
如下图:
image
具体这个训练效果图要如何分析,先跳过,先尝试把流程跑完。

使用训练的模型

写个脚本来使用模型,看看效果:

# test_use_ppo.py
import gymnasium as gym
import torch
from stable_baselines3 import PPO

# 检测设备
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
print("device: ", device)

model = PPO.load("ppo_LunarLander", device=device)

env = gym.make("LunarLander-v3", render_mode="human")
obs, _ = env.reset()

for _ in range(1000):
    # 模型预测动作
    action, _states = model.predict(obs, deterministic=True)
    obs, reward, terminated, truncated, info = env.step(action)
    
    if terminated or truncated:
        obs, _ = env.reset()

env.close()

效果如下图所示:
training-ezgif.com-video-to-gif-converter

至此,训练就完成了。
剩下就可以一步一步针对全流程中的内容进行拆解学习。