|
| 1 | +import tensorflow as tf |
| 2 | +import numpy as np |
| 3 | +from .utils import layer |
| 4 | +import time |
| 5 | + |
| 6 | +class Critic(): |
| 7 | + |
| 8 | + def __init__(self, sess, env, layer_number, FLAGS, learning_rate=0.001, gamma=0.98, tau=0.05): |
| 9 | + self.sess = sess |
| 10 | + self.critic_name = 'critic_' + str(layer_number) + str(time.time()) |
| 11 | + self.learning_rate = learning_rate |
| 12 | + self.gamma = gamma |
| 13 | + self.tau = tau |
| 14 | + |
| 15 | + self.q_limit = -FLAGS.time_scale |
| 16 | + |
| 17 | + # Dimensions of goal placeholder will differ depending on layer level |
| 18 | + if layer_number == FLAGS.layers - 1: |
| 19 | + self.goal_dim = env.end_goal_dim |
| 20 | + else: |
| 21 | + self.goal_dim = env.subgoal_dim |
| 22 | + |
| 23 | + self.loss_val = 0 |
| 24 | + self.state_dim = env.state_dim |
| 25 | + self.state_ph = tf.placeholder(tf.float32, shape=(None, env.state_dim), name=self.critic_name + 'state_ph') |
| 26 | + self.goal_ph = tf.placeholder(tf.float32, shape=(None, self.goal_dim)) |
| 27 | + |
| 28 | + |
| 29 | + # Dimensions of action placeholder will differ depending on layer level |
| 30 | + if layer_number == 0: |
| 31 | + action_dim = env.action_dim |
| 32 | + else: |
| 33 | + action_dim = env.subgoal_dim |
| 34 | + |
| 35 | + self.action_ph = tf.placeholder(tf.float32, shape=(None, action_dim), name=self.critic_name + 'action_ph') |
| 36 | + |
| 37 | + self.features_ph = tf.concat([self.state_ph, self.goal_ph, self.action_ph], axis=1) |
| 38 | + |
| 39 | + # Set parameters to give critic optimistic initialization near q_init |
| 40 | + self.q_init = -0.067 |
| 41 | + self.q_offset = -np.log(self.q_limit/self.q_init - 1) |
| 42 | + |
| 43 | + # Create critic network graph |
| 44 | + self.infer = self.create_nn(self.features_ph, self.critic_name) |
| 45 | + self.weights = [v for v in tf.trainable_variables() if self.critic_name in v.op.name] |
| 46 | + |
| 47 | + # Create target critic network graph. Please note that by default the critic networks are not used and updated. To use critic networks please follow instructions in the "update" method in this file and the "learn" method in the "layer.py" file. |
| 48 | + |
| 49 | + # Target network code "repurposed" from Patrick Emani :^) |
| 50 | + self.target = self.create_nn(self.features_ph, name=self.critic_name + '_target') |
| 51 | + self.target_weights = [v for v in tf.trainable_variables() if self.critic_name in v.op.name][len(self.weights):] |
| 52 | + |
| 53 | + self.update_target_weights = \ |
| 54 | + [self.target_weights[i].assign(tf.multiply(self.weights[i], self.tau) + |
| 55 | + tf.multiply(self.target_weights[i], 1. - self.tau)) |
| 56 | + for i in range(len(self.target_weights))] |
| 57 | + |
| 58 | + self.wanted_qs = tf.placeholder(tf.float32, shape=(None, 1)) # 期望 |
| 59 | + |
| 60 | + self.loss = tf.reduce_mean(tf.square(self.wanted_qs - self.infer)) |
| 61 | + |
| 62 | + self.train = tf.train.AdamOptimizer(learning_rate).minimize(self.loss) |
| 63 | + |
| 64 | + self.gradient = tf.gradients(self.infer, self.action_ph) |
| 65 | + |
| 66 | + |
| 67 | + def get_Q_value(self,state, goal, action): |
| 68 | + return self.sess.run(self.infer, |
| 69 | + feed_dict={ |
| 70 | + self.state_ph: state, |
| 71 | + self.goal_ph: goal, |
| 72 | + self.action_ph: action |
| 73 | + })[0] |
| 74 | + |
| 75 | + def get_target_Q_value(self,state, goal, action): |
| 76 | + return self.sess.run(self.target, |
| 77 | + feed_dict={ |
| 78 | + self.state_ph: state, |
| 79 | + self.goal_ph: goal, |
| 80 | + self.action_ph: action |
| 81 | + })[0] |
| 82 | + |
| 83 | + |
| 84 | + def update(self, old_states, old_actions, rewards, new_states, goals, new_actions, is_terminals): |
| 85 | + |
| 86 | + # Be default, repo does not use target networks. To use target networks, comment out "wanted_qs" line directly below and uncomment next "wanted_qs" line. This will let the Bellman update use Q(next state, action) from target Q network instead of the regular Q network. Make sure you also make the updates specified in the "learn" method in the "layer.py" file. |
| 87 | + wanted_qs = self.sess.run(self.infer, |
| 88 | + feed_dict={ |
| 89 | + self.state_ph: new_states, |
| 90 | + self.goal_ph: goals, |
| 91 | + self.action_ph: new_actions |
| 92 | + }) |
| 93 | + |
| 94 | + """ |
| 95 | + # Uncomment to use target networks |
| 96 | + wanted_qs = self.sess.run(self.target, |
| 97 | + feed_dict={ |
| 98 | + self.state_ph: new_states, |
| 99 | + self.goal_ph: goals, |
| 100 | + self.action_ph: new_actions |
| 101 | + }) |
| 102 | + """ |
| 103 | + |
| 104 | + for i in range(len(wanted_qs)): |
| 105 | + if is_terminals[i]: |
| 106 | + wanted_qs[i] = rewards[i] |
| 107 | + else: |
| 108 | + wanted_qs[i] = rewards[i] + self.gamma * wanted_qs[i][0] |
| 109 | + |
| 110 | + # Ensure Q target is within bounds [-self.time_limit,0] |
| 111 | + wanted_qs[i] = max(min(wanted_qs[i],0), self.q_limit) |
| 112 | + assert wanted_qs[i] <= 0 and wanted_qs[i] >= self.q_limit, "Q-Value target not within proper bounds" |
| 113 | + |
| 114 | + self.loss_val, _ = self.sess.run([self.loss, self.train], |
| 115 | + feed_dict={ |
| 116 | + self.state_ph: old_states, |
| 117 | + self.goal_ph: goals, |
| 118 | + self.action_ph: old_actions, |
| 119 | + self.wanted_qs: wanted_qs |
| 120 | + }) |
| 121 | + |
| 122 | + def get_gradients(self, state, goal, action): |
| 123 | + grads = self.sess.run(self.gradient, |
| 124 | + feed_dict={ |
| 125 | + self.state_ph: state, |
| 126 | + self.goal_ph: goal, |
| 127 | + self.action_ph: action |
| 128 | + }) |
| 129 | + |
| 130 | + return grads[0] |
| 131 | + |
| 132 | + # Function creates the graph for the critic function. The output uses a sigmoid, which bounds the Q-values to between [-Policy Length, 0]. |
| 133 | + def create_nn(self, features, name=None): |
| 134 | + |
| 135 | + if name is None: |
| 136 | + name = self.critic_name |
| 137 | + |
| 138 | + with tf.variable_scope(name + '_fc_1'): |
| 139 | + fc1 = layer(features, 64) |
| 140 | + # with tf.variable_scope(name + '_fc_2'): |
| 141 | + # fc2 = layer(fc1, 64) |
| 142 | + # with tf.variable_scope(name + '_fc_3'): |
| 143 | + # fc3 = layer(fc2, 64) |
| 144 | + with tf.variable_scope(name + '_fc_4'): |
| 145 | + fc4 = layer(fc1, 1, is_output=True) |
| 146 | + |
| 147 | + # A q_offset is used to give the critic function an optimistic initialization near 0 |
| 148 | + output = tf.sigmoid(fc4 + self.q_offset) * self.q_limit |
| 149 | + |
| 150 | + return output |
0 commit comments