跳至主要內容

Chapter 8 Value Function Methods

RyanLee_ljx...大约 9 分钟RL

Chapter 8 Value Function Methods

In this chapter we move from previous tabular representation for state/action value to function representation. That is to say, we use a function to fit the true expression of the state/action value function. Such a function can be predifined, e.g., a linear function, or a neural network.

The reason why we move from tabular-based representation to function-based are:

  1. storage, compared with tabular, we only need to store a few parameters describing the function.
  2. generalization, action and state may be infinite (or continuous), we cannot store all the state/action value. We need to find a unified representation.
An illustration of the function approximation method. The x-axis and y-axis correspond to  and , respectively.
An illustration of the function approximation method. The x-axis and y-axis correspond to ss and v^(s)\hat v(s), respectively.
An illustration of the process for retrieving the value of s when using the function approximation method.
An illustration of the process for retrieving the value of s when using the function approximation method.

As shown in the figure, we update parameters ω\omega to update the value.

Tabular TD updates an individual value entry. With function approximation, TD updates shared parameters, so an update can change predictions for many states. The usual update is a semi-gradient update: it differentiates the current prediction while treating the bootstrapped target as fixed. It should not be identified with ordinary gradient descent on the full Bellman residual.

Function-based Value representation

A simple linear function approximation:

v^(s,w)=as+b=[s,1]⏟ϕT(s)[ab]⏟w=ϕT(s)w \hat{v}(s, w) = as + b = \underbrace{[s, 1]}_{\phi^T(s)} \underbrace{\begin{bmatrix} a \\ b \end{bmatrix}}_{w} = \phi^T(s)w

here

  • ww is the parameter vector
  • ϕ(s)\phi(s) is the feature vector of ss
  • v^(s,w)\hat{v}(s, w) is linear in ww

We can also fit the points using a second-order curve:

v^(s,w)=as2+bs+c=[s2,s,1]⏟ϕT(s)[abc]⏟w=ϕT(s)w. \hat{v}(s, w) = as^2 + bs + c = \underbrace{[s^2, s, 1]}_{\phi^T(s)} \underbrace{\begin{bmatrix} a \\ b \\ c \end{bmatrix}}_{w} = \phi^T(s)w.

In this case,The dimensions of ww and ϕ(s)\phi(s) increase, but the values may be fitted more accurately. Although v^(s,w)\hat{v}(s, w) is nonlinear in ss, it is linear in ww. The nonlinearity is contained in ϕ(s)\phi(s).

we can use even higher-order polynomial curves or other complex curves to fit the dots, which we can better approximate true value function but needs more parameter.

TD learning of state values based on function approximation

objective

As we are trying to fit the value function, our optimization objective is:

J(w)=E[(vπ(S)−v^(S,w))2], J(w) = \mathbb{E}[(v_{\pi}(S) - \hat{v}(S, w))^2],

here we invovle the random variable SS, if the state is in uniform, we have:

J(w)=1n∑s∈S(vπ(s)−v^(s,w))2, J(w) = \frac{1}{n} \sum_{s \in \mathcal{S}} (v_{\pi}(s) - \hat{v}(s, w))^2,

however, we can write it into a more general form:

J(w)=∑s∈Sdπ(s)(vπ(s)−v^(s,w))2, J(w) = \sum_{s \in \mathcal{S}} d_{\pi}(s)(v_{\pi}(s) - \hat{v}(s, w))^2,

here dπ(s)d_{\pi}(s) is called the the stationary distribution of the Markov process under policy π\pi. That is, the probability for the agent visiting ss after a long period of time is dπ(s)d_\pi(s). By definition, ∑s∈Sdπ(s)=1\sum_{s \in \mathcal{S}} d_\pi(s) = 1.

It is notable that the value of dπ(s)d_\pi(s) is nontrivial to obtain because it requires knowing the state transition probability matrix PπP_\pi. Fortunately, we do not need to calculate the specific value of dπ(s)d_\pi(s) to minimize this objective function as shown in the next subsection. If the state space is continuous, we can replace the summations with integrals in the above equation.

Stationary distribution

Once a policy π\pi is given, the MDP becomes a Markov process, and its dynamics are governed by the probability transition matrix Pπ∈Rn×nP_\pi \in \mathbb{R}^{n \times n}.

Let d0∈Rnd_0 \in \mathbb{R}^n be a vector representing the probability distribution of the states at the initial time step. The probability distribution vector after exactly kk steps, denoted as dkd_k, can be formulated in matrix-vector form as:

dkT=d0TPπk, d_k^T = d_0^T P_\pi^k,

here, PπkP_{\pi}^{k} represents the probability matrix of the agent transitioning from state sis_{i} to sjs_{j} after exactly kk steps under policy π\pi.

For a finite irreducible and aperiodic Markov chain, there is a unique stationary distribution and PπkP_\pi^k converges as kk approaches infinity:

lim⁡k→∞Pπk=1ndπT \lim_{k \to \infty} P_\pi^k = 1_n d_\pi^T

Here, 1n=[1,…,1]T∈Rn1_n = [1, \dots, 1]^T \in \mathbb{R}^n, which means 1ndπT1_n d_\pi^T is a constant matrix where all of its rows are equal to dπTd_\pi^T. Substituting this limit back into the state evolution equation yields:

lim⁡k→∞dkT=d0Tlim⁡k→∞Pπk=d0T1ndπT=dπT \lim_{k \to \infty} d_k^T = d_0^T \lim_{k \to \infty} P_\pi^k = d_0^T 1_n d_\pi^T = d_\pi^T

Under these assumptions, the initial probabilities sum to one (d0T1n=1d_0^T1_n=1), so the limiting distribution is independent of d0d_0. Without such assumptions, a stationary distribution may not be unique or the time-indexed distribution may not converge to it. Stationarity should not be assumed for every trajectory from its first sample.

The analytical value of dπd_\pi can be calculated by leveraging the recursive nature of the transition process. By taking the limit of both sides of the iterative equation dkT=dk−1TPπd_k^T = d_{k-1}^T P_\pi, we obtain:

lim⁡k→∞dkT=lim⁡k→∞dk−1TPπ \lim_{k \to \infty} d_k^T = \lim_{k \to \infty} d_{k-1}^T P_\pi

Since both distributions converge as k→∞k \to \infty, this directly resolves to the stationary equation:

dπT=dπTPπ d_\pi^T = d_\pi^T P_\pi

In linear algebra, the left eigenvector xx and its corresponding eigenvalue λ\lambda of a matrix AA satisfy the definition xTA=λxTx^{T}A = \lambda x^{T}.

Comparing this to the steady-state equation dπTPπ=1⋅dπTd_{\pi}^{T}P_{\pi} = 1 \cdot d_{\pi}^{T}, it is clear that dπd_{\pi} is precisely the left eigenvector of the probability transition matrix PπP_{\pi} corresponding to the eigenvalue λ=1\lambda=1. when transforming the formula to (PπT−I)dπ=0(P_{\pi}^{T} - I)d_{\pi} = 0, we find the corresponding eigenvector with an eigenvalue of 1 by solving this homogeneous linear system of equations.

Optimization

We can adopt the gradient descent method to optimaize the objective:

wk+1=wk−αk∇wJ(wk), w_{k+1} = w_k - \alpha_k \nabla_w J(w_k),

where

∇wJ(wk)=∇wE[(vπ(S)−v^(S,wk))2]=E[∇w(vπ(S)−v^(S,wk))2]=2E[(vπ(S)−v^(S,wk))(−∇wv^(S,wk))]=−2E[(vπ(S)−v^(S,wk))∇wv^(S,wk)]. \begin{aligned} \nabla_w J(w_k) &= \nabla_w \mathbb{E}[(v_{\pi}(S) - \hat{v}(S, w_k))^2] \\ &= \mathbb{E}[\nabla_w (v_{\pi}(S) - \hat{v}(S, w_k))^2] \\ &= 2\mathbb{E}[(v_{\pi}(S) - \hat{v}(S, w_k))(-\nabla_w \hat{v}(S, w_k))] \\ &= -2\mathbb{E}[(v_{\pi}(S) - \hat{v}(S, w_k))\nabla_w \hat{v}(S, w_k)]. \end{aligned}

Therefore, the gradient descent algorithm is

wk+1=wk+2αkE[(vπ(S)−v^(S,wk))∇wv^(S,wk)], w_{k+1} = w_k + 2\alpha_k \mathbb{E}[(v_{\pi}(S) - \hat{v}(S, w_k))\nabla_w \hat{v}(S, w_k)],

where the coefficient 22 before αk\alpha_k can be merged into αk\alpha_k without loss of generality. The algorithm requires calculating the expectation. In the spirit of stochastic gradient descent (SGD), we can replace the true gradient with a stochastic gradient. Then, we have

wt+1=wt+αt(vπ(st)−v^(st,wt))∇wv^(st,wt), w_{t+1} = w_t + \alpha_t (v_{\pi}(s_t) - \hat{v}(s_t, w_t))\nabla_w \hat{v}(s_t, w_t),

where sts_t is a sample of SS at time tt. It requires the true state value vπv_{\pi}, which is unknown and must be estimated. We can replace vπ(st)v_{\pi}(s_t) with an approximation to make the algorithm implementable.

The following two methods can be used to do so.

  1. Monte Carlo method: Suppose that we have an episode (s0,r1,s1,r2,… )(s_0, r_1, s_1, r_2, \dots). Let gtg_t be the discounted return starting from sts_t. Then, gtg_t can be used as an approximation of vπ(st)v_{\pi}(s_t). The algorithm in (8.12) becomes:

wt+1=wt+αt(gt−v^(st,wt))∇wv^(st,wt) w_{t+1} = w_t + \alpha_t (g_t - \hat{v}(s_t, w_t))\nabla_w \hat{v}(s_t, w_t)

This is the algorithm of Monte Carlo learning with function approximation.

  1. Temporal-difference method: In the spirit of TD learning, rt+1+γv^(st+1,wt)r_{t+1} + \gamma \hat{v}(s_{t+1}, w_t) can be used as an approximation of vπ(st)v_{\pi}(s_t). The algorithm becomes

wt+1=wt+αt[rt+1+γv^(st+1,wt)−v^(st,wt)]∇wv^(st,wt). w_{t+1} = w_t + \alpha_t [r_{t+1} + \gamma \hat{v}(s_{t+1}, w_t) - \hat{v}(s_t, w_t)]\nabla_w \hat{v}(s_t, w_t).

This is semi-gradient TD. During the update, yt=rt+1+γv^(st+1,wt)y_t=r_{t+1}+\gamma\hat v(s_{t+1},w_t) is treated as a constant target: gradients flow through v^(st,w)\hat v(s_t,w), not through the target's dependence on wtw_t. In code, the target is detached or calculated without recording gradients. At a true terminal transition, omit the bootstrap term. Replacing vπv_\pi with this target does not make the update an unbiased gradient of the original mean-squared value error for an arbitrary approximate critic.

We can use the function introduce in Function-based Value representation to represent which function is we use to approximate the true value function. If we use linear approximation function, the gradient is ∇wv^(s,w)=ϕ(s)\nabla_w \hat{v}(s, w) = \phi(s). We can also use neural network.

Theoretical analysis

For the convergence, see at book.

The true state-value function vπv_{\pi} strictly satisfies the Bellman equation: vπ=rπ+γPπvπv_{\pi}=r_{\pi}+\gamma P_{\pi}v_{\pi}. When we introduce a function approximator (e.g., a linear function or neural network with parameters ww), we hope that the estimated value v^(w)\hat{v}(w) also satisfies this equation as much as possible. Therefore, the difference between the two sides of the equation is the Bellman error:

JBE(w)=∥v^(w)−(rπ+γPπv^(w))∥D2≐∥v^(w)−Tπ(v^(w))∥D2. J_{BE}(w) = \|\hat{v}(w) - (r_\pi + \gamma P_\pi \hat{v}(w))\|^2_D \doteq \|\hat{v}(w) - T_\pi(\hat{v}(w))\|^2_D.

The Bellman error often cannot be optimized to 0 because the function approximators we use (such as low-dimensional linear features) have limited approximation ability, the result mapped by the Bellman operator Tπ(v^(w))=rπ+γPπv^(w)T_{\pi}(\hat{v}(w))=r_\pi + \gamma P_\pi \hat{v}(w) often "runs out" of the space that our function approximator can represent.

For linear function approximation, the TD fixed point can instead be characterized using a projection back into the feature space. The mean-squared projected Bellman error is:

JPBE(w)=∥v^(w)−MTπ(v^(w))∥D2. J_{PBE}(w)=\|\hat{v}(w)-MT_{\pi}(\hat{v}(w))\|_{D}^{2}.

Here, D=diag⁡(dπ)D=\operatorname{diag}(d_\pi) and MM is the orthogonal projection onto the linear feature space in the D-weighted norm. Under standard discounted, on-policy linear-TD assumptions, including suitable feature rank, state coverage, and step sizes, TD converges to the projected Bellman fixed point. At this point the projected error is zero. This conclusion requires those assumptions; merely projecting into the feature space does not establish it for arbitrary settings.

Ordinary semi-gradient TD is not generally gradient descent on JPBEJ_{PBE} itself. The linear on-policy result must not be extended into a blanket convergence claim for neural networks or off-policy TD. Function approximation, bootstrapping, and off-policy sampling can together lead to instability or divergence.

TD learning of action values based on function approximation

Here we use function to approximate action values.

The Sarsa algorithm with function approximation can be readily obtained by replacing the state values with action values in TD learning of state values based on function approximation section. In particular, suppose that qπ(s,a)q_{\pi}(s, a) is approximated by q^(s,a,w)\hat{q}(s, a, w). Replacing v^(s,w)\hat{v}(s, w) by q^(s,a,w)\hat{q}(s, a, w) gives:

wt+1=wt+αt[rt+1+γq^(st+1,at+1,wt)−q^(st,at,wt)]∇wq^(st,at,wt). w_{t+1} = w_t + \alpha_t [r_{t+1} + \gamma \hat{q}(s_{t+1}, a_{t+1}, w_t) - \hat{q}(s_t, a_t, w_t)] \nabla_w \hat{q}(s_t, a_t, w_t).

Pseudocode: Sarsa with function approximation
Pseudocode: Sarsa with function approximation

Tabular Q-learning can also be extended to the case of function approximation. The update rule is

wt+1=wt+αt[rt+1+γmax⁡a∈A(st+1)q^(st+1,a,wt)−q^(st,at,wt)]∇wq^(st,at,wt). w_{t+1} = w_t + \alpha_t \left[ r_{t+1} + \gamma \max_{a \in \mathcal{A}(s_{t+1})} \hat{q}(s_{t+1}, a, w_t) - \hat{q}(s_t, a_t, w_t) \right] \nabla_w \hat{q}(s_t, a_t, w_t).

Deep Q-learning

To connect DQN with Q-learning, first consider the mean-squared sampled TD error:

J=E[(R+γmax⁡a∈A(S′)q^(S′,a,w)−q^(S,A,w))2], J = \mathbb{E} \left[ \left( R + \gamma \max_{a \in \mathcal{A}(S')} \hat{q}(S', a, w) - \hat{q}(S, A, w) \right)^2 \right],

Here (S,A,R,S′)(S,A,R,S') denotes a sampled transition. The Bellman optimality equation requires the conditional mean TD error to be zero at q∗q^*; it does not require every sampled TD error, or its mean square, to be zero. Writing Y=R+γmax⁡aq^(S′,a,w)Y=R+\gamma\max_a\hat q(S',a,w) gives:

E[(Y−q^(s,a,w))2∣s,a]=(E[Y∣s,a]−q^(s,a,w))2+Var⁡(Y∣s,a). \mathbb E[(Y-\hat q(s,a,w))^2|s,a] =(\mathbb E[Y|s,a]-\hat q(s,a,w))^2+\operatorname{Var}(Y|s,a).

Thus, mean-squared sampled TD error includes target variance in addition to the squared Bellman residual. For episodic tasks, all DQN targets below use R+γ(1−d)max⁡aq^(S′,a,⋅)R+\gamma(1-d)\max_a\hat q(S',a,\cdot), where d indicates true termination; the displayed formulas omit d for brevity.

DQN treats the target as fixed during each gradient update rather than differentiating both occurrences of w in the expression above. It additionally uses a target network that changes more slowly to reduce instability from rapidly moving bootstrap targets. Stopping target gradients and maintaining a lagged target network are distinct choices. The two networks are:

  1. Main Network: represent q^(s,a,w)\hat{q}(s, a, w), namely the approximation of action value.
  2. Target network: a lagged copy q^(s,a,wT)\hat q(s,a,w_T) of the main network used to construct bootstrap targets. It is also an approximation and does not already know the optimal action values.

The objective function in this case becomes:

J=E[(R+γmax⁡a∈A(S′)q^(S′,a,wT)−q^(S,A,w))2], J = \mathbb{E} \left[ \left( R + \gamma \max_{a \in \mathcal{A}(S')} \hat{q}(S', a, w_T) - \hat{q}(S, A, w) \right)^2 \right],

where wTw_T is the target network's parameter. When wTw_T is fixed, the gradient of JJ is

∇wJ=−E[(R+γmax⁡a∈A(S′)q^(S′,a,wT)−q^(S,A,w))∇wq^(S,A,w)], \nabla_w J = -\mathbb{E} \left[ \left( R + \gamma \max_{a \in \mathcal{A}(S')} \hat{q}(S', a, w_T) - \hat{q}(S, A, w) \right) \nabla_w \hat{q}(S, A, w) \right],

where some constant coefficients are omitted without loss of generality.

The main network is updated in every iteration. By contrast, the target network is set to be the same as the main network every certain number of iterations to satisfy the assumption that wTw_T is fixed when calculating the gradient.

DQN commonly uses experience replay to reuse transitions and reduce the temporal correlation within training batches. We store transitions, including terminal information, in a replay buffer B\mathcal B and sample minibatches to update the main network. This supports data reuse; using a neural network does not by itself make replay mandatory.

Uniformly sampling buffer entries is a simple choice, but it does not make the state-action distribution uniform. If 90% of stored transitions visit one state, approximately 90% of uniformly sampled entries will still visit that state. Replay neither guarantees state coverage nor requires uniform sampling. Prioritized experience replay uses nonuniform probabilities, with importance weights to address the resulting sampling bias relative to its intended replay objective. The buffer composition and sampling rule determine how states and actions are weighted in the training loss.

Pseudocode: DQN
Pseudocode: DQN

The earlier algorithms were presented as sequential updates for clarity, not because linear models forbid replay or guarantee convergence in all cases. Suitable on-policy linear TD evaluation admits convergence analysis under Markov sampling; this does not imply convergence of arbitrary linear Q-learning or that samples collected while a policy changes already follow a single stationary distribution.

Deep on-policy methods can learn from recent rollouts without a long-term replay buffer; A2C and PPO are examples. For ordinary on-policy Sarsa, reusing a stored next action from an old policy as though it were sampled from the current policy creates a mismatch in the target q^(st+1,at+1,wt)\hat q(s_{t+1},a_{t+1},w_t). Reusing such data requires an appropriate off-policy treatment rather than an assumption that all deep methods should share DQN's replay procedure.

评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.1.3