Deep Q-Network 学习笔记(六)—— 改进④:dueling dqn
这篇同样是完全没看懂 Orz,这里只做实现记录。。
要改动的地方只是在神经网络的最后一层做下调整即可。
def create(self): neuro_layer_1 = 3 w_init = tf.random_normal_initializer(0, 0.3) b_init = tf.constant_initializer(0.1) # -------------- 创建 eval 神经网络, 及时提升参数 -------------- # self.q_eval_input = tf.placeholder(shape=[None, self.INPUT_NUM], dtype=tf.float32, name="q_eval_input") self.q_eval_target = tf.placeholder(shape=[None, self.OUTPUT_NUM], dtype=tf.float32, name="q_target") def _dueling_builder(input_data, name, nl_1, w_initializer, b_initializer): with tf.variable_scope('l1'): w1 = tf.get_variable('w1', [self.INPUT_NUM, nl_1], initializer=w_initializer, collections=name) b1 = tf.get_variable('b1', [1, nl_1], initializer=b_initializer, collections=name) l1 = tf.nn.relu(tf.matmul(input_data, w1) + b1) with tf.variable_scope('Value'): w2 = tf.get_variable('w2', [nl_1, 1], initializer=w_initializer, collections=name) b2 = tf.get_variable('b2', [1, 1], initializer=b_initializer, collections=name) self.V = tf.matmul(l1, w2) + b2 with tf.variable_scope('Advantage'): w2 = tf.get_variable('w2', [nl_1, self.OUTPUT_NUM], initializer=w_initializer, collections=name) b2 = tf.get_variable('b2', [1, self.OUTPUT_NUM], initializer=b_initializer, collections=name) self.A = tf.matmul(l1, w2) + b2 with tf.variable_scope('Q'): out = self.V + (self.A - tf.reduce_mean(self.A, axis=1, keep_dims=True)) return out with tf.variable_scope("eval_net"): q_name = ['eval_net_params', tf.GraphKeys.GLOBAL_VARIABLES] self.q_eval_output = _dueling_builder(self.q_eval_input, q_name, neuro_layer_1, w_init, b_init) self.q_predict = tf.argmax(self.q_eval_output, 1) with tf.variable_scope('loss'): self.loss = tf.reduce_mean(tf.squared_difference(self.q_eval_target, self.q_eval_output)) with tf.variable_scope('train'): self.train_op = tf.train.GradientDescentOptimizer(self.learning_rate).minimize(self.loss) # -------------- 创建 target 神经网络, 及时提升参数 -------------- # self.q_target_input = tf.placeholder(shape=[None, self.INPUT_NUM], dtype=tf.float32, name="q_target_input") with tf.variable_scope("target_net"): t_name = ['target_net_params', tf.GraphKeys.GLOBAL_VARIABLES] self.q_target_output = _dueling_builder(self.q_target_input, t_name, neuro_layer_1, w_init, b_init)
完整代码
优先采样:
import numpy as np import random class SumTree: write = 0 def __init__(self, capacity): # 容量。 self.capacity = capacity self.tree = np.zeros(2 * capacity - 1) self.data = np.zeros(capacity, dtype=object) def _propagate(self, idx, change): # 整数除法。 parent = (idx - 1) // 2 self.tree[parent] += change if parent != 0: self._propagate(parent, change) def _retrieve(self, idx, s): """ 示例: 样本数(sample num):4 个,则 Tree 的 size 是:4 X 2 - 1 = 7。 其中,size 的后 sample num 个是单个样本的优先级,其它的是父级,即:父级的优先级=SUM(子级的优先级)。 如果 data = ["test1", "test2", "test3", "test4"],它们单个的优先级对应的就是下图中的“3, 4, 5, 6”。 Tree structure and array storage: Tree index: 18 -> storing priority sum / \ 7 11 / \ / \ 3 4 5 6 -> storing priority for transitions Array type for storing: [18,7,11,3,4,5,6] """ left = 2 * idx + 1 right = left + 1 if left >= len(self.tree): return idx if s <= self.tree[left]: return self._retrieve(left, s) else: return self._retrieve(right, s - self.tree[left]) def total(self): return self.tree[0] def add(self, p, data): idx = self.write + self.capacity - 1 self.data[self.write] = data self.update(idx, p) self.write += 1 if self.write >= self.capacity: self.write = 0 def update(self, idx, p): change = p - self.tree[idx] self.tree[idx] = p self._propagate(idx, change) def get(self, s): idx = self._retrieve(0, s) data_index = idx - self.capacity + 1 return idx, self.tree[idx], self.data[data_index] class Memory: e = 0.01 a = 0.6 def __init__(self, capacity): self.tree = SumTree(capacity) def _get_priority(self, error): return (error + self.e) ** self.a def add(self, error, sample): p = self._get_priority(error) self.tree.add(p, sample) def sample(self, n): batch = [] # 计算样本抽取的区间。 segment = self.tree.total() / n for i in range(n): # 第 i 个样本抽取区间的开始序号。 a = segment * i # 第 i 个样本抽取区间的结束序号。 b = segment * (i + 1) # 在(a, b)的区间内随机选数。 s = random.uniform(a, b) # 根据 s 来抽取,并将数据(数据序号,优先级,数据)返回。 (idx, p, data) = self.tree.get(s) # 将样本增加到集合里。 batch.append((idx, data)) return batch def update(self, idx, error): p = self._get_priority(error) self.tree.update(idx, p) if __name__ == "__main__": memory = Memory(4) memory.add(1, "test1") print(memory.tree.data) print(memory.tree.tree) memory.add(1, "test2") print(memory.tree.data) print(memory.tree.tree) memory.add(2, "test3") memory.add(3, "test4") samples, sample_index, i_s_weight = memory.sample(2) print("samples:", samples) print("sample_index:", sample_index) print("Importance-Sampling Weight", i_s_weight)
神经网络:
import tensorflow as tf import numpy as np class DeepQNetwork: # q_eval 网络状态输入参数。 q_eval_input = None # q_eval 网络中 q_target 的输入参数。 q_eval_target = None # q_eval 网络输出结果。 q_eval_output = None # q_eval 网络输出的结果中的最优得分。 q_predict = None # q_eval 网络输出的结果中当前选择的动作得分。 reward_action = None # q_eval 网络损失函数。 loss = None # q_eval 网络训练。 train_op = None # q_target 网络状态输入参数。 q_target_input = None # q_target 网络输出结果。 q_target_output = None # 更换 target_net 的步数。 replace_target_stepper = 0 V = None A = None def __init__(self, input_num, output_num, learning_rate=0.001, replace_target_stepper=300, session=None): self.learning_rate = learning_rate self.INPUT_NUM = input_num self.OUTPUT_NUM = output_num self.replace_target_stepper = replace_target_stepper self.create() if session is None: self.session = tf.InteractiveSession() self.session.run(tf.initialize_all_variables()) else: self.session = session def create(self): neuro_layer_1 = 3 w_init = tf.random_normal_initializer(0, 0.3) b_init = tf.constant_initializer(0.1) # -------------- 创建 eval 神经网络, 及时提升参数 -------------- # self.q_eval_input = tf.placeholder(shape=[None, self.INPUT_NUM], dtype=tf.float32, name="q_eval_input") self.q_eval_target = tf.placeholder(shape=[None, self.OUTPUT_NUM], dtype=tf.float32, name="q_target") def _dueling_builder(input_data, name, nl_1, w_initializer, b_initializer): with tf.variable_scope('l1'): w1 = tf.get_variable('w1', [self.INPUT_NUM, nl_1], initializer=w_initializer, collections=name) b1 = tf.get_variable('b1', [1, nl_1], initializer=b_initializer, collections=name) l1 = tf.nn.relu(tf.matmul(input_data, w1) + b1) with tf.variable_scope('Value'): w2 = tf.get_variable('w2', [nl_1, 1], initializer=w_initializer, collections=name) b2 = tf.get_variable('b2', [1, 1], initializer=b_initializer, collections=name) self.V = tf.matmul(l1, w2) + b2 with tf.variable_scope('Advantage'): w2 = tf.get_variable('w2', [nl_1, self.OUTPUT_NUM], initializer=w_initializer, collections=name) b2 = tf.get_variable('b2', [1, self.OUTPUT_NUM], initializer=b_initializer, collections=name) self.A = tf.matmul(l1, w2) + b2 with tf.variable_scope('Q'): out = self.V + (self.A - tf.reduce_mean(self.A, axis=1, keep_dims=True)) return out with tf.variable_scope("eval_net"): q_name = ['eval_net_params', tf.GraphKeys.GLOBAL_VARIABLES] self.q_eval_output = _dueling_builder(self.q_eval_input, q_name, neuro_layer_1, w_init, b_init) self.q_predict = tf.argmax(self.q_eval_output, 1) with tf.variable_scope('loss'): self.loss = tf.reduce_mean(tf.squared_difference(self.q_eval_target, self.q_eval_output)) with tf.variable_scope('train'): self.train_op = tf.train.GradientDescentOptimizer(self.learning_rate).minimize(self.loss) # -------------- 创建 target 神经网络, 及时提升参数 -------------- # self.q_target_input = tf.placeholder(shape=[None, self.INPUT_NUM], dtype=tf.float32, name="q_target_input") with tf.variable_scope("target_net"): t_name = ['target_net_params', tf.GraphKeys.GLOBAL_VARIABLES] self.q_target_output = _dueling_builder(self.q_target_input, t_name, neuro_layer_1, w_init, b_init) def replace_target_params(self): """ 使用 Tensorflow 中的 assign 功能替换 target_net 所有参数。 :return: """ # 提取 target_net 的参数。 t_params = tf.get_collection('target_net_params') # 提取 eval_net 的参数。 e_params = tf.get_collection('eval_net_params') # 更新 target_net 参数。 self.session.run([tf.assign(t, e) for t, e in zip(t_params, e_params)]) def get_q(self, input_data): return self.session.run(self.q_eval_output, {self.q_eval_input: input_data}) def get_next_q(self, input_data): return self.session.run(self.q_target_output, {self.q_target_input: input_data}) def get_predict(self, input_data): return np.max(self.get_q(input_data)) def get_action(self, input_data): return np.argmax(self.get_q(input_data)) def train(self, input_data, y_): _, cost = self.session.run([self.train_op, self.loss], feed_dict={self.q_eval_input: input_data, self.q_eval_target: y_}) return cost
主逻辑功能:
import numpy as np from collections import deque import random from q_network import DeepQNetwork from tree import Memory class Agent: 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], ]) # 神经网络。 network = None def __init__(self, train_num=2000): # 执行步数。 self.step_index = 0 # 状态数。 self.STATE_NUM = 6 # 动作数。 self.ACTION_NUM = 6 # 记忆上限。 self.memory_size = 5000 # 当前记忆数。 self.memory_counter = 0 self.replay_memory_store = Memory(self.memory_size) # 训练之前观察多少步。 self.OBSERVE = 5000 self.TRAIN_NUM = train_num # 训练步数统计。 self.learn_step_counter = 0 # 选取的小批量训练样本数。 self.BATCH = 20 # γ经验折损率。 self.gamma = 0.9 # -------------------- 探索策略 -------------------- # # epsilon 的最小值,当 epsilon 小于该值时,将不在随机选择行为。 self.FINAL_EPSILON = 0.0001 # epsilon 的初始值,epsilon 逐渐减小。 self.INITIAL_EPSILON = 0.1 # epsilon 衰减的总步数。 self.EXPLORE = 3000000. # 探索模式计数。 self.epsilon = 0 # -------------------- 探索策略 -------------------- # # 生成神经网络。 self.network = DeepQNetwork(input_num=self.STATE_NUM, output_num=self.ACTION_NUM, learning_rate=0.001, replace_target_stepper=1000, session=None) # 生成一个状态矩阵(6 X 6),每一行代表一个状态。 self.state_list = np.identity(self.STATE_NUM) # 生成一个动作矩阵。 self.action_list = np.identity(self.ACTION_NUM) # 输出图表。 self.r_list = [0] def select_action(self, current_state_index): """ 根据策略选择动作。 :param current_state_index: :return: """ # 获得当前状态。 current_state = self.state_list[current_state_index:current_state_index + 1] # 根据当前状态获得在 Q 网络中最有价值的动作,并返回动作序号。 current_action_index = self.network.get_action(current_state) if np.random.uniform() < self.epsilon: current_action_index = np.random.randint(0, self.ACTION_NUM) # 开始训练后,在 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] # 保存数据(当前状态, 当前执行的动作, 当前动作的得分,下一个状态,是否结束)。 memory_data = (current_state, current_action, current_reward, next_state, done) x, y, errors = self._get_targets([(0, memory_data)]) self.replay_memory_store.add(errors[0], memory_data) self.memory_counter += 1 def run_game(self, state_index, action_index): """ 执行动作。 :param state_index: 当前状态。 :param action_index: 执行的动作。 :return: """ reward = self.r[state_index][action_index] next_state = action_index done = False if action_index == 5: done = True return next_state, reward, done def experience_replay(self): """ 记忆回放。 :return: """ # 检查是否替换 target_net 参数 if self.learn_step_counter % self.network.replace_target_stepper == 0: self.network.replace_target_params() # 随机选择一小批记忆样本。 batch = self.BATCH if self.memory_counter > self.BATCH else self.memory_counter minibatch = self.replay_memory_store.sample(batch) x, y, errors = self._get_targets(minibatch) # update errors for i in range(len(minibatch)): idx = minibatch[i][0] self.replay_memory_store.update(idx, errors[i]) self.network.train(x, y) self.learn_step_counter += 1 def _get_targets(self, batch): # (当前状态,当前动作,当前得分,下一个状态) = (s, a, r, s_)。 current_states = np.vstack([o[1][0] for o in batch]) current_actions = np.vstack([o[1][1] for o in batch]) current_rewards = np.vstack([o[1][2] for o in batch]) next_states = np.vstack([o[1][3] for o in batch]) # 当前状态在 Q 网络的得分。 p = self.network.get_q(current_states) # 下一状态在 Q 网络的得分。 p_ = self.network.get_q(next_states) # 下一状态在 Target 网络的得分。 p_target = self.network.get_next_q(next_states) x = np.zeros((len(batch), self.STATE_NUM)) y = np.zeros((len(batch), self.ACTION_NUM)) errors = np.zeros(len(batch)) for i in range(len(batch)): s = current_states[i] a = current_actions[i] r = current_rewards[i][0] s_ = next_states[i] a_index = np.argmax(a) # 获得第 i 个样本当前状态的所有动作的 Q 值表。 target_q = p[i] # 获得第 i 个样本当前动作的 Q 得分。 current_q = target_q[a_index] max_q = p_target[i][np.argmax(p_[i])] if r <= -1 or s_ is None: target_q[a_index] = r else: target_q[a_index] = r + self.gamma * max_q x[i] = s y[i] = target_q errors[i] = abs(current_q - target_q[a_index]) return x, y, errors 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.run_game(current_state, action) test_reward = reward / 10 self.r_list.append(test_reward + self.r_list[-1]) # 保存记忆。 self.save_store(current_state, action, reward, next_state, done) # 先观察一段时间累积足够的记忆在进行训练。 if self.step_index > self.OBSERVE: self.experience_replay() if self.step_index - self.OBSERVE > self.TRAIN_NUM: 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: next_state = self.network.get_action(self.state_list[current_state:current_state + 1]) print("Agent 由", current_state, "号房间移动到了", next_state, "号房间") current_state = next_state step += 1 print("Agent 在", start_room, "号房间开始移动了", step, "步到达了目标房间 5") print("#############################", "Agent 在", 5, "结束行动", "#############################") def show_plt(self): import matplotlib.pyplot as plt plt.plot(np.array(self.r_list), c='r', label='dueling') # plt.plot(np.array(q_double), c='b', label='double') plt.legend(loc='best') plt.ylabel('accumulated reward') plt.xlabel('training steps') plt.grid() plt.show() if __name__ == "__main__": agent = Agent(train_num=20000) agent.pay() agent.show_plt()