Continuous Multi-Step TD, Eligibility Traces and TD(λ): A brief note

简介: Thanks Richard S. Sutton and Andrew G. Barto for their great work in Reinforcement Learning: An Introduction.We focus on episodic case only and deal with continuous state and action space

Thanks Richard S. Sutton and Andrew G. Barto for their great work in Reinforcement Learning: An Introduction.

We focus on episodic case only and deal with continuous state and action spaces. Suppose you already have the basic knowledge of TD(0) methods in reinforcement learning.

Stochastic-gradient and Semi-gradient Methods

Stochastic-gradient methods are among the most widely used of all function approximation methods and are particularly well suited to online reinforcement learning.

Let v^(s,θ) be the approximation of the true value function v(s) with real valued components θ=(θ1,θ2,…,θn)T for all s∈S. We assume that states appear in examples with the same distribution d, over which we are trying to minimize error on the Mean Square Value Error (MSVE(θ)=∑s∈Sd(s)[vπ(s)−v^(s,θ)]2). A straight-forward strategy is to try to minimize the error on the observed examples. Stochastic-gradient methods do this by adjusting the weight vector after each example by a small amount in the direction that would most reduce the error on that example:

θt+1=θt−12α∇θ[vπ(St)−v^(St,θt)]2=θt+α[vπ(St)−v^(St,θt)]∇θv^(St,θt)

SGD methods are gradient descent methods because the overall step in θt is proportional to the negetive gradient of the example’s squared error. This is the direction in which the error falls most rapidly. Meanwhile, stochastic means that the update is done on only a single example, which might have been selected stochastically.

Why we take only a small step in the direction of the gradient? We do not seek to find a value function that has zero error for all states, but only an approximation that balances the errors in different states. In fact, the convergence results for SGD methods assume that α decreases over time. By that way, the SGD method is guaranteed to converge to a local optimum.

However, we do not know the true value function vπ(s) in practice. Hence it is necessary to substitute an estimate Ut in place of vπ(s). If Ut is an unbiased estimate, this is, if E[Ut]=vπ(St) for each t, then θt is guaranteed to converge to a local optimum. For example, the Monte Carlo target Ut=Gt is by definition an unbiased estimate of vπ(St).

If a bootstrapping estimate of vπ(St) is used as the target Ut (such as Ut=Rt+1+γv^(St+1,θt)), one does not obtain the same guarantees since the update step would rely on the target failing to be independent of θt. Bootstrapping methods take into account the effect of changing the weight vector θt on the estimate, but ignore its effect on the target. They include only a part of the gradient and, accordingly, we call them semi-gradient methods.

Although semi-gradient methods do not converge as robustly as gradient methods, they do converge reliably in important case such as the linear case.

Episodic Semi-gradient Control

In the view of control instead of prediction, the action-value function q(St,At) accounts more for the application than value function v(St). The general gradient-descent update for action-value prediction is

θt+1=θt+α[Ut−q^(St,At,θt)]∇q^(St,At,θt)

For example, the update for the one-step SARSA method is
θt+1=θt+α[Rt+1+γq^(St+1,At+1,θt)−q^(St,At,θt)]∇q^(St,At,θt)

We call this method episodic semi-gradient one-step SARSA. For a constant policy, this method converges in the same way that TD(0) does, with the same kind of error bound MSVE(θTD)≤11−γminθMSVE(θ).

n-Step Semi-gradient SARSA

We can obtain an n-step version of episodic semi-gradient SARSA by using an n-step return as the update target.

G(n)t=Rt+1+γRt+2+⋯+γn−1Rt+n+γnq^(St+n,At+n,θt+n−1),n≥1,0≤t<T−n

The n-step update update equation is
θt+n=θt+n−1+α[G(n)t−q^(St,At,θt+n−1)]∇q^(St,At,θt+n−1)

The performance is the best if an intermediate level of bootstrapping is used, corresponding to an n larger than 1.

The λ-Return

To begin with, we note that a valid update can be done not just toward any n-step return, but toward any average of n-step returns. The composite return possesses an error reduction property similar to that of individual n-step returns and thus can be used to construct backups with guaranteed convergence properties. A backup that average simpler component backups is called a compound backup.

The λ-return is defined by

Gλt=(1−λ)∑n=1∞λn−1G(n)t

which can be understood as one particular way of averaging n-step backups. This average contains all the n-step backups, each weighted proportional to λn−1, where λ∈[0,1], and normalized by a factor of 1−λ to ensure that the weights sum to 1. After a terminal state (if exists) has been reached, all subsequent n-step returns are equal to Gt. If we want, we can separate these post-termination terms from the main sum, yielding
Gλt=(1−λ)∑n=1T−t−1λn−1G(n)t+λT−t−1Gt

Now it is time to define our first learning algorithm based on λ-return: the off-line λ-return algorithm. As an off-line algorithm, it makes no changes to the weight vector during the episode. Then, at the end of the episode, a whole sequence of off-line updates are made according to usual semi-gradient rule:
θt+1=θt+α[Gλt−v^(St,θt)]∇v^(St,θt)

The approach is what we call the theoretical, or forward view of a learning algorithm. For each state visited, we look forward in time to all the future rewards and decide how best to combine them.

Eligibility Traces

Eligibility traces are one of the basic mechanisms of reinforcement learning. Almost any temporal-difference methods, such as Q-Learning or SARSA, can be combined with eligibility traces to obtain a more general method that may learn more efficiently. What eligibility traces offer beyond these is an elegant algorithmic mechanism with significant computational advantages. Only a single trace vector is required rather than a store of the last n feature vectors. Learning also occurs continually and uniformly in time rather than being delayed and ten catching up at the end of the episode.

The mechanism is a short-term memory vector, the eligibility trace et∈Rn,that parallels the long-term memory weight vector θt∈Rn. The rough idea is that when a component of θt participates in producing an estimated value, then the corresponding component of et is bumped up and then begins to fade away. Learning will then occur in that component of θt is a nonzero TD error occurs before the trace falls back to zero. The trace-decay parameter λ∈[0,1] determines the rate at which the trace falls.

The idea behind eligibility traces is very simple. Each time a state is visited it initializes a short-term memory process, a trace, which then decays gradually over time. This trace marks the state as eligibility for learning. If an unexpectedly good or bad event occurs while the trace is non-zero, then the state is assigned credit accordingly. There have been two kinds of traces: the accumulating trace and the replacing trace. In a accumulating trace, the trace builds up each time the state is entered. In a replacing trace, on the other hand, each time the state is visited the trace is reset to 1 regardless of the presence of a prior trace. Typically, eligibility traces decay exponentially according to the product of a decay parameter, λ, 0≤λ≤1, and a discount-rate parameter, γ, 0≤γ≤1. The accumulating trace is defined by:

et+1(s)={γλet(s),γλet(s)+1,s≠sts=st

where et(s) represents the trace for state s at time t. and st is the actual state at time t. The corresponding replacing trace is defined by:
et+1(s)={γλet(s),1s≠sts=st

In the gradient descent case, the (accumulating) eligibility trace is initialized to zero at the beginning of the episode, is incremental on each time step by the value gradient, and then fades away by γλ:

e0et=0,=∇v^(St,θt)+γλet−1

The eligibility trace keeps track of which components of the weight vector have contributed, positively or negatively, to recent state valuations, where recent is defined in terms γλ.

TD(λ) Methods

TD(λ) improves over the off-line λ-return algorithm in three ways:

  1. It updates the weight vector on every step of an episode rather than only at the end.
  2. Its computations are equally distributed in time rather that all at the end of the episode.
  3. It can be applied to continuing problems rather than just episodic problems.

To begin with, the TD error for state-value prediction is:

δt=Rt+1+γv^(St+1,θt)−v^(St,θt)

In TD(λ), the weight vector is updated on each step proportional to the scalar TD error and the vector eligibility trace:
θt+1=θt+αδtet

TD(λ) is oriented backward in time. At each moment we look at the current TD error and assign it backward to each prior state according to how much that state contributed to the current eligibility trace at that time. The complete update rule of TD(λ) (with so-called accumulating traces) is as follows:
  1. e=0
  2. Choose A∼π(⋅|S)
  3. Observe R, S′
  4. e=γλe+∇v^(S,θ)
  5. δ=R+γv^(S′,θ)−v^(S,θ)
  6. θ=θ+αδe
  7. S=S′

In another variant of the algorithm, the eligibility traces are updated according to:

e=max(γλe,∇v^(S,θ))

This is called the replacing traces update.

TD(0) is the case in which only the one state preceding the current one is changed by the TD error. For larger values of λ, but still λ<1, more of the preceding states are changed but each more temporally distant state is changed less because the corresponding eligibility trace is smaller. We say that the earlier states are given less credit for the TD error.

TD(1) is a way of implementing Monte Carlo algorithms. On-line TD(1) learns in an n-step TD way from the incomplete ongoing episode, where the n steps are all the way up to the current step. If something unusually good or bad happens during an episode, control methods based on TD(1) can learn immediately and alter their behavior on that same episode.

相关文章
|
9月前
|
运维 监控 网络协议
别再说 IPv6 只是“未来”了:我在生产环境踩过的那些坑
别再说 IPv6 只是“未来”了:我在生产环境踩过的那些坑
1079 3
|
7月前
|
存储 人工智能 缓存
长上下文大模型的企业成本治理:从调用链到预算控制
Claude 1M 长上下文、Claude Code、MCP、Agent workflow 的讨论升温后,企业侧的关注点正在从“模型能力是否足够强”转向“能不能稳定、合规、可控地跑在业务里”。
394 2
|
8月前
|
自然语言处理 安全
Stop Fixating on Prompts: Reasoning Hijacking and Constraint Tightening for Red-Teaming LLM Agents(论文解读)
本文提出的JailAgent框架,通过不修改用户提示词的隐式攻击方式,实现了对LLM智能体推理轨迹与记忆检索的高效劫持,兼具高攻击成功率、强泛化性、高隐蔽性与低计算开销,为LLM智能体的红队测试与安全评估提供了全新范式。
384 2
|
消息中间件 安全 API
Apache RocketMQ ACL 2.0 全新升级
RocketMQ 作为一款流行的分布式消息中间件,被广泛应用于各种大型分布式系统和微服务中,承担着异步通信、系统解耦、削峰填谷和消息通知等重要的角色。随着技术的演进和业务规模的扩大,安全相关的挑战日益突出,消息系统的访问控制也变得尤为重要。然而,RocketMQ 现有的 ACL 1.0 版本已经无法满足未来的发展。因此,我们推出了 RocketMQ ACL 2.0 升级版,进一步提升 RocketMQ 数据的安全性。本文将介绍 RocketMQ ACL 2.0 的新特性、工作原理,以及相关的配置和实践。
14648 118
|
10月前
|
存储 缓存 前端开发
知识变现系统源码
本文详解知识变现系统源码架构:基于SpringBoot+Vue+MySQL+Redis,覆盖高并发读写、强权限控制、私有化存储等核心能力,提供模块设计、数据库结构及JWT鉴权、动态签名、HLS切片、缓存限流等关键代码,助你快速落地稳定可扩展的付费内容平台。(239字)
|
机器学习/深度学习 运维 算法
深入探索机器学习中的支持向量机(SVM)算法:原理、应用与Python代码示例全面解析
【8月更文挑战第6天】在机器学习领域,支持向量机(SVM)犹如璀璨明珠。它是一种强大的监督学习算法,在分类、回归及异常检测中表现出色。SVM通过在高维空间寻找最大间隔超平面来分隔不同类别的数据,提升模型泛化能力。为处理非线性问题,引入了核函数将数据映射到高维空间。SVM在文本分类、图像识别等多个领域有广泛应用,展现出高度灵活性和适应性。
930 2
|
人工智能 算法 搜索推荐
刘慈欣预言下的创作革命:刘慈欣称DeepSeek完全可能替代人类作家!
科幻作家刘慈欣谈及AI未来或可替代科幻作家,认为从理论上讲,10至20年后AI完全可能取代科幻小说作家。尽管如此,具有“巅峰创造力”的作家仍难以被完全取代。文章探讨了生成式AI在文学创作中的三重境界:工具化、主体化和生态级重构,并提出GAI认证体系助力创作者掌握AI技术,保持独特性。最终,文学可能演化为共生、抵抗与进化三种形态,在人机协作中迎来更繁荣的未来。
|
网络虚拟化
网络实验 VlAN 中 Trunk Access端口的说明及实验
本文介绍了VLAN技术及其在网络实验中的应用,包括Access端口和Trunk端口的工作原理,以及如何通过实验划分不同VLAN实现内部通信和跨交换机实现VLAN间通信的详细步骤和配置。
网络实验 VlAN 中 Trunk Access端口的说明及实验
|
存储 数据采集 人工智能
信息系统框架标准TOGAF
信息系统框架标准TOGAF
1172 7
|
开发框架 IDE .NET
.NET 6 之 ABP vNext 初体验
本文将介绍在 .net6 平台的 asp.net core webapi 框架中,如何使用 abp vnext 框架进行模块化开发,重在思想理解。
1908 1
.NET 6 之 ABP vNext 初体验