深度強化學習DQN(Deep Q Network)原理及例子:如何解決迷宮問題,附源碼

代碼可以參見https://blog.csdn.net/bbbeoy/...,本文我做了一些改動

目前,強化學習中很火的當屬Q-Learning了,關於Q-Learning的具體介紹請參加我上一篇文章。從上一篇文章中,我們可以看到,Q table可以看做Q-Learning的大腦,Q table對應了一張state-action的表,但在實際應用中,state和action往往很多,內存很難裝下Q table,因此需要用神經網絡替代Q table。

訓練樣本

首先要解決的問題是如何獲取訓練樣本。在 DQN 中有 Experience Replay 的概念,就是經驗回放。即先讓agent去探索環境,將經驗(記憶)累積到一定程度,再隨機抽取出一批樣本進行訓練。爲什麼要隨機抽取?因爲agent去探索環境時採集到的樣本是一個時間序列,樣本之間具有連續性,如果每次得到樣本就更新Q值,受樣本分佈影響,會對收斂造成影響。

這裏我們聯想到數據庫領域,我們需要使用benchmark去回放得到不同的action對應的Q值。增強學習是試錯學習(Trail-and-error),由於沒有直接的指導信息,agent要以不斷與環境進行交互,通過試錯的方式來獲得最佳策略。因此一開始可以看做是盲目的、隨機的試驗,但是根據反饋的reward來優化損失函數可以使得我們想要的Q table慢慢收斂。

損失函數

上面提到了損失函數,那麼如何選取呢。在DQN中,Q值表中表示的是當前已學習到的經驗。而根據公式計算出的 Q 值是agent通過與環境交互及自身的經驗總結得到的一個分數(即:目標 Q 值)。最後使用目標 Q 值(target_q)去更新原來舊的 Q 值(q)。而目標 Q 值與舊的 Q 值的對應關係,正好是監督學習神經網絡中結果值與輸出值的對應關係。

所以,loss = (target_q - q)^2

即:整個訓練過程其實就是 Q 值(q)向目標 Q 值(target_q)逼近的過程。

代碼實現

看代碼是最直觀的,我先給出整個代碼流程,然後再詳細解釋。

import tensorflow as tf
import numpy as np
from collections import deque
import random


class DeepQNetwork:
    r = np.array([[-1, -1, -1, -1, 0, -1],
                  [-1, -1, -1, 0, -1, 100.0],
                  [-1, -1, -1, 0, -1, -1],
                  [-1, 0, 0, -1, 0, -1],
                  [0, -1, -1, 1, -1, 100],
                  [-1, 0, -1, -1, 0, 100],
                  ])

    # 執行步數。
    step_index = 0

    # 狀態數。
    state_num = 6

    # 動作數。
    action_num = 6

    # 訓練之前觀察多少步。
    OBSERVE = 1000.

    # 選取的小批量訓練樣本數。
    BATCH = 20

    # epsilon 的最小值,當 epsilon 小於該值時,將不在隨機選擇行爲。
    FINAL_EPSILON = 0.0001

    # epsilon 的初始值,epsilon 逐漸減小。
    INITIAL_EPSILON = 0.1

    # epsilon 衰減的總步數。
    EXPLORE = 3000000.

    # 探索模式計數。
    epsilon = 0

    # 訓練步數統計。
    learn_step_counter = 0

    # 學習率。
    learning_rate = 0.001

    # γ經驗折損率。
    gamma = 0.9

    # 記憶上限。
    memory_size = 5000

    # 當前記憶數。
    memory_counter = 0

    # 保存觀察到的執行過的行動的存儲器,即:曾經經歷過的記憶。
    replay_memory_store = deque()

    # 生成一個狀態矩陣(6 X 6),每一行代表一個狀態。
    state_list = None

    # 生成一個動作矩陣。
    action_list = None

    # q_eval 網絡。
    q_eval_input = None
    action_input = None
    q_target = None
    q_eval = None
    predict = None
    loss = None
    train_op = None
    cost_his = None
    reward_action = None

    # tensorflow 會話。
    session = None

    def __init__(self, learning_rate=0.001, gamma=0.9, memory_size=5000):
        self.learning_rate = learning_rate
        self.gamma = gamma
        self.memory_size = memory_size

        # 初始化成一個 6 X 6 的狀態矩陣。
        self.state_list = np.identity(self.state_num)

        # 初始化成一個 6 X 6 的動作矩陣。
        self.action_list = np.identity(self.action_num)

        # 創建神經網絡。
        self.create_network()

        # 初始化 tensorflow 會話。
        self.session = tf.InteractiveSession()

        # 初始化 tensorflow 參數。
        self.session.run(tf.initialize_all_variables())

        # 記錄所有 loss 變化。
        self.cost_his = []

    def create_network(self):
        """
        創建神經網絡。
        :return:
        """
        self.q_eval_input = tf.placeholder(shape=[None, self.state_num], dtype=tf.float32)
        self.action_input = tf.placeholder(shape=[None, self.action_num], dtype=tf.float32)
        self.q_target = tf.placeholder(shape=[None], dtype=tf.float32)

        neuro_layer_1 = 3
        w1 = tf.Variable(tf.random_normal([self.state_num, neuro_layer_1]))
        b1 = tf.Variable(tf.zeros([1, neuro_layer_1]) + 0.1)
        l1 = tf.nn.relu(tf.matmul(self.q_eval_input, w1) + b1)

        w2 = tf.Variable(tf.random_normal([neuro_layer_1, self.action_num]))
        b2 = tf.Variable(tf.zeros([1, self.action_num]) + 0.1)
        self.q_eval = tf.matmul(l1, w2) + b2

        # 取出當前動作的得分。
        self.reward_action = tf.reduce_sum(tf.multiply(self.q_eval, self.action_input), reduction_indices=1)
        self.loss = tf.reduce_mean(tf.square((self.q_target - self.reward_action)))
        self.train_op = tf.train.GradientDescentOptimizer(self.learning_rate).minimize(self.loss)

        self.predict = tf.argmax(self.q_eval, 1)

    def select_action(self, state_index):
        """
        根據策略選擇動作。
        :param state_index: 當前狀態。
        :return:
        """
        current_state = self.state_list[state_index:state_index + 1]

        if np.random.uniform() < self.epsilon:
            current_action_index = np.random.randint(0, self.action_num)
        else:
            actions_value = self.session.run(self.q_eval, feed_dict={self.q_eval_input: current_state})
            action = np.argmax(actions_value)
            current_action_index = action

        # 開始訓練後,在 epsilon 小於一定的值之前,將逐步減小 epsilon。
        if self.step_index > self.OBSERVE and self.epsilon > self.FINAL_EPSILON:
            self.epsilon -= (self.INITIAL_EPSILON - self.FINAL_EPSILON) / self.EXPLORE

        return current_action_index

    def save_store(self, current_state_index, current_action_index, current_reward, next_state_index, done):
        """
        保存記憶。
        :param current_state_index: 當前狀態 index。
        :param current_action_index: 動作 index。
        :param current_reward: 獎勵。
        :param next_state_index: 下一個狀態 index。
        :param done: 是否結束。
        :return:
        """
        current_state = self.state_list[current_state_index:current_state_index + 1]
        current_action = self.action_list[current_action_index:current_action_index + 1]
        next_state = self.state_list[next_state_index:next_state_index + 1]
        # 記憶動作(當前狀態, 當前執行的動作, 當前動作的得分,下一個狀態)。
        self.replay_memory_store.append((
            current_state,
            current_action,
            current_reward,
            next_state,
            done))

        # 如果超過記憶的容量,則將最久遠的記憶移除。
        if len(self.replay_memory_store) > self.memory_size:
            self.replay_memory_store.popleft()

        self.memory_counter += 1

    def step(self, state, action):
        """
        執行動作。
        :param state: 當前狀態。
        :param action: 執行的動作。
        :return:
        """
        reward = self.r[state][action]

        next_state = action

        done = False

        if action == 5:
            done = True

        return next_state, reward, done

    def experience_replay(self):
        """
        記憶回放。
        :return:
        """
        # 隨機選擇一小批記憶樣本。
        batch = self.BATCH if self.memory_counter > self.BATCH else self.memory_counter
        minibatch = random.sample(self.replay_memory_store, batch)

        batch_state = None
        batch_action = None
        batch_reward = None
        batch_next_state = None
        batch_done = None

        for index in range(len(minibatch)):
            if batch_state is None:
                batch_state = minibatch[index][0]
            elif batch_state is not None:
                batch_state = np.vstack((batch_state, minibatch[index][0]))

            if batch_action is None:
                batch_action = minibatch[index][1]
            elif batch_action is not None:
                batch_action = np.vstack((batch_action, minibatch[index][1]))

            if batch_reward is None:
                batch_reward = minibatch[index][2]
            elif batch_reward is not None:
                batch_reward = np.vstack((batch_reward, minibatch[index][2]))

            if batch_next_state is None:
                batch_next_state = minibatch[index][3]
            elif batch_next_state is not None:
                batch_next_state = np.vstack((batch_next_state, minibatch[index][3]))

            if batch_done is None:
                batch_done = minibatch[index][4]
            elif batch_done is not None:
                batch_done = np.vstack((batch_done, minibatch[index][4]))

        # q_next:下一個狀態的 Q 值。
        q_next = self.session.run([self.q_eval], feed_dict={self.q_eval_input: batch_next_state})

        q_target = []
        for i in range(len(minibatch)):
            # 當前即時得分。
            current_reward = batch_reward[i][0]

            # # 遊戲是否結束。
            # current_done = batch_done[i][0]

            # 更新 Q 值。
            q_value = current_reward + self.gamma * np.max(q_next[0][i])

            # 當得分小於 0 時,表示走了不可走的位置。
            if current_reward < 0:
                q_target.append(current_reward)
            else:
                q_target.append(q_value)

        _, cost, reward = self.session.run([self.train_op, self.loss, self.reward_action],
                                           feed_dict={self.q_eval_input: batch_state,
                                                      self.action_input: batch_action,
                                                      self.q_target: q_target})

        self.cost_his.append(cost)

        # if self.step_index % 1000 == 0:
        #     print("loss:", cost)

        self.learn_step_counter += 1

    def train(self):
        """
        訓練。
        :return:
        """
        # 初始化當前狀態。
        current_state = np.random.randint(0, self.action_num - 1)
        self.epsilon = self.INITIAL_EPSILON

        while True:
            # 選擇動作。
            action = self.select_action(current_state)

            # 執行動作,得到:下一個狀態,執行動作的得分,是否結束。
            next_state, reward, done = self.step(current_state, action)

            # 保存記憶。
            self.save_store(current_state, action, reward, next_state, done)

            # 先觀察一段時間累積足夠的記憶在進行訓練。
            if self.step_index > self.OBSERVE:
                self.experience_replay()

            if self.step_index > 10000:
                break

            if done:
                current_state = np.random.randint(0, self.action_num - 1)
            else:
                current_state = next_state

            self.step_index += 1

    def pay(self):
        """
        運行並測試。
        :return:
        """
        self.train()

        # 顯示 R 矩陣。
        print(self.r)

        for index in range(5):

            start_room = index

            print("#############################", "Agent 在", start_room, "開始行動", "#############################")

            current_state = start_room

            step = 0

            target_state = 5

            while current_state != target_state:
                out_result = self.session.run(self.q_eval, feed_dict={
                    self.q_eval_input: self.state_list[current_state:current_state + 1]})

                next_state = np.argmax(out_result[0])

                print("Agent 由", current_state, "號房間移動到了", next_state, "號房間")

                current_state = next_state

                step += 1

            print("Agent 在", start_room, "號房間開始移動了", step, "步到達了目標房間 5")

            print("#############################", "Agent 在", 5, "結束行動", "#############################")


if __name__ == "__main__":
    q_network = DeepQNetwork()
    q_network.pay()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章