시간차 학습

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Fouad Trad

Machine Learning Engineer

TD 학습 vs. 몬테카를로

 

TD 학습
  • 모델 없이 학습
  • 상호작용으로 Q-테이블 추정
  • 에피소드 내 매 스텝마다 Q-테이블 업데이트
  • 길거나 불확정 에피소드에 적합

 

몬테카를로
  • 모델 없이 학습
  • 상호작용으로 Q-테이블 추정
  • 최소 한 에피소드 종료 시 업데이트
  • 짧은 에피소드 작업에 적합
Python으로 배우는 Gymnasium 기반 Reinforcement Learning

날씨 예측으로 보는 TD 학습

같은 장소의 서로 다른 시간대 날씨를 보여주는 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

SARSA

  • TD 알고리즘
  • 온정책 방법: 수행한 행동에 따라 전략 조정

SARSA가 현재 상태, 취한 행동, 받은 보상, 관찰된 다음 상태, 다음 행동을 의미함을 보여주는 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

SARSA 업데이트 규칙

SARSA 업데이트 규칙의 수학 공식을 보여주는 이미지.

  • $\alpha$: 학습률
  • $\gamma$: 할인율
  • 둘 다 0~1 사이
Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Frozen Lake

Frozen Lake 환경을 보여주는 이미지

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

초기화

env = gym.make("FrozenLake", is_slippery=False)

num_states = env.observation_space.n num_actions = env.action_space.n
Q = np.zeros((num_states, num_actions))
alpha = 0.1 gamma = 1 num_episodes = 1000
Python으로 배우는 Gymnasium 기반 Reinforcement Learning

SARSA 루프

for episode in range(num_episodes):

state, info = env.reset() action = env.action_space.sample()
terminated = False while not terminated: next_state, reward, terminated, truncated, info = env.step(action)
next_action = env.action_space.sample()
update_q_table(state, action, reward, next_state, next_action)
state, action = next_state, next_action
Python으로 배우는 Gymnasium 기반 Reinforcement Learning

SARSA 업데이트

def update_q_table(state, action, reward, next_state, next_action):

old_value = Q[state, action]
next_value = Q[next_state, next_action]
Q[state, action] = (1 - alpha) * old_value + alpha * (reward + gamma * next_value)

  SARSA 업데이트 규칙의 수학 공식을 보여주는 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

최적 정책 도출

policy = get_policy()
print(policy)
{ 0: 1,  1: 2,  2: 1,  3: 0, 
  4: 1,  5: 0,  6: 1,  7: 0, 
  8: 2,  9: 1, 10: 1, 11: 0, 
 12: 0, 13: 2, 14: 2, 15: 0}

Frozen Lake에서 최적 정책을 화살표로 표시한 이미지로, 에이전트가 구멍을 피하는 것을 보여줍니다.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

연습해 봅시다!

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Preparing Video For Download...