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")