Epsilon-greediness
在本練習中,你將實作套用遞減 epsilon-greediness 的 select_action() 函式。
Epsilon-greediness 會鼓勵你的智能體探索環境,進而提升學習效果!
epsilon-greediness 的時程會依據給定的 step 決定門檻 $\varepsilon$,其公式如下:
$$\varepsilon = end + (start-end) \cdot e^{-\frac{step}{decay}}$$
select_action() 應該以機率 \(\varepsilon\) 回傳隨機動作,以機率 \(1-\varepsilon\) 回傳具有最高 Q 值的動作。
本練習屬於課程
Python 的深度強化學習
練習說明
- 針對給定的
step計算門檻epsilon。 - 產生一個介於 0 到 1 的隨機數。
- 以機率
epsilon回傳隨機動作。 - 以機率
1-epsilon回傳具有最高 Q 值的動作。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
def select_action(q_values, step, start, end, decay):
# Calculate the threshold value for this step
epsilon = end + (____) * math.exp(____ / ____)
# Draw a random number between 0 and 1
sample = random.____
if sample < epsilon:
# Return a random action index
return random.____
# Return the action index with highest Q-value
return torch.____.item()
for step in [1, 500, 2500]:
actions = [select_action(torch.Tensor([1, 2, 3, 5]), step, .9, .05, 1000) for _ in range(20)]
print(f"Selecting 20 actions at step {step}.\nThe action with highest q-value is action 3.\nSelected actions: {actions}\n\n")