[강화학습] Deep Q-Network <DQN> - 고급

2025.03.14 - [공부/강화학습] - [강화학습] Deep Q-Network ((DQN)) - 기본

 

[강화학습] Deep Q-Network ((DQN)) - 기본

2025.03.10 - [공부/강화학습] - [강화 학습] Markov Decision Process ((MDP)) [강화 학습] Markov Decision Process ((MDP))강화 학습이란 무엇인가?- 에이전트가 환경에 대한 사전 지식없이 환경과 상호 작용하여 경

occident.tistory.com

 

1. Multi-step Learning

$$
L(\theta)=\left[r_{t+1}+\gamma \max _a Q\left(s_{t+1}, a ; \theta\right)-Q\left(s_t, a_t ; \theta\right)\right]^2
$$

먼저, DQN은 single reward $r_{t+1}$과 다음 step의 greedy action을 사용한다.

하지만, 경우에 따라 바로 다음에 받는 보상도 중요하지만, 더 먼 미래의 보상을 파악하면 더 좋을 수 있는 경우가 많다.

 

Multi-step Learning은 truncated n-step return을 사용해, 즉 몇 step 앞까지의 보상과 그것을 target으로 삼는다.

$$
\begin{aligned}
&n \text {-step return }\\
&\begin{aligned}
& G_t^{(1)}=R_{t+1}+\gamma V\left(S_{t+1}\right) \leftarrow \text { TD target } \\
& G_t^{(n)}=R_{t+1}+\gamma R_{t+2}+\cdots+\gamma^{n-1} R_{t+n}+\gamma^n V\left(S_{t+n}\right) \\
& G_t^{(\infty)}=R_{t+1}+\gamma R_{t+2}+\cdots+\gamma^{T-1} R_T \leftarrow \mathrm{MC} \text { target }
\end{aligned}
\end{aligned}
$$

2번째 중간항을 truncated return이라 부른다.

 

$$
r_{t+1}^{(n)}=\sum_{k=0}^{n-1} \gamma^k r_{t+k+1}
$$

 

$$
L(\theta)=\left[r_{t+1}^{(n)}+\gamma^n \max _a \hat{Q}\left(s_{t+n}, a ; \hat{\theta}\right)-Q\left(s_t, a_t ; \theta\right)\right]^2
$$

 

2. Double DQN

Q-learning 

$Q(S, A) \leftarrow Q(S, A)+\alpha\left[R+\gamma \max _a Q\left(S^{\prime}, a\right)-Q(S, A)\right]$
Double Q-learning
$$
Q_2(S, A) \leftarrow Q_2(S, A)+\alpha\left[R+\gamma Q_1\left(S^{\prime}, \arg \max _a Q_2\left(S^{\prime}, a\right)\right)-Q_2(S, A)\right]
$$

2.1. Q-learning

Q-learning에서는 max operation에 의해 positive biased, 즉 Q-value가 overestivated 된다. Target이 계속 과대 평가로 인해 적절한 값보다 커지면, 실제로 Q-table은 true value보다 더 큰 값들로 채워질 것이고, 이것은 positive-biased를 불러일으킨다.

 

2.2. Double Q-learning

Double Q-learning은 2개의 Q-table을 사용한다. Target Q값만 다른 table에서 가져오는 것. 내 테이블에서는 가장 Q-value를 max로 해주는 action이겠지만, 다른 table에서 그 action은 max라는 보장이 없으니 전체적인 값을 낮춰주고 over estimation 및 positive-biased 문제를 해결해준다. 

 

2.3. Double DQN 

Contribution: Uniformly overestimating value는 오히려 문제가 되지 않을지 모른다. 왜냐하면 어떤 action을 선택해야되는지에 대한 특성은 보존되기 때문이다.

하지만, DQN의 경우 game에 등장하는 state,action pair에 관해서만 Q-value가 update가 될 것이다. 즉, 일부만 update되기 때문에 overestimation이 일부에만 등장하게 된다.

 

- DQN: $L(\theta)=\left[r_{t+1}+\gamma \max _a \hat{Q}\left(s_{t+1}, a ; \hat{\theta}\right)-Q\left(s_t, a_t ; \theta\right)\right]^2$
- Double DQN: $L(\theta)=\left[r_{t+1}+\gamma \hat{Q}\left(s_{t+1}, \arg \max _a Q\left(s_{t+1}, a ; \theta\right) ; \hat{\theta}\right)-Q\left(s_t, a_t ; \theta\right)\right]^2$

 

기존 DQN과의 차이점은 behavior network의 다음 상태의 action value를 가장 크게 만드는 action의 target Q value를 target network으로 설정한다. 이미 network, 즉 Q 테이블이 2개였기 때문에 굳이 만들어 줄 필요가 없다.

 

double Q-learning은 Q-table을 2개를 만들었어야 하지만

DQN은 이미 network이 2개가 있다. (behavior network과 target network)

 

 

3. Prioritized Replay

Contribution: Experience replay는 strongly temporal corelation을 줄이며, sparse하며 귀중한 경험을 한 번 사용하고 버리고 싶지 않기 위해 사용되었다. 단순히 새로운 것은 buffer에 쌓고 오래된 것은 삭제하며, 하지만 prioritized replay는 그 중, sparse하며 귀중한 경험에 대한 기억과 update를 더 중요하게 여기기 위해 나온 것이다. 

 

로봇 워킹을 학습할 때, 계속 넘어지다가 한번 섰을 때의 경험을 계속해서 mini-batch에 넣어주고 싶기 때문에, 그런 경험을 골라서 선택할 수 있는 방법이 무엇이 있을까? 생각하면, 바로 Temporal Diffrence Error

 

$$
P(i)=\frac{p_i^\alpha}{\sum_k p_k^\alpha} \text { where } p_i=\left|r_{i+1}+\gamma \max _a \hat{Q}\left(s_{i+1}, a ; \hat{\theta}\right)-Q\left(s_i, a_i ; \theta\right)\right|+\epsilon
$$

Replay buffer의 sample 마다 probability를 부여해 mini batch로 가져오기 위한 우선 순위를 책정한다.

 

중요성을 나타내는 probability를 책정하는 척도는 "Temporal Diffrence Error

 

$확률=\frac{\mathrm{단일 샘플의 TD 에러}}{\mathrm{replay buffer 안의 모든 TD 에러의 합}}$

 

3.1. 하지만, Prioritize하게 mini-batch를 선택하면 생기는 문제들!

  • Loss of diversity: Priority가 높은 건 매우 자주 선택, 그렇지 않은 건 거의 배제되는 문제((쏠림 현상 발생))
    • Solution:  Stochastic sampling prioritization
  • Bias: Sampling하는 확률 변수가 바뀌어서 bias 발생한다.
    • Solution: Importance sampling weight

3.2. Update Methods

  • 10만개의 replay buffer에 추가로 TD-error가 있을 것이다. 또한, 우리가 behavior network을 update할 때, mini batch로 update하는데, 예를 들어 200개의 sample을 update할 때, 또 다시 TD-error가 발생한다. 이때는 이 200개에 대한 TD-error를 replay memory에도 update 해준다. update 된 Q network으로 10만개 다시 update하기엔 너무 시간이 많이 걸리기 때문이다.
  • 학습 초기에는 TD-error가 오차가 커서 성능이 좋지 않다. 좋은 action이 과소 평가되고 안좋은 action이 과대 평가될 수 있다. 따라서 lack of diversity 문제가 무조건 발생할 것이다. 이를 해결하기 위해 stochastic sampling prioritization 사용! 

stochastic sampling prioritization

$$
\begin{aligned}
& P(i)=\frac{p_i^\alpha}{\sum_k p_k^\alpha}=\frac{1}{N} \\
& (\alpha=0) .
\end{aligned}
$$

$\alpha$가 0이면 그냥 기존 experience memory와 동일 1이면 완전 prioritization! 따라서, $\alpha$를 조절하여 쏠림 현상 및 overfitting을 방지한다.

 

Importance sampling weight

$$
w_i=\left(\frac{1}{N} \cdot \frac{1}{P(i)}\right)^\beta
$$

random sampling이 아니라 prioritization sampling을 하면 distribution이 uniform 하지 않다. 심지어, prioritization sampling 을 하면  priority 또한 계속 변하기에 distribution이 update 과정에서 계속 변할 것이다. -> 또 다른 Bias가 발생

Importance sampling weights을 통해 bias가 생긴 부분의 weight을 보정해주자!

즉, Importance sampling weight $w_i = (\frac{1}{N} \frac{1}{P(i)})^{\beta}$을 Q-learning update할 때, $w_i \delta _i$를 사용하고 이렇게만 사용하면 stabilization에 문제가 생기기에 $1 / \mathrm{max}_k w_k$로 normalization하여 사용한다.

 

$\beta$가 1이면 거의 priority가 없는 것과 같은 효과 ((즉, uniform distribution 효과))가 된다. Bias를 많이 줄일 수 있고, 학습이 거의 끝나 갈 때 Bias가 심하면 문제가 되니, unbiased update가 되는 것이 중요하기 때문에 학습의 거의 끝나갈 때 $\beta$를 1로 가도록 update해준다.

 

 

형광이 칠해진 부분, prioritized replay, stochastic samlpling, importance sampling 부분을 제외하면 기존의 DQN과 동일하다.