การสร้างสมดุลระหว่างการสำรวจและการใช้ประโยชน์

Reinforcement Learning with Gymnasium ใน Python

Fouad Trad

Machine Learning Engineer

การฝึกด้วยการกระทำแบบสุ่ม

  • เอเจนต์สำรวจสภาพแวดล้อม
  • ไม่มีการปรับปรุงกลยุทธ์จากความรู้ที่เรียนรู้
  • เอเจนต์ใช้ความรู้เมื่อการฝึกเสร็จสิ้น

ภาพแสดงเอเจนต์ภายในสภาพแวดล้อม

Reinforcement Learning with Gymnasium ใน Python

การแลกเปลี่ยนระหว่างการสำรวจและการใช้ประโยชน์

 

  • สร้างสมดุลระหว่างการสำรวจและการใช้ประโยชน์
  • การสำรวจตลอดเวลาขัดขวางการปรับปรุงกลยุทธ์
  • การใช้ประโยชน์อย่างเดียวพลาดโอกาสที่ยังไม่ค้นพบ

ภาพแสดงเอเจนต์ที่พยายามสำรวจการกระทำใหม่เพื่อค้นพบรางวัลเพิ่มเติม และพยายามใช้ประโยชน์จากความรู้ขณะที่อาจพลาดรางวัลบางส่วน

Reinforcement Learning with Gymnasium ใน Python

การเลือกร้านอาหาร

ภาพแสดงโต๊ะอาหารในร้านอาหาร

Reinforcement Learning with Gymnasium ใน Python

กลยุทธ์ Epsilon-greedy

 

  • สำรวจด้วยความน่าจะเป็น epsilon

ไดอะแกรมแสดงว่าด้วยความน่าจะเป็น epsilon เอเจนต์สำรวจโดยเลือกการกระทำแบบสุ่ม

Reinforcement Learning with Gymnasium ใน Python

กลยุทธ์ Epsilon-greedy

 

  • สำรวจด้วยความน่าจะเป็น epsilon
  • ใช้ประโยชน์ด้วยความน่าจะเป็น 1-epsilon
  • รับประกันการสำรวจอย่างต่อเนื่องควบคู่กับการใช้ความรู้

ไดอะแกรมแสดงว่าด้วยความน่าจะเป็น epsilon เอเจนต์สำรวจโดยเลือกการกระทำแบบสุ่ม และด้วยความน่าจะเป็น 1 - epsilon เอเจนต์ใช้ประโยชน์โดยเลือกการกระทำที่ดีที่สุดที่รู้จัก

Reinforcement Learning with Gymnasium ใน Python

กลยุทธ์ Decayed Epsilon-greedy

 

  • ลดค่า epsilon ลงตามเวลา
  • สำรวจมากขึ้นในช่วงแรก
  • ใช้ประโยชน์มากขึ้นในภายหลัง
  • เอเจนต์พึ่งพาความรู้สะสมมากขึ้นเรื่อยๆ

ภาพแสดงการลดลงของค่า epsilon ตามเวลา

Reinforcement Learning with Gymnasium ใน Python

การนำไปใช้กับ Frozen Lake

env = gym.make('FrozenLake', is_slippery=True)

action_size = env.action_space.n
state_size = env.observation_space.n
Q = np.zeros((state_size, action_size))

alpha = 0.1 gamma = 0.99 total_episodes = 10000

ภาพแสดงภาพนิ่งของสภาพแวดล้อม Frozen Lake

Reinforcement Learning with Gymnasium ใน Python

การสร้างฟังก์ชัน `epsilon_greedy()`

def epsilon_greedy(state):

if np.random.rand() < epsilon: action = env.action_space.sample() # Explore
else: action = np.argmax(Q[state, :]) # Exploit return action
Reinforcement Learning with Gymnasium ใน Python

การฝึกด้วย Epsilon-greedy

epsilon = 0.9   # Exploration rate

rewards_eps_greedy = []
for episode in range(total_episodes):
    state, info = env.reset()
    terminated = False
    episode_reward = 0
    while not terminated:
        action = epsilon_greedy(state)
        new_state, reward, terminated, truncated, info = env.step(action)       
        Q[state, action] = update_q_table(state, action, new_state) 
        state = new_state

episode_reward += reward rewards_eps_greedy.append(episode_reward)
Reinforcement Learning with Gymnasium ใน Python

การฝึกด้วย Decayed Epsilon-greedy

epsilon = 1.0   # Exploration rate
epsilon_decay = 0.999
min_epsilon = 0.01

rewards_decay_eps_greedy = [] for episode in range(total_episodes): state, info = env.reset() terminated = False episode_reward = 0 while not terminated: action = epsilon_greedy(state) new_state, reward, terminated, truncated, info = env.step(action) episode_reward += reward Q[state, action] = update_q_table(state, action, new_state) state = new_state rewards_decay_eps_greedy.append(episode_reward)
epsilon = max(min_epsilon, epsilon * epsilon_decay)
Reinforcement Learning with Gymnasium ใน Python

การเปรียบเทียบกลยุทธ์

avg_eps_greedy= np.mean(rewards_eps_greedy)
avg_decay = np.mean(rewards_decay_eps_greedy)
plt.bar(['Epsilon Greedy', 'Decayed Epsilon Greedy'],
        [avg_eps_greedy, avg_decay], 
        color=['blue', 'green'])
plt.title('Average Reward per Episode')
plt.ylabel('Average Reward')
plt.show()

ภาพแสดงกราฟแท่งที่แสดงว่ารางวัลเฉลี่ยของ epsilon-greedy อยู่ที่ประมาณ 0.02 ขณะที่ decayed epsilon-greedy อยู่ที่ประมาณ 0.55

Reinforcement Learning with Gymnasium ใน Python

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

Reinforcement Learning with Gymnasium ใน Python

Preparing Video For Download...