기대 SARSA

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Fouad Trad

Machine Learning Engineer

기대 SARSA

  • TD 방법
  • 모델 기반 없음
  • SARSA·Q-learning과 다른 방식으로 Q-테이블 갱신

기대 SARSA의 단계: Q-테이블 초기화, 행동 선택, 보상 수신, 테이블 갱신. 일정 에피소드 후 수렴할 때까지 반복.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

기대 SARSA 갱신

SARSA

SARSA 갱신 규칙의 수식 이미지.

Q-learning

Q-learning 갱신 규칙의 수식 이미지.

기대 SARSA

기대 SARSA 갱신 규칙의 수식 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

다음 상태의 기대값

기대 SARSA 갱신 규칙의 수식 이미지.

  • 모든 행동을 고려

다음 상태의 기대 Q값 수식 이미지.

  • 무작위 행동 → 동일 확률

행동을 동일 확률로 무작위 선택할 때 다음 상태의 기대 Q값 수식 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Frozen Lake로 구현

env = gym.make('FrozenLake-v1', 
               is_slippery=False)

num_states = env.observation_space.n
num_actions = env.action_space.n
Q = np.zeros((num_states, num_actions))

gamma = 0.99 alpha = 0.1 num_episodes = 1000

Frozen Lake 환경 이미지

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

기대 SARSA 갱신 규칙

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

expected_q = np.mean(Q[next_state])
Q[state, action] = (1-alpha) * Q[state, action] + alpha * (reward + gamma * expected_q)

기대 SARSA 갱신 규칙의 수식 이미지.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

학습

for i in range(num_episodes):
    state, info = env.reset()    
    terminated = False  

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

에이전트의 정책

policy = {state: np.argmax(Q[state]) 
          for state in range(num_states)}
print(policy)
{ 0: 1,  1: 2,  2: 1,  3: 0, 
  4: 1,  5: 0,  6: 1,  7: 0, 
  8: 2,  9: 2, 10: 1, 11: 0, 
 12: 0, 13: 2, 14: 2, 15: 0}

에이전트가 학습한 정책: 각 상태에서 수행할 행동을 표시.

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Ayo berlatih!

Python으로 배우는 Gymnasium 기반 Reinforcement Learning

Preparing Video For Download...