class DuelingDQN: def __init__(self, …, dueling True, sess None): self.dueling dueling if sess is None: self.sess tf.Session() self.sess.run(tf.global_variables_initializer()) else: self.sess sess #省略
def _build_net(self): def build_layers(s, c_names, n_l1, w_initializer, b_initializer): 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(s, w1) b1) if self.dueling: # Dueling DQN with tf.variable_scope( Value ): # 专门分析state的Value w2 tf.get_variable( w2 , [n_l1, 1], initializer w_initializer, collections c_names) b2 tf.get_variable( b2 , [1, 1], initializer b_initializer, collections c_names) self.V tf.matmul(l1, w2) b2 with tf.variable_scope( Advantage ): # 专门分析每种动作的Advantage 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.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)) else: with tf.variable_scope( Q ): #普通的DQN第二层 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) out tf.matmul(l1, w2) b2 return out



