【RLlib使用指南】Soft Actor Critic (SAC)

2023-11-23 14:31

本文主要是介绍【RLlib使用指南】Soft Actor Critic (SAC),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1 概要

SAC 的框架与 DQN 相似。Rollout Workers 负责采样,Learner 负责训练参数,然后更新 Rollout Workers。

在这里插入图片描述

2 相关参数设置

DEFAULT_CONFIG = with_common_config({# === Model ===# 使用两个 Q-网络(而不是一个)进行动作价值估计。# Note: 每一个 Q-网络都有自己的 target 网络."twin_q": True,# 使用(例如 conv2D)状态预处理网络,然后将结果(特征)向量与 Q-网络输入的动作输入连接起来。"use_state_preprocessor": DEPRECATED_VALUE,# Q-网络的模型选项。这些设置将覆盖 MODEL_DEFAULTS。# `Q_model` dict 在设置 Q-网络时被视为顶级 `model` dict(如果 twin_q=True 则为 2)。# 这意味着,您可以针对不同的观察空间进行操作:# obs=Box(1D) -> Tuple(Box(1D) + Action) -> concat -> post_fcnet# obs=Box(3D) -> Tuple(Box(3D) + Action) -> vision-net -> concat w/ action#   -> post_fcnet# obs=Tuple(Box(1D), Box(3D)) -> Tuple(Box(1D), Box(3D), Action)#   -> vision-net -> concat w/ Box(1D) and action -> post_fcnet# 您还可以让 SAC 使用您的 custom_model 作为 Q-model,# 只需在下面的 dict 中指定 `custom_model` 子键(就像您在顶级 `model` dict 中所做的那样)。"Q_model": {"fcnet_hiddens": [256, 256],"fcnet_activation": "relu","post_fcnet_hiddens": [],"post_fcnet_activation": None,"custom_model": None,  # Use this to define custom Q-model(s)."custom_model_config": {},},# 策略功能的模型选项(有关详细信息,请参阅上面的“Q_model”)。# 与上面的 `Q_model` 不同的是,在 post_fcnet 堆栈之前不执行任何操作连接。"policy_model": {"fcnet_hiddens": [256, 256],"fcnet_activation": "relu","post_fcnet_hiddens": [],"post_fcnet_activation": None,"custom_model": None,  # Use this to define a custom policy model."custom_model_config": {},},# 动作已经标准化,无需进一步剪辑。"clip_actions": False,# === Learning ===# 更新 target: \tau * policy + (1-\tau) * target_policy."tau": 5e-3,# 用于熵权 alpha 的初始值。"initial_alpha": 1.0,# 目标熵下限。 If "auto", will be set to -|A| (e.g. -2.0 for# Discrete(2), -3.0 for Box(shape=(3,))).# This is the inverse of reward scale, and will be optimized automatically."target_entropy": "auto",# N-步 target 更新。 If >1, sars' tuples in trajectories will be# postprocessed to become sa[discounted sum of R][s t+n] tuples."n_step": 1,# 在单个“train()”调用中累积的最小 env 采样时间步长。 # 此值不影响学习,仅影响 `Trauber.train()` 调用 `Trainer.step_attempt()` 的次数。# If - after one `step_attempt()`, the env sampling# timestep count has not been reached, will perform n more `step_attempt()` calls# until the minimum timesteps have been executed. Set to 0 for no minimum timesteps."min_sample_timesteps_per_reporting": 100,# === Replay buffer ==="replay_buffer_config": {# 启用新的 ReplayBuffer API"_enable_replay_buffer_api": True,"type": "MultiAgentPrioritizedReplayBuffer","capacity": int(1e6),# 在学习开始之前要对模型采样多少步"learning_starts": 1500,# 一次重放的连续环境步骤数。 这可以设置为大于 1 以支持循环模型"replay_sequence_length": 1,# 如果 True,优先重放缓冲区将被使用。"prioritized_replay": False,"prioritized_replay_alpha": 0.6,# 用于从优先重放缓冲区中采样的 Beta 参数"prioritized_replay_beta": 0.4,# 更新优先级时添加到 TD 错误的 Epsilon"prioritized_replay_eps": 1e-6,},# 将此设置为 True,如果您希望缓冲区的内容也存储在任何已保存的检查点中。# Warnings will be created if:# - This is True AND restoring from a checkpoint that contains no buffer#   data.# - This is False AND restoring from a checkpoint that does contain#   buffer data."store_buffer_in_checkpoints": False,# Whether to LZ4 compress observations"compress_observations": False,# The intensity with which to update the model (vs collecting samples from# the env). If None, uses the "natural" value of:# `train_batch_size` / (`rollout_fragment_length` x `num_workers` x# `num_envs_per_worker`).# If provided, will make sure that the ratio between ts inserted into and# sampled from the buffer matches the given value.# Example:#   training_intensity=1000.0#   train_batch_size=250 rollout_fragment_length=1#   num_workers=1 (or 0) num_envs_per_worker=1#   -> natural value = 250 / 1 = 250.0#   -> will make sure that replay+train op will be executed 4x as#      often as rollout+insert op (4 * 250 = 1000).# See: rllib/agents/dqn/dqn.py::calculate_rr_weights for further details."training_intensity": None,# === Optimization ==="optimization": {"actor_learning_rate": 3e-4,"critic_learning_rate": 3e-4,"entropy_learning_rate": 3e-4,},# 如果不是 None,则在优化期间以该值剪辑渐变"grad_clip": None,# Update the replay buffer with this many samples at once. Note that this# setting applies per-worker if num_workers > 1."rollout_fragment_length": 1,# 从重播缓冲区中采样以进行训练的批量采样的大小"train_batch_size": 256,# 每 `target_network_update_freq` 步骤更新目标网络"target_network_update_freq": 0,# === Parallelism ===# Whether to use a GPU for local optimization."num_gpus": 0,# Number of workers for collecting samples with. This only makes sense# to increase if your environment is particularly slow to sample, or if# you"re using the Async or Ape-X optimizers."num_workers": 0,# Whether to allocate GPUs for workers (if > 0)."num_gpus_per_worker": 0,# Whether to allocate CPUs for workers (if > 0)."num_cpus_per_worker": 1,# Whether to compute priorities on workers."worker_side_prioritization": False,# Prevent reporting frequency from going lower than this time span."min_time_s_per_reporting": 1,# Whether the loss should be calculated deterministically (w/o the# stochastic action sampling step). True only useful for cont. actions and# for debugging!"_deterministic_loss": False,# Use a Beta-distribution instead of a SquashedGaussian for bounded,# continuous action spaces (not recommended, for debugging only)."_use_beta_distribution": False,# Deprecated.# The following values have moved because of the new ReplayBuffer API."prioritized_replay": DEPRECATED_VALUE,"prioritized_replay_alpha": DEPRECATED_VALUE,"prioritized_replay_beta": DEPRECATED_VALUE,"prioritized_replay_eps": DEPRECATED_VALUE,"learning_starts": DEPRECATED_VALUE,"buffer_size": DEPRECATED_VALUE,"replay_batch_size": DEPRECATED_VALUE,"replay_sequence_length": DEPRECATED_VALUE,
})

3 简单案例

from ray import tune
tune.run(“SAC”, config={"env": "CartPole-v0", "train_batch_size": 4000}
)

随后出现训练中的状态情况:
在这里插入图片描述
可以看到训练了一段时间后,reward 的最大值和平均值都有所增加:
在这里插入图片描述

这篇关于【RLlib使用指南】Soft Actor Critic (SAC)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/418581

相关文章

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

sqlite3 命令行工具使用指南

《sqlite3命令行工具使用指南》本文系统介绍sqlite3CLI的启动、数据库操作、元数据查询、数据导入导出及输出格式化命令,涵盖文件管理、备份恢复、性能统计等实用功能,并说明命令分类、SQL语... 目录一、启动与退出二、数据库与文件操作三、元数据查询四、数据操作与导入导出五、查询输出格式化六、实用功

Python虚拟环境与Conda使用指南分享

《Python虚拟环境与Conda使用指南分享》:本文主要介绍Python虚拟环境与Conda使用指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python 虚拟环境概述1.1 什么是虚拟环境1.2 为什么需要虚拟环境二、Python 内置的虚拟环境工具

Java JSQLParser解析SQL的使用指南

《JavaJSQLParser解析SQL的使用指南》JSQLParser是一个Java语言的SQL语句解析工具,可以将SQL语句解析成为Java类的层次结构,还支持改写SQL,下面我们就来看看它的具... 目录一、引言二、jsQLParser常见类2.1 Class Diagram2.2 Statement

正则表达式r前缀使用指南及如何避免常见错误

《正则表达式r前缀使用指南及如何避免常见错误》正则表达式是处理字符串的强大工具,但它常常伴随着转义字符的复杂性,本文将简洁地讲解r的作用、基本原理,以及如何在实际代码中避免常见错误,感兴趣的朋友一... 目录1. 字符串的双重翻译困境2. 为什么需要 r?3. 常见错误和正确用法4. Unicode 转换的

Python Selenium动态渲染页面和抓取的使用指南

《PythonSelenium动态渲染页面和抓取的使用指南》在Web数据采集领域,动态渲染页面已成为现代网站的主流形式,本文将从技术原理,环境配置,核心功能系统讲解Selenium在Python动态... 目录一、Selenium技术架构解析二、环境搭建与基础配置1. 组件安装2. 驱动配置3. 基础操作模

Spring Validation中9个数据校验工具使用指南

《SpringValidation中9个数据校验工具使用指南》SpringValidation作为Spring生态系统的重要组成部分,提供了一套强大而灵活的数据校验机制,本文给大家介绍了Spring... 目录1. Bean Validation基础注解常用注解示例在控制器中应用2. 自定义约束验证器定义自

Python Websockets库的使用指南

《PythonWebsockets库的使用指南》pythonwebsockets库是一个用于创建WebSocket服务器和客户端的Python库,它提供了一种简单的方式来实现实时通信,支持异步和同步... 目录一、WebSocket 简介二、python 的 websockets 库安装三、完整代码示例1.

Kotlin 作用域函数apply、let、run、with、also使用指南

《Kotlin作用域函数apply、let、run、with、also使用指南》在Kotlin开发中,作用域函数(ScopeFunctions)是一组能让代码更简洁、更函数式的高阶函数,本文将... 目录一、引言:为什么需要作用域函数?二、作用域函China编程数详解1. apply:对象配置的 “流式构建器”最

2025最新版Python3.13.1安装使用指南(超详细)

《2025最新版Python3.13.1安装使用指南(超详细)》Python编程语言自诞生以来,已经成为全球最受欢迎的编程语言之一,它简单易学易用,以标准库和功能强大且广泛外挂的扩展库,为用户提供包罗... 目录2025最新版python 3.13.1安装使用指南1. 2025年Python语言最新排名2.