Image from unsplash.com by helloquence
前面的文章我們介紹了 Q-learning, DQN 等方法都是基于價值的強(qiáng)化學(xué)習(xí)方法,今天我們介紹的 Policy Gradient 方法是基于策略的強(qiáng)化學(xué)習(xí)方法。該方法的理論部分已經(jīng)介紹過了,這里就不贅述了崇摄,直接上手項(xiàng)目饼问。
本文的全部代碼可在我的 github repo 中查看
https://github.com/zht007/tensorflow-practice
1. 監(jiān)督學(xué)習(xí)回顧
為了更好地理解 Policy Gradient 算法稚照,我們用監(jiān)督學(xué)習(xí)的分類問題作為類比。
以手寫數(shù)字識別的項(xiàng)目為例凯沪,:
- 將圖片作為輸入傳給神經(jīng)網(wǎng)絡(luò)第焰。
- 神經(jīng)網(wǎng)絡(luò)會給該圖片屬于哪一類(數(shù)字 0 到 9)給出一個評分(logits)。
- 評分(logits)通過 Softmax 就可以轉(zhuǎn)換為屬于每一類(數(shù)字 0 到 9)的概率(Probability)妨马。
- 通過 Cross Entropy (交叉商) 對比與真實(shí)標(biāo)簽的“距離”
- 最后這個“距離” 就作為loss function (損失函數(shù)) 反向傳回神經(jīng)網(wǎng)絡(luò)進(jìn)行參數(shù)更新挺举。
Image from github repo with Apache-2.0 license
如上圖所示 Cross entropy 的公式如下
2. Policy 梯度上升
在強(qiáng)化學(xué)習(xí)中,我們用神經(jīng)網(wǎng)絡(luò)來參數(shù)化策略身笤,神經(jīng)網(wǎng)絡(luò)扮演策略的角色豹悬,在神經(jīng)網(wǎng)絡(luò)輸入狀態(tài),就可以輸出策略價值函數(shù)液荸,指導(dǎo)智能體的行動瞻佛。我們在之前的文章中講到,優(yōu)化策略函數(shù)(神經(jīng)網(wǎng)絡(luò))的過程可以利用用梯度上升(Gradient Ascent)的方法娇钱,以獲取最大價值伤柄。然而目標(biāo)函數(shù)的策略梯度與 Cross entropy 非常相似:
定理:對于任何可微的策略π(s,a),對于任何策略的目標(biāo)函數(shù)J(θ)策略梯度都是:
如果 Q(s, a) 作為“標(biāo)簽”的話,上面的公式與 Cross Entropy 公式僅相差一個負(fù)號文搂,這個負(fù)號的作用恰好可以將反向傳播的梯度下降适刀,轉(zhuǎn)換成我們需要的梯度上升。
當(dāng)然如果覺得這個理論理解起來比較困難煤蹭,我還有一個更加簡單的角度笔喉,我們是這樣操作的:
- 首先,我們采用蒙特卡洛的方法完成一個完整的 episode 記錄其中每一個[s, a, r]:
然后硝皂,將 s1, s2, ... sT 帶入神經(jīng)網(wǎng)絡(luò)常挚,預(yù)測得到每一個狀態(tài)下的行動概率。將實(shí)際行動作為“標(biāo)簽”帶入 corss entropy 的公式稽物。但是這個“標(biāo)簽” 并不是真正的標(biāo)簽奄毡,并不能指導(dǎo)神經(jīng)網(wǎng)絡(luò)朝正確的方向更新。我們需要乘以獎勵r贝或,獎勵r 的作用相當(dāng)于對“標(biāo)簽”的評價吼过,獎勵越大神經(jīng)網(wǎng)絡(luò)就朝著“標(biāo)簽”的方向更新,反之就向相反的方向更新咪奖。
最后盗忱,我們再講這個新的函數(shù)作為 loss fuction 傳給神經(jīng)網(wǎng)絡(luò)進(jìn)行更新。
Image from github repo with Apache-2.0 license
最后羊赵,還需要注意的是售淡,這里的獎勵 R 即 r1, r2, ... rT 在送入上式進(jìn)行反向傳播之前是需要進(jìn)行,discount 和 normalize 兩步處理慷垮。discount 很好理解揖闸,即離結(jié)束越遠(yuǎn)的獎勵需要 discount 越多,離結(jié)束回合越近需要 discount 越少料身。同時汤纸,normalize 的目的是為了鼓勵那些正確地動作,因?yàn)樘貏e是在游戲初期芹血,在大多數(shù)的隨機(jī)行動中贮泞,正確步伐所獲得的獎勵往往會容易被淹沒在眾多的隨機(jī)行動所帶來的獎勵了。
3. Tensorflow 代碼實(shí)踐
關(guān)于 CartPole 的游戲之前已經(jīng)介紹夠多了幔烛,初始化就不多說了啃擦,與之前相似。重點(diǎn)是構(gòu)建 PGAgent 對象饿悬。
3.1 Agent 神經(jīng)網(wǎng)絡(luò)大腦
對于神經(jīng)網(wǎng)絡(luò)令蛉,self.states, self.actions 和 self.discounted_episode_rewards 即前面介紹的輸入,“標(biāo)簽”狡恬,和處理后的獎勵珠叔。self.sample_op 的作用是根據(jù)概率選擇行動
def _build_net(self):
self.states = tf.placeholder(tf.float32, [None,OBSERVATION_SPACE_SIZE])
self.actions = tf.placeholder(tf.int32, [None,])
self.discounted_episode_rewards = tf.placeholder(tf.float32, [None,])
fc1 = tf.layers.dense(
inputs = self.states,
units = 10,
activation = tf.nn.relu,
kernel_initializer=tf.random_normal_initializer(mean=0, stddev=0.3),
bias_initializer=tf.constant_initializer(0.1)
)
act_logits = tf.layers.dense(
inputs = fc1,
units = ACTION_SPACE_SIZE,
kernel_initializer=tf.random_normal_initializer(mean=0, stddev=0.3),
bias_initializer=tf.constant_initializer(0.1),
)
self.actions_prob = tf.nn.softmax(act_logits)
#sample an action from predicted probabilities
self.sample_op = tf.multinomial(logits=act_logits, num_samples=1)
neg_log_prob = tf.reduce_sum(-tf.log(self.actions_prob) * tf.one_hot(self.actions, ACTION_SPACE_SIZE),axis =1)
loss = tf.reduce_mean(neg_log_prob * self.discounted_episode_rewards)
self.train_op = tf.train.AdamOptimizer(0.001).minimize(loss)
Code from github repo with MIT license
3.2 Discount and Normalize Rewards
正如之前提到的,Rewards需要經(jīng)過 discount 和 normalize 兩個步驟弟劲,函數(shù)如下:
def discount_rewards(self, rewards):
discount_rewards = np.zeros_like(rewards)
running_add = 0
for t in reversed(range(len(rewards))):
running_add = running_add * GAMMA + rewards[t]
discount_rewards[t] = running_add
mean = np.mean(discount_rewards)
std = np.std(discount_rewards)
discount_rewards = (discount_rewards - mean)/std
return discount_rewards
3.4 訓(xùn)練過程
訓(xùn)練過程分為三個步驟
首先祷安,初始化每個episode (回合) 的 states, actions 和 rewards。
episode_states, episode_actions, episode_rewards = [],[],[] #Reset single step reward episode_reward = 0
然后兔乞, 通過agent 的“大腦“ 選擇行動得到該行動的 state, action 和 reward
action = agent.choose_action(current_state) next_state, reward, done, _ = env.step(action)
接著汇鞭,將s, a 和 r 收集到 states, actions 和 rewards 中
episode_states.append(current_state) episode_actions.append(action) episode_rewards.append(reward)
最后,將收集到的 states, actions 和 rewards 傳回 agent “大腦” 進(jìn)行學(xué)習(xí)
agent.train(np.vstack(episode_states), np.array(episode_actions), np.array(episode_rewards))
Code from github repo with MIT license
3.5 訓(xùn)練結(jié)果
Policy Gradient 的訓(xùn)練時間還是蠻久的庸追,經(jīng)過30000 個回合的訓(xùn)練霍骄,最大,平均和最小獎勵都在穩(wěn)步上升锚国。對于最大獎勵在5000 個回合之后獎勵就基本穩(wěn)定在最大獎勵200了腕巡,平均獎勵和最小獎勵在訓(xùn)練過程中都有上下起伏,但是總體上是在收斂和上升的血筑。
Image crested by @hongtao
4. 總結(jié)
本文介紹的 Policy Gradient 方法是深度學(xué)習(xí)與強(qiáng)化學(xué)習(xí)結(jié)合的一個非常典型的案例绘沉,由于跟監(jiān)督學(xué)習(xí)非常相似,所以比起 Q-learning 來說更加容易理解豺总。Policy Gradient 作為基于策略的強(qiáng)化學(xué)習(xí)方法如何與基于價值的Q learning 相結(jié)合呢车伞? 這將是我們接下來研究的問題。
參考資料
[1] Reinforcement Learning: An Introduction (2nd Edition)
[2] David Silver's Reinforcement Learning Course (UCL, 2015)
[3] Github repo: Reinforcement Learning
相關(guān)文章
強(qiáng)化學(xué)習(xí)——MC(蒙特卡洛)玩21點(diǎn)撲克游戲
強(qiáng)化學(xué)習(xí)實(shí)戰(zhàn)——動態(tài)規(guī)劃(DP)求最優(yōu)MDP
強(qiáng)化學(xué)習(xí)——強(qiáng)化學(xué)習(xí)的算法分類
強(qiáng)化學(xué)習(xí)——重拾強(qiáng)化學(xué)習(xí)的核心概念
AI學(xué)習(xí)筆記——Sarsa算法
AI學(xué)習(xí)筆記——Q Learning
AI學(xué)習(xí)筆記——動態(tài)規(guī)劃(Dynamic Programming)解決MDP(1)
AI學(xué)習(xí)筆記——動態(tài)規(guī)劃(Dynamic Programming)解決MDP(2)
AI學(xué)習(xí)筆記——MDP(Markov Decision Processes馬可夫決策過程)簡介
AI學(xué)習(xí)筆記——求解最優(yōu)MDP
首發(fā)steemit
歡迎掃描二維碼關(guān)注我的微信公眾號“tensorflow機(jī)器學(xué)習(xí)”喻喳,一起學(xué)習(xí)另玖,共同進(jìn)步