본문으로 건너뛰기

RL 009

· 약 11분

Function Approximation​

v^(s,w)≈vπ(s)\hat{v}(s, \mathbf{w}) \approx v_\pi(s)

q^(s,a,w)≈qπ(s,a)\hat{q}(s, a, \mathbf{w}) \approx q_\pi(s, a)

  • Incremental methods for prediction
  • Model-free VFA: MC and TD targets

GPI, Generalized Policy Iteration​

  • Iterate between policy evaluation and policy improvement
  • Policy Evaluation: Approximate policy evaluation. q^(⋅,⋅,w)≈qπ\hat{q}(\cdot, \cdot, \mathbf{w}) \approx q_\pi
  • Policy Improvement: ϵ\epsilon-greedy policy improvement.

Types of Action-Value FA​

s→FA(w)→q^(s,w)s \to \text{FA}(\mathbf{w}) \to \hat{q}(s, \mathbf{w})
  • State Value Function Approximation
(s,a)→FA(w)→q^(s,a,w)(s, a) \to \text{FA}(\mathbf{w}) \to \hat{q}(s, a, \mathbf{w})
  • Action Value Function Approximation
    • q^(s,Left,w)=3.2\hat{q}(s, \text{Left}, \mathbf{w}) = 3.2
    • q^(s,Right,w)=8.7\hat{q}(s, \text{Right}, \mathbf{w}) = 8.7
s→FA(w)→q^(s,a1,w)⋯q^(s,an,w)s \to \text{FA}(\mathbf{w}) \to \hat{q}(s,a_1, \mathbf{w}) \cdots \hat{q}(s,a_n, \mathbf{w})
  • The network output is a vector of Q-values for each action.
  • More convenient to use discrete action spaces.
ActionQ-value
Left3.2
Right8.7
Jump5.3

Action VFA​

q^(s,a,w)≈qπ(s,a)\hat{q}(s, a, \mathbf{w}) \approx q_\pi(s, a)

  • Approximate the Action-value function.
  • Doing action aa at state ss, Approximate the Q-value of that action.

J=Eπ[(qπ(s,a)−q^(s,a,w))2]\mathcal{J} = \mathbb{E}_{\pi} \left[ (q_\pi(s, a) - \hat{q}(s, a, \mathbf{w}))^2 \right]

  • Minimize mean-squared error (MSE) between the approximate Q-value q^(s,a,w)\hat{q}(s, a, \mathbf{w}) and the true Q-value qπ(s,a)q_\pi(s, a).

Δw=α(qπ(s,a)−q^(s,a,w))∇wq^(s,a,w) \Delta \mathbf{w} = \alpha (q_{\pi}(s,a) - \hat{q}(s,a,\mathbf{w})) \nabla_{\mathbf{w}} \hat{q}(s,a,\mathbf{w})

  • Use SGD to find the local minimum.
  • qπ−q^q_{\pi} - \hat{q} tells us how much the prediction is off from the target.
  • ∇wq^(s,a,w)\nabla_{\mathbf{w}} \hat{q}(s,a,\mathbf{w}) tells us how each weight affects the predicted QQ-value.

Linear Action VFA​

x(s,a)=[x1(s,a)x2(s,a)⋮xn(s,a)]x(s, a) = \begin{bmatrix} x_1(s, a) \\ x_2(s, a) \\ \vdots \\ x_n(s, a) \end{bmatrix}
  • Feature vector is used to represent the state and action pair.

q^(s,a,w)≈x(s,a)Tw=∑jxj(s,a)wj\hat{q}(s, a, w) \approx x(s, a)^T w = \sum_j x_j (s, a) w_j

  • Predicted Q-value is a linear combination of the feature values. (sum of each feature value xjx_j multiplied by the weight wjw_j)
q^(s,a,w)=x(s,a)Twq^=x1w1+x2w2+⋯+xnwn\begin{aligned} \hat{q}(s, a, w) = x(s, a)^T w \\ \hat{q} = x_1w_1 + x_2w_2 + \cdots + x_n w_n \end{aligned}
  • x1,x2,⋯x_1, x_2, \cdots: features of the state and action pair.
  • w1,w2,⋯w_1, w_2, \cdots: weights that need to be learned.
  • derivative q^\hat{q} with respect to ww.
    • ∂q^∂w1=x1\frac{\partial \hat{q}}{\partial w_1} = x_1
    • ∂q^∂w2=x2\frac{\partial \hat{q}}{\partial w_2} = x_2
∇wq^=[x1x2⋮xn]=x(s,a)\nabla_w\hat{q} = \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix} = x(s, a)
  • e.g. q^=2w1+5w2\hat{q} = 2w_1 + 5w_2, then ∇wq^=[25]\nabla_w\hat{q} = \begin{bmatrix} 2 \\ 5 \end{bmatrix}
Δw=α(qπ−q^)∇wq^=α(qπ−q^)x(s,a)=α(qπ(s,a)−q^(s,a,w))x(s,a)\begin{aligned} \Delta w \\ &= \alpha (q_{\pi} - \hat{q}) \nabla_w\hat{q} \\ &= \alpha (q_{\pi} - \hat{q}) x(s, a) \\ &= \alpha (q_{\pi}(s, a) - \hat{q}(s, a, w)) x(s, a) \end{aligned}

Incremental Prediction Algorithm​

  • Assumption so far: qπ(s,a)q_{\pi}(s,a) is given.
  • But an actual RL agent does not know the true qπ(s,a)q_{\pi}(s,a).
  • Therefore, we need to estimate a target.
  • Monte Carlo (MC): the target is the return GtG_t.
    • Gt=Rt+1+γRt+2+⋯G_t = R_{t+1} + \gamma R_{t+2} + \cdots
    • Update: Δw=α(Gt−q^(st,at,w))∇wq^(st,at,w)\Delta w = \alpha \bigl(G_t - \hat q(s_t,a_t,w)\bigr)\nabla_w\hat q(s_t,a_t,w)
    • It compares the predicted Q-value with the actual return observed after the episode.
    • MC does not use another prediction inside the target.
  • Temporal Difference (TD): the target is Rt+1+γq^(st+1,at+1,w)R_{t+1} + \gamma \hat q(s_{t+1},a_{t+1},w).
    • Update: Δw=α(Rt+1+γq^(st+1,at+1,w)−q^(st,at,w))∇wq^(st,at,w)\Delta w = \alpha \bigl(R_{t+1} + \gamma\hat q(s_{t+1},a_{t+1},w) - \hat q(s_t,a_t,w)\bigr)\nabla_w\hat q(s_t,a_t,w)
    • It compares the current Q prediction with the current reward plus the discounted next Q prediction.
    • Bootstrapping: update the current prediction using another prediction.
    • Because the next prediction can also be wrong, we ask: can repeated updates converge to a stable value?
  • On-policy: behavior policy = target policy.
  • Off-policy: behavior policy ≠\ne target policy.
    • The policy that generates the experience is different from the policy that we want to evaluate or improve.
PolicyAlgorithmLookup TableLinearNon-linear
On-policyMC✓✓✓
On-policyTD(0)✓✓✗
Off-policyMC✓✓✓
Off-policyTD(0)✓✗✗
  • Deadly Triad: Function Approximation + Bootstrapping + Off-policy.
AlgorithmLookup TableLinearNon-linear
MC Control✓(✓)✕
SARSA✓(✓)✕
Q-Learning✓✕✕
  • SARSA learns the policy that it actually follows.
    • Target: Rt+1+γQ(st+1,at+1)R_{t+1} + \gamma Q(s_{t+1},a_{t+1}), On-policy TD
  • Q-Learning learns the optimal policy.
    • Target: max⁡a′Q(st+1,a′)\max_{a'} Q(s_{t+1},a'), Off-policy TD
  • (✓): Moved around the near-optimal value function.
    • Linear FA may not represent the exact Q∗Q^* because all state-action values share the same weight vector and must fit the same linear function.
  • x: Divergence

Batch Methods​

  • Incremental learning: an experience -> update the weights.
  • Batch methods: collect experiences -> store in D\mathcal{D} -> reuse D\mathcal{D} for training.
  • It seeks to find the best fitting value functions.

D={(s1,v1π),(s2,v2π),⋯ ,(sN,vNπ)} \mathcal{D} = \{(s_1, v^{\pi}_1), (s_2, v^{\pi}_2), \cdots, (s_N, v^{\pi}_N)\}

  • Which ww makes v^(s,w)\hat{v}(s, w) fit all stored data best?
    • Using the agents' experience or training data.
    • Training data: a fixed batch of previously collected data.

Least Squares Prediction​

  • Given a value function approximation: v^(s,w)≈vπ(s)\hat{v}(s,w) \approx v^\pi(s)
  • Given experience data: D={(s1,v1π),(s2,v2π),…,(sN,vNπ)}\mathcal{D} = \{(s_1,v_1^\pi),(s_2,v_2^\pi),\ldots,(s_N,v_N^\pi)\} consisting of state-value pairs.
LS(w)=∑t=1N(vtπ−v^(st,w))2LS(w)=\sum_{t=1}^{N}\left(v_t^\pi-\hat{v}(s_t,w)\right)^2
  • LS(w)LS(w) is the sum of all squared prediction errors.
  • The Least Squares method finds the parameter vector ww that minimizes the squared error between v^(s,w)\hat v(s,w) and vπ(s)v^\pi(s).
wπ=arg⁡min⁡wLS(w)w^\pi=\arg\min_w LS(w)
  • If we use the mean squared error instead:
ED[(vtπ−v^(st,w))2]=1N∑t=1N(vtπ−v^(st,w))2\mathbb{E}_{\mathcal D} \left[ \left(v_t^\pi-\hat v(s_t,w)\right)^2 \right] = \frac{1}{N} \sum_{t=1}^{N} \left(v_t^\pi-\hat v(s_t,w)\right)^2
  • The factor 1/N1/N does not change the minimizing ww.

SGD Procedure​

  1. Sample a {state,value}\{\text{state},\text{value}\} pair from D\mathcal D.
  2. Apply the SGD update:
Δw=α(vπ−v^(s,w))∇wv^(s,w)\Delta w = \alpha \left(v^\pi-\hat v(s,w)\right) \nabla_w\hat v(s,w)
  • Sample from D\mathcal D -> calculate the error -> update ww -> repeat.
  1. Under appropriate conditions, SGD converges toward the Least Squares solution.
  • Then, what model should represent v^(s,w)\hat v(s,w)?
    • Linear function approximator
    • ANN
    • CNN

DNN, Deep neural network​

  • Universal function approximator
    • Linear FA limited to linear combinations of features v^(s,w)=x(s)Tw\hat{v}(s, w) = x(s)^T w
    • DNN can represent non-linear mappings of features. s→DNN(w)→v^(s,w)s \rightarrow \text{DNN}(\mathbf{w}) \rightarrow \hat{v}(s, \mathbf{w})
  • Combination of both Linear and Non-Linear transformations
    • z=Wx+bz = Wx + b
    • h=ReLU(z)h = \text{ReLU}(z)
  • It may require exponentially less nodes or parameters to represent the sample Linear function.
  • SGD can be used to learn parameters
    • prediction -> loss -> gradient -> SGD -> ww update.
  • It can generalize from observed states to unseen states.
  • It can handle continuous state spaces, which are common in real world problems.
    • s=(x,y,velocity,angle)s = (x, y, \text{velocity}, \text{angle})
    • s=(2.371,5.826,0.43,⋯ )→DNN→v^(s,w)s = (2.371, 5.826, 0.43, \cdots) \rightarrow \text{DNN} \rightarrow \hat{v}(s, \mathbf{w})
  • E2E learning (learn feature representation and policy together) can be more effective.
  • Function approximation using images, environments with visual observations.
  • DNNs can directly process pixel data and learn from raw images.
  • DNNs can be used in environments with continuous action spaces for approximating policies (Robotics)
  • It can achieve SOTA performance in several complex tasks.

CNN, Convolutional neural network​

  • CONV is the first layer to extract features from an input image.
    • Core building block of a CNN
    • A number of filters such as Edge Detectors are applied to the input image.
  • Padding: add extra pixels around the image to handle edge cases.
    • Valid Padding: no padding, output size is smaller than input size.
      • p=0p = 0.
      • Output size: (n−f+1)×(n−f+1)(n - f + 1) \times (n - f + 1)
      • ff: filter size.
    • Same Padding: add padding so that output size is the same as input size.
      • p=f−12p = \frac{f-1}{2}
  • Stride: the number of pixels the filter moves across the input image.
    • S=1S = 1: Move the filter by one pixel horizontally and vertically.
    • S=2S = 2: Move the filter by two pixels horizontally and vertically.
  • POOL: down sampling operation which reduces the dimensionality of a matrix.
    • It reduces the number of parameters for large image but retain the valuable information.
    • Max pooling: Max(7, 8, 9, 10) = 10
    • Average pooling: Avg(7, 8, 9, 10) = 8.5
    • Sum pooling: Sum(7, 8, 9, 10) = 34
  • FC: The output matrix after convolution layer is flattened and feed into a fully connected layer similar to ANN.
    • It's a traditional Multi-layer Perceptron/Neural Network.
    • For multi-class classification, Softmax activation function is usually used.
    • Output of the CONV and POOL layers represent a high-level features of the Input image.
    • FC layer uses this feature to classify the input image into the desired class.

Deep Reinforcement Learning​

  • Use DNNs to represent
    • Value and Q-Value function (Action-value function)
    • Policy (Policy Gradient methods)
    • Model
  • Use SGD to optimize loss function.
  • RL makes the target, SGD trains DNN to fit the target.
  • In TD: Δw=learning rate×TD error×gradient\Delta w = \text{learning rate} \times \text{TD error} \times \text{gradient}
    • TD error: Rt+1+γq^(st+1,at+1,w)⏟Target−q^(st,at,w)⏟Current Prediction\underbrace{R_{t+1} + \gamma \hat{q}(s_{t+1},a_{t+1},w)}_{\text{Target}} - \underbrace{\hat{q}(s_t,a_t,w)}_{\text{Current Prediction}}
  • In Q-Learning: max⁡a′Q(st+1,a′,w)\max_{a'} Q(s_{t+1},a',w) is the target.

Q-Learning with VFA​

  • Original Q-Learning converges to the optimal Q∗(s,a)Q^*(s, a) using look-up table.
  • VFA Q-Learning minimizes MSD Loss by SGD using target Q estimates instead of true Q.
    • so it can diverge due to the correlation between samples or Non-stationary targets.

Experience Replay​

Environment
│
│ experience
▼
(s, a, r, s')
│
▼
Replay Buffer D
┌──────────────────────┐
│ old experience │
│ old experience │
│ old experience │
│ old experience │
└──────────────────────┘
│
│ random sample
▼
Mini-batch
│
├──────────────► Current Q Network
│ │
│ ▼
│ Q(s,a)
│
└──────────────► Target calculation
│
▼
r + γ max Q(s',a')
│
▼
Target − Prediction
│
▼
SGD
│
▼
Update weights
  • DQN stores past transitions (s,a,r,s′)(s,a,r,s') in a replay buffer and randomly samples them for training, which reduces correlation between consecutive samples and improves training stability.
    • Sample (s,a,r,s′)(s, a, r, s') from D\mathcal{D}
    • Compute the target value of the samples s:(r+γmax⁡a′q^(s′,a′,w))s: (r + \gamma \max_{a'} \hat{q}(s',a',w))
    • Use SGD to update the network weight: Δw=α(r+γmax⁡a′q^(s′,a′,w)−q^(s,a,w))∇wq^(s,a,w)\Delta w = \alpha (r + \gamma \max_{a'} \hat{q}(s',a',w) - \hat{q}(s,a,w))\nabla_w \hat{q}(s,a,w)

Fixed Q-Targets​

[Same structure of network 2]

Current state s Next state s'
│ │
▼ ▼
Online Q Network Target Network
weights = w weights = w⁻
│ │
▼ ▼
Q(s,a;w) max Q(s',a';w⁻)
│ │
└────────── compare ────────────────┘
│
▼
Loss
│
▼
update only w
  • Improve stability by fixing the target weights used in the target calculation for multiple updates.
  • Target network uses different set of weights that the weights being updated.
  • w−w^-: set of weights used in the target.
  • ww: set of weights being updated.

r+γmax⁡a′Q(s′,a′,w−)r + \gamma \max_{a'} Q(s',a',\color{red}w^-\color{black})

  • Sample (s,a,r,s′)(s, a, r, s') from D\mathcal{D}
  • Compute the target value of the samples s:(r+γmax⁡a′Q(s′,a′,w−))s: (r + \gamma \max_{a'} Q(s',a',w^-))
  • Use SGD to update the network weight: Δw=α(r+γmax⁡a′Q(s′,a′,w−)−Q(s,a,w))∇wQ(s,a,w)\Delta w = \alpha (r + \gamma \max_{a'} Q(s',a',w^-) - Q(s,a,w))\nabla_w Q(s,a,w)

DQN​

DQN algorithm

DQN in Atari​

  • E2E learning of Q(s,a)valuesfrompixelsQ(s,a) values from pixels s$
  • Input state ss is a stack or raw pixels from last 4 frames.
  • Output is Q(s,a)Q(s,a) for 18 joystick/button positions
  • Reward is chance in score for that step.
  • Network Architecture and hyper-parameters are fixed across all games.
  • 4x84x84 stack of 4 previous frames -> 16 8x8 filters CONV -> 32 4x4 filters CONV -> 256 hidden units FC -> Fully connected linear output layer.

DQN Atari