学习资料 理解深度学习 第 19 章 · 强化学习
学 习 资 料 · 中 英 对 照

第 19 章 · 强化学习

Understanding Deep Learning — Reinforcement learning

强化学习
Reinforcement learning

Reinforcement learning (RL) is a sequential decision-making framework in which agents learn to perform actions in an environment with the goal of maximizing received rewards. For example, an RL algorithm might control the moves (actions) of a character (the agent) in a video game (the environment), aiming to maximize the score (the reward). In robotics, an RL algorithm might control the movements (actions) of a robot (the agent) in the world (the environment) to perform a task (earning a reward). In finance, an RL algorithm might control a virtual trader (the agent) who buys or sells assets (the actions) on a financial exchange (the environment) to maximize profit (the reward).

Consider learning to play chess. Here, there is a reward of +1, −1, or 0 at the end of the game if the agent wins, loses, or draws and 0 at every other time step. This illustrates the challenges of RL. First, the reward is sparse; here, we must play an entire game to receive feedback. Second, the reward is temporally offset from the action that caused it; a decisive advantage might be gained thirty moves before victory. We must associate the reward with this critical action. This is termed the temporal credit assignment problem. Third, the environment is stochastic; the opponent doesn’t always make the same move in the same situation, so it’s hard to know if an action was truly good or just lucky. Finally, the agent must balance exploring the environment (e.g., trying new opening moves) with exploiting what it already knows (e.g., sticking to a previously successful opening). This is termed the exploration-exploitation trade-off.

Reinforcement learning is an overarching framework that does not necessarily require deep learning. However, in practice, state-of-the-art systems often use deep networks. They encode the environment (the video game display, robot sensors, financial time series, or chessboard) and map this directly or indirectly to the next action (figure 1.13).

强化学习(RL)是一种序贯决策框架,其中智能体学习在环境中执行动作,以最大化所获得的奖励。例如,一个强化学习算法可以控制电子游戏中角色(智能体)的移动(动作),以最大化得分(奖励)。在机器人领域,强化学习算法可以控制机器人(智能体)在世界(环境)中的运动(动作),以完成某项任务(获得奖励)。在金融领域,强化学习算法可以控制虚拟交易员(智能体),在金融交易所(环境)中买卖资产(动作),以最大化利润(奖励)。

考虑学习下国际象棋。在这里,如果智能体获胜、失败或平局,则在游戏结束时分别获得 +1、 −1 或 0 的奖励,而在其他所有时间步的奖励均为 0。这说明了强化学习面临的挑战。首先,奖励是稀疏的;在这里,我们必须下完整局游戏才能获得反馈。其次,奖励与导致它的动作在时间上存在错位;决定性的优势可能在获胜前三十步就已获得。我们必须将奖励与这一关键动作关联起来。这被称为时序信用分配问题。第三,环境具有随机性;对手在同一情境下并不总是走出相同的棋步,因此很难判断某个动作究竟是真正的好棋还是仅仅出于运气。最后,智能体必须在探索环境(例如尝试新的开局着法)与利用已有知识(例如坚持以往成功的开局)之间取得平衡。这被称为探索 - 利用权衡。

强化学习是一个总体框架,并不一定需要深度学习。然而,在实践中,最先进的系统通常使用深度网络。它们对环境的编码(电子游戏画面、机器人传感器、金融时间序列或棋盘),并直接或间接地将其映射到下一个动作(图 1.13)。

19.1 马尔可夫决策过程、回报与策略
19.1 Markov decision processes, returns, and policies

Reinforcement learning maps observations of an environment to actions, aiming to maximize a numerical quantity that is connected to the rewards received. In the most common case, we learn a policy that maximizes the expected return in a Markov decision process. This section explains these terms.

强化学习将环境观测映射为动作,旨在最大化与所获奖励相关的数值量。在最常见的情况下,我们学习一个策略,以最大化马尔可夫决策过程中的期望回报。本节解释这些术语。

图 19.1 马尔可夫过程。马尔可夫过程由一组状态和转移概率  Pr (st+1|st) 组成,这些概率定义了在当前状态为 st 的情况下转移到状态  st+1
图 19.1 马尔可夫过程。马尔可夫过程由一组状态和转移概率 Pr (st+1|st) 组成,这些概率定义了在当前状态为 st 的情况下转移到状态 st+1 的概率。 a) 企鹅可以在冰面上访问 16 个不同的位置(状态)。 b) 冰面很滑,因此在每个时刻,它转移到任意相邻状态的概率相等。例如,在位置 6,它有 25% 的概率转移到状态 2、 5、 7 和 10。该过程的一条轨迹 τ=[s1, s2, s3,...] 由一系列状态组成。Figure 19.1 Markov process. A Markov process consists of a set of states and transition probabilities Pr(st+1|st) that define the probability of moving to state st+1 given the current state is st. a) The penguin can visit 16 different positions (states) on the ice. b) The ice is slippery, so at each time, it has an equal probability of moving to any adjacent state. For example, in position 6, it has a 25% chance of moving to states 2, 5, 7, and 10. A trajectory τ=[s1, s2, s3,...] from this process consists of a sequence of states.
19.1.1 马尔可夫过程
19.1.1 Markov process

A Markov process assumes that the world is always in one of a set of possible states. The word Markov implies that the probability of being in a state depends only on the previous state and not on the states before. The changes between states are captured by the transitionprobabilities Pr(st+1|st) of moving to the next state st+1 given the current state st, where t indexes the time step. Hence, a Markov process is an evolving system that produces a sequence s1, s2, s3... of states (figure 19.1).

一个马尔可夫过程假设世界始终处于一组可能状态中的某一个状态。马尔可夫一词意味着处于某个状态的概率仅取决于前一个状态,而不取决于更早的状态。状态之间的变化由转移概率 Pr (st+1|st) 刻画,它表示在给定当前状态 st 的情况下转移到下一状态 st+1 的概率,其中 t 用于索引时间步。因此,马尔可夫过程是一个不断演化的系统,它产生一个状态序列 s1, s2, s3... (图 19.1)。

19.1.2 马尔可夫奖励过程
19.1.2 Markov reward process

A Markov reward process extends the Markov process to include a distribution Pr(rt+1|st) over the possible rewards rt+1 received at the next time step, given that we are in state st. This produces a sequence s1, r2, s2, r3, s3, r4... of states and the associated rewards (figure 19.2). The Markov reward process also includes a discountfactorγ∈(0, 1] that is used to compute the returnGt at time t:

这会产生一个状态序列 s1, r2, s2, r3, s3, r4... 及其相关奖励(图 19.2)。马尔可夫奖励过程还包括一个折扣因子 γ∈ (0, 1] 用于计算时间 t 的回报 Gt:

The return is the sum of the cumulative discounted future rewards; it measures the future benefit of being on this trajectory. A discount factor of less than one makes rewards that are closer in time more valuable than rewards that are further away.

回报是累计折现未来奖励的总和;它衡量处于该轨迹上的未来收益。折扣因子小于 1 会使时间上更近的奖励比更远的奖励更有价值。

图 19.2 马尔可夫奖励过程。它将奖励 rt+1 的分布 Pr (rt+1|st) 与每个状态 st 相关联。 a) 这里,奖励是确定性的;如果企鹅落在鱼上,
图 19.2 马尔可夫奖励过程。它将奖励 rt+1 的分布 Pr (rt+1|st) 与每个状态 st 相关联。 a) 这里,奖励是确定性的;如果企鹅落在鱼上,将获得 +1 的奖励,否则为 0。轨迹 τ 现在由交替的状态和奖励组成的序列 s1, r2, s2, r3, s3, r4... 构成,在八步后终止。该序列的回报 Gt 是折现未来奖励之和,此处折现因子为 γ= 0.9。 b-c) 随着企鹅沿轨迹前进并更接近获得奖励,回报增加。Figure 19.2 Markov reward process. This associates a distribution Pr(rt+1|st) of rewards rt+1 with each state st. a) Here, the rewards are deterministic; the penguin will receive a reward of +1 if it lands on a fish and 0 otherwise. The trajectory τ now consists of a sequence s1, r2, s2, r3, s3, r4... of alternating states and rewards, terminating after eight steps. The return Gt of the sequence is the sum of discounted future rewards, here with discount factor γ= 0.9. b-c) As the penguin proceeds along the trajectory and gets closer to reaching the rewards, the return increases.
图 19.3 马尔可夫决策过程。 a) 智能体(企鹅)在每个状态下可以执行一组动作中的一个。 b) 这里,四个动作分别对应向上、向右、向下和向左移动。 c) 对
图 19.3 马尔可夫决策过程。 a) 智能体(企鹅)在每个状态下可以执行一组动作中的一个。 b) 这里,四个动作分别对应向上、向右、向下和向左移动。 c) 对于任意状态(这里为状态 6),动作会改变转移到下一状态的概率。企鹅有 50% 的概率朝预期方向移动,但冰面很滑,因此它也可能以相等的概率滑向其他相邻位置之一。因此,在图 (a) 中,所采取的动作(灰色箭头)并不总是与轨迹(橙色线)一致。通常,动作也可以影响获得奖励的概率,但在这个例子中,无论采取什么动作,奖励都相同,所以 Pr (rt+1|st, at) =Pr (rt+1|st)。来自马尔可夫决策过程的轨迹 τ 由成。请注意,在这里,企鹅在离开一个有鱼的状态时获得奖励(即,只要经过鱼格就会获得奖励,无论企鹅是否有意到达那里)。Figure 19.3 Markov decision process. a) The agent (penguin) can perform one of a set of actions in each state. b) Here, the four actions correspond to moving up, right, down, and left. c) For any state (here, state 6), the action changes the probability of moving to the next state. The penguin moves in the intended direction with 50% probability, but the ice is slippery, so it may slide to one of the other adjacent positions with equal probability. (a), the action taken (gray arrows) doesn’t always line up with the trajectory (orange line). In general, the action can also influence the probability of receiving rewards, but in this example the reward is the same regardless of the action, so Pr(rt+1|st, at)=Pr(rt+1|st). The trajectory τ from an MDP consists of a sequence s1, a1, r2, s2, a2, r3, s3, a3, r4... of alternating states st, actions at, and rewards, rt+1. Note that here the penguin receives the reward when it leaves a state with a fish (i.e., the reward is received for passing through the fish square, regardless of whether the penguin arrived there intentionally or not). Accordingly, in panel
图 19.4 部分可观测马尔可夫决策过程(POMDP)。智能体看不到完整状态。这里企鹅只能看到附近的格子(虚线框)。真实状态(三)与它在状态九中会看到的情况无法
图 19.4 部分可观测马尔可夫决策过程(POMDP)。智能体看不到完整状态。这里企鹅只能看到附近的格子(虚线框)。真实状态(三)与它在状态九中会看到的情况无法区分。第一种情况下向右会进入冰洞(回报 −2),第二种情况下则会到达鱼。Figure 19.4 Partially observable Markov decision process (POMDP). The agent does not see the whole state. Here the penguin only sees nearby tiles (dashed box). The true state (three) is indistinguishable from what it would see in state nine. Moving right from the first leads to the hole (−2); from the second, it leads to the fish.
图 19.5 策略。 a) 确定性策略在每个状态中总是选择相同的动作(由箭头指示)。有些策略优于其他策略。该策略并非最优,但通常仍会引导企鹅从左上角走向右下角的
图 19.5 策略。 a) 确定性策略在每个状态中总是选择相同的动作(由箭头指示)。有些策略优于其他策略。该策略并非最优,但通常仍会引导企鹅从左上角走向右下角的回报所在处。 b) 该策略更随机。 c) 随机策略为每个状态定义了一个动作概率分布(概率由箭头大小指示)。其优势在于智能体会更充分地探索状态,并且在部分可观测马尔可夫决策过程中,这可能对最优表现是必要的。为 +3)。Figure 19.5 Policies. a) A deterministic policy always chooses the same action in each state (indicated by arrow). Some policies are better than others. This policy is not optimal but still generally steers the penguin from top-left to bottom-right where the reward lies. b) This policy is more random. c) A stochastic policy has a probability distribution over actions for each state (probability indicated by size of arrows). This has the advantage that the agent explores the states more thoroughly and can be necessary for optimal performance in partially observable Markov decision processes. the fish (with +3 reward).
图 19.6 强化学习循环。智能体根据状态 s_t,按照策略 π[a_t|s_t],在时间 t 采取动作 a_t。这会通过状态转移函数产生新状态 s_{t+1}
图 19.6 强化学习循环。智能体根据状态 s_t,按照策略 π[a_t|s_t],在时间 t 采取动作 a_t。这会通过状态转移函数产生新状态 s_{t+1},并通过奖励函数产生奖励 r_{t+1}。二者传回智能体,然后智能体选择下一个动作。Figure 19.6 Reinforcement learning loop. The agent takes action a_t at time t from state s_t according to the policy π[a_t|s_t]. That produces a new state s_{t+1} and a reward r_{t+1}. Both are passed back, and the agent then chooses the next action.
19.1.3 马尔可夫决策过程
19.1.3 Markov decision process

A Markov decision process or MDP adds a set of possible actions at each time step. The action at changes the transition probabilities, which are now written as Pr(st+1|st, at). The rewards can also depend on the action and are now written as Pr(rt+1|st, at). An MDP produces a sequence(s1, a1, r2), (s2, a2, r3), (s3, a3, r4)... of states st, actions at, and rewards rt+1 which are received at the subsequent time-step (figure 19.3). The entity that performs the actions is known as the agent.

一个马尔可夫决策过程或 MDP 在每个时间步引入一组可能的动作。动作 at 会改变转移概率,现在写为 Pr (st+1|st, at)。奖励也可以依赖于动作,现在写为 Pr (rt+1|st, at)。 MDP 会产生一个序列 (s1, a1, r2) , (s2, a2, r3) , (s3, a3, r4) ...,由状态 st、动作 at 和奖励 rt+1 组成,其中奖励在随后的时间步接收(图 19.3)。执行动作的实体称为智能体。

19.1.4 部分可观测马尔可夫决策过程
19.1.4 Partially observable Markov decision process

In a partially observable Markov decision process or POMDP, the state is not directly visible (figure 19.4). Instead, the agent receives an observation ot drawn from Pr(ot|st). Hence, a POMDP generates a sequence s1, o1, a1, r2, s2, o2, a2, r3, o3, a3, s3, r4,... of states, observations, actions, and rewards. In general, each observation will be more compatible with some states than others but insufficient to identify the state uniquely.

在部分可观测马尔可夫决策过程或 POMDP 中,状态不可直接观测(图 19.4)。相反,智能体接收一个观测值 ot,该观测值从 Pr (ot|st) 中抽取。因此, POMDP 会生成一个

每个观测值与某些状态的相容性高于其他状态,但不足以唯一识别状态。

19.1.5 策略
19.1.5 Policy

The rules that determine the agent’s action for each state are known as the policy (figure 19.5). This may be stochastic (the policy defines a distribution over actions for each state) or deterministic (the agent always takes the same action in a given state). A stochastic policy π[a|s] returns a probability distribution over each possible action a for state s, from which a new action is sampled. A deterministic policy π[a|s] returns one for the action a that is chosen for state s and zero otherwise. A stationary policy depends only on the current state. A non-stationary policy also depends on the time step. The environment and the agent form a loop (figure 19.6). The agent receives the state st and reward rt from the last time step. Based on this, it can modify the policy π[at|st] if desired and choose the next action at. The environment then advances to the

确定智能体在每个状态下的动作的规则被称为策略(图 19.5)。它可以是随机的(策略为每个状态定义一个动作分布),也可以是确定性的(智能体在给定状态下始终采取相同动作)。随机策略 π[a|s] 返回每个可能动作 a 在状态 s 下的概率分布,并从中采样一个新动作。确定性策略 π[a|s] 对动作 a 返回 1,该动作是为状态 s 选择的,否则返回 0。平稳的策略仅依赖当前状态。非平稳的策略还依赖时间步。环境与智能体形成一个循环(图 19.6)。智能体接收

next state according to Pr(st+1|st, at) and issues a reward according to Pr(rt+1|st, at).

19.2 期望回报
19.2 Expected return

The previous section introduced the Markov decision process and the idea of an agent carrying out actions according to a policy. We want to choose a policy that maximizes the expected return. In this section, we make this idea mathematically precise. To do that, we assign a value to each state st and state-action pair{st, at}.

上一节介绍了马尔可夫决策过程以及智能体根据策略执行动作的思想。我们想要选择一个最大化期望回报的策略。在本节中,我们将这一思想用数学方式精确化。为此,我们为每个状态 st 和状态 - 动作对 {st, at} 赋予一个价值。

19.2.1 状态与动作值
19.2.1 State and action values

The return Gt depends on the state st and the policy π[a|s]. From this state, the agent will pass through a sequence of states, taking actions and receiving rewards. This

sequence differs every time the agent starts in the same place since, in general, the policy π[at|st], the state transitions Pr(st+1|st, at), and the rewards issued Pr(rt+1|st, at) are all stochastic.

回报 Gt 取决于状态 st 和策略 π[a|s]。从该状态出发,智能体将经过一系列状态,采取行动并获得奖励。这一

序列每次智能体从相同位置开始时都不同,因为一般而言,策略 π[at|st]、状态转移 Pr (st+1|st, at) 以及发放的奖励 Pr (rt+1|st, at) 都是随机的。

图 19.7 状态和动作值。 a) v[st|π] 是状态 st (每个位置处的数字)在给定策略 π (灰色箭头)下的期望回报。它是在许多从该状态开始的轨迹中收
图 19.7 状态和动作值。 a) v[st|π] 是状态 st (每个位置处的数字)在给定策略 π (灰色箭头)下的期望回报。它是在许多从该状态开始的轨迹中收到的折扣奖励之和的平均值。这里,越靠近鱼的状态越有价值。 b) q[st, at, π] 是动作 at 在状态 st (每个位置 / 状态处的四个数字,分别对应四个动作)中的期望回报。在这种情况下,越靠近鱼,该值越大;而且,朝鱼方向的动作对应的值也更大。 c) 如果我们知道某个状态处的动作值,则可以修改策略,使其选择这些值中的最大值(面板 b 中的红色数字)。Figure 19.7 State and action values. a) The value v[st|π] of a state st (number at each position) is the expected return for this state for a given policy π (gray arrows). It is the average sum of discounted rewards received over many trajectories started from this state. Here, states closer to the fish are more valuable. b) The value q[st, at, π] of an action at in state st (four numbers at each position/state corresponding to four actions) is the expected return given that this particular action is taken in this state. In this case, it gets larger as we get closer to the fish and is larger for actions that head in the direction of the fish. c) If we know the action values at a state, then the policy can be modified so that it chooses the maximum of these values (red numbers in panel b).

We can characterize how “good” a state is under a given policy π by considering the expected return v[st|π]. This is the return that would be received on average from sequences that start from this state and is termed the state value or state-value function (figure 19.7a):

Informally, the state value tells us the long-term reward we can expect on average if we start in this state and follow the specified policy thereafter. It is highest for states where it’s probable that subsequent transitions will bring large rewards soon (assuming the discount factor γ is less than one).

在给定策略 π 下,我们可以通过考虑来刻画一个状态有多“好”该期望回报 v[st|π]。这是从该状态开始的序列平均可获得的回报,称为状态值或状态值函数(图 19.7a):

非正式地说,状态值告诉我们,如果从该状态开始并随后遵循指定策略,平均可以获得的长期回报。对于后续转移很可能很快带来大量回报的状态,其值最高(假设折扣因子 γ 小于一)。

Similarly, the actionvalue or state-actionvaluefunctionq[st, at|π] is the expected return from executing action at in state st (figure 19.7b):

The action value tells us the long-term reward we can expect on average if we start in this state, take this action, and follow the specified policy thereafter. Through this quantity, reinforcement learning algorithms connect future rewards to current actions (i.e., resolve the temporal credit assignment problem).

动作价值告诉我们,如果从该状态出发、执行该动作,并此后遵循指定策略,我们平均可以期望获得的长期回报。通过这一量,强化学习算法将未来回报与当前动作联系起来(即解决时序信用分配问题)。

19.2.2 最优策略
19.2.2 Optimal policy

We want a policy that maximizes the expected return. For MDPs (but not POMDPs), there is always a deterministic, stationary policy that maximizes the value of every state. If we know this optimal policy, then we get the optimal state-value function v∗[st]:

公式

Indeed, some reinforcement learning algorithms are based on alternately estimating the action values and the policy (see section 19.3).

at 来导出最优策略(图 19.7c): 1

实际上,一些强化学习算法基于交替估计动作价值和策略(见第 19.3 节)。

19.2.3 贝尔曼方程
19.2.3 Bellman equations

We may not know the state values v[st] or action values q[st, at] for any policy. 2 However, we know that they must be consistent with one another, and it’s easy to write relations between these quantities. The state value v[st] can be found by taking a weighted sum of the action values q[st, at], where the weights depend on the probability under the policy π[at|st] of taking that action (figure 19.8):

我们可能不知道任何策略下的状态值 v[st] 或动作值 q[st, at]。 2 然而,我们知道它们必须彼此一致,并且很容易写出这些量之间的关系。状态值 v[st] 可以通过对动作值 q[st, at] 取加权和来求得,其中权重取决于在策略 π[at|st] 下采取该动作的概率(图 19.8):

Similarly, the value of an action is the immediate reward rt+1=r[st, at] generated by taking the action, plus the value v[st+1] of being in the subsequent state st+1 discounted by γ (figure 19.9). 3 Since the assignment of st+1 is not deterministic, we weight the values v[st+1] according to the transition probabilities Pr(st+1|st, at):

Substituting equation 19.8 into equation 19.7 provides a relation between the state value at time t and t + 1:

同样,动作的价值是采取该动作产生的即时奖励 rt+1=r[st, at],加上处于后续状态 st+1 的价值 v[st+1] 经 γ 折扣后的值(图 19.9)。 3 由于 st+1 的赋值不是确定性的,我们根据转移概率 Pr 为值 v[st+1] 分配权重(st+1|st, at):

将公式 19.8 代入公式 19.7,可得到时间 t 的状态值与 t + 1 之间的关系:

图 19.8 状态值与动作值之间的关系。状态六的值 v[st =6] 是状态六处动作值 q[st =6, at] 的加权和,其中权重是采取该动作的策略概率 π[
图 19.8 状态值与动作值之间的关系。状态六的值 v[st =6] 是状态六处动作值 q[st =6, at] 的加权和,其中权重是采取该动作的策略概率 π[at|st =6]。Figure 19.8 Relationship between state values and action values. The value of state six v[st =6] is a weighted sum of the action values q[st =6, at] at state six, where the weights are the policy probabilities π[at|st =6] of taking that action.
图 19.9 动作值与状态值之间的关系。在状态六采取动作二的值 q[st = 6 , at =2] 是采取该动作获得的奖励 r[st =6, at =2],加上
图 19.9 动作值与状态值之间的关系。在状态六采取动作二的值 q[st = 6 , at =2] 是采取该动作获得的奖励 r[st =6, at =2],加上处于后继状态的折现值 v[st+1] 的加权和,其中权重为转移概率 Pr (st+1|st = 6 , at = 2)。贝尔曼方程将此关系与图 19.8 的关系连接起来,以关联当前和下一个 (i) 状态值和 (ii) 动作值。Figure 19.9 Relationship between action values and state values. The value q[st = 6, at =2] of taking action two in state six is the reward r[st =6, at =2] from taking that action plus a weighted sum of the discounted values v[st+1] of being in successor states, where the weights are the transition probabilities Pr(st+1|st = 6, at = 2). The Bellman equations chain this relation with that of figure 19.8 to link the current and next (i) state values and (ii) action values.

Similarly, substituting equation 19.7 into equation 19.8 provides a relation between the action value at time t and t + 1:

The latter two relations are the Bellman equations and are the backbone of many RL methods. In short, they say that the state (action) values have to be self-consistent. Consequently, when we update an estimate of one state (action) value, this will have a ripple effect that causes modifications to all the others.

类似地,将式 19.7 代入式 19.8,可得到时刻 t 和 t + 1 的动作值之间的关系:

后两个关系式是贝尔曼方程,也是许多强化学习方法的骨干。简言之,它们说明状态(动作)值必须自洽。因此,当我们更新一个状态(动作)值的估计时,这会产生连锁效应,导致所有其他估计值也随之改变。

19.3 表格型强化学习
19.3 Tabular reinforcement learning

Tabular RL algorithms (i.e., those that don’t rely on function approximation) are divided into model-based and model-free methods. Model-based methods 4 use the MDP structure explicitly and find the best policy from the transition matrix Pr(st+1|st, at) and reward structure r[s, a]. If these are known, this is a straightforward optimization problem that can be tackled using dynamic programming. If they are unknown, they can (in principle) be estimated from observed MDP trajectories. 5

Conversely, model-free methods assume that the transition matrix and reward structure of the underlying MDP are unknown. These methods fall into two families: 1. Value estimation approaches estimate the optimal state-action value function and

then assign the policy according to the action in each state with the greatest value. 2. Policy estimation approaches directly estimate the optimal policy using a gradient

表格型强化学习算法(即那些不依赖函数近似的方法)分为基于模型和无模型方法。基于模型的方法 4 显式利用 MDP 结构,并从转移矩阵 Pr (st+1|st, at) 和奖励结构 r[s, a] 中找到最优策略。如果这些已知,这就是一个可直接用动态规划处理的简单优化问题。如果它们未知,原则上可以根据观测到的 MDP 轨迹进行估计。 5

相反,无模型方法假设底层 MDP 的转移矩阵和奖励结构未知。这些方法分为两类: 1. 价值估计方法估计最优状态 - 动作价值函数,然后根据每个状态下价值最大的动作来分配策略。 2. 策略估计方法使用梯度下降技术直接估计最优策略,而无需估计模

descent technique without the intermediate steps of estimating the model or values.

Within each family, Monte Carlo methods simulate many trajectories through the MDP for a given policy to gather information about how to improve this policy. Sometimes it is not feasible or practical to simulate many trajectories before updating the policy. Temporal difference (TD) methods update the policy while the agent traverses the MDP.

We now briefly describe dynamic programming methods, Monte Carlo value estimation methods, and TD value estimation methods. Section 19.4 describes how deep networks have been used in TD value estimation methods. We return to policy estimation in section 19.5.

型或价值的中间步骤。

在每一类中,蒙特卡洛方法针对给定策略在 MDP 中模拟许多轨迹,以收集如何改进该策略的信息。有时,在更新策略之前模拟许多轨迹并不可行或不切实际。时序差分(TD)方法在智能体遍历 MDP 的同时更新策略。

我们现在简要介绍动态规划方法、蒙特卡洛价值估计方法和 TD 价值估计方法。第 19.4 节介绍了深度网络如何用于 TD 价值估计方法。我们在第 19.5 节回到策略估计。

图 19.10 动态规划。 a) 状态值初始化为零,策略(箭头)随机选择。 b) 状态值更新为与其相邻状态保持一致(式 19.11,显示了两次迭代后的结果)。策
图 19.10 动态规划。 a) 状态值初始化为零,策略(箭头)随机选择。 b) 状态值更新为与其相邻状态保持一致(式 19.11,显示了两次迭代后的结果)。策略更新为将智能体移动到具有最高值的状态(式 19.12)。 c) 经过多次迭代后,算法收敛到最优策略,其中企鹅试图避开洞并到达鱼。Figure 19.10 Dynamic programming. a) The state values are initialized to zero, and the policy (arrows) is chosen randomly. b) The state values are updated to be consistent with their neighbors (equation 19.11, shown after two iterations). The policy is updated to move the agent to states with the highest value (equation 19.12). c) After several iterations, the algorithm converges to the optimal policy, in which the penguin tries to avoid the holes and reach the fish.
19.3.1 动态规划
19.3.1 Dynamic programming

Dynamic programming algorithms assume we have perfect knowledge of the transition and reward structure. In this respect, they are distinguished from most RL algorithms which observe the agent interacting with the environment to gather information about these quantities indirectly.

The state values v[s] are initialized arbitrarily (usually to zero). The deterministic policy π[a|s] is also initialized (e.g., by choosing a random action for each state). The algorithm then alternates between iteratively computing the state values for the current policy (policy evaluation) and improving that policy (policy improvement).

动态规划算法假设我们拥有完全的转移和奖励结构知识。在这方面,它们与大多数 RL 算法不同,后者通过观察智能体与环境交互来间接收集有关这些量的信息。

状态值 v[s] 被任意初始化(通常为零)。确定性策略 π[a|s] 也会被初始化(例如,为每个状态选择一个随机动作)。该算法随后在迭代计算当前策略的状态值(策略评估)与改进该策略(策略改进)之间交替进行。

Policy evaluation:

We sweep through the states st, updating their values:

策略评估:我们遍历状态 st,更新其值:

公式
公式

where st+1 is the successor state and Pr(st+1|st, at) is the state transition probability. Each update makes v[st] consistent with the value at the successor state st+1 using the Bellman equation for state values (equation 19.9). This is termed bootstrapping.

Policy improvement:

To update the policy, we greedily choose the action that maximizes the value for each state:

其中 st+1 是后继状态, Pr (st+1|st, at) 是状态转移概率。每次更新都使用状态价值的贝尔曼方程(公式 19.9),使 v[st] 与后继状态 st+1 处的价值保持一致。这称为自举。

策略改进:为了更新策略,我们对每个状态贪婪地选择使价值最大化的动作:

公式

This is guaranteed to improve the policy according to the policy improvement theorem.

These two steps are iterated until the policy converges (figure 19.10). There are many variations of this approach. In policy iteration, the policy evaluation step is iterated until convergence before policy improvement. The values can be updated either in place or synchronously in each sweep. In value iteration, the policy evaluation procedure sweeps through the values just once before policy improvement. Asynchronous dynamic programming algorithms don’t have to systematically sweep through all the

根据策略改进定理,这可以保证策略得到改进。

这两个步骤会迭代执行,直到策略收敛(图 19.10)。这种方法有许多变体。在策略迭代中,策略评估步骤在策略改进之前迭代直至收敛。每次扫描时,价值可以就地更新或同步更新。在价值迭代中,策略评估过程在策略改进之前仅对价值扫描一次。异步动态规划算法不必在每一步系统地扫描所

values at each step but can update a subset of the states in place in an arbitrary order.

有价值,但可以按任意顺序就地更新状态子集。

19.3.2 蒙特卡洛方法
19.3.2 Monte Carlo methods

Unlike dynamic programming algorithms, Monte Carlo methods don’t assume knowledge of the MDP’s transition probabilities and reward structure. Instead, they gain experience by repeatedly sampling trajectories from the MDP and observing the rewards. They alternate between computing the action values (based on this experience) and updating the policy (based on the action values).

To estimate the action values q[s, a], a series of episodes are run. Each starts with a given state and action and thereafter follows the current policy, producing a series of actions, states, and rewards (figure 19.11a). The action value for a given state-action pair under the current policy is estimated as the average of the empirical returns (i.e., cumulative sums of time-discounted rewards) that follow each time this pair occurs (figure 19.11b). Then the policy is updated by choosing the action with the maximum value at every state (figure 19.11c): This is an on-policy method; the current best policy is used to guide the agent through the environment. This policy is based on the observed action values in every state, but of course, it’s not possible to estimate the value of actions that haven’t been used, and there is nothing to encourage the algorithm to explore these. One solution is to use exploring starts. Here, episodes with all possible state-action pairs are initiated, so every combination is observed at least once. However, this is impractical if the number of states is large or the starting point cannot be controlled. A different approach is to

与动态规划算法不同,蒙特卡洛方法不假设已知 MDP 的转移概率和奖励结构。相反,它们通过反复从 MDP 中采样轨迹并观察奖励来获取经验。它们在计算动作值(基于这些经验)和更新策略(基于动作值)之间交替进行。

为了估计动作值 q[s, a],会运行一系列回合。每个回合都从给定的状态和动作开始,此后遵循当前策略,产生一系列动作、状态和奖励(图 19.11a)。当前策略下给定状态动作对的动作值,估计为每次出现该对时后续经验回报(即时间折扣奖励的累计和)的平均值(图 19.11b)。然后,通过在每个状态选择具有最大值的动作来更新策略(图 19.11c):这是一种同策略方法;当前最佳策略用于引导智能体在环境中行动。该策略基于在每个状态中观察到的动作值,但当然,无法估计尚未使用过的动作的价值,也没有任何机制鼓励算法去探索这些动作。一种解决方案是使用探索性起始。在这里,会针对所有可能的状态 - 动作对启动回合,因此每个组合至少会被观察到一次。然而,如果状态数量很大或无法控制起始点,这就不切实际。另一种方法是

图 19.11 蒙特卡洛方法。 a) 策略(箭头)被随机初始化。反复模拟 MDP,并存储这些回合的轨迹(橙色和棕色路径代表两条轨迹)。 b) 动作价值基于在这些
图 19.11 蒙特卡洛方法。 a) 策略(箭头)被随机初始化。反复模拟 MDP,并存储这些回合的轨迹(橙色和棕色路径代表两条轨迹)。 b) 动作价值基于在这些轨迹上平均的观测回报进行经验估计。在本例中,动作价值最初均为零,并在观察到某个动作的位置进行了更新。 c) 然后根据获得最佳(或最不坏)奖励的动作来更新策略。Figure 19.11 Monte Carlo methods. a) The policy (arrows) is initialized randomly. The MDP is repeatedly simulated, and the trajectories of these episodes are stored (orange and brown paths represent two trajectories). b) The action values are empirically estimated based on the observed returns averaged over these trajectories. In this case, the action values were all initially zero and have been updated where an action was observed. c) The policy can then be updated according to the action which received the best (or least bad) reward.
公式

use an epsilongreedy policy, in which a random action is taken with probability ϵ, and the optimal action is allotted the remaining probability 1−ϵ. The choice of ϵ trades off exploitation and exploration. Here, an on-policy method will seek the best policy from this epsilon-greedy family, which will not generally be the best overall policy.

Conversely, in off-policy methods, the optimal policy π (the targetpolicy) is learned based on episodes generated by a different behaviorpolicyπ′. Typically, the target policy is deterministic, and the behavior policy is stochastic (e.g., an epsilon-greedy policy). Hence, the behavior policy can explore the environment, but the learned target policy remains efficient. Some off-policy methods explicitly use importance sampling

采用一个 epsilon 贪婪策略,其中以概率 ϵ 采取随机动作,并将剩余概率 1−ϵ 分配给最优动作。 ϵ 的选择在利用与探索之间进行权衡。这里,同策略方法将从这一 epsilon 贪婪策略族中寻找最佳策略,而这通常不会成为整体最佳策略。相反,在异策略方法中,最优策略 π (目标策略)是基于由不同的行为策略 π′ 生成的回合来学习的。通常,目标策略是确定性的,而行为策略是随机性的(例如, epsilon 贪婪策略)。因此,行为策略可以探索环境,但学习到的目标

(section 17.8.1) to estimate the action value under policy π using samples from π′.

Others, such as Q-learning (described in the next section), estimate the values based on the greedy action, even though this is not necessarily what was chosen.

策略仍然高效。一些异策略方法显式使用重要性采样(17.8.1 节),利用来自 π′ 的样本估计策略 π 下的动作价值。另一些方法,如 Q 学习(下一节描述),基于贪婪动作估计价值,即使这未必是实际选择的动作。

19.3.3 时序差分方法
19.3.3 Temporal difference methods

Dynamic programming methods use a bootstrapping process to update the values to make them self-consistent under the current policy. Monte Carlo methods sample the MDP to acquire information. Temporal difference (TD) methods combine both bootstrapping and sampling. However, unlike Monte Carlo methods, they update the values and policy while the agent traverses the states of the MDP instead of afterward.

SARSA (State-Action-Reward-State-Action) is an on-policy algorithm with update:

动态规划方法使用自举过程来更新价值,使其在当前策略下自洽。蒙特卡洛方法对 MDP 进行采样以获取信息。时序差分(TD)方法结合了自举和采样。然而,与蒙特卡洛方法不同,它们会在智能体遍历 MDP 状态的同时更新价值估计和策略,而不是事后。 SARSA (状态 - 动作 - 奖励 - 状态 - 动作)是一种同策略算法,其更新为

:其中,现在每一步动作的选择来自另一个行为策略 π′

公式

where α∈R+ is the learning rate.

The bracketed term is called the TD error and measures the consistency between the estimated action value q[st, at] and the estimate r[st, at]+γ · q[st+1, at+1] after taking a single step.

By contrast, Q-Learning is an off-policy algorithm with update (figure 19.12):

公式
公式

where now the choice of action at each step is derived from a different behavior policy π′. In both cases, the policy is updated by taking the maximum of the action values

at each state (equation 19.13). It can be shown that these updates are contraction mappings (see equation 16.20); the action values will eventually converge, assuming that

映射(见公式 16.20);假设每个状态 - 动作对被访问无限多次,动作值最终会收敛。

every state-action pair is visited an infinite number of times.

图 19.12 Q 学习。 a) 智能体从状态 st 开始,并按照策略采取行动 at=2。它没有在冰面上打滑,因此向下移动,因离开原始状态而获得奖励 r[st,
图 19.12 Q 学习。 a) 智能体从状态 st 开始,并按照策略采取行动 at=2。它没有在冰面上打滑,因此向下移动,因离开原始状态而获得奖励 r[st, at]=0。 b) 找到新状态处的最大动作值(此处为 0.43)。 c) 原始状态下动作 2 的动作值基于后续状态最大动作值的当前估计、奖励、折扣因子 γ=0.9 和学习率 α = 0.1 更新为 1.12。这改变了原始状态处的最高动作值,因此策略发生变化。Figure 19.12 Q-learning. a) The agent starts in state st and takes action at= 2 according to the policy. It does not slip on the ice, so it moves downward, receiving reward r[st, at]=0 for leaving the original state. b) The maximum action value at the new state is found (here 0.43). c) The action value for action 2 in the original state is updated to 1.12 based on the current estimate of the maximum action value at the subsequent state, the reward, discount factor γ=0.9, and learning rate α = 0.1. This changes the highest action value at the original state, so the policy changes.
19.4 拟合 Q 学习
19.4 Fitted Q-learning

The tabular Monte Carlo and TD algorithms described above repeatedly traverse the entire MDP and update the action values. However, this is only practical if the stateaction space is small. Unfortunately, this is rarely the case; even for the constrained environment of a chessboard, there are more than 1040 possible legal states.

上述表格型蒙特卡洛和 TD 算法反复遍历整个 MDP 并更新动作值。然而,只有当状态 - 动作空间很小时,这才是可行的。不幸的是,这种情况很少见;即使对于棋盘这样受限的环境,可能的合法状态也超过 1040 个。

In fitted Q-learning, the discrete representation q[st, at] of the action values is replaced by a machine learning model q[ s t, at, ϕ], where now the state is represented by a vector s t rather than just an index. We then define a least squares loss based on the consistency of adjacent action values (similar to the loss in Q-learning, see equation 19.15):

在拟合 Q 学习中,动作值的离散表示 q[st, at] 被机器学习模型 q[ s t, at, ϕ] 取代,其中现在状态由向量 s t 表示,而不仅仅是索引。然后我们基于相邻动作值之间的一致性定义最小二乘损失(类似于 Q 学习中的损失,见公式 19.15):

公式

Fitted Q-learning differs from Q-Learning in that convergence is no longer guaranteed. A change to the parameters potentially modifies both the target r[ s t, at] + γ· maxat+1 [q[ s t+1, at+1, ϕ]] (the maximum value may change) and the prediction q[ s t, at, ϕ]. This can be shown both theoretically and empirically to damage convergence.

拟合 Q 学习与 Q 学习的不同之处在于,收敛性不再得到保证。参数的变化可能同时修改目标值 r[ s t, at] + γ· max at+1 [q[ s t+1, at+1, ϕ]] (最大值可能改变)和预测值 q[ s t, at, ϕ]。这在理论和实证上都可以证明会损害收敛性。

图 19.13 Atari 基准。 Atari 基准包含 49 款 Atari 2600 游戏,其中包括《Breakout》(如图所示)、《Pong》以及多种射
图 19.13 Atari 基准。 Atari 基准包含 49 款 Atari 2600 游戏,其中包括《Breakout》(如图所示)、《Pong》以及多种射击类、平台类和其他类型的游戏。 a-d) 即使对于只有单一屏幕的游戏,仅从单帧也无法完全观察状态,因为物体的速度未知。因此,通常使用若干相邻帧(此处为四帧)来表示状态。 e) 该动作通过摇杆模拟用户输入。 f) 共有 18 个动作,对应八个移动方向或不动;对于这九种情况中的每一种,按键可按下或未按。Figure 19.13 Atari Benchmark. The Atari benchmark consists of 49 Atari 2600 games, including Breakout (pictured), Pong, and various shoot-em-up, platform, and other types of games. a-d) Even for games with a single screen, the state is not fully observable from a single frame because the velocity of the objects is unknown. Consequently, it is usual to use several adjacent frames (here, four) to represent the state. e) The action simulates the user input via a joystick. f) There are eighteen actions corresponding to eight directions of movement or no movement, and for each of these nine cases, the button being pressed or not.
19.4.1 用于玩 ATARI 游戏的深度 Q 网络
19.4.1 Deep Q-networks for playing ATARI games

Deep networks are ideally suited to making predictions from a high-dimensional state space, so they are a natural choice for the model in fitted Q-learning. In principle, they could take both state and action as input and predict the values, but in practice, the network takes only the state and simultaneously predicts the values for each action.

The Deep Q-Network was a breakthrough reinforcement learning architecture that exploited deep networks to learn to play ATARI 2600 games. The observed data comprises 220 × 160 images with 128 possible colors at each pixel (figure 19.13). This was reshaped to size 84 × 84, and only the brightness value was retained. Unfortunately, the full state is not observable from a single frame. For example, the velocity of game objects is unknown. To help resolve this problem, the network ingests the last four frames at each time step to form s t. It maps these frames through three convolutional layers followed by a fully connected layer to predict the value of every action (figure 19.14).

Several modifications were made to the standard training procedure. First, the rewards (which were driven by the score in the game) were clipped to −1 for a negative change and +1 for a positive change. This compensates for the wide variation in scores between different games and allows the same learning rate to be used. Second, the system exploited experience replay. Rather than update the network based on the tuple < s t, at, rt+1, s t+1 > at the current step or with a batch of the last I tuples, all recent

深度网络非常适合从高维状态空间中进行预测,因此它们是拟合 Q 学习中模型的自然选择。原则上,它们可以同时以状态和动作作为输入并预测值,但实际上,网络仅以状态作为输入,并同时预测每个动作的值。

深度 Q 网络是一种突破性的强化学习架构,它利用深度网络来学习玩 ATARI 2600 游戏。观测数据包含 220 × 160 幅图像,每个像素有 128 种可能的颜色(图 19.13)。这些图像被重塑为 84 × 84 尺寸,并且只保留亮度值。不幸的是,仅从单帧无法观察到完整状态。例如,游戏对象的速度未知。为帮助解决这一问题,网络在每个时间步输入最近四帧以形成 s t。它通过三个卷积层和随后的一个全连接层对这些帧进行映射,以预测每个动作的价值(图 19.14)。对标准训练过程做了几项修改。首先,奖励(由游戏中的得分驱动)在负变化时被裁剪为 −1,在正变化时被裁剪为 +1。这补偿了不同游戏之间得分的巨大差异,并允许使用相同的学习率。其次,系统利用了经验回放。而不是根据当前步的元组 < s t, at, rt+1, s t+1 > 或最近 I 个元组的批次来更新网络,所有近期

tuples were stored in a buffer. This buffer was sampled randomly to generate a batch at each step. This approach reuses data samples many times and reduces correlations between the samples in the batch that arise due to the similarity of adjacent frames.

Finally, the issue of convergence in fitted Q-Networks was tackled by fixing the target parameters to values ϕ− and only updating them periodically. This gives the update:

元组被存储在缓冲区中。该缓冲区被随机采样,以在每一步生成一个批次。这种方法多次复用数据样本,并减少由于相邻帧相似性而在批次样本之间产生的相关性。

最后,通过将拟合 Q 网络中的目标参数固定为值 ϕ− 并仅定期更新它们,解决了收敛问题。由此得到更新式:

图 19.14 深度 Q 网络架构。输入 s t 由 ATARI 游戏的四个相邻帧组成。每一帧都被调整为 84 × 84 并转换为灰度。这些帧被表示为四个通道,
图 19.14 深度 Q 网络架构。输入 s t 由 ATARI 游戏的四个相邻帧组成。每一帧都被调整为 84 × 84 并转换为灰度。这些帧被表示为四个通道,并经过一个 8 × 8 卷积(步幅为 4),随后是一个 4×4 卷积(步幅为 2),再经过两个全连接层。最终输出预测该状态下 18 个动作中每个动作的动作价值 q[ s t, at]。Figure 19.14 Deep Q-network architecture. The input s t consists of four adjacent frames of the ATARI game. Each is resized to 84 × 84 and converted to grayscale. These frames are represented as four channels and processed by an 8 × 8 convolution with stride four, followed by a 4×4 convolution with stride 2, followed by two fully connected layers. The final output predicts the action value q[ s t, at] for each of the 18 actions in this state.
公式

Now the network no longer chases a moving target and is less prone to oscillation.

Using these and other heuristics and with an ϵ -greedy policy, Deep Q-Networks performed at a level comparable to a professional game tester across a set of 49 games using the same network architecture (trained separately for each game). It should be noted that the training process was data-intensive. It took around 38 full days of experience to learn each game. In some games, the algorithm exceeded human performance. On other games like “Montezuma’s Revenge,” it barely made any progress. This game features sparse rewards and multiple screens with quite different appearances.

现在,网络不再追逐一个移动的目标,因此更不容易发生振荡。

利用这些及其他启发式方法,并采用一个 ϵ - 贪婪策略,深度 Q 网络在一组 49 款游戏中达到了与专业游戏测试员相当的水平,这些游戏均使用相同的网络架构(针对每款游戏分别训练)。需要注意的是,训练过程是数据密集型的。学习每款游戏大约需要 38 个完整天的经验。在某些游戏中,该算法超越了人类表现。在诸如“蒙特祖玛的复仇”这样的其他游戏中,它几乎没有取得任何进展。这款游戏的特征是具有稀疏奖励和多个外观差异很大的画面。

19.4.2 双重 Q 学习与双重深度 Q 网络
19.4.2 Double Q-learning and double deep Q-networks

Q 学习的一个潜在缺陷是,在更新中对动作进行最大化

One potential flaw of Q-Learning is that the maximization over the actions in the update:

:

公式

leads to a systematic bias in the estimated action values q[st, at]. Consider two actions that provide the same average reward, but one is stochastic and the other deterministic. The stochastic reward will exceed the average roughly half of the time and be chosen by the maximum operation, causing the corresponding action value q[st, at] to be overestimated. A similar argument can be made about random inaccuracies in the output of the network q[ s t, at, ϕ] or random initializations of the q-function.

导致估计动作值 q[st, at] 中出现系统性偏差。考虑两个动作提供相同的平均奖励,但一个是随机的,另一个是确定性的。随机奖励大约有一半时间高于平均值,并被最大值操作选中,导致对应的动作值 q[st, at] 被高估。关于网络 q[ s t, at, ϕ] 输出中的随机不准确性,或 Q 函数的随机初始化,也可以作出类似的论证。

The underlying problem is that the same network both selects the target (by the maximization operation) and updates the value. Double Q-Learning tackles this problem by training two models q1[st, at, π1] and q2[st, at, π2] simultaneously:

根本问题在于,同一个网络既选择目标(通过最大化操作),又更新价值。双重 Q 学习通过同时训练两个模型 q1[st, at, π1] 和 q2[st, at, π2] 来解决这一问题:

公式

Now the choice of the target and the target itself are decoupled, which helps prevent these biases. In practice, new tuples <s, a, r, s′> are randomly assigned to update one model or another. This is known as double Q-learning. Double deep Q-networks or double DQNs use deep networks q[ s t, at, ϕ1] and q[ s t, at, ϕ2] to estimate the action values, and the update becomes:

现在,目标的选择与目标本身是解耦的,这有助于防止这些偏差。在实践中,新的元组 <s, a, r, s′> 被随机分配,用于更新其中一个模型。这被称为双重 Q 学习。双重深度 Q 网络或双重 DQN 使用深度网络 q[ s t, at, ϕ1] 和 q[ s t, at, ϕ2] 来估计动作值,更新变为:

公式
19.5 策略梯度方法
19.5 Policy gradient methods

Q-learning estimates the action values first and then uses these to update the policy. Conversely, policy-basedmethods directly learn a stochastic policy π[at| s t, θ]. This is a function with trainable parameters θ that maps a state s t to a distribution Pr(at| s t) over actions at from which we can sample. In MDPs, there is always an optimal deterministic policy. However, there are three reasons to use a stochastic policy: 1. A stochastic policy naturally helps with exploration of the space; we are not obliged

to take the best action at each time step. 2. The loss changes smoothly as we modify a stochastic policy. This means we can use gradient descent methods even though the rewards are discrete. This is similar to using maximum likelihood in (discrete) classification problems. The loss changes

smoothly as the model parameters change to make the true class more likely. 3. The MDP assumption is often incorrect; we usually don’t have complete knowl- edge of the state. For example, consider an agent navigating in an environment where it can only observe nearby locations (e.g., figure 19.4). If two locations look identical, but the nearby reward structure is different, a stochastic policy allows

Q 学习先估计动作值,然后使用这些估计值来更新策略。相反,基于策略的方法直接学习一个随机策略 π[at| s t, θ]。这是一个带有可训练参数 θ 的函数,它将状态 s t 映射为动作 at 上的分布 Pr (at| s t),我们可以从中采样。在 MDP 中,总存在一个最优确定性策略。然而,使用随机策略有三个原因: 1. 随机策略自然有助于空间探索;我们不必在每个时间步都采取最佳动作。 2. 当我们修改随机策略时,损失会平滑变化。这意味着即使奖励是离散的,也可以使用梯度下降方法。这类似于在(离散)分类问题中使用极大似然。随着模型参数变化以增大真实类别的可能性,损失会平滑变化。 3. MDP 假设通常不成立;我们通常不具备对状态的完全知识。例如,考虑一个智能体在只能观察附近位置的环境中导航(例如,图 19.4)。如果两个位置看起来相同,但附近的奖励结构不同,随机策略允许在歧

义消除之前采取不同动作的可能性。

the possibility of taking different actions until this ambiguity is resolved.

19.5.1 梯度更新的推导
19.5.1 Derivation of gradient update

Consider a trajectory τ=[ s 1, a1, s 2, a2,..., s T, aT, s T +1] through an MDP. The probability of this trajectory Pr(τ|θ) depends on both the state evolution function Pr( s t+1| s t, at) and the current stochastic policy π[at| s t, θ]:

取决于状态演化函数 Pr (s t+1| s t, at) 和当前随机策略 π[at| s t, θ]:

策略梯度算法旨在最大化许多这样的轨迹上的期望回报:

Policy gradient algorithms aim to maximize the expected return over many such trajectories:

where here the return r[τ] is the sum of all the rewards received along the trajectory.

To maximize this quantity, we use the gradient ascent update:

其中这里的回报 r[τ] 是沿该轨迹获得的所有奖励之和。为了最大化该量,我们使用梯度上升更新:

公式

where α is the learning rate.

We want to approximate this integral with a sum over empirically observed trajectories. These are drawn from the distribution Pr(τ|θ), so to make progress, we multiply and divide the integrand by this distribution:

其中 α 是学习率。

我们希望通过经验观测到的轨迹之和来近似这个积分。这些轨迹从分布 Pr (τ|θ) 中采样,因此为了推进推导,我们将被积函数乘以并除以该分布:

公式

This equation has a simple interpretation (figure 19.15); the update changes the parameters θ to increase the likelihood Pr(τ i|θ) of an observed trajectory τ i in proportion to the return r[τ i] from that trajectory. However, it also normalizes by the probability of observing that trajectory in the first place to compensate for the fact that some trajectories are observed more often than others. If a trajectory is already common and yields a high return, then we don’t need to change much. The biggest updates will come from trajectories that are uncommon but create large returns.

We can simplify this expression using the likelihood ratio identity:

这个方程有一个简单的解释(图 19.15);该更新改变参数 θ,以按观测轨迹 τ i 的回报 r[τ i] 的比例提高其似然 Pr (τ i|θ)。然而,它还会根据最初观测到该轨迹的概率进行归一化,以弥补某些轨迹比其他轨迹更常被观测到的情况。如果一条轨迹已经很常见且产生高回报,那么就不需要进行太大改变。最大的更新将来自那些不常见但能产生大回报的轨迹。

我们可以使用似然比恒等式来简化该表达式: a 并且注意到只有中间项依赖于 θ,我们可以将更新从

图 19.15 策略梯度。同一策略下的五个回合(颜色越亮表示奖励越高)。轨迹1、 2和3持续产生高奖励,但类似轨迹在该策略下已频繁出现,因此无需改变。相反, 轨
图 19.15 策略梯度。同一策略下的五个回合(颜色越亮表示奖励越高)。轨迹1、 2和3持续产生高奖励,但类似轨迹在该策略下已频繁出现,因此无需改变。相反, 轨迹4获得低奖励,因此应修改策略以避免产生类似轨迹。轨迹5获得高奖励且不常见。根据公式19.25,这将对策略造成最大改变。Figure 19.15 Policy gradients. Five episodes for the same policy (brighter inTrajectories 1, dicates higher reward). 2, and 3 generate consistently high rewards, but similar trajectories already frequently occur with this policy, so there is no need to change. Conversely, trajectory 4 receives low rewards, so the policy should be modified to avoid producing similar trajectories. Trajectory 5 receives high rewards and is unusual. This will cause the largest change to the policy under equation 19.25.
公式

and noting that only the center term depends on θ, we can rewrite the update from equation 19.27 as:

公式

where s it is the state at time t in episode i, and ait is the action taken at time t in episode i. Note that the terms relating to the state evolution Pr( s t+1| s t, at) disappear from this formulation. It follows that this parameter update does not assume a Markov time evolution process.

其中 s it 是时刻 t 在回合 i 中的状态,而 ait 是时刻 t 在回合 i 中采取的动作。注意,与状态演化 Pr (s t+1| s t, at) 相关的项在该表述中消失了。因此,该参数更新并不假设马尔可夫时间演化过程。我们可以进一步简化这一点,注意到 th

We can further simplify this by noting that:

where rit is the reward at time t in the ith episode. It can (non-obviously) be proved that the first term (the rewards before time t) does not affect the update from time t, so we can write:

at:

其中 rit 是 t 时刻在第 ith 个回合中的奖励。可以(非显然地)证明第一项(t 时刻之前的奖励)不影响从 t 时刻起的更新,因此我们可以写成:

公式
19.5.2 REINFORCE 算法
19.5.2 REINFORCE algorithm

REINFORCE is an early policy gradient algorithm that exploits this result and incorporates discounting.

It is a Monte Carlo method that generates episodes τ i= [ s i1, ai1, ri2, s i2, ai2, ri3,..., riT] based on the current policy π[a| s , θ]. For discrete actions, this policy could be determined by a neural network π[ s , θ], which takes the current state s and returns one output for each possible action. These outputs are passed through a softmax function to create a distribution over actions, which is sampled at each time step.

REINFORCE 是一种早期的策略梯度算法,它利用了这一结果并纳入了折扣。它是一种

π[a| s θ]。对于离散动作,该策略可以由神经网络 π[ s θ] 确定,该神经网络接收当前状态 s,并为每个可能的动作返回一个输出。这些输出经过 Softmax 函数处理,以生成动作上的分布,并在每个时间步对该分布进行采样。

For each episode i, we loop through each step t and calculate the empirical discounted return for the partial trajectory τ it that starts at time t:

对于每个回合 i,我们遍历每个时间步 t,并计算部分轨迹 τ it 的经验折扣回报,该轨迹从时间 t 开始: w 这里 πat[ s t, θ] 是在给定当前状态下由神经网络产生的 at 的概率

公式

where πat[ s t, θ] is the probability of at produced by the neural network given the current state s t and parameters θ, and α is the learning rate.

nt 状态 s t 和参数 θ,且 α 是学习率。

19.5.3 基线
19.5.3 Baselines

Policy gradient methods exhibit high variance; many episodes may be needed to get stable estimates of the derivatives. One way to reduce this variance is to subtract a baseline b from the trajectory returns r[τ]:

策略梯度方法方差较大;可能需要许多回合才能获得导数的稳定估计。降低这种方差的一种方法是从轨迹回报 r[τ] 中减去基线 b:

公式

As long as the baseline b doesn’t depend on the actions:

只要基线 b 不依赖于动作:并且期望值不会改变。然而,如果基线与无关的协变

图 19.16 使用控制变量降低估计量的方差。 a) 考虑尝试从少量样本中估计 E[a]。该估计量(样本均值)会因样本数量和样本方差的不同而变化。 b) 现在考
图 19.16 使用控制变量降低估计量的方差。 a) 考虑尝试从少量样本中估计 E[a]。该估计量(样本均值)会因样本数量和样本方差的不同而变化。 b) 现在考虑观察另一个变量 b,它与 a 协变,并具有 E[b] = 0 以及与 a 相同的方差。 c) a −b 样本的方差远小于 a 样本的方差,但期望值 E[a −b] = E[a],因此我们得到了一个方差更低的估计量。Figure 19.16 Decreasing variance of estimates using control variates. a) Consider trying to estimate E[a] from a small number of samples. The estimate (the mean of the samples) will vary based on the number of samples and the variance of those samples. b) Now consider observing another variable b that co-varies with a and has E[b] = 0 and the same variance as a. c) The variance of the samples of a −b is much less than that of a, but the expected value E[a −b] = E[a], so we get an estimator with lower variance.

and the expected value will not change. However, if the baseline co-varies with irrelevant factors that add uncertainty, then subtracting it reduces the variance (figure 19.16). This is a special case of the method of control variates (see problem 19.7).

This raises the question of how we should choose b. We can find the value of b that minimizes the variance by writing an expression for the variance, taking the derivative with respect to b, setting the result to zero, and solving to yield:

这引出了我们应该如何选择 b 的问题。我们可以通过写出方差的表达式、对 b 求导、令结果为零并求解,来找到使方差最小的 b 值:

In practice, this is often approximated as:

Subtracting this baseline factors out variance that might occur when the returns r[τ i] from all trajectories are greater than is typical but only because they happen to pass through states with higher than average returns whatever actions are taken.

在实践中,这通常近似为:

减去这个基线可以消除方差,这种方差可能出现在所有轨迹的回报 r[τ i] 高于通常水平时,但这只是因为它们恰好经过了回报高于平均水平的状态,而无论采取什么行动。

19.5.4 状态相关基线
19.5.4 State-dependent baselines

A better option is to use a baseline b[ s it] that depends on the current state s it.

更好的选择是使用一个基线 b[ s it],它取决于当前状态 s it。

公式

Here, we are compensating for variance introduced by some states having greater overall returns than others, whichever actions we take.

A sensible choice is the expected future reward based on the current state, which is just the state value v[ s ]. In this case, the difference between the empirically observed rewards and the baseline is known as the advantage estimate. Since we are in a Monte Carlo context, this can be parameterized by a neural network b[ s ] = v[ s , ϕ] with parameters ϕ, which we can fit to the observed returns using least squares loss:

一个合理的选择是基于当前状态的期望未来奖励,也就是状态价值 v[ s ]。在这种情况下,经验观测到的奖励与基线之间的差异称为优势估计。由于我们处于蒙特卡洛情境中,这可以用神经网络 b[ s ] = v[ s ϕ] 进行参数化,参数为 ϕ,我们可以使用最小二乘损失将其拟合到观测到的回报:

19.6 Actor-Critic 方法
19.6 Actor-critic methods

Actor-critic algorithms are temporal difference (TD) policy gradient algorithms. They can update the parameters of the policy network at each step.

This contrasts with the Monte Carlo REINFORCE algorithm, which must wait for one or more episodes to complete before updating the parameters.

In the TD approach, we do not have access to the future rewards r[τ t]=PT along this trajectory. Actor-critic algorithms approximate the sum over all the future rewards with the observed current reward plus the discounted value of the next state:

Actor-Critic 算法是时间差分(TD)策略梯度算法。它们可以在每一步更新策略网络的参数。这与蒙特卡洛 REINFORCE 算法形成对比,后者必须等待一个或多个回合完成之后才能更新参数。在 TD 方法中,我们无法获得沿该轨迹的未来奖励 r[τ t]=PT

k=trk。 Actor-Critic 算法用观测到的当前奖励加上下一个状态的折扣值来近似所有未来奖励之和:

Here the value v[ s i,t+1, ϕ] is estimated by a second neural network with parameters ϕ.

Substituting this into equation 19.38 gives the update: The policy network π[ s t, θ] that predicts Pr(a| s t) is termed the actor. The value network v[ s t, ϕ] is termed the critic. Often the same network represents both actor and

这里的值 v[ s i,t+1, ϕ] 由具有参数 ϕ 的第二个神经网络估计。将其代入公式 19.38,可得更新:策略网络 π[ s t, θ] 预测 Pr (a| s t),称为演员。价值网络 v[ s t, ϕ] 称为评论家。通常同一网络同时表示演员和

公式

the critic, with two sets of outputs that predict the policy and the values, respectively. Note that although actor-critic methods can update the policy parameters at each step, this is rarely done in practice. The agent typically collects a batch of experience over many time steps before the policy is updated.

评论家,分别用两组输出预测策略和价值。注意,尽管 Actor-Critic 方法可以在每一步更新策略参数,但这在实践中很少见。智能体通常会在许多时间步中收集一批经验,然后才更新策略。

图 19.17 决策 Transformer。决策 Transformer 将离线强化学习视为序列预测任务。输入是由状态、动作和待回报(本回合中的剩余奖励)组成
图 19.17 决策 Transformer。决策 Transformer 将离线强化学习视为序列预测任务。输入是由状态、动作和待回报(本回合中的剩余奖励)组成的序列,每个元素都会映射为固定大小的嵌入。在每个时间步,网络预测下一个动作。在测试阶段,待回报是未知的;在实践中,会先给出一个初始估计,然后从中减去后续观察到的奖励。Figure 19.17 Decision transformer. The decision transformer treats offline reinforcement learning as a sequence prediction task. The input is a sequence of states, actions, and returns-to-go (remaining rewards in the episode), each of which is mapped to a fixed-size embedding. At each time step, the network predicts the next action. During testing, the returns-to-go are unknown; in practice, an initial estimate is made from which subsequent observed rewards are subtracted.
19.7 离线强化学习
19.7 Offline reinforcement learning

Interaction with the environment is at the core of reinforcement learning. However, there are some scenarios where it is not practical to send a naïve agent into an environment to explore the effect of different actions. This may be because erratic behavior in the environment is dangerous (e.g., driving autonomous vehicles) or because data collection is time-consuming or expensive (e.g., making financial trades).

However, it is possible to gather historical data from human agents in both cases. Offline RL or batch RL aims to learn how to take actions that maximize rewards on future episodes by observing past sequences s 1, a1, r2, s 2, a2, r3,..., without ever interacting with the environment. It is distinct from imitation learning, a related technique that (i) does not have access to the rewards and (ii) attempts to replicate the performance of a historical agent rather than improve it. Although there are offline RL methods based on Q-Learning and policy gradients,

this paradigm opens up new possibilities. In particular, we can treat this as a sequence learning problem, in which the goal is to predict the next action, given the history of states, rewards, and actions. The decision transformer exploits a transformer decoder framework (section 12.7) to make these predictions (figure 19.17). However, the goal is to predict actions based on future rewards, and these are not captured in a standard s , a, r sequence. Hence, the decision transformer replaces the reward rt with the returns-to-go Rt:T=PT

与环境交互是强化学习的核心。然而,在某些场景中,将未经训练的智能体送入环境以探索不同动作的影响并不实际。这可能是因为环境中异常行为具有危险性(例如,驾驶自动驾驶汽车),或者因为数据收集耗时或昂贵(例如,进行金融交易)。

然而,在两种情况下,确实可以从人类智能体收集历史数据。离线强化学习或批量强化学习旨在通过观察过去的序列 s 1, a1, r2, s 2, a2, r3,...,而无需与环境交互。它不同于模仿学习,后者是一种相关技术: (i) 无法获得奖励,并且 (ii) 试图复制历史智能体的表现,而不是改进表现。尽管存在基于 Q 学习和策略梯度的离线强化学习方法,

这一范式开辟了新的可能性。特别是,我们可以将其视为一个序列学习问题,其中目标是在给定状态、奖励和动作历史的情况下预测下一个动作。决策 Transformer 利用 Transformer 解码器框架(第 12.7 节)来做出这些预测(图 19.17)。

t′=trt′ (i.e., the sum of the empirically observed future rewards). The remaining framework is very similar to a standard transformer decoder. The states, actions, and returns-to-go are converted to fixed-size embeddings via learned mappings. For Atari games, the state embedding might be converted via a convolutional network similar to that in figure 19.14. The embeddings for the actions and returns-to-go can be learned in the same way as word embeddings (figure 12.9). The transformer is trained with masked self-attention and position embeddings.

This formulation is natural during training but poses a quandary during inference because we don’t know the returns-to-go. This can be resolved by using the desired total return at the first step and decrementing this as rewards are received. For example, in an Atari game, the desired total return would be the total score required to win.

Decision transformers can also be fine-tuned from online experience and hence learn over time. They have the advantage of dispensing with most of the reinforcement learning machinery and its associated instability and replacing this with standard supervised learning. Transformers can learn from enormous quantities of data and integrate information across large time contexts (making the temporal credit assignment problem more tractable). This represents an intriguing new direction for reinforcement learning.

然而,目标是根据未来奖励来预测动作,而这些信息并未在标准的 s a, r 序列中被捕获。因此,决策 Transformer 将奖励 rt 替换为剩余回报 Rt:T=PT 际观测到的未来奖励之和)。其余框架与标准的 Transformer 解码器非常相似。状态、动作和剩余回报通过学习到的映射转换为固定大小的嵌入。对于 Atari 游戏,状态嵌入可能通过与图 19.14 类似的卷积网络进行转换。动作和剩余回报的嵌入可以像词嵌入(图 12.9)一样学习。该 Transformer 使用掩码自注意力和位置嵌入进行训练。

这种表述在训练过程中很自然,但在推理时带来一个难题,因为我们不知道剩余回报。这可以通过在第一步使用期望总回报,并在收到奖励时将其递减来解决。例如,在 Atari 游戏中,期望总回报将是获胜所需的总分。

决策 Transformer 也可以基于在线经验进行微调,因此能够随时间不断学习。它们的优势在于省去大部分强化学习机制及其相关的不稳定性,并用标准监督学习取而代之。 Transformer 模型能够从海量数据中学习,并在长时间上下文中整合信息(使时间信用分配问题更易处理)。这代表了强化学习的一个引人入胜的新方向。

19.8 小结
19.8 Summary

Reinforcement learning is a sequential decision-making framework for Markov decision processes and similar systems. This chapter reviewed tabular approaches to RL, including dynamic programming (in which the environment model is known), Monte Carlo methods (in which multiple episodes are run and the action values and policy subsequently changed based on the rewards received), and temporal difference methods (in which these values are updated while the episode is ongoing).

Deep Q-Learning is a temporal difference method where deep neural networks are used to predict the action value for every state. It can train agents to perform Atari 2600 games at a level similar to humans. Policy gradient methods directly optimize the policy rather than assigning values to actions. They produce stochastic policies, which are important when the environment is partially observable. The updates are noisy, and many refinements have been introduced to reduce their variance.

Offline reinforcement learning is used when we cannot interact with the environment but must learn from historical data. The decision transformer leverages recent progress in deep learning to build a model of the state-action-reward sequence and predict the actions that will maximize the rewards.

强化学习是一种面向马尔可夫决策过程及类似系统的序贯决策框架。本章回顾了表格型强化学习方法,包括动态规划(其中环境模型已知)、蒙特卡洛方法(其中运行多个回合,并根据所获得的奖励随后改变动作值和策略)以及时序差分方法(其中在回合进行期间更新这些值)。

深度 Q 学习是一种时序差分方法,它使用深层神经网络来预测每个状态的动作值。它能够训练智能体以接近人类的水平进行 Atari 2600 游戏。策略梯度方法直接优化策略,而不是为动作分配值。它们产生随机策略,这在环境部分可观测时非常重要。更新过程含有噪声,并且已引入许多改进方法来降低其方差。

离线强化学习用于我们无法与环境交互但必须从历史数据中学习的情形。决策 Transformer 利用深度学习领域的最新进展,对状态 - 动作 - 奖励序列建模,并预测能够最大化奖励的动作。

注释
Notes

Landmarks in deep reinforcement learning:

Classical reinforcement learning methods:

Deep Q-Networks:

Policy gradients:

Modern policy gradients:

Actor-critic:

Actor-Critic:在 19.6 节描述的 Actor-Critic 算法(Konda & Tsitsiklis, 1999)中,评论家使用了

Offline RL:

Reinforcement learning and chatbots:

Other areas of RL:

问题
Problems
公式
公式
公式
公式
图 19.18 一条经过MRP的轨迹。企鹅在到达第一个鱼格时获得奖励 +1 ,在掉进洞中时获得 −2 ,在到达第二个鱼格时获得 +1 。折扣因子 γ 为0.9。
图 19.18 一条经过MRP的轨迹。企鹅在到达第一个鱼格时获得奖励 +1 ,在掉进洞中时获得 −2 ,在到达第二个鱼格时获得 +1 。折扣因子 γ 为0.9。Figure 19.18 One trajectory through an MRP. The penguin receives a reward of +1 when it reaches the first fish tile, −2 when it falls in the hole, and +1 for reaching the second fish tile. The discount factor γ is 0.9.