← Notebook← 返回笔记

Understanding On-policy and Off-policy理解 On-policy 与 Off-policy

Who collects the experience, and who learns from it? A note on behavior policies, target policies, SARSA, and Q-learning.谁收集经验,谁从经验中学习?从行为策略与目标策略出发,理解 SARSA 与 Q-learning。

Policy & experience策略与经验
πBehavior行为策略
τExperience经验
πTarget目标策略
The behavior and target policies are the same.
1 / 3

This article discusses the difference between on-policy and off-policy methods in reinforcement learning. On-policy methods use data derived from the same policy to optimize the target policy, while off-policy methods use data from a behavior policy for optimization. The concepts are exemplified with SARSA and Q-learning.

First, let’s define our subject matter: whether it’s on-policy or off-policy, the ‘policy’ in question here refers to the target policy. The key difference between the two lies in the source of the data used to optimize the target policy. Here, ‘source’ refers to the target policy itself.

As Sutton once said:

On-policy methods attempt to evaluate or improve the policy that is used to make decisions, whereas off-policy methods evaluate or improve a policy different from that used to generate the data.

In an ideal world, we would like to head straight for the optimal policy and identify the target policy. However, in reality, merely using the currently known optimal options may lead to convergence to a local optimum. Adding some degree of exploration could slow down the learning efficiency, which is a drawback of on-policy methods. To balance exploration and exploitation, we use an Epsilon-greedy strategy.

In the case of off-policy methods, there are two types of policies. First, we generate a large amount of behavior data under a certain probability distribution (behavior policy), with the intention of exploration. Then, from this data that deviates (is off) from the optimal policy, we seek the target policy.

Therefore, on-policy methods use data that is dependent on itself to update the target policy, hence often referred to as “same policy”. On the other hand, off-policy updates are independent of the target policy itself, hence referred to as “different policy”.

Typically, this difference manifests when calculating the \(Q(s, a)\) value, where:

  • In on-policy, the next action \(a'\) is generated by the target policy itself.

  • In off-policy, the next action \(a'\) can be generated by the behavior policy.

We can better understand this through the update methods of SARSA and Q-learning:

  • When SARSA calculates the \(Q\) function, \(a'\) here is obtained from the current target policy.

  • In Q-learning when calculating the \(Q\) function, \(a'\) here stands for all actions. This equation essentially means taking out \(s'\) and all actions to calculate a \(Q\) value, then selecting the maximum value among them.

本文讨论强化学习中 On-policy 与 Off-policy 方法的区别。On-policy 使用同一策略产生的数据来优化目标策略;Off-policy 则可以使用行为策略产生的数据。文中以 SARSA 和 Q-learning 为例理解这两个概念。

首先明确讨论的对象:无论 On-policy 还是 Off-policy,这里的 policy 都围绕我们希望评估或改进的目标策略展开。两者的关键区别,是优化目标策略所使用的数据来自哪里,以及产生数据的策略是否就是目标策略本身。

Sutton 对这一区别的表述是:

On-policy 方法尝试评估或改进实际用于决策的策略;Off-policy 方法评估或改进的策略,则与产生数据时使用的策略不同。

理想情况下,我们希望直接找到最优策略。但在实际学习中,如果只使用当前看来最好的选项,就可能陷入局部最优。因此,我们需要一定程度的探索。探索可能放慢学习过程,却能帮助发现更好的行动。为了兼顾探索与利用,可以使用 Epsilon-greedy 策略。

Off-policy 方法区分两种策略:首先,用行为策略在某个概率分布下生成经验,以进行探索;然后,利用这些可能偏离目标策略的经验,学习我们希望得到的目标策略。

因此,On-policy 使用与目标策略自身相关的数据更新目标策略,也常被称为“同策略”;Off-policy 使用的数据可以来自另一种策略,也就是“异策略”。

这种区别经常体现在 \(Q(s, a)\) 的更新中:

  • 在 On-policy 中,下一步动作 \(a'\) 由目标策略自身选出。
  • 在 Off-policy 中,经验中的下一步动作可以来自行为策略,学习目标则可以对应另一种策略。

用 SARSA 与 Q-learning 的更新方式,可以进一步理解:

  • SARSA 更新 \(Q\) 函数时,使用当前策略实际选择的下一步动作 \(a'\)。
  • Q-learning 更新 \(Q\) 函数时,考察下一状态 \(s'\) 下的动作价值,并选择其中的最大值作为更新目标。