评估多臂老虎机中的收敛性
在多臂老虎机问题中评估策略的性能与收敛性,有助于判断其有效性。通过分析各拉杆随时间被选择的频率,您可以推断学习过程,以及策略识别并利用最佳拉杆的能力。本练习将通过可视化各拉杆在多次迭代中的选择百分比,来评估 epsilon-greedy 策略的收敛情况。
数组 selected_arms(显示每次迭代拉动了哪一根拉杆)已为您预先加载。
本练习是课程的一部分
Python 中的 Gymnasium 强化学习
练习说明
- 用全零初始化数组
selections_percentage,其维度用于跟踪各 bandit 随时间的被选百分比。 - 通过对每个 bandit 在各次迭代中的选择进行累积求和,并除以对应的迭代次数,得到随时间变化的
selections_percentage。 - 绘制每个 bandit 的累积选择百分比曲线,以可视化在迭代过程中各 bandit 被选择的频率变化。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Initialize the selection percentages with zeros
selections_percentage = ____
for i in range(n_iterations):
selections_percentage[i, selected_arms[i]] = 1
# Compute the cumulative selection percentages
selections_percentage = np.____(____, axis=____) / np.arange(1, ____).reshape(-1, 1)
for arm in range(n_bandits):
# Plot the cumulative selection percentage for each arm
plt.plot(____, label=f'Bandit #{arm+1}')
plt.xlabel('Iteration Number')
plt.ylabel('Percentage of Bandit Selections (%)')
plt.legend()
plt.show()
for i, prob in enumerate(true_bandit_probs, 1):
print(f"Bandit #{i} -> {prob:.2f}")