Week 9:强化学习

📅 2026/8/3 4:21:16
Week 9:强化学习
目录摘要Abstract1 马尔可夫决策过程1.1 MDP的五元组1.2 轨迹与累积奖励2 策略梯度2.1 参数化2.2 策略梯度定理2.3 REINFORCE 算法流程3 Q-Learning 与值函数方法3.1 状态-动作值函数3.2 更新规则3.3 深度Q网络DQN4 课堂代码5 总结摘要本周学习最后一节课程强化学习这是机器学习中与监督、无监督并列的第三种学习范式。强化学习让智能体在环境中通过试错自主学会决策广泛应用于游戏、机器人控制、推荐系统等领域。本周重点学习强化学习的数学框架和两种核心算法AbstractThis week we will cover the final lesson on reinforcement learning, which represents the third learning paradigm in machine learning, alongside supervised and unsupervised learning. Reinforcement learning enables agents to learn how to make decisions on their own through trial and error within a given environment, and it is widely used in areas such as games, robot control, and recommendation systems. This week’s focus will be on the mathematical framework of reinforcement learning as well as two of its core algorithms.1 马尔可夫决策过程1.1 MDP的五元组强化学习的问题被抽象为MDP包含以下五个核心要素1、状态集合2、动作集合3、P(s|s,a)状态转移概率执行动作 a 后从状态 s 转移到 s 的概率4、R(s,a)即时奖励函数5、折扣因子用于平衡短期与长期回报1.2 轨迹与累积奖励智能体在环境中执行策略产生一条轨迹该轨迹的累积折扣奖励为强化学习的目标是找到一个最优策略使得期望累积奖励最大化2 策略梯度2.1 参数化策略通常用一个神经网络参数化记为输出在状态 s 下执行各动作的概率。课程的目标就是优化参数来最大化期望回报2.2 策略梯度定理对求梯度需要处理期望里的采样。李宏毅老师在课堂上给出了一个简洁的推导利用对数求导技巧。一条轨迹的概率为两边取对数注意到只有项与参数有关因此现在对目标函数求导上述计算过程即为策略梯度定理。它告诉我们要沿着使高奖励轨迹更可能发生的方向更新参数。在实际算法中对于采样到的轨迹更新参数由于是整条轨迹的累积奖励直接这样使用会导致高方差。李宏毅老师随后介绍了引入基线baseline 来减小方差即用当前状态的值函数估计作为基线替换为优势函数。实际中更常用的是每一时刻使用“从该时刻起的累积奖励”乘以该时刻的对数概率梯度这其实是上面的完整形式从期望推导后的等价实用版本2.3 REINFORCE 算法流程1. 用当前策略采样多条完整轨迹2. 对每条轨迹的每个时间步计算3. 计算梯度估计4. 用梯度上升更新参数3 Q-Learning 与值函数方法3.1 状态-动作值函数定义 Q 函数为在状态 s 采取动作 a 后遵循策略所能获得的期望累积奖励最优 Q 函数满足贝尔曼最优方程3.2 更新规则基于以上方程Q-Learning 通过与环境交互的数据来迭代更新 Q 值其中是学习率。当状态和动作空间离散时可以用表格存储 Q 值。3.3 深度Q网络DQNDQN 使用一个神经网络来逼近最优 Q 值。损失函数基于贝尔曼方程的残差4 课堂代码李宏毅老师在课堂上用 Keras 和 OpenAI Gym 演示了简单的策略梯度算法解决 CartPole 问题import numpy as np import gym from keras.models import Sequential from keras.layers import Dense from keras.optimizers import Adam env gym.make(CartPole-v0) model Sequential([ Dense(24, input_dim4, activationrelu), Dense(24, activationrelu), Dense(2, activationsoftmax) ]) model.compile(optimizerAdam(lr0.01), losscategorical_crossentropy) def train(episodes1000): for e in range(episodes): state env.reset() state np.reshape(state, [1, 4]) states, actions, rewards [], [], [] done False while not done: # 根据策略采样动作 action_prob model.predict(state)[0] action np.random.choice(2, paction_prob) next_state, reward, done, _ env.step(action) next_state np.reshape(next_state, [1, 4]) # 记录 states.append(state) actions.append(action) rewards.append(reward) state next_state # 计算每个时刻的回报 Gt 0 discounted_rewards np.zeros_like(rewards) for t in reversed(range(len(rewards))): Gt rewards[t] 0.99 * Gt discounted_rewards[t] Gt # 标准化回报稳定训练 discounted_rewards - np.mean(discounted_rewards) discounted_rewards / np.std(discounted_rewards) # 准备训练数据 X np.vstack(states) Y np.zeros((len(states), 2)) for t in range(len(states)): Y[t][actions[t]] discounted_rewards[t] # 伪标签 优势加权 # 自定义损失这里使用简单技巧用交叉熵 sample_weight # 伪代码示意实际课堂中可能使用 Keras 的 sample_weight 参数 model.train_on_batch(X, Y, sample_weightdiscounted_rewards) if e % 100 0: print(fEpisode {e} finished with total reward {sum(rewards)}) train()5 总结经过本节课粗略的学习与了解发现强化学习是区别于监督学习的通过与环境交互的学习方式这种试错学习的方式更接近人类和动物我理解了为什么这种学习方式在游戏、机器人领域被高频使用。