RL Taxonomy
Drawbacks of previous methods
Large state space:
Go: 10 170 10^{170} 1 0 170
Backgammon: 10 20 10^{20} 1 0 20
Atari games: 10 9 to 10 11 10^9 \text{ to } 10^{11} 1 0 9 to 1 0 11
Scale-up model-free based techniques for prediction and control is challenging.
Value function used look-up table representation
All states has an entry in V ( s ) V(s) V ( s )
All state-action pair ( s , a ) (s,a) ( s , a ) has an entry in Q ( s , a ) Q(s,a) Q ( s , a )
Too many state, and stat-action pair to store in memory
Slow learning process for each states, due to large space.
Each states needs to be explored sufficiently.
For large MDPs
Function approximation: Almost near optimal value.
Estimate value function with function approximation.
v ^ ( s , w ) ≈ v π ( s ) \hat{v}(s,w) \approx v_\pi(s) v ^ ( s , w ) ≈ v π ( s )
q ^ ( s , a , w ) ≈ q π ( s , a ) \hat{q}(s,a,w) \approx q_\pi(s,a) q ^ ( s , a , w ) ≈ q π ( s , a )
Generalize from seen states to unseen states.
Using MC or TD learning techniques: Update parameter w w w .
Function Approximation
A technique for estimating unknown underlying function using historical or available observations.
Assumption: an underlying mapping function exists
f ( x ) ≈ f ^ ( x ) f(x) \approx \hat{f}(x) f ( x ) ≈ f ^ ( x )
Function: x → f y x \xrightarrow{f} y x f y
Types of Value Function Approximation
State-value: state s s s → scalar v ^ ( s , w ) \hat{v}(s,w) v ^ ( s , w )
Action-value (s, a input): state s s s and action a a a → scalar q ^ ( s , a , w ) \hat{q}(s,a,w) q ^ ( s , a , w )
Action-value (s input, all actions out): state s s s → q ^ ( s , a 1 , w ) , … , q ^ ( s , a m , w ) \hat{q}(s,a_1,w),\ldots,\hat{q}(s,a_m,w) q ^ ( s , a 1 , w ) , … , q ^ ( s , a m , w )
w w w is the parameter vector of the function approximator.
e.g. Neural Network weights
Types of Function Approximator
Tabular
V-Table: s → v ( s ) s \rightarrow v(s) s → v ( s )
Q-Table: ( s , a ) → q ( s , a ) (s,a) \rightarrow q(s,a) ( s , a ) → q ( s , a )
Decision Trees, Nearest Neighbors
s = [ speed = 49 , distance = 10 ] s = [\text{speed} = 49, \text{distance} = 10] s = [ speed = 49 , distance = 10 ]
s → Decision Tree / KNN → v ^ ( s ) s \rightarrow \text{Decision Tree / KNN} \rightarrow \hat{v}(s) s → Decision Tree / KNN → v ^ ( s )
Linear Function approximation
Linear Combination of Features
Values are linear function for features v ^ ( s , w ) = w T x ( s ) \hat{v}(s,w) = w^Tx(s) v ^ ( s , w ) = w T x ( s )
x ( s ) = [ speed distance fuel ] x(s) = \begin{bmatrix} \text{speed} \\ \text{distance} \\ \text{fuel} \end{bmatrix} x ( s ) = speed distance fuel
v ^ ( s , w ) = w T x ( s ) \hat{v}(s,w) = w^Tx(s) v ^ ( s , w ) = w T x ( s )
v ^ ( s , w ) = w 1 x 1 ( s ) + ⋯ + w n x n ( s ) \hat{v}(s,w) = w_1x_1(s) + \cdots + w_n x_n(s) v ^ ( s , w ) = w 1 x 1 ( s ) + ⋯ + w n x n ( s )
Differentiable function approximation
v ^ ( s , w ) \hat{v}(s,w) v ^ ( s , w ) is a differentiable function of w w w , can be non-linear in s s s
e.g. Neural Network, or CNN
∂ v ^ ( s , w ) ∂ w \frac{\partial \hat{v}(s,w)}{\partial w} ∂ w ∂ v ^ ( s , w )
w ← w − α ∇ w L w \leftarrow w - \alpha \nabla_wL w ← w − α ∇ w L
Pixels → C N N v ^ ( s ) \text{Pixels} \xrightarrow{CNN} \hat{v}(s) Pixels CNN v ^ ( s )
Which FA to use?
In principle, any FA that fits the RL framework can be used.
FA Notes Tabular Easy; not scalable; does not generalise Linear Requires good features Differentiable (better choice) Scalable; not always well understood Neural Networks Performs well Deep Neural Networks Popular choice; performs well
Need training methods that are suitable for non-stationary data.
ANN
Logistic Regression
L ( a , y ) = − y log ( a ) − ( 1 − y ) log ( 1 − a ) \mathcal{L}(a, y) = -y \log(a) - (1-y) \log(1-a) L ( a , y ) = − y log ( a ) − ( 1 − y ) log ( 1 − a )
Loss function for Logistic Regression
Gradient Descent
Goal: Find w w w tht minimize J ( w ) J(w) J ( w ) or find a local minimum of J ( w ) J(w) J ( w ) .
An iterative approach for error correction.
For one sample, L ( a , y ) = − y log ( a ) − ( 1 − y ) log ( 1 − a ) \mathcal{L}(a, y) = -y \log(a) - (1-y) \log(1-a) L ( a , y ) = − y log ( a ) − ( 1 − y ) log ( 1 − a )
For m m m samples,
J ( w , b ) = 1 m ∑ i = 1 m L ( a ( i ) , y ( i ) ) J(w, b) = \frac{1}{m} \sum_{i=1}^m \mathcal{L}(a^{(i)}, y^{(i)}) J ( w , b ) = m 1 ∑ i = 1 m L ( a ( i ) , y ( i ) )
Find w w w and b b b that will minimize J ( w , b ) J(w, b) J ( w , b )
min w , b J ( w , b ) \min_{w, b} J(w, b) min w , b J ( w , b )
J ( w ) J(w) J ( w ) is loss function.
w ← w − α ∂ J ( w , b ) ∂ w , b ← b − α ∂ J ( w , b ) ∂ b w \leftarrow w - \alpha \frac{\partial J(w, b)}{\partial w}, \quad b \leftarrow b - \alpha \frac{\partial J(w, b)}{\partial b} w ← w − α ∂ w ∂ J ( w , b ) , b ← b − α ∂ b ∂ J ( w , b )
Δ w J ( w ) = ( ∂ J ( w ) ∂ w 1 , … , ∂ J ( w ) ∂ w n ) \Delta w J(w) = \big(\frac{\partial J(w)}{\partial w_1}, \ldots, \frac{\partial J(w)}{\partial w_n}\big) Δ w J ( w ) = ( ∂ w 1 ∂ J ( w ) , … , ∂ w n ∂ J ( w ) )
Adjust the w w w in the direction of the -ve gradient.
w = w − α Δ w , b = b − α Δ b w = w - \alpha \Delta w, \quad b = b - \alpha \Delta b w = w − α Δ w , b = b − α Δ b
where α \alpha α is the learning rate.
and, Δ w = ∂ J ( w , b ) ∂ w , Δ b = ∂ J ( w , b ) ∂ b \Delta w = \frac{\partial J(w, b)}{\partial w}, \quad \Delta b = \frac{\partial J(w, b)}{\partial b} Δ w = ∂ w ∂ J ( w , b ) , Δ b = ∂ b ∂ J ( w , b )
ANN as a Function Approximator
Need a method to estimate: Δ w \Delta w Δ w
Method to incrementally adjust and update: w w w
Representation of some aspects of RL environments as feature vector x ( s ) x(s) x ( s )
Aspects can be approximated: V ( s ) or Q ( s , a ) V(s) \text{ or } Q(s, a) V ( s ) or Q ( s , a )
Estimate value functions with function approximation.
V ^ ( s , w ) ≈ V ( s ) \hat{V}(s, \color{red}{w}\color{black}) \approx V(s) V ^ ( s , w ) ≈ V ( s )
Q ^ ( s , a , w ) ≈ Q ( s , a ) \hat{Q}(s, a, \color{red}{w}\color{black}) \approx Q(s, a) Q ^ ( s , a , w ) ≈ Q ( s , a )
A mechanism to plugin MC and TD for function approximation.
Value Function Approximation for policy evaluation
VFA: Value Function Approximation
v π ( s ) v_\pi(s) v π ( s ) is given, so making it supervised.
v ^ ( s , w ) \hat{v}(s, w) v ^ ( s , w ) is estimated value if it follows the policy π \pi π by using ANN.
x → y ^ vs y x \rightarrow \hat{y} \quad \text{vs} \quad y x → y ^ vs y (supervised learning)
s → v ^ ( s , w ) vs v π ( s ) s \rightarrow \hat{v}(s, w) \quad \text{vs} \quad v_\pi(s) s → v ^ ( s , w ) vs v π ( s ) (Reinforcement learning)
J ( w ) = E π [ ( v π ( s ) − v ^ ( s , w ) ) 2 ] J(w) = \mathbb{E}_\pi[\big(v_\pi(s) - \hat{v}(s, w)\big)^2] J ( w ) = E π [ ( v π ( s ) − v ^ ( s , w ) ) 2 ]
Find w w w that minimizes J ( w ) J(w) J ( w )
J ( w ) J(w) J ( w ) : Loss function.
v ^ ( s , w ) \hat{v}(s, w) v ^ ( s , w ) : Guess from the value function approximation.
v π ( s ) − v ^ ( s , w ) v_\pi(s) - \hat{v}(s, w) v π ( s ) − v ^ ( s , w ) : Error between the true value and the estimated value.
E π [ ⋅ ] \mathbb{E}_\pi[\cdot] E π [ ⋅ ] : Expected average over all states s s s according to the policy π \pi π .
Δ w = − 1 2 α ∇ w J ( w ) Δ w = α E π [ ( v π ( s ) − v ^ ( s , w ) ) ⏟ E r r o r ∇ w v ^ ( s , w ) ⏟ G r a d i e n t ] \begin{aligned}
& \Delta w = -\frac{1}{2} \alpha \nabla_w J(w) \\
& \Delta w = \alpha \mathbb{E}_\pi[\underbrace{\big(v_\pi(s) - \hat{v}(s, w)\big)}_{Error} \underbrace{\nabla_w \hat{v}(s, w)}_{Gradient}]
\end{aligned} Δ w = − 2 1 α ∇ w J ( w ) Δ w = α E π [ E rror ( v π ( s ) − v ^ ( s , w ) ) G r a d i e n t ∇ w v ^ ( s , w ) ]
Δ w = \Delta w = Δ w = Step size X Error X Gradient
VFA with SGD
Δ w = α ( v π ( s ) − v ^ ( s , w ) ) ∇ w v ^ ( s , w ) \Delta w = \alpha (v_\pi(s) - \hat{v}(s, w)) \nabla_w \hat{v}(s, w) Δ w = α ( v π ( s ) − v ^ ( s , w )) ∇ w v ^ ( s , w )
Stochastic Gradient Descent sample the gradient
Sample a state randomly, find what v π ( s ) v_\pi(s) v π ( s ) is, and find the estimate
Δ w = α [ Error at s × Gradient at s ] \Delta w = \alpha [\text{Error at } s \times \text{Gradient at } s] Δ w = α [ Error at s × Gradient at s ]
Sample s t s_t s t from π \pi π , and update w w w
Expected value update using SGD update is same as full gradient update
Random sampling will be eventually the same as full gradient update
Linear Value Function Approximation
Represent Value function (state or action) by a linear combination of features
v ^ ( s , w ) = x ( s ) T w = ∑ j = 0 n x j ( s ) w j \hat{v}(s, w) = x(s)^Tw = \sum_{j=0}^n x_j(s) w_j v ^ ( s , w ) = x ( s ) T w = ∑ j = 0 n x j ( s ) w j
Loss function: J ( w ) = E π [ ( v π ( s ) − v ^ ( s , w ) ) 2 ] J(w) = \mathbb{E}_\pi[\big(v_\pi(s) - \hat{v}(s, w)\big)^2] J ( w ) = E π [ ( v π ( s ) − v ^ ( s , w ) ) 2 ]
Δ w = α ( v π ( s ) − v ^ ( s , w ) ) ⏟ E r r o r x ( s ) ⏟ F e a t u r e \Delta w = \alpha \underbrace{\big(v_\pi(s) - \hat{v}(s, w)\big)}_{Error} \underbrace{x(s)}_{Feature} Δ w = α E rror ( v π ( s ) − v ^ ( s , w ) ) F e a t u re x ( s )
Weight update: step size X Error X Feature value
SGD converges to global mininum
Incremental methods for prediction
Real-world RL is not supervised, instead, it is guided by rewards.
No v π ( s ) v_\pi(s) v π ( s ) is given.
Instead, substitute a target for v π ( s ) v_\pi(s) v π ( s )
Δ w = α ( G t − v ^ ( s t , w ) ) ∇ w v ^ ( s t , w ) \Delta w = \alpha(G_t - \hat{v}(s_t, w)) \nabla_w \hat{v}(s_t, w) Δ w = α ( G t − v ^ ( s t , w )) ∇ w v ^ ( s t , w )
Monte Carlo updates: target is the return G t G_t G t
Δ w = α ( R t + 1 + γ v ^ ( s t + 1 , w ) ⏟ TD Target − v ^ ( s t , w ) ) ∇ w v ^ ( s t , w ) \Delta w = \alpha(\underbrace{R_{t+1} + \gamma \hat{v}(s_{t+1}, w)}_{\text{TD Target}} - \hat{v}(s_t, w)) \nabla_w \hat{v}(s_t, w) Δ w = α ( TD Target R t + 1 + γ v ^ ( s t + 1 , w ) − v ^ ( s t , w )) ∇ w v ^ ( s t , w )
TD updates: use TD Target
VFA for Monte Carlo
MC return G t G_t G t is an unbiased, noisy sample of true value v π ( s t ) v_\pi(s_t) v π ( s t )
if G t = 3 , 7 , 4 , 6 , 5 , ⋯ G_t = 3, 7, 4, 6, 5, \cdots G t = 3 , 7 , 4 , 6 , 5 , ⋯
but in average, 1 N ∑ i = 1 N G t ( i ) ≈ v π ( s ) \frac{1}{N} \sum_{i=1}^N G_t^{(i)} \approx v_\pi(s) N 1 ∑ i = 1 N G t ( i ) ≈ v π ( s )
⟨ S 1 , G 1 ⟩ , ⟨ S 2 , G 2 ⟩ , ⋯ , ⟨ S N , G N ⟩ \langle S_1, G_1 \rangle, \langle S_2, G_2 \rangle, \cdots, \langle S_N, G_N \rangle ⟨ S 1 , G 1 ⟩ , ⟨ S 2 , G 2 ⟩ , ⋯ , ⟨ S N , G N ⟩ is an estimate of v π ( s ) v_\pi(s) v π ( s )
x → y x \rightarrow y x → y (supervised learning)
s t → G t s_t \rightarrow G_t s t → G t (Reinforcement learning)
so it learns to v ^ ( s , w ) ≈ G t \hat{v}(s, w) \approx G_t v ^ ( s , w ) ≈ G t
Δ w = α ( G t − v ^ ( s , w ) ) ∇ w v ^ ( s , w ) Δ w = α ( G t − v ^ ( s , w ) ) x ( s ) \begin{aligned}
& \Delta w = \alpha(G_t - \hat{v}(s, w)) \nabla_w \hat{v}(s, w) \\
& \Delta w = \alpha \big(G_t - \hat{v}(s, w)\big) x(s)
\end{aligned} Δ w = α ( G t − v ^ ( s , w )) ∇ w v ^ ( s , w ) Δ w = α ( G t − v ^ ( s , w ) ) x ( s )
In Linear VFA, v ^ ( s , w ) = w T x ( s ) \hat{v}(s,w) = w^Tx(s) v ^ ( s , w ) = w T x ( s )
x ( s ) x(s) x ( s ) is the feature vector of state s s s
w w w is the weight vector
so ∇ w v ^ ( s , w ) = ∇ w ( w T x ( s ) ) = x ( s ) \nabla_w \hat{v}(s, w) = \nabla_w (w^Tx(s)) = x(s) ∇ w v ^ ( s , w ) = ∇ w ( w T x ( s )) = x ( s )
Update = Learning rate X Error X state features
Initialize w = 0 , k = 1 L o o p : Sample k-th episode ( s k 1 , a k 1 , r k 1 , . . . , S k L k ) using policy π For t = 1 , . . . L k : if First Visit to ( s ) in episode k , t h e n G t ( s ) = ∑ j = t L k γ k , j Weight update: w ← w − α ( G t − v ^ ( s , w ) ) x ( s ) k = k + 1 \begin{aligned}
& \text{Initialize } w = 0, k = 1 \\
& Loop: \\
& \quad \text{Sample k-th episode } (s_{k1}, a_{k1}, r_{k1}, ..., S_{kL_k}) \text{ using policy } \pi \\
& \quad \text{For } t = 1, ... L_k: \\
& \quad \quad \text{if First Visit to } (s) \text{ in episode } k, then \\
& \quad \quad \quad G_t(s) = \sum_{j=t}^{L_k} \gamma_{k,j} \\
& \quad \quad \quad \text{Weight update: } \quad w \leftarrow w - \alpha(G_t - \hat{v}(s,w))x(s) \\
& \quad \quad k = k + 1 \\
& \end{aligned} Initialize w = 0 , k = 1 L oo p : Sample k-th episode ( s k 1 , a k 1 , r k 1 , ... , S k L k ) using policy π For t = 1 , ... L k : if First Visit to ( s ) in episode k , t h e n G t ( s ) = j = t ∑ L k γ k , j Weight update: w ← w − α ( G t − v ^ ( s , w )) x ( s ) k = k + 1
G t = R t + 1 + γ R t + 2 + γ 2 R t + 3 + ⋯ + γ L k − t R L k G_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \cdots + \gamma^{L_k-t} R_{L_k} G t = R t + 1 + γ R t + 2 + γ 2 R t + 3 + ⋯ + γ L k − t R L k
VFA for TD Learning
Use bootstrapping and sampling to approximate the target v π v_\pi v π
Sampling: Use a sampled transition S t , A t , R t + 1 , S t + 1 S_t, A_t, R_{t+1}, S_{t+1} S t , A t , R t + 1 , S t + 1 instead of computing the full expectation over possible transitions.
Bootstrapping: Construct the TD target using the current estimate v ^ ( S t + 1 , w ) \hat{v}(S_{t+1}, w) v ^ ( S t + 1 , w ) , instead of waiting for the complete episodic return.
R t + 1 + γ v ^ ( S t + 1 , w ) R_{t+1} + \gamma \hat{v}(S_{t+1}, w) R t + 1 + γ v ^ ( S t + 1 , w )
TD Target is a biased sample of true value v π ( S t ) v_\pi(S_t) v π ( S t )
Can use supervised learning on a training set of ⟨ S , R ⟩ \langle S, R \rangle ⟨ S , R ⟩ paris
⟨ S 1 , R 2 + γ v ^ ( S 2 , w ) ⟩ , ⟨ S 2 , R 3 + γ v ^ ( S 3 , w ) ⟩ , ⋯ , ⟨ S T − 1 , R T ⟩ \langle S_1, R_2 + \gamma \hat{v}(S_2, w) \rangle, \langle S_2, R_3 + \gamma \hat{v}(S_3, w) \rangle, \cdots, \langle S_{T-1}, R_T \rangle ⟨ S 1 , R 2 + γ v ^ ( S 2 , w )⟩ , ⟨ S 2 , R 3 + γ v ^ ( S 3 , w )⟩ , ⋯ , ⟨ S T − 1 , R T ⟩
Δ w = α ( R + γ v ^ ( s ′ , w ) − v ^ ( s , w ) ) ∇ w v ^ ( s , w ) \Delta w = \alpha(R + \gamma \hat{v}(s', w) - \hat{v}(s, w)) \nabla_w \hat{v}(s, w) Δ w = α ( R + γ v ^ ( s ′ , w ) − v ^ ( s , w )) ∇ w v ^ ( s , w )
Linear VFA + TD(0) for prediction and policy evaluation
TD Error: δ t = R t + 1 + γ v ^ ( S t + 1 , w ) − v ^ ( S t , w ) \delta_t = R_{t+1} + \gamma \hat{v}(S_{t+1}, w) - \hat{v}(S_t, w) δ t = R t + 1 + γ v ^ ( S t + 1 , w ) − v ^ ( S t , w )
Δ w = α δ t ∇ w v ^ ( S t , w ) \Delta w = \alpha \delta_t \nabla_w \hat{v}(S_t, w) Δ w = α δ t ∇ w v ^ ( S t , w )
∇ w v ^ ( s , w ) = x ( s ) \nabla_w \hat{v}(s, w) = x(s) ∇ w v ^ ( s , w ) = x ( s )
Δ w = α δ t x ( s ) \Delta w = \alpha \delta_t x(s) Δ w = α δ t x ( s )
w ← w − α δ t x ( s ) w \leftarrow w - \alpha \delta_t x(s) w ← w − α δ t x ( s )
Initialize w = 0 L o o p : Initialize s Sample transition ( s , a , r , s ′ ) using policy π Weight update: w ← w − α ( R + γ v ^ ( s ′ , w ) − v ^ ( s , w ) ) x ( s ) \begin{aligned}
& \text{Initialize } w = 0 \\
& Loop: \\
& \quad \text{Initialize } s \\
& \quad \text{Sample transition } (s, a, r, s') \text{ using policy } \pi \\
& \quad \text{Weight update: } \quad w \leftarrow w - \alpha(R + \gamma \hat{v}(s', w) - \hat{v}(s, w))x(s) \\
& \end{aligned} Initialize w = 0 L oo p : Initialize s Sample transition ( s , a , r , s ′ ) using policy π Weight update: w ← w − α ( R + γ v ^ ( s ′ , w ) − v ^ ( s , w )) x ( s )
Overall
Linear VFA: v ^ ( s , w ) = w T x ( s ) \hat{v}(s, w) = w^Tx(s) v ^ ( s , w ) = w T x ( s )
MC Target = G t G_t G t
TD Target = R t + 1 + γ v ^ ( s ′ , w ) R_{t+1} + \gamma \hat{v}(s', w) R t + 1 + γ v ^ ( s ′ , w )
MC: w ← w + α ( G t − v ^ ( s , w ) ) x ( s ) w \leftarrow w + \alpha(G_t - \hat{v}(s, w)) x(s) w ← w + α ( G t − v ^ ( s , w )) x ( s )
TD: w ← w + α ( R t + 1 + γ v ^ ( s ′ , w ) − v ^ ( s , w ) ) x ( s ) w \leftarrow w + \alpha(R_{t+1} + \gamma \hat{v}(s', w) - \hat{v}(s, w))x(s) w ← w + α ( R t + 1 + γ v ^ ( s ′ , w ) − v ^ ( s , w )) x ( s )