最简单的网站模板下载,多个域名的网站,wordpress禁用google字体,铁汉生态建设有限公司网站一、强化学习的基本概念 注#xff1a; 圈图与折线图引用知乎博主斜杠青年 1. 任务与奖赏
任务#xff1a;强化学习的目标是让智能体#xff08;agent#xff09;在一个环境#xff08;environment#xff09;中采取一系列行动#xff08;actions#xff09;以完成一个…一、强化学习的基本概念 注 圈图与折线图引用知乎博主斜杠青年 1. 任务与奖赏
任务强化学习的目标是让智能体agent在一个环境environment中采取一系列行动actions以完成一个或多个目标。智能体通过与环境进行交互根据环境的状态states选择动作并根据环境的反馈调整自己的行为。奖赏环境会给智能体一个反馈信号即奖赏reward奖赏是一个标量值代表智能体采取行动后的即时奖励或惩罚。智能体的目标是最大化累积奖赏通常使用折扣累积奖赏公式 其中 是在时刻 获得的奖赏(\gamma\in[0,1]) 是折扣因子用于平衡短期和长期奖赏越接近 0 表示越关注短期奖赏越接近 1 表示越关注长期奖赏。
二、k-摇臂赌博机
1. 基本概念
k-摇臂赌博机是强化学习中的一个经典问题它有 (k) 个摇臂每个摇臂被拉动时会给出一个随机的奖赏。智能体的任务是通过多次试验找到能带来最大累积奖赏的摇臂。
2. 代码示例(\epsilon)-贪心算法
import numpy as npdef k_arm_bandit(k, num_steps, epsilon):# 初始化每个摇臂的真实奖赏期望这里假设服从正态分布true_rewards np.random.normal(0, 1, k)estimated_rewards np.zeros(k)num_pulls np.zeros(k)rewards []for step in range(num_steps):if np.random.rand() epsilon:# 以 epsilon 的概率随机选择一个摇臂action np.random.randint(k)else:# 以 1 - epsilon 的概率选择估计奖赏最大的摇臂action np.argmax(estimated_rewards)# 从选中的摇臂获得一个随机奖赏假设服从正态分布reward np.random.normal(true_rewards[action], 1)rewards.append(reward)# 更新估计奖赏和拉动次数num_pulls[action] 1estimated_rewards[action] (reward - estimated_rewards[action]) / num_pulls[action]return rewards# 示例运行
k 10
num_steps 1000
epsilon 0.1
rewards k_arm_bandit(k, num_steps, epsilon)
print(Total rewards:, np.sum(rewards))三、有模型学习
1. 基本概念
有模型学习中智能体尝试学习环境的模型即状态转移概率 (P(s’|s,a))从状态 (s) 采取动作 (a) 转移到状态 (s’) 的概率和奖赏函数 (R(s,a))在状态 (s) 采取动作 (a) 获得的奖赏。然后可以使用规划算法如动态规划来求解最优策略。
2. 数学公式Bellman 方程 状态值函数 (V(s)) 的 Bellman 期望方程 动作值函数 (Q(s,a)) 的 Bellman 期望方程 其中 (\pi(a|s)) 是策略表示在状态 (s) 下采取动作 (a) 的概率。
3. 代码示例价值迭代
import numpy as npdef value_iteration(P, R, gamma, theta):num_states P.shape[0]num_actions P.shape[1]V np.zeros(num_states)while True:delta 0for s in range(num_states):v V[s]V[s] max([sum([P[s][a][s_prime] * (R[s][a] gamma * V[s_prime])for s_prime in range(num_states)]) for a in range(num_actions)])delta max(delta, abs(v - V[s]))if delta theta:breakreturn V# 示例运行
# 假设环境的状态转移矩阵 P 和奖赏矩阵 R
P np.random.rand(3, 2, 3) # P[s][a][s_prime]
R np.random.rand(3, 2) # R[s][a]
gamma 0.9
theta 0.001
V value_iteration(P, R, gamma, theta)
print(Optimal state values:, V)四、免模型学习
1. 基本概念
免模型学习不尝试学习环境的完整模型而是直接学习价值函数或策略函数。常见的方法包括蒙特卡洛Monte Carlo、时序差分Temporal DifferenceTD学习等。
2. 数学公式TD(0) 更新 其中 (S_t) 和 (S_{t1}) 是连续的状态(R_{t1}) 是从 (S_t) 到 (S_{t1}) 获得的奖赏(\alpha) 是学习率。
3. 代码示例TD(0)
import numpy as npdef td_0(env, num_episodes, alpha, gamma):V np.zeros(env.num_states)for _ in range(num_episodes):state env.reset()done Falsewhile not done:action np.random.randint(env.num_actions) # 这里使用随机策略next_state, reward, done env.step(action)V[state] alpha * (reward gamma * V[next_state] - V[state])state next_statereturn Vclass SimpleEnvironment:def __init__(self):self.num_states 5self.num_actions 2def reset(self):return 0def step(self, action):# 简单模拟环境的状态转移和奖赏实际应用中需要根据具体环境定义if action 0:next_state np.random.choice(self.num_states)reward np.random.normal(0, 1)else:next_state np.random.choice(self.num_states)reward np.random.normal(1, 1)done False # 假设不会结束return next_state, reward, done# 示例运行
env SimpleEnvironment()
num_episodes 1000
alpha 0.1
gamma 0.9
V td_0(env, num_episodes, alpha, gamma)
print(Estimated state values:, V)五、值函数近似
1. 基本概念
当状态空间很大或连续时使用表格存储值函数变得不可行因此使用值函数近似。通常使用函数逼近器如线性函数、神经网络来表示 (V(s)) 或 (Q(s,a))。
2. 数学公式线性值函数近似
(V(s;\theta)\theta^T\phi(s))其中 (\theta) 是参数向量(\phi(s)) 是状态 (s) 的特征向量。
3. 代码示例线性函数近似
import numpy as npdef linear_value_approximation(env, num_episodes, alpha, gamma, theta):for _ in range(num_episodes):state env.reset()done Falsewhile not done:action np.random.randint(env.num_actions) # 随机策略next_state, reward, done env.step(action)# 特征向量表示phi_state np.array([state, state**2])phi_next_state np.array([next_state, next_state**2])target reward gamma * np.dot(theta, phi_next_state)delta target - np.dot(theta, phi_state)theta alpha * delta * phi_statestate next_statereturn thetaclass SimpleEnvironment:def __init__(self):self.num_states 5self.num_actions 2def reset(self):return 0def step(self, action):# 简单模拟环境的状态转移和奖赏if action 0:next_state np.random.choice(self.num_states)reward np.random.normal(0, 1)else:next_state np.random.choice(self.num_states)reward np.random.normal(1, 1)done False # 假设不会结束return next_state, reward, done# 示例运行
env SimpleEnvironment()
num_episodes 1000
alpha 0.1
gamma 0.9
theta np.random.rand(2)
theta linear_value_approximation(env, num_episodes, alpha, gamma, theta)
print(Estimated theta:, theta)六、模仿学习
1. 基本概念 模仿学习旨在让智能体通过模仿专家的行为来学习策略通常用于解决难以通过奖赏函数定义的任务。包括行为克隆Behavior Cloning、逆强化学习Inverse Reinforcement Learning等方法。 2. 代码示例行为克隆
import numpy as np
from sklearn.linear_model import LogisticRegressiondef behavior_cloning(expert_states, expert_actions):# 假设专家状态和动作是已知的model LogisticRegression()model.fit(expert_states, expert_actions)return model# 示例运行
expert_states np.random.rand(100, 2) # 假设专家状态是二维的
expert_actions np.random.randint(0, 2, 100) # 专家动作是 0 或 1
model behavior_cloning(expert_states, expert_actions)
print(Trained model:, model)代码解释
k-摇臂赌博机代码解释 k_arm_bandit 函数 true_rewards每个摇臂的真实期望奖赏。estimated_rewards对每个摇臂奖赏的估计。num_pulls每个摇臂被拉动的次数。使用 (\epsilon)-贪心算法以概率 (\epsilon) 随机选择摇臂以概率 (1 - \epsilon) 选择估计奖赏最高的摇臂。 有模型学习代码解释 value_iteration 函数 P状态转移矩阵。R奖赏矩阵。通过迭代更新状态值函数 (V(s))直到收敛(\Delta \theta)。 免模型学习代码解释 td_0 函数 V状态值函数。通过 TD(0) 更新规则 (V(S_t)\leftarrow V(S_t)\alpha(R_{t1}\gamma V(S_{t1})-V(S_t))) 来更新值函数。 值函数近似代码解释 linear_value_approximation 函数 使用线性函数 (V(s;\theta)\theta^T\phi(s)) 来近似值函数。通过更新参数 (\theta) 来学习。 模仿学习代码解释 behavior_cloning 函数 使用逻辑回归模型来学习专家的状态 - 动作映射。 算法比对 请注意上述代码仅为简单示例在实际应用中可能需要更复杂的环境和算法调整。同时对于使用的库如 numpy 和 sklearn你可以使用 pip 安装
pip install numpy sklearn