初识深度强化学习
专业术语
概率学上,通常以小写字母表示观测值,以大写字母表示随机变量。 \(p(x)\) 是概率密度函数( PDF )。 对于 \(X \in \chi\): 若离散分布,期望 \(\mathbb{E}(f(X)) = \int_{\chi} p(x) \cdot f(x) dx\) 若离散分布,期望 \(\mathbb{E}(f(X)) = \sum_{x \in \chi} p(x) \cdot f(x)\)
State \(s\)
当前环境的状态。
Action \(a\)
发起的动作。
Agent
Action 的发出者,这里一般指智能体。
Policy \(\pi\)
根据观测到的 state 做出策略来控制 agent 发起 action 。
Policy 函数 \(\pi: (s, a) \mapsto [0, 1]\):
\(\mathbb{P}(A = a | S = s)\) 指在 \(S = s\) 的前提下 \(A = a\) 的 PDF 。
\(\pi\) 可以是确定的(即在某个 state 下一定发起某个 action),也可以是随机的(即在某个 state 下有概率发起某个 action)。
Reward \(R\)
对 agent 发起 action 的奖励。奖励的值可正可负。奖励的值越大,越能激励 agent 发起对应的 action ,反之则 discourage agent 发起该 action 。
State Transition
这一过程称为状态转移。函数表示:
AI 玩游戏的基本流程
观测到 state \(s_1\);
发起 action \(a_1\);
观测到新 state \(s_2\),收到 reward \(r_1\);
发起 action \(a_2\);
...
游戏结束,轨迹 trajectory 为:(state, action, reward)
Rewards and Returns
Returns (aka Cumulative Future Reward)
将 \(t\) 时刻的 returns 记作 \(U_t\):
但 \(t + n\)(未来)时刻的 reward 更多时候不如 \(t\) 时刻的重要,所以需要 discount future reward 。
Discounted Returns (aka Cumulative Discounted Future Reward)
其中 \(\gamma\) 是 discount 率。 由以上可知,给定 \(s_t\) ,\(U_t\) 取决于随机变量 \(A_t, A_{t + 1}, A_{t + 2}, A_{t + 3}, \dots \) 和 \(S_{t + 1}, S_{t + 2}, \dots \) 。
Value Functions
价值函数,为某个对象打分。
对于 policy \(\pi \) 的 Action-value Function
动作价值函数,为 action \(a_t\) 打分:
\(Q_{\pi }\) 与 \(\pi , s_t, a_t\) 有关,与 \(A_{t + 1}, A_{t + 2}, A_{t + 3}, \dots \) 和 \(S_{t + 1}, S_{t + 2}, \dots \) 无关。
Optimal Action-value Function
最佳动作价值函数,用于判断在 state \(s_t\) 下哪个 action 最好:
\(Q^*\) 与 \(\pi\) 无关。
State-value Function
状态价值函数,可以用于判断游戏当前局势的好坏:
\(\mathbb{E}_S(V_{\pi }(S))\) 可以用于评估 policy \(\pi \) 的好坏。
AI 如何控制 agent
两种思路:
假设已有好的 policy \(\pi (a | s)\)
观察 state \(s_t\);
随机取样:\(a_t \sim \pi (\pi | s_t)\)。
假设已有 Optimal Action-value Function \(Q^*(s, a)\)
观察 state \(s_t\);
选择使值 \(a_t = \mathrm{argmax}_a Q^*(s_t, a)\) 最大的 action 。
强化学习的任务就是学习 \(\pi (a | s)\) 或 \(Q^*(s, a)\) 两者之一。
Gym 入门
Gym is an open source Python library for developing and comparing reinforcement learning algorithms by providing a standard API to communicate between learning algorithms and environments, as well as a standard set of environments compliant with that API. ... To install the base Gym library, usepip install gym. This does not include dependencies for all families of environments (there's a massive number, and some can be problematic to install on certain systems). You can install these dependencies for one family likepip install gym[atari]or usepip install gym[all]to install all dependencies. We support Python 3.7, 3.8, 3.9 and 3.10 on Linux and macOS. We will accept PRs related to Windows, but do not officially support it.
代码示例:
import gym
env = gym.make('CartPole-v0') # 参数为欲解决问题的名称,此处是解决 Cart Pole 问题
state = env.reset()
for t in range(100):
env.render() # 弹出一个 Cart Pole 问题的窗口
print(state)
action = env.action_space.sample() # 一次随机 action
state, reward, done, info = env.step(action)
if done: # done == 1 时结束(赢下或输掉这次游戏)
print('Finished')
break
env.close
这里env提供了 state 和 reward 。