lr - 如何动态更改 RLlib 训练代理的学习率

标签 lr ray

我正在使用 ray RLlib 库在 5 排游戏中训练多智能体训练器。这是零和环境,所以我有代理人行为退化的问题(总是赢得第一个代理人,5 步获胜)。我有一个想法以这种方式改变代理人的学习率:首先训练第一个代理人,第二个作为随机学习率等于零。在第一个代理学习如何赢得超过 90% 的游戏转换之后。然后重复 但是在构造函数中初始化后我无法更改学习率。这可能吗?

def gen_policy(GENV, lr=0.001):
    config = {
        "model": {
            "custom_model": 'GomokuModel',
            "custom_options": {"use_symmetry": True, "reg_loss": 0},
        },
        "custom_action_dist": Categorical,
        "lr": lr
    }
    return (None, GENV.observation_space, GENV.action_space, config)

def map_fn(agent_id):
    if agent_id=='agent_0':
        return "policy_0"
    else:
        return "policy_1"

trainer = ray.rllib.agents.a3c.A3CTrainer(env="GomokuEnv", config={
        "multiagent": {
            "policies": {"policy_0": gen_policy(GENV, lr = 0.001), "policy_1": gen_policy(GENV,lr=0)},
            "policy_mapping_fn": map_fn,
            },
        "callbacks":
            {"on_episode_end": clb_episode_end},


while True:
    rest = trainer.train()
    #here I want to change learning rate of my policies based on environment statistics

我尝试在 True 循环中添加这些行

new_config = trainer.get_config()
new_config["multiagent"]["policies"]["policy_0"]=gm.gen_policy(GENV, lr = 0.00321)
new_config["multiagent"]["policies"]["policy_1"]=gm.gen_policy(GENV, lr = 0.00175)

trainer["raw_user_config"]=new_config
trainer.config = new_config

没用

最佳答案

我偶然发现了同样的问题,并对 RLlib 的实现做了一些研究。

从测试脚本来看,lr_schedule 是由类似这样的间隔给出的

lr_schedule: [
            [0, 0.0005],
            [20000000, 0.000000000001],
        ]

之后我检查了实现细节。
ray/rllib/policy/torch_policy.py LearningRateSchedule 函数实现入口点。
定义 lr_schedule 时,将使用 PiecewiseSchedule

来自 ray/rllib/utils/schedules/piecewise_schedule.py PiecewiseSchedule 的实现如下:

endpoints (List[Tuple[int,float]]): A list of tuples
                `(t, value)` such that the output
                is an interpolation (given by the `interpolation` callable)
                between two values.
                E.g.
                t=400 and endpoints=[(0, 20.0),(500, 30.0)]
                output=20.0 + 0.8 * (30.0 - 20.0) = 28.0
                NOTE: All the values for time must be sorted in an increasing
                order.

这意味着学习率计划由两个参数组成:
时间步长 t (int) 和支持学习率 (float)

对于这些值之间的每个时间步,使用插值。
可以通过参数 interpolation 在函数 PiecewiseSchedule 内指定插值,默认为 _linear_interpolation

interpolation (callable): A function that takes the left-value,
                the right-value and an alpha interpolation parameter
                (0.0=only left value, 1.0=only right value), which is the
                fraction of distance from left endpoint to right endpoint.

TL;DR;

因此lr_schedule描述了线性插值的支持点(使用默认插值)。

此外,在训练期间更改参数 Github Issue最好的选择似乎是重新初始化训练器:

state = trainer.save()
trainer.stop()
#re_initialise trainer
trainer.restore(state)

关于lr - 如何动态更改 RLlib 训练代理的学习率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57745954/

相关文章:

python - 并行运行 openai-gym 环境

python - 如何让 Actor 同时做两件事?

python - 如何在 Ray 中使用 python 日志记录?

python - 如何使用 python Ray 并行处理一个大列表?

python - 限制 Ray 的 CPU 资源

algorithm - 解析上下文无关文法

c - 是否存在没有 LL(1) 等价物的 LR(k) 文法

java - 任何适用于 Java 的 L(AL)R 语法