深度强化学习–Deep Q Network(using tensorflow)

文章目录

前言

总结记录深度强化学习算法Deep Q Network
参考自莫凡python

深度强化学习--Deep Q Network(using tensorflow)

; 一、什么是Q-learning

Q-Learning
α-学习效率 γ-衰减率 ε-贪婪度
r-环境所对应回报
s-状态
s_-下一状态
a-行为
Q(s,a)-Q表(某状态下采取某行为的价值)

ε:每次执行时,会有ε的概率选择Q表当中的最优项,1-ε的概率选择随机项(用于学习)。
γ:越高,效果越有远见,不会只看到近期的回报。

Q(s,a) += α[r + γQ(s_,a).max – Q(s,a)]

深度强化学习--Deep Q Network(using tensorflow)

二、什么是Deep Q Network

之前传统的强化学习算法,比如: Q-learning, Sarsa等方法都是用 表格的方式去存储每一个状态的state,和在这个state每个action所拥有的Q值。局限性在于如果 实际问题中的state过多,计算机内存无法满足存储要求。
于是 Deep Q Network将传统的 强化学习算法神经网络结合.我们可以将 状态和动作当成神经网络的 输入, 然后经过 神经网络分析后得到动作的 Q 值, 这样我们就没必要在表格中记录 Q 值, 而是直接使用 神经网络生成 Q 值. 还有一种形式的是这样, 我们也能 只输入状态值, 输出所有的 动作值, 然后按照 Q learning 的原则, 直接选择拥有最大值的动作当做下一步要做的动作.

三、DQN的两大利器

深度强化学习--Deep Q Network(using tensorflow)

; 1.Experience replay

Experience replay是指DQN可以 建立记忆库用于存储 之前学习过的一些经历,Q-learning是一种 off-policy的算法,可以学习过去的或者是别人的经历。所以每次 DQN 更新的时候, 我们都可以 随机抽取一些之前的经历进行学习. 随机抽取这种做法 打乱了经历之间的相关性, 也使得神经网络更新更有效率.

2.Fixed Q-targets

Fixed Q-targets 也是一种 打乱相关性的机理, 如果使用 fixed Q-targets, 我们就会在 DQN 中使用到两个 结构相同但参数不同的神经网络, 预测 Q 估计 的神经网络具备 最新的参数, 而预测 Q 现实 的神经网络使用的参数则是 很久以前的.

Q估计神经网络参数是随着学习的进行 不断更新的,而 Q现实的参数是被 冻结的,我们可以设定参数,固定在X次更新之后,将 Q估计中的参数 覆盖Q现实中的参数。

深度强化学习--Deep Q Network(using tensorflow)

; 四、DQN算法

深度强化学习--Deep Q Network(using tensorflow)
在Q-learning算法的基础上增加了以下特点:
记忆库 (用于重复学习)
神经网络计算 Q 值
暂时冻结 q_target 参数 (切断相关性)

五、DQN的实现(using tensorflow)

1.run_this.py

首先import所需模块

from maze_env import Maze
from RL_brain import DeepQNetwork

DQN与环境的交互

def run_maze():
    step = 0
    for episode in range(300):

        observation = env.reset()

        while True:

            env.render()

            action = RL.choose_action(observation)

            observation_, reward, done = env.step(action)

            RL.store_transition(observation, action, reward, observation_)

            if (step > 200) and (step % 5 == 0):
                RL.learn()

            observation = observation_

            if done:
                break
            step += 1

    print('game over')
    env.destroy()

if __name__ == "__main__":
    env = Maze()
    RL = DeepQNetwork(env.n_actions, env.n_features,
                      learning_rate=0.01,
                      reward_decay=0.9,
                      e_greedy=0.9,
                      replace_target_iter=200,
                      memory_size=2000,

                      )
    env.after(100, run_maze)
    env.mainloop()
    RL.plot_cost()

2.RL_brain.py

1)DQN类的整体框架

class DeepQNetwork:

    def _build_net(self):

    def __init__(self):

    def store_transition(self, s, a, r, s_):

    def choose_action(self, observation):

    def learn(self):

    def plot_cost(self):

2)建立两个神经网络(Q估计和Q现实)

class DeepQNetwork:
    def _build_net(self):

        self.s = tf.placeholder(tf.float32, [None, self.n_features], name='s')
        self.q_target = tf.placeholder(tf.float32, [None, self.n_actions], name='Q_target')
        with tf.variable_scope('eval_net'):

            c_names, n_l1, w_initializer, b_initializer = \
                ['eval_net_params', tf.GraphKeys.GLOBAL_VARIABLES], 10, \
                tf.random_normal_initializer(0., 0.3), tf.constant_initializer(0.1)

            with tf.variable_scope('l1'):
                w1 = tf.get_variable('w1', [self.n_features, n_l1], initializer=w_initializer, collections=c_names)
                b1 = tf.get_variable('b1', [1, n_l1], initializer=b_initializer, collections=c_names)
                l1 = tf.nn.relu(tf.matmul(self.s, w1) + b1)

            with tf.variable_scope('l2'):
                w2 = tf.get_variable('w2', [n_l1, self.n_actions], initializer=w_initializer, collections=c_names)
                b2 = tf.get_variable('b2', [1, self.n_actions], initializer=b_initializer, collections=c_names)
                self.q_eval = tf.matmul(l1, w2) + b2

        with tf.variable_scope('loss'):
            self.loss = tf.reduce_mean(tf.squared_difference(self.q_target, self.q_eval))
        with tf.variable_scope('train'):
            self._train_op = tf.train.RMSPropOptimizer(self.lr).minimize(self.loss)

        self.s_ = tf.placeholder(tf.float32, [None, self.n_features], name='s_')
        with tf.variable_scope('target_net'):

            c_names = ['target_net_params', tf.GraphKeys.GLOBAL_VARIABLES]

            with tf.variable_scope('l1'):
                w1 = tf.get_variable('w1', [self.n_features, n_l1], initializer=w_initializer, collections=c_names)
                b1 = tf.get_variable('b1', [1, n_l1], initializer=b_initializer, collections=c_names)
                l1 = tf.nn.relu(tf.matmul(self.s_, w1) + b1)

            with tf.variable_scope('l2'):
                w2 = tf.get_variable('w2', [n_l1, self.n_actions], initializer=w_initializer, collections=c_names)
                b2 = tf.get_variable('b2', [1, self.n_actions], initializer=b_initializer, collections=c_names)
                self.q_next = tf.matmul(l1, w2) + b2

tensorboard神经网络可视化

深度强化学习--Deep Q Network(using tensorflow)

3)初始值

class DeepQNetwork:
    def __init__(
            self,
            n_actions,
            n_features,
            learning_rate=0.01,
            reward_decay=0.9,
            e_greedy=0.9,
            replace_target_iter=300,
            memory_size=500,
            batch_size=32,
            e_greedy_increment=None,
            output_graph=False,
    ):
        self.n_actions = n_actions
        self.n_features = n_features
        self.lr = learning_rate
        self.gamma = reward_decay
        self.epsilon_max = e_greedy
        self.replace_target_iter = replace_target_iter
        self.memory_size = memory_size
        self.batch_size = batch_size
        self.epsilon_increment = e_greedy_increment
        self.epsilon = 0 if e_greedy_increment is not None else self.epsilon_max

        self.learn_step_counter = 0

        self.memory = np.zeros((self.memory_size, n_features*2+2))

        self._build_net()

        t_params = tf.get_collection('target_net_params')
        e_params = tf.get_collection('eval_net_params')
        self.replace_target_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)]

        self.sess = tf.Session()

        if output_graph:

            tf.summary.FileWriter("logs/", self.sess.graph)

        self.sess.run(tf.global_variables_initializer())
        self.cost_his = []

4)存储记忆

class DeepQNetwork:
    def __init__(self):
        ...

    def store_transition(self, s, a, r, s_):
        if not hasattr(self, 'memory_counter'):
            self.memory_counter = 0

        transition = np.hstack((s, [a, r], s_))

        index = self.memory_counter % self.memory_size
        self.memory[index, :] = transition

        self.memory_counter += 1

5)选行为

class DeepQNetwork:
    def __init__(self):
        ...

    def store_transition(self, s, a, r, s_):
        ...

    def choose_action(self, observation):

        observation = observation[np.newaxis, :]

        if np.random.uniform() < self.epsilon:

            actions_value = self.sess.run(self.q_eval, feed_dict={self.s: observation})
            action = np.argmax(actions_value)
        else:
            action = np.random.randint(0, self.n_actions)
        return action

6)学习

class DeepQNetwork:
    def __init__(self):
        ...

    def store_transition(self, s, a, r, s_):
        ...

    def choose_action(self, observation):
        ...

    def _replace_target_params(self):
        ...

    def learn(self):

        if self.learn_step_counter % self.replace_target_iter == 0:
            self.sess.run(self.replace_target_op)
            print('\ntarget_params_replaced\n')

        if self.memory_counter > self.memory_size:
            sample_index = np.random.choice(self.memory_size, size=self.batch_size)
        else:
            sample_index = np.random.choice(self.memory_counter, size=self.batch_size)
        batch_memory = self.memory[sample_index, :]

        q_next, q_eval = self.sess.run(
            [self.q_next, self.q_eval],
            feed_dict={
                self.s_: batch_memory[:, -self.n_features:],
                self.s: batch_memory[:, :self.n_features]
            })

        q_target = q_eval.copy()
        batch_index = np.arange(self.batch_size, dtype=np.int32)
        eval_act_index = batch_memory[:, self.n_features].astype(int)
        reward = batch_memory[:, self.n_features + 1]

        q_target[batch_index, eval_act_index] = reward + self.gamma * np.max(q_next, axis=1)

"""
        假如在这个 batch 中, 我们有2个提取的记忆, 根据每个记忆可以生产3个 action 的值:
        q_eval =
        [[1, 2, 3],
         [4, 5, 6]]

        q_target = q_eval =
        [[1, 2, 3],
         [4, 5, 6]]

        然后根据 memory 当中的具体 action 位置来修改 q_target 对应 action 上的值:
        比如在:
            记忆 0 的 q_target 计算值是 -1, 而且我用了 action 0;
            记忆 1 的 q_target 计算值是 -2, 而且我用了 action 2:
        q_target =
        [[-1, 2, 3],
         [4, 5, -2]]

        所以 (q_target - q_eval) 就变成了:
        [[(-1)-(1), 0, 0],
         [0, 0, (-2)-(6)]]

        最后我们将这个 (q_target - q_eval) 当成误差, 反向传递会神经网络.

        所有为 0 的 action 值是当时没有选择的 action, 之前有选择的 action 才有不为0的值.

        我们只反向传递之前选择的 action 的值,
"""

        _, self.cost = self.sess.run([self._train_op, self.loss],
                                     feed_dict={self.s: batch_memory[:, :self.n_features],
                                                self.q_target: q_target})
        self.cost_his.append(self.cost)

        self.epsilon = self.epsilon + self.epsilon_increment if self.epsilon < self.epsilon_max else self.epsilon_max
        self.learn_step_counter += 1

7)看学习效果

class DeepQNetwork:
    def __init__(self):
        ...

    def store_transition(self, s, a, r, s_):
        ...

    def choose_action(self, observation):
        ...

    def _replace_target_params(self):
        ...

    def learn(self):
        ...

    def plot_cost(self):
        import matplotlib.pyplot as plt
        plt.plot(np.arange(len(self.cost_his)), self.cost_his)
        plt.ylabel('Cost')
        plt.xlabel('training steps')
        plt.show()

深度强化学习--Deep Q Network(using tensorflow)

Original: https://blog.csdn.net/qq_52654678/article/details/121895814
Author: 旺仔不涨价
Title: 深度强化学习–Deep Q Network(using tensorflow)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/511984/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球