开始使用免费开始使用

固定 Q 目标(Fixed Q-targets)

您将为 Lunar Lander 训练引入固定 Q 目标。作为前提,您需要实例化在线网络(用于选择动作)和目标网络(用于 TD 目标计算)。

您还需要实现一个 update_target_network 函数,以便在每个训练步使用。目标网络不会通过梯度下降更新;相反,update_target_network 会将其权重以一个很小的幅度朝 Q 网络方向推进,从而确保其在时间上保持相当稳定。

请注意,仅在本练习中,我们使用一个非常小的网络,便于打印并检查其状态字典。该网络只有一个大小为 2 的隐藏层;其动作空间和状态空间的维度也都是 2。

环境中已提供 print_state_dict() 函数用于打印状态字典。

本练习是课程的一部分

Python 中的深度强化学习

查看课程

练习说明

  • 获取目标网络和在线网络各自的 .state_dict()
  • 使用在线网络的参数与目标网络的参数按权重做加权平均来更新目标网络的状态字典,其中在线网络的权重为 tau
  • 将更新后的状态字典加载回目标网络。

交互式实操练习

通过完成这段示例代码来试试这个练习。

def update_target_network(target_network, online_network, tau):
    # Obtain the state dicts for both networks
    target_net_state_dict = ____
    online_net_state_dict = ____
    for key in online_net_state_dict:
        # Calculate the updated state dict for the target network
        target_net_state_dict[key] = (online_net_state_dict[____] * ____ + target_net_state_dict[____] * ____)
        # Load the updated state dict into the target network
        target_network.____
    return None
  
print("online network weights:", print_state_dict(online_network))
print("target network weights (pre-update):", print_state_dict(target_network))
update_target_network(target_network, online_network, .001)
print("target network weights (post-update):", print_state_dict(target_network))
编辑并运行代码