David Silver 교수님의 Introduction to Reinforcement Learning (Website)
Lecture 6: Value Function Approximation (Youtube) 강의 내용을 정리했습니다.
Introduction
Large-Scale Reinforcement Learning
- RL can be used to solve large problems, e.g.
- Backgammon: 1020 states
- Computer Go: 10170 states
- Helicopter: continuous state space
- 이때까지 우리는 Q(s,a)를 lookup table로 생각하고 각 table의 값을 update 해왔다.
하지만 state가 위 예시들처럼 엄청 많거나 continuous space인 경우엔 table을 만들 수 없거나 지나치게 비효율적이다.
- How can we scale up the model-free methods for prediction and control from the last two lectures?
=> Value function approximation
Value Function Approximation
- For large MDPs:
- Estimate value function with function approximation
- v^(s,w)≈vπ(s)
- q^(s,a,w)≈qπ(s,a)
- Generalize from seen states to unseen states
- Update parameter w using MC or TD learning
- Function Approximators
- Linear combinations of features
- Neural network
- Decision tree
- Nearest neighbor
- ...
- Note: We will only consider differentiable approximators, the "Linear combinations of features" and the "Neural network".
Incremental Methods
Gradient Descent
- Let J(w) be a differentiable function of parameter vector w
- Gradient of J(w) is:
∇wJ(w)=⎝⎜⎜⎜⎜⎜⎜⎛∂w1∂J(w)⋮∂wn∂J(w)⎠⎟⎟⎟⎟⎟⎟⎞
- To find a local minimum of J(w), adjust w in direction of −∇wJ(w)
- i.e., w←w−21α∇wJ(w), where α is a step-size parameter
Value Function Approx. By Stochastic Gradient Descent
- vπ(s)를 알고 있다고 가정하고, 우리의 value function approximator를 v^(s,w)라고 하자.
- Let J(w)=Eπ[(vπ(S)−v^(S,w))2]
(MSE between vπ(s) and v^(s,w), J를 최소화하면 v와 v^가 가까워진다.)
- Then,
∇wJ(w)=−Eπ[2×(vπ(S)−v^(S,w))∇wv^(S,w)],
and,
Δw=−21α∇wJ(w) =αEπ[(vπ(S)−v^(S,w))∇wv^(S,w)]
- Stochastic gradient descent samples the gradient
Δw=α(vπ(S)−v^(S,w))∇wv^(S,w)
Linear Value Function Approximation
- Represent state by a feature vector
x(S)=⎝⎜⎜⎛x1(S)⋮xn(S)⎠⎟⎟⎞
- e.g. robot의 3차원 좌표 (기준점에서의 xyz 거리), 포트폴리오에 있는 주식들의 가격
- Represent value function by a linear combination of features
v^(S,w)=x(S)Tw=j=1∑nxj(S)wj
- Objective function is quadratic in parameters w
J(w)=Eπ[(vπ(S)−x(S)Tw)2]
(여전히 우리가 vπ 알고 있다고 가정!)
- Stochastic gradient descent converges on global optimum
(∵ convex optimization)
- ∇wv^(S,w)=x(S)
∴Δw=α(vπ(S)−v^(S,w))∇wv^(S,w) =α(vπ(S)−v^(S,w))x(S)
Update = step-size × prediction error × feature value
Note: update 크기와 feature value의 크기가 비례한다. 즉, features 중 값이 큰 feature들의 변화량이 더 커야한다.
Note2: w를 계속 update 하면, v^(s,w)=x(S)Tw≈vπ(s)
Table Lookup Features
-
Table lookup is a special case of linear value function approximation
-
xtable(S)=⎝⎜⎜⎛1(S=s1)⋮1(S=sn)⎠⎟⎟⎞
-
v^(S,w)=⎝⎜⎜⎛1(S=s1)⋮1(S=sn)⎠⎟⎟⎞⋅⎝⎜⎜⎛w1⋮wn⎠⎟⎟⎞
v^(sk,w)=wk
-
∴w converges to vπ
Incremental Prediction Algorithm
- 이제 vπ(S)를 모른다고 가정하자. (보다 일반적인 상황)
- No supervisor, but we have rewards!
- In practice, we substitute a target for vπ(s)
- 앞에서는 v^π(s)가 vπ(s)와 가까워지도록 수정했다.
J(w)=Eπ[(vπ(S)−v^(S,w))2]
- 지금은 vπ(s)가 없으니 target과 가까워지도록 v^π(s)를 수정하고자 한다.
=> J(w)=Eπ[target−v^(S,w))2]
=> Δw=−21α∇wJ(w) =α(target−v^(S,w))∇wv^(S,w) ... (SGD)
Note: 단, 이때 target은 상수로 여긴다! 우리가 개선시키고자 하는 방향이 target이기 때문에 TD에서처럼 target에 w term이 들어가더라도 미분 대상이 아니다. TD에서 target을 같이 미분해버리면 기준점(1-step 이후의 estimated value)이 기준점으로서의 역할을 못해서 제대로 된 학습이 이뤄지지 않는다. (참고: 강의영상 45:55~)
- Targets
- MC: Gt
- TD(0): Rt+1+γv^(St+1,w)
- TD(λ): Gtλ
- MC with Value Function Approximation
- Return Gt is an unbiased but noisy sample of vπ(St)
- Apply supervised learning to "training data":
⟨S1,G1⟩,⟨S2,G2⟩,...,⟨ST,GT⟩
- Linear MC policy evaluation
(앞에서 본 linear combination 방식으로 v^를 정의)
Δw=α(Gt−v^(St,w))x(St)
- MC with linear val. func. guarantees convergence to global optimum
MC with non-linear val. func. guarantees convergence to local optimum
- TD(0) with Value Function Approximation
- The TD-target Rt+1+γv^(St+1,w) is a biased sample of true value vπ(St)
- Again, apply supervised learning to "training data":
⟨S1,R2+γv^(S2,w)⟩,⟨S2,R3+γv^(S3,w)⟩,...,⟨ST,RT⟩
- Linear TD(0) policy evaluation
Δw=α(Rt+1+γv^(St+1,w)−v^(St,w))x(St)
- Linear TD(0) converges close to global optimum, but doesn't guarantee to obtain global optimum
- TD(λ) with Value Function Approximation
- The λ-return Gtλ is also a biased sample of true value vπ(s)
- Again, apply supervised learning to "training data":
⟨S1,G1λ⟩,⟨S2,G2λ⟩,...,⟨ST,GTλ⟩
- Forward view linear TD(λ)
Δw=α(Gtλ−v^(St,w))x(St)
- Backward view linear TD(λ)
δt=Rt+1+γv^(St+1,w)−v^(St,w)
Et=γλEt−1+x(St)
Δw=αδtEt
Note: ∇wv^(St,w)=x(St), 즉, 각 feature가 v^에 responsible한 만큼 eligibility를 준다.
또는, 원래는 방문한 state St의 eligibility를 1만큼 증가시키는 방식으로 했었는데, linear FA에선 St의 각 feature의 크기만큼 eligibility를 준다고 생각할 수 있다.
Note2: Et가 이전에 방문한 state에 대응되는 feature들에 대해서도 weight를 주기 때문에 backward view로 update가 된다!
- Forward view and backward view linear TD(λ) are equivalent!
Incremental Control Algorithm
- Model free control에서 V가 아닌 Q를 썼듯이, 여기서도 FA를 action value function q에 적용한다.
- Policy Evaluation: Approximate policy evaluation q^(⋅,⋅,w)≈qπ
- Policy Improvement: ϵ-greedy policy improvement
- Action-Value Function Approximation
- We want:
q^(S,A,w)≈qπ(S,A)
- To achieve this,
J(w)=Eπ[(qπ(S,A)−q^(S,A,w))2] ... MSE
Note: J(w)를 minimize => q^(S,A,w)와 qπ(S,A)가 가까워짐
- Use SGD to find a local minimum
- w←w+Δw
- Δw=−21α∇wJ(w) =α(qπ(S,A)−q^(S,A,w))∇wq^(S,A,w)
- Linear Action-Value Function Approximation
- Represent state and action by a feature vector
x(S,A)=⎝⎜⎜⎛x1(S,A)⋮xn(S,A)⎠⎟⎟⎞
- Represent action-value function by linear combination of features
q^(S,A,w)=x(S,A)Tw=j=1∑nxj(S,A)wj
- Stochastic gradient descent update
∇wq^(S,A,w)=x(S,A)
∴Δw=α(qπ(S,A)−q^(S,A,w))x(S,A)
- Linear Incremental Control Algorithms
- Like prediction, we must substitute a target for qπ(S,A)
- MC: Gt
- TD(0): Rt+1+γq^(St+1,At+1,w)
- TD(λ): qtλ
- For MC, TD(0), or forward-view TD(λ)
- Δw=α(target−q^(S,A,w))x(S,A)
- For backward-view TD(λ),
- δt=Rt+1+γq^(St+1,At+1,w)−q^(St,At,w)
- Et=γλEt−1+∇wq^(St,At,w) =γλEt−1+x(St,At)
- Δw=αδtEt
MC vs. TD
- Example: Mountain Car
Image from: here
- Recall, TD(1) ≡ MC
- MC는 제대로 학습 되지 않는 모습을 보인다. (∵ high variance)
(일반적으로는 bootstrap을 쓰는 게 낫더라!)
- Example2: Baird's Counterexample (TD(0)로 학습)
Image from: here
- parameter θ가 explode
- TD는 항상 convergence를 보장하지는 않더라!
- Convergence of Prediction Algorithms
Image from: here
- TD가 off-policy나 non-linear FA에서 발산하는 이유는 TD update가 어떤 함수의 gradient가 아니기 때문이다! (앞의 Incremental Prediction Algorithm 부분 참고)
- Gradient TD follows true gradient of projected Bellman error, thus converges! (자세한 내용은 생략)
- Convergence of Control Algorithms
Image from: here
- Recall,
Sarsa -> On-policy TD control
Q-learning => Off-policy TD control
Batch Methods
Batch Reinforcement Learning
- 지금까지 논의한 알고리즘들은 sample을 순차적으로 하나씩 받아서 weight를 update
- Simple but not sample efficient (update가 많다)
- Sample 하나로 한 번만 update (여러번 update 하는 게 더 좋다)
- Batch methods seek to find the best fitting value function given the agent's experience(=batch)
i.e., batch 안에 있는 모든 samples에 가장 잘 fit 하는 value function을 찾아준다.
Least Squares Prediction
- Using linear value function approximation v^(s,w)=x(s)Tw
and experience D={⟨s1,v1π⟩⟨s2,v2π⟩...,⟨sT,vTπ⟩},
which parameters w give the best fitting value function v^(s,w)?
- Least square algorithm minimizes sum-squared error between v^(st,w) and target values vtπ
LS(w)=t=1∑T(vtπ−v^(st,w))2
- v^가 w에 대해 linear function이라고 가정했기 때문에 wargminLS(w)를 closed form solution으로 바로 찾을 수 있다.
Let w∗=wargminLS(w∗), then ED[Δw∗]=0
⟺αED[∇wLS(w∗)]=0
⟺αt=1∑T(vtπ−x(st)Tw∗)x(st)=0 ................................ (!)
⟺t=1∑Tvtπx(st)=t=1∑T(x(st)Tw∗)x(st)
⟺t=1∑Tvtπx(st)=t=1∑T(x(st)x(st)T)w∗
⟺w∗=(t=1∑T(x(st)x(st)T))−1⋅t=1∑Tvtπx(st)
(For N features, direct solution time is O(N3))
- 실제로는 vtπ를 모르는 상태이기 때문에:
- 1) MC
ED[Δw∗]=0 with vtπ=Gt
⟹ (!)식에 vtπ 대신 Gt 대입
⟹w∗=(t=1∑Tx(st)x(st)T)−1⋅t=1∑TGtx(st)
- 2) TD
ED[Δw∗]=0 with vtπ=Rt+1+γv^(St+1,w)
⟹ (!)식에 vtπ 대신 Rt+1+γv^(St+1,w) 대입
⟹w=(t=1∑Tx(st)(x(st)−γx(st+1))T)−1⋅t=1∑TRt+1x(st)
- 3) TD(λ)
ED[Δw∗]=0 with Backward view TD(λ)
⟹t=1∑TαδtEt=αt=1∑T(Rt+1+γv^(St+1,w)+v^(st,w))=0
⟹w=(t=1∑TEt(x(st)−γx(st+1))T)−1⋅t=1∑TRt+1Et
- Convergence of Least Square Prediction Algorithms
Image from: here
- Linear FA + Least Square이면 항상 v^→vπ 수렴이 보장된다! (closed form solution이 존재하기 때문)
- 앞의 incremental prediction에서는 (linear FA임에도) Off-policy TD에 대해서 수렴을 보장하지 못했다.
- Non-linear일 때는 적용할 수 없다.
- Non-linear function이거나 N이 큰 경우에는 w를 directly 구하기 어렵기 때문에 위와 같은 방법을 적용할 수 없다.
=> 앞의 incremental method와 비슷한 방식으로, iterative한 방법을 활용한다.
=> SGD with experience replay ... 하나의 experience에서 반복적으로 sampling 한다.
단, update가 많다는 문제는 해결하지 못한다.
- Repeat:
- Sample state, value from experience
⟨s,vπ⟩∼D
- Apply SGD update
Δw=−21α∇wLS(w) ... 여기선 sample-wise이므로 LS(w)=(vπ−v^(s,w))2
=α(vπ−v^(s,w))∇wv^(s,w)
- Converges to least squares solution: (replay가 수렴을 도와준다!)
wπ=wargminLS(w)
v^(s,wπ)≈vπ(s)
- Experience Replay in Deep Q-Network (DQN)
- DQN uses experience replay and fixed Q-targets
- Experience replay decorrelate the trajectories (stabilize 역할)
- Fixed Q-targets means that we freeze the old network (w−) and bootstrap towards the frozen target (이것도 stabilize 역할, target이 움직이면 불안정하기 때문이다!)
- DQN Algorithm
- Take action at (by ϵ-greedy)
- Store transition (st,at,rt+1,st+1) in replay memory D
- Repeat
- Sample random mini-batch of transitions (s,a,r,s′) from D
- Compute Q-learning targets w.r.t. old, fixed paramters w−
Note: for a sample, Q-learning target =r+γa′maxQ(s′,a′;w−)
- Optimize MSE between Q-network and Q-learning targets using variant of SGD
Li(wi)=Es,a,r,s′∼Di[(r+γa′maxQ(s′,a′;wi−)−Q(s,a,;wi))]
Note: 현재 mini-batch에 대해 w−로 얻은 one-step further Q에 가장 잘 fit하도록 w 조정
- w−←w
- DQN in Atari
Image from: here
- Input (Stack of 4 previous frames) = State
- Output (linear output layer with 18 elements) = Q(s,a) for 18 joystick/button positions
Least Squares Control
- Least Squares Policy Iteration
Image from: here
- Policy Evaluation: Least squares Q-learning
Policy Improvement: Greedy policy improvement
- Least Squares Action-Value Function Approximation
- Approximate action-value function qπ(s,a)
using linear combination of features x(s,a),
q^(s,a,w)=x(s,a)Tw≈qπ(s,a)
- Minimize least squares error between q^(s,a,w) and qπ(s,a) from experience D generated using policy π,
D={⟨(s1,a1,v1π),(s2,a2,v2π),...,(sT,aT,vTπ)⟩}
- Least Squares Q-learning
Off-policy 상황(old-policy is the behaviour policy)을 생각하고 Q-learning을 적용한다.
- Use experience generated by old policy
St,At,Rt+1,St+1∼πold
- Consider alternative successor action A′=πnew(St+1)
- Update q^(St,At,w) towards value of alternative action,
Rt+1+γq^(St+1,A′,w)
- Q-learning Update:
δ=Rt+1+γq^(St+1,A′,w)−q^(St,At,w)
Δw=αδx(St,At)
- LSTDQ algorithm: Total update = 0
ED[Δw]=0
⟺t=1∑Tαδx(St,At)=0
⟺w=(t=1∑Tx(St,At)(x(St,At)−γx(St+1,π(St+1)))T)−1⋅t=1∑Tx(St,At)Rt+1
- LSPI-TD with LSTDQ Algorithm
Image from: here
- Experience D를 반복적으로 evaluate! (매번 update된 policy로 evaluate)
=> 그래서 Off-policy!
- Lec. 5에서의 Off-policy control by Q-learning 과는 batch(=experience) 단위로 update 된다는 점에서 차이가 있다. (더 효율적)
혹시 오타나 잘못된 부분이 있다면 댓글로 알려주시면 감사하겠습니다!