Expected SARSA

Reinforcement Learning with Gymnasium ใน Python

Fouad Trad

Machine Learning Engineer

Expected SARSA

  • วิธี TD
  • เทคนิคแบบ Model-free
  • อัปเดต Q-table ต่างจาก SARSA และ Q-learning

แผนภาพแสดงขั้นตอนของ Expected SARSA ได้แก่ การเริ่มต้น Q-table การเลือกการกระทำ การรับรางวัลจากสภาพแวดล้อม และการอัปเดตตาราง โดย Agent วนซ้ำจนกว่าจะลู่เข้าหลังจากผ่านจำนวน Episode ที่กำหนด

Reinforcement Learning with Gymnasium ใน Python

การอัปเดตแบบ Expected SARSA

SARSA

ภาพแสดงสูตรคณิตศาสตร์ของกฎการอัปเดต SARSA

Q-learning

ภาพแสดงสูตรคณิตศาสตร์ของกฎการอัปเดต Q-learning

Expected SARSA

ภาพแสดงสูตรคณิตศาสตร์ของกฎการอัปเดต Expected SARSA

Reinforcement Learning with Gymnasium ใน Python

ค่าคาดหวังของสถานะถัดไป

ภาพแสดงสูตรคณิตศาสตร์ของกฎการอัปเดต Expected SARSA

  • คำนึงถึงการกระทำทั้งหมด

ภาพแสดงสูตรคณิตศาสตร์ของค่า Q ที่คาดหวังสำหรับสถานะถัดไป

  • การกระทำแบบสุ่ม → ความน่าจะเป็นเท่ากัน

ภาพแสดงสูตรคณิตศาสตร์ของค่า Q ที่คาดหวังสำหรับสถานะถัดไป เมื่อเลือกการกระทำแบบสุ่มด้วยความน่าจะเป็นเท่ากัน

Reinforcement Learning with Gymnasium ใน Python

การนำไปใช้กับ 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

Reinforcement Learning with Gymnasium ใน Python

กฎการอัปเดตแบบ Expected 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)

ภาพแสดงสูตรคณิตศาสตร์ของกฎการอัปเดต Expected SARSA

Reinforcement Learning with Gymnasium ใน Python

การฝึก Agent

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
Reinforcement Learning with Gymnasium ใน Python

Policy ของ Agent

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}

ภาพแสดง Policy ที่ Agent เรียนรู้ได้ โดยระบุการกระทำที่ควรทำในแต่ละสถานะ

Reinforcement Learning with Gymnasium ใน Python

มาฝึกกันเถอะ!

Reinforcement Learning with Gymnasium ใน Python

Preparing Video For Download...