let us not love with words or tongue but actions and truth.

IT/강화학습

Policy Gradient Methods

sarah0518 2022. 11. 15. 19:37

1. Value-based RL

정의: 높은 Q값에 근거하여 value를 선택하는 것

단점: continuous-action space한 환경에서는 바로 적용되기 어려움

eg1) 스피드를 150이상으로 선택하겠다.

eg2) 각도를 10도 변경하겠다.

해결책: discretization (별로 효과적이지 않음)

 

 

 

 

2. Policy-based RL: policy gradient

DQN 

input: state

output: Q-value

Policy gradient

input: state

output: action을 취할 probability

→ stochatic policy와 유사함

    (policy-based method에서는 stochatic policy를 사용)

→ ㅠ(a|s)로 표현

 

• If we get a positive return, we increase the probability of all the actions

   that we took in each state until the end of the episode. 

• If we get a negative return, we decrease the probability of all the actions

  that we took in each state until the end of the episode.

 

 

 

Process of Learning in the Policy Gradient Method

위와 같이 결과가 좋은 action들은 모두 강화됨

(s1,left), (s2, right), (s4, left), (s6,right)에 대해 모두 강화됨

 

 

 

policy gradient의 목적함수

 

 

Gradient ascent

policy gradient에서 목적함수를 최대화 하기위해

아래의 gradient ascent를 사용함

** gradient descent와 ascent는 가운데 +, - 부호만 반대로 해주면 됨

 

 

 

 

Gradient ascent 구하기(수식)

 

아래의 기대값을 적분을 사용하는 수식을 적용하여

 

목적함수는 아래와 같이 변경할 수 있고

목적함수의 미분값은 아래와 같으며,

또 아래와 같은 trick을 써서 표현하고

위치를 바꿔준 다음에

아래의 log 미분 트릭을 사용해서

** 참고로 log(2x)를 미분하면 2/2x 임

** 참고로 log(x)를 미분하면 1/x 임

아래와 같이 표현하고

그 결과 아래식을 얻을 수 있음

 

아래식을 계산하는 방법

기존에 우리는 아래 식에대해서 배웠음

시작 s0를 선택할 확률 다음에, 그 다음의 stochastic policy에 따른 action을 취할 확률인데

여기서, log를 씌우면 모두 더하기로 표현할 수 있으므로

아래와 같은 식으로 표현됨

그 결과, 목적함수의 미분값(파랑1)은 최종적으로 아래와 같이 표현할 수 있음

즉, Return값을 취할 확률을 곱해서 다 더해준것임

따라서 gradient ascent (파랑2)도 아래와 같이 표현할 수 있음

참고로 밑줄친 부분은 이미 NN에서 구할 수 있음

 

 

 

코드 구현

노랑색 영역은 action prob.에 따라서 action을 선택하고,

위에 정의한 수식에 따라 logP를 계산해 주는 수식이 그 아래 코드임

 

아래는 한개의 trajectory에 대해서 list로 저장해 놓는 것임

 

아래의 노란색 영역은 아래 수식을 계산하는 것임

-1을 곱해주는 이유는 gradient descent이기 때문임

 

 

policy gradient 단점

policy gradient는 return에 대한 기대값에 대한 분포가 너무 큼

→ 목적함수를 미분한 값이 변동이 큼

+4 점을 얻은 경우, 모든 action에 대해 동일한 +4점을 적용하기 때문에 증가 되는 변동폭이 큼

 

해결책: reward to go

자기부터 각자 끝까지의 return까지의 합만 적용 함

** 코드는 returns[i][0]에서 returns[i][j]로 변경됨

   returns[i][0]는 항상 처음부터 더해주는 것임

reward to go에서는 오른쪽 처럼 state시점에서의 return값만 가져옴

(전체 trajectory의 return값이 아님)

 

 

 

 

해결책2: policy gradient with baseline

   return값이 크다는 단점을  해결하기 위해

   리턴값에서 현재의 state의 value(=V(s)) 빼준 값을 사용하는 것

   즉, "어떤 액셨을 취했을 때"에서 "그 액션을 취하지 몰랐을 때"의 평균을 보겠다는 의미임

   그러므로 양수값이 나오면 평균보다 잘하고 있다는 뜻으로 해석 가능함

V(st)를 구하기 위해 value network를 사용함

 

 

 

 

Cart Pole Balancing with Policy Gradient Summary

  While DQN is a value-based method, policy gradient is a policy-based RL method.

 

In policy gradient, we do not calculate Q values.

Instead, the neural network outpus probability distribution of actions in a state.

 

– To train the policy network, we run episodes with stochastic policy.

If the agent obtains a positive reward in an episode, probability of the actions taken is increased.

If the agent obtains a negative reward, probability of the actions is decreased.

 

– Since the original policy gradient method can lead to high variance of gradients,

which can make the training take long time to converge.

 

– We can use two improved algorithms: policy gradient

with reward-to-go and policy gradient with baseline.

 

– In policy gradient with baseline, we use another neural network to approximate value function,

and use it as the baseline. (value network)

 

'IT > 강화학습' 카테고리의 다른 글

Multi-Armed Bandit (MAB)  (0) 2022.12.04
Actor Critic methods - DDPG  (0) 2022.11.29
Deep Q Network  (0) 2022.11.01
Temporal Difference Learning  (0) 2022.10.25
Monte Carlo methods - Importance Sampling  (0) 2022.10.12