深度强化学习
第二篇:深度强化学习之Q学习
提纲
- 什么是Q学习?
- Q学习实战
内容
🙋 Q学习:一种基于数值的强化学习算法。
🙋 Q表:创建一个表格,用于计算每种状态下采取的没中行动的最大的未来预期奖励。Q表示动作的质量每个单元格的值是该给定状态和行动的最大未来预期奖励。
🙋 Q学习算法:学习动作值函数(Q函数)
动作值函数有两个输入:“状态”和“动作”,它返回该动作在该状态下的预期未来奖励。
💭 注:探索之前,Q表中的值是固定的初始值(一般为0)。在探索环境时,使用Bellman方程迭代更新Q(s,a),Q表中的值将趋近于更好。
Q学习算法过程:
- 算法概述
xxxxxxxxxx
511. initialize Q-value(Q(s,a))arbitrarily for all state-action pairs
22. For life or until learning is stopped:
33. choose an action in the current world state based on current Q-value estimate(Q(s,·))
44. take the action and observe the outcome state(s') and reward(r)
55. update Q(s,a) := Q(s,a)+$\alpha$[r+$\gamma$max_{a'}Q(s',a')-Q(s,a)](Bellman方程)
算法详述
初始化Q值
构建一个Q表,有m列(m=行动数)和n行(n=状态数),将值初始化为0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 终身学习或直到学习停止
选择操作(和第一篇中的explore & exploit有关)
- 开始时,使用探索率(初始化为1),即随机执行的step的速度,随机选择行动进行大量探索
- 生成一个随机数,如果这个数字,那么我们将进行使用(这意味着我们使用已知的方法来选择每一步最佳动作),否则,继续探索。
- 在Q函数训练开始时,有一个较大的,随着agent做的越来越好,这个越来越小。
- 评估:采取行动a,观察结果s‘和r,更新Q函数(使用Bellman方程)
其中为学习率
💪 Bellman方程更新代码
xxxxxxxxxx
31New Q value =
2Current Q value +
3lr * [Reward + discount_rate * (highest Q value between possible actions from the new state s’ ) — Current Q value ]
🙋 实战
💪 冰冻游戏
💪 代码
🐭 Q表生成过程
xxxxxxxxxx
401for episode in range(total_episodes):
2# Reset the environment
3state = env.reset()
4step = 0
5done = False
6total_rewards = 0
7
8for step in range(max_steps):
9# 3. Choose an action a in the current world state (s)
10## First we randomize a number
11exp_exp_tradeoff = random.uniform(0, 1)
12
13## If this number > greater than epsilon --> exploitation (taking the biggest Q value for this state)
14if exp_exp_tradeoff > epsilon:
15action = np.argmax(qtable[state, :])
16
17# Else doing a random choice --> exploration
18else:
19action = env.action_space.sample()
20
21# Take the action (a) and observe the outcome state(s') and reward (r)
22new_state, reward, done, info = env.step(action)
23
24# Update Q(s,a):= Q(s,a) + lr [R(s,a) + gamma * max Q(s',a') - Q(s,a)]
25# qtable[new_state,:] : all the actions we can take from new state
26qtable[state, action] = qtable[state, action] + learning_rate * (
27reward + gamma * np.max(qtable[new_state, :]) - qtable[state, action])
28
29total_rewards += reward
30
31# Our new state is state
32state = new_state
33
34# If done (if we're dead) : finish episode
35if done == True:
36break
37
38# Reduce epsilon (because we need less and less exploration)
39epsilon = min_epsilon + (max_epsilon - min_epsilon) * np.exp(-decay_rate * episode)
40rewards.append(total_rewards)
🐭 将表格代入运行得:
xxxxxxxxxx
571Score over time: 0.47946666666666665
2[[ 2.09715899e-01 4.01579846e-02 3.97892517e-02 5.11616870e-02]
3[ 6.09804136e-03 3.78155051e-03 7.45077453e-03 4.18456951e-02]
4[ 7.38449709e-03 3.10248950e-02 1.00028654e-02 1.03187557e-02]
5[ 8.39087322e-03 5.30955563e-03 5.18508696e-04 1.65090334e-02]
6[ 3.25147050e-01 2.77311522e-02 7.40381004e-03 7.38136569e-03]
7[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
8[ 9.55174498e-04 1.43121026e-04 6.37218288e-05 2.06232845e-05]
9[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
10[ 1.45102140e-01 3.63228268e-05 8.16112810e-03 2.09215137e-01]
11[ 3.90073099e-02 5.99622579e-01 1.00548149e-02 5.52889004e-03]
12[ 1.52065961e-01 1.77304978e-04 1.14593313e-03 6.34562290e-03]
13[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
14[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
15[ 3.35100592e-03 7.83322896e-02 5.55265907e-01 7.80362727e-02]
16[ 2.10515209e-01 9.81306840e-01 1.76139237e-01 1.44002689e-01]
17[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]]
18****************************************************
19EPISODE 0
20(Left)
21SFFF
22FHFH
23FFFH
24HFFG
25Number of steps 34
26****************************************************
27EPISODE 1
28(Down)
29SFFF
30FHFH
31FFFH
32HFFG
33Number of steps 50
34****************************************************
35EPISODE 2
36(Down)
37SFFF
38FHFH
39FFFH
40HFFG
41Number of steps 17
42****************************************************
43EPISODE 3
44(Down)
45SFFF
46FHFH
47FFFH
48HFFG
49Number of steps 28
50****************************************************
51EPISODE 4
52(Down)
53SFFF
54FHFH
55FFFH
56HFFG
57Number of steps 64
🐭 使用图像引擎可观察到,在最后测试的五个例子里,有4次都成功到达G。
💭 注:S-start ;F-frozen ice;H-hole ;G-goal
💭 关于OpenAI的gym图像引擎、物理引擎和相关环境函数将在下一篇中列出。