class DDPG(object): @store_args def __init__(self, input_dims, buffer_size, hidden, layers, network_class_actor_critic, network_class_discriminator, polyak, batch_size, Q_lr, pi_lr, mi_lr, sk_lr, r_scale, mi_r_scale, sk_r_scale, et_r_scale, norm_eps, norm_clip, max_u, action_l2, clip_obs, scope, T, rollout_batch_size, subtract_goals, relative_goals, clip_pos_returns, clip_return, sample_transitions, gamma, env_name, max_timesteps, pretrain_weights, finetune_pi, mi_prioritization, sac, reuse=False, history_len=10000, **kwargs): """Implementation of DDPG that is used in combination with Hindsight Experience Replay (HER). Args: input_dims (dict of ints): dimensions for the observation (o), the goal (g), and the actions (u) buffer_size (int): number of transitions that are stored in the replay buffer hidden (int): number of units in the hidden layers layers (int): number of hidden layers network_class (str): the network class that should be used (e.g. 'baselines.her.ActorCritic') polyak (float): coefficient for Polyak-averaging of the target network batch_size (int): batch size for training Q_lr (float): learning rate for the Q (critic) network pi_lr (float): learning rate for the pi (actor) network norm_eps (float): a small value used in the normalizer to avoid numerical instabilities norm_clip (float): normalized inputs are clipped to be in [-norm_clip, norm_clip] max_u (float): maximum action magnitude, i.e. actions are in [-max_u, max_u] action_l2 (float): coefficient for L2 penalty on the actions clip_obs (float): clip observations before normalization to be in [-clip_obs, clip_obs] scope (str): the scope used for the TensorFlow graph T (int): the time horizon for rollouts rollout_batch_size (int): number of parallel rollouts per DDPG agent subtract_goals (function): function that subtracts goals from each other relative_goals (boolean): whether or not relative goals should be fed into the network clip_pos_returns (boolean): whether or not positive returns should be clipped clip_return (float): clip returns to be in [-clip_return, clip_return] sample_transitions (function) function that samples from the replay buffer gamma (float): gamma used for Q learning updates reuse (boolean): whether or not the networks should be reused """ if self.clip_return is None: self.clip_return = np.inf self.create_actor_critic = import_function( self.network_class_actor_critic) self.create_discriminator = import_function( self.network_class_discriminator) input_shapes = dims_to_shapes(self.input_dims) self.dimo = self.input_dims['o'] self.dimz = self.input_dims['z'] self.dimg = self.input_dims['g'] self.dimu = self.input_dims['u'] self.env_name = env_name # Prepare staging area for feeding data to the model. stage_shapes = OrderedDict() for key in sorted(self.input_dims.keys()): if key.startswith('info_'): continue stage_shapes[key] = (None, *input_shapes[key]) for key in ['o', 'g']: stage_shapes[key + '_2'] = stage_shapes[key] stage_shapes['r'] = (None, ) stage_shapes['w'] = (None, ) stage_shapes['m'] = (None, ) stage_shapes['s'] = (None, ) stage_shapes['m_w'] = () stage_shapes['s_w'] = () stage_shapes['r_w'] = () stage_shapes['e_w'] = () self.stage_shapes = stage_shapes # Create network. with tf.variable_scope(self.scope): self.staging_tf = StagingArea( dtypes=[tf.float32 for _ in self.stage_shapes.keys()], shapes=list(self.stage_shapes.values())) self.buffer_ph_tf = [ tf.placeholder(tf.float32, shape=shape) for shape in self.stage_shapes.values() ] self.stage_op = self.staging_tf.put(self.buffer_ph_tf) self._create_network(pretrain_weights, mi_prioritization, reuse=reuse) # Configure the replay buffer. buffer_shapes = { key: (self.T if key != 'o' else self.T + 1, *input_shapes[key]) for key, val in input_shapes.items() } buffer_shapes['g'] = (buffer_shapes['g'][0], self.dimg) buffer_shapes['ag'] = (self.T + 1, self.dimg) buffer_size = (self.buffer_size // self.rollout_batch_size) * self.rollout_batch_size self.buffer = ReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions, mi_prioritization) self.mi_r_history = deque(maxlen=history_len) self.gl_r_history = deque(maxlen=history_len) self.sk_r_history = deque(maxlen=history_len) self.et_r_history = deque(maxlen=history_len) self.mi_current = 0 self.finetune_pi = finetune_pi def _random_action(self, n): return np.random.uniform(low=-self.max_u, high=self.max_u, size=(n, self.dimu)) def _preprocess_og(self, o, ag, g): if self.relative_goals: g_shape = g.shape g = g.reshape(-1, self.dimg) ag = ag.reshape(-1, self.dimg) g = self.subtract_goals(g, ag) g = g.reshape(*g_shape) o = np.clip(o, -self.clip_obs, self.clip_obs) g = np.clip(g, -self.clip_obs, self.clip_obs) return o, g def get_actions(self, o, z, ag, g, noise_eps=0., random_eps=0., use_target_net=False, compute_Q=False): o, g = self._preprocess_og(o, ag, g) policy = self.target if use_target_net else self.main # values to compute if self.sac: vals = [policy.mu_tf] else: vals = [policy.pi_tf] if compute_Q: vals += [policy.Q_pi_tf] feed = { policy.o_tf: o.reshape(-1, self.dimo), policy.z_tf: z.reshape(-1, self.dimz), policy.g_tf: g.reshape(-1, self.dimg), policy.u_tf: np.zeros((o.size // self.dimo, self.dimu), dtype=np.float32) } ret = self.sess.run(vals, feed_dict=feed) # action postprocessing u = ret[0] noise = noise_eps * self.max_u * np.random.randn( *u.shape) # gaussian noise u += noise u = np.clip(u, -self.max_u, self.max_u) u += np.random.binomial(1, random_eps, u.shape[0]).reshape(-1, 1) * ( self._random_action(u.shape[0]) - u) # eps-greedy if u.shape[0] == 1: u = u[0] u = u.copy() ret[0] = u if len(ret) == 1: return ret[0] else: return ret def store_episode(self, episode_batch, update_stats=True): """ episode_batch: array of batch_size x (T or T+1) x dim_key 'o' is of size T+1, others are of size T """ # update the mutual information reward into the episode batch episode_batch['m'] = np.empty([episode_batch['o'].shape[0], 1]) episode_batch['s'] = np.empty([episode_batch['o'].shape[0], 1]) # # self.buffer.store_episode(episode_batch, self) if update_stats: # add transitions to normalizer episode_batch['o_2'] = episode_batch['o'][:, 1:, :] episode_batch['ag_2'] = episode_batch['ag'][:, 1:, :] num_normalizing_transitions = transitions_in_episode_batch( episode_batch) transitions = self.sample_transitions(self, False, episode_batch, num_normalizing_transitions, 0, 0, 0) o, o_2, g, ag = transitions['o'], transitions['o_2'], transitions[ 'g'], transitions['ag'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) self.o_stats.update(transitions['o']) self.g_stats.update(transitions['g']) self.o_stats.recompute_stats() self.g_stats.recompute_stats() def get_current_buffer_size(self): return self.buffer.get_current_size() def _sync_optimizers(self): self.Q_adam.sync() self.pi_adam.sync() self.mi_adam.sync() self.sk_adam.sync() def _grads_mi(self, data): mi, mi_grad = self.sess.run([ self.main_ir.mi_tf, self.mi_grad_tf, ], feed_dict={self.o_tau_tf: data}) return mi, mi_grad def _grads_sk(self, o_s_batch, z_s_batch): sk, sk_grad = self.sess.run([ self.main_ir.sk_tf, self.sk_grad_tf, ], feed_dict={ self.main_ir.o_tf: o_s_batch, self.main_ir.z_tf: z_s_batch }) return sk, sk_grad def _grads(self): critic_loss, actor_loss, Q_grad, pi_grad, neg_logp_pi, e_w = self.sess.run( [ self.Q_loss_tf, self.main.Q_pi_tf, self.Q_grad_tf, self.pi_grad_tf, self.main.neg_logp_pi_tf, self.e_w_tf, ]) return critic_loss, actor_loss, Q_grad, pi_grad, neg_logp_pi, e_w def _update_mi(self, mi_grad): self.mi_adam.update(mi_grad, self.mi_lr) def _update_sk(self, sk_grad): self.sk_adam.update(sk_grad, self.sk_lr) def _update(self, Q_grad, pi_grad): self.Q_adam.update(Q_grad, self.Q_lr) self.pi_adam.update(pi_grad, self.pi_lr) def sample_batch(self, ir, t): transitions = self.buffer.sample(self, ir, self.batch_size, self.mi_r_scale, self.sk_r_scale, t) weights = np.ones_like(transitions['r']).copy() if ir: self.mi_r_history.extend( ((np.clip((self.mi_r_scale * transitions['m']), *(0, 1)) - (1 if not self.mi_r_scale == 0 else 0)) * transitions['m_w']).tolist()) self.sk_r_history.extend( ((np.clip(self.sk_r_scale * transitions['s'], *(-1, 0))) * 1.00).tolist()) self.gl_r_history.extend(self.r_scale * transitions['r']) o, o_2, g = transitions['o'], transitions['o_2'], transitions['g'] ag, ag_2 = transitions['ag'], transitions['ag_2'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) transitions['o_2'], transitions['g_2'] = self._preprocess_og( o_2, ag_2, g) transitions['w'] = weights.flatten().copy() # note: ordered dict transitions_batch = [ transitions[key] for key in self.stage_shapes.keys() ] return transitions_batch def stage_batch(self, ir, t, batch=None): if batch is None: batch = self.sample_batch(ir, t) assert len(self.buffer_ph_tf) == len(batch) self.sess.run(self.stage_op, feed_dict=dict(zip(self.buffer_ph_tf, batch))) def run_mi(self, o_s): feed_dict = {self.o_tau_tf: o_s.copy()} neg_l = self.sess.run(self.main_ir.mi_tf, feed_dict=feed_dict) return neg_l def run_sk(self, o, z): feed_dict = {self.main_ir.o_tf: o, self.main_ir.z_tf: z} sk_r = self.sess.run(self.main_ir.sk_r_tf, feed_dict=feed_dict) return sk_r def train_mi(self, data, stage=True): mi, mi_grad = self._grads_mi(data) self._update_mi(mi_grad) self.mi_current = -mi.mean() return -mi.mean() def train_sk(self, o_s_batch, z_s_batch, stage=True): sk, sk_grad = self._grads_sk(o_s_batch, z_s_batch) self._update_sk(sk_grad) return -sk.mean() def train(self, t, stage=True): if not self.buffer.current_size == 0: if stage: self.stage_batch(ir=True, t=t) critic_loss, actor_loss, Q_grad, pi_grad, neg_logp_pi, e_w = self._grads( ) self._update(Q_grad, pi_grad) self.et_r_history.extend(((np.clip( (self.et_r_scale * neg_logp_pi), *(-1, 0))) * e_w).tolist()) return critic_loss, actor_loss def _init_target_net(self): self.sess.run(self.init_target_net_op) def update_target_net(self): self.sess.run(self.update_target_net_op) def clear_buffer(self): self.buffer.clear_buffer() def _vars(self, scope): res = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.scope + '/' + scope) assert len(res) > 0 return res def _global_vars(self, scope): res = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.scope + '/' + scope) return res def _create_network(self, pretrain_weights, mi_prioritization, reuse=False): if self.sac: logger.info("Creating a SAC agent with action space %d x %s..." % (self.dimu, self.max_u)) else: logger.info("Creating a DDPG agent with action space %d x %s..." % (self.dimu, self.max_u)) self.sess = tf.get_default_session() if self.sess is None: self.sess = tf.InteractiveSession() # running averages with tf.variable_scope('o_stats') as vs: if reuse: vs.reuse_variables() self.o_stats = Normalizer(self.dimo, self.norm_eps, self.norm_clip, sess=self.sess) with tf.variable_scope('g_stats') as vs: if reuse: vs.reuse_variables() self.g_stats = Normalizer(self.dimg, self.norm_eps, self.norm_clip, sess=self.sess) # mini-batch sampling. batch = self.staging_tf.get() batch_tf = OrderedDict([ (key, batch[i]) for i, key in enumerate(self.stage_shapes.keys()) ]) batch_tf['r'] = tf.reshape(batch_tf['r'], [-1, 1]) batch_tf['w'] = tf.reshape(batch_tf['w'], [-1, 1]) batch_tf['m'] = tf.reshape(batch_tf['m'], [-1, 1]) batch_tf['s'] = tf.reshape(batch_tf['s'], [-1, 1]) self.o_tau_tf = tf.placeholder(tf.float32, shape=(None, None, self.dimo)) # networks with tf.variable_scope('main') as vs: if reuse: vs.reuse_variables() self.main = self.create_actor_critic(batch_tf, net_type='main', **self.__dict__) vs.reuse_variables() with tf.variable_scope('target') as vs: if reuse: vs.reuse_variables() target_batch_tf = batch_tf.copy() target_batch_tf['o'] = batch_tf['o_2'] target_batch_tf['g'] = batch_tf['g_2'] self.target = self.create_actor_critic(target_batch_tf, net_type='target', **self.__dict__) vs.reuse_variables() assert len(self._vars("main")) == len(self._vars("target")) # intrinsic reward (ir) network for mutual information with tf.variable_scope('ir') as vs: if reuse: vs.reuse_variables() self.main_ir = self.create_discriminator(batch_tf, net_type='ir', **self.__dict__) vs.reuse_variables() # loss functions mi_grads_tf = tf.gradients(tf.reduce_mean(self.main_ir.mi_tf), self._vars('ir/state_mi')) assert len(self._vars('ir/state_mi')) == len(mi_grads_tf) self.mi_grads_vars_tf = zip(mi_grads_tf, self._vars('ir/state_mi')) self.mi_grad_tf = flatten_grads(grads=mi_grads_tf, var_list=self._vars('ir/state_mi')) self.mi_adam = MpiAdam(self._vars('ir/state_mi'), scale_grad_by_procs=False) sk_grads_tf = tf.gradients(tf.reduce_mean(self.main_ir.sk_tf), self._vars('ir/skill_ds')) assert len(self._vars('ir/skill_ds')) == len(sk_grads_tf) self.sk_grads_vars_tf = zip(sk_grads_tf, self._vars('ir/skill_ds')) self.sk_grad_tf = flatten_grads(grads=sk_grads_tf, var_list=self._vars('ir/skill_ds')) self.sk_adam = MpiAdam(self._vars('ir/skill_ds'), scale_grad_by_procs=False) target_Q_pi_tf = self.target.Q_pi_tf clip_range = (-self.clip_return, self.clip_return if self.clip_pos_returns else np.inf) self.e_w_tf = batch_tf['e_w'] if not self.sac: self.main.neg_logp_pi_tf = tf.zeros(1) target_tf = tf.clip_by_value( self.r_scale * batch_tf['r'] * batch_tf['r_w'] + (tf.clip_by_value(self.mi_r_scale * batch_tf['m'], *(0, 1)) - (1 if not self.mi_r_scale == 0 else 0)) * batch_tf['m_w'] + (tf.clip_by_value(self.sk_r_scale * batch_tf['s'], *(-1, 0))) * batch_tf['s_w'] + (tf.clip_by_value(self.et_r_scale * self.main.neg_logp_pi_tf, *(-1, 0))) * self.e_w_tf + self.gamma * target_Q_pi_tf, *clip_range) self.td_error_tf = tf.stop_gradient(target_tf) - self.main.Q_tf self.errors_tf = tf.square(self.td_error_tf) self.errors_tf = tf.reduce_mean(batch_tf['w'] * self.errors_tf) self.Q_loss_tf = tf.reduce_mean(self.errors_tf) self.pi_loss_tf = -tf.reduce_mean(self.main.Q_pi_tf) self.pi_loss_tf += self.action_l2 * tf.reduce_mean( tf.square(self.main.pi_tf / self.max_u)) Q_grads_tf = tf.gradients(self.Q_loss_tf, self._vars('main/Q')) pi_grads_tf = tf.gradients(self.pi_loss_tf, self._vars('main/pi')) assert len(self._vars('main/Q')) == len(Q_grads_tf) assert len(self._vars('main/pi')) == len(pi_grads_tf) self.Q_grads_vars_tf = zip(Q_grads_tf, self._vars('main/Q')) self.pi_grads_vars_tf = zip(pi_grads_tf, self._vars('main/pi')) self.Q_grad_tf = flatten_grads(grads=Q_grads_tf, var_list=self._vars('main/Q')) self.pi_grad_tf = flatten_grads(grads=pi_grads_tf, var_list=self._vars('main/pi')) # optimizers self.Q_adam = MpiAdam(self._vars('main/Q'), scale_grad_by_procs=False) self.pi_adam = MpiAdam(self._vars('main/pi'), scale_grad_by_procs=False) self.main_vars = self._vars('main/Q') + self._vars('main/pi') self.target_vars = self._vars('target/Q') + self._vars('target/pi') # polyak averaging self.stats_vars = self._global_vars('o_stats') + self._global_vars( 'g_stats') self.init_target_net_op = list( map(lambda v: v[0].assign(v[1]), zip(self.target_vars, self.main_vars))) self.update_target_net_op = list( map( lambda v: v[0].assign(self.polyak * v[0] + (1. - self.polyak) * v[1]), zip(self.target_vars, self.main_vars))) # initialize all variables tf.variables_initializer(self._global_vars('')).run() if pretrain_weights: load_weight(self.sess, pretrain_weights, ['state_mi']) if self.finetune_pi: load_weight(self.sess, pretrain_weights, ['main']) self._sync_optimizers() if pretrain_weights and self.finetune_pi: load_weight(self.sess, pretrain_weights, ['target']) else: self._init_target_net() def logs(self, prefix=''): logs = [] logs += [('stats_o/mean', np.mean(self.sess.run([self.o_stats.mean])))] logs += [('stats_o/std', np.mean(self.sess.run([self.o_stats.std])))] logs += [('stats_g/mean', np.mean(self.sess.run([self.g_stats.mean])))] logs += [('stats_g/std', np.mean(self.sess.run([self.g_stats.std])))] logs += [('mi_reward/mean', np.mean(self.mi_r_history))] logs += [('mi_reward/std', np.std(self.mi_r_history))] logs += [('mi_reward/max', np.max(self.mi_r_history))] logs += [('mi_reward/min', np.min(self.mi_r_history))] logs += [('mi_train/-neg_l', self.mi_current)] logs += [('sk_reward/mean', np.mean(self.sk_r_history))] logs += [('sk_reward/std', np.std(self.sk_r_history))] logs += [('sk_reward/max', np.max(self.sk_r_history))] logs += [('sk_reward/min', np.min(self.sk_r_history))] logs += [('et_reward/mean', np.mean(self.et_r_history))] logs += [('et_reward/std', np.std(self.et_r_history))] logs += [('et_reward/max', np.max(self.et_r_history))] logs += [('et_reward/min', np.min(self.et_r_history))] logs += [('gl_reward/mean', np.mean(self.gl_r_history))] logs += [('gl_reward/std', np.std(self.gl_r_history))] logs += [('gl_reward/max', np.max(self.gl_r_history))] logs += [('gl_reward/min', np.min(self.gl_r_history))] if prefix is not '' and not prefix.endswith('/'): return [(prefix + '/' + key, val) for key, val in logs] else: return logs def __getstate__(self): """Our policies can be loaded from pkl, but after unpickling you cannot continue training. """ excluded_subnames = [ '_tf', '_op', '_vars', '_adam', 'buffer', 'sess', '_stats', 'main', 'target', 'lock', 'sample_transitions', 'stage_shapes', 'create_actor_critic', 'create_discriminator', '_history' ] state = { k: v for k, v in self.__dict__.items() if all([not subname in k for subname in excluded_subnames]) } state['buffer_size'] = self.buffer_size state['tf'] = self.sess.run( [x for x in self._global_vars('') if 'buffer' not in x.name]) return state def __setstate__(self, state): if 'sample_transitions' not in state: # We don't need this for playing the policy. state['sample_transitions'] = None if 'env_name' not in state: state['env_name'] = 'FetchPickAndPlace-v1' if 'network_class_discriminator' not in state: state[ 'network_class_discriminator'] = 'baselines.her.discriminator:Discriminator' if 'mi_r_scale' not in state: state['mi_r_scale'] = 1 if 'mi_lr' not in state: state['mi_lr'] = 0.001 if 'sk_r_scale' not in state: state['sk_r_scale'] = 1 if 'sk_lr' not in state: state['sk_lr'] = 0.001 if 'et_r_scale' not in state: state['et_r_scale'] = 1 if 'finetune_pi' not in state: state['finetune_pi'] = None if 'no_train_mi' not in state: state['no_train_mi'] = None if 'load_weight' not in state: state['load_weight'] = None if 'pretrain_weights' not in state: state['pretrain_weights'] = None if 'mi_prioritization' not in state: state['mi_prioritization'] = None if 'sac' not in state: state['sac'] = None self.__init__(**state) # set up stats (they are overwritten in __init__) for k, v in state.items(): if k[-6:] == '_stats': self.__dict__[k] = v # load TF variables vars = [x for x in self._global_vars('') if 'buffer' not in x.name] assert (len(vars) == len(state["tf"])) node = [tf.assign(var, val) for var, val in zip(vars, state["tf"])] self.sess.run(node)
class DDPG(object): @store_args def __init__(self, input_dims, buffer_size, hidden, layers, network_class, polyak, batch_size, Q_lr, pi_lr, norm_eps, norm_clip, max_u, action_l2, clip_obs, scope, T, rollout_batch_size, subtract_goals, relative_goals, clip_pos_returns, clip_return, sample_transitions, gamma, temperature, prioritization, env_name, alpha, beta0, beta_iters, eps, max_timesteps, rank_method, reuse=False, **kwargs): """Implementation of DDPG that is used in combination with Hindsight Experience Replay (HER). Args: input_dims (dict of ints): dimensions for the observation (o), the goal (g), and the actions (u) buffer_size (int): number of transitions that are stored in the replay buffer hidden (int): number of units in the hidden layers layers (int): number of hidden layers network_class (str): the network class that should be used (e.g. 'baselines.her.ActorCritic') polyak (float): coefficient for Polyak-averaging of the target network batch_size (int): batch size for training Q_lr (float): learning rate for the Q (critic) network pi_lr (float): learning rate for the pi (actor) network norm_eps (float): a small value used in the normalizer to avoid numerical instabilities norm_clip (float): normalized inputs are clipped to be in [-norm_clip, norm_clip] max_u (float): maximum action magnitude, i.e. actions are in [-max_u, max_u] action_l2 (float): coefficient for L2 penalty on the actions clip_obs (float): clip observations before normalization to be in [-clip_obs, clip_obs] scope (str): the scope used for the TensorFlow graph T (int): the time horizon for rollouts rollout_batch_size (int): number of parallel rollouts per DDPG agent subtract_goals (function): function that subtracts goals from each other relative_goals (boolean): whether or not relative goals should be fed into the network clip_pos_returns (boolean): whether or not positive returns should be clipped clip_return (float): clip returns to be in [-clip_return, clip_return] sample_transitions (function) function that samples from the replay buffer gamma (float): gamma used for Q learning updates reuse (boolean): whether or not the networks should be reused """ if self.clip_return is None: self.clip_return = np.inf self.create_actor_critic = import_function(self.network_class) input_shapes = dims_to_shapes(self.input_dims) self.dimo = self.input_dims['o'] self.dimg = self.input_dims['g'] self.dimu = self.input_dims['u'] self.prioritization = prioritization self.env_name = env_name self.temperature = temperature self.rank_method = rank_method # Prepare staging area for feeding data to the model. stage_shapes = OrderedDict() for key in sorted(self.input_dims.keys()): if key.startswith('info_'): continue stage_shapes[key] = (None, *input_shapes[key]) for key in ['o', 'g']: stage_shapes[key + '_2'] = stage_shapes[key] stage_shapes['r'] = (None, ) stage_shapes['w'] = (None, ) self.stage_shapes = stage_shapes # Create network. with tf.variable_scope(self.scope): self.staging_tf = StagingArea( dtypes=[tf.float32 for _ in self.stage_shapes.keys()], shapes=list(self.stage_shapes.values())) self.buffer_ph_tf = [ tf.placeholder(tf.float32, shape=shape) for shape in self.stage_shapes.values() ] self.stage_op = self.staging_tf.put(self.buffer_ph_tf) self._create_network(reuse=reuse) # Configure the replay buffer. buffer_shapes = { key: (self.T if key != 'o' else self.T + 1, *input_shapes[key]) for key, val in input_shapes.items() } buffer_shapes['g'] = (buffer_shapes['g'][0], self.dimg) buffer_shapes['ag'] = (self.T + 1, self.dimg) buffer_size = (self.buffer_size // self.rollout_batch_size) * self.rollout_batch_size if self.prioritization == 'entropy': self.buffer = ReplayBufferEntropy(buffer_shapes, buffer_size, self.T, self.sample_transitions, self.prioritization, self.env_name) elif self.prioritization == 'tderror': self.buffer = PrioritizedReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions, alpha, self.env_name) if beta_iters is None: beta_iters = max_timesteps self.beta_schedule = LinearSchedule(beta_iters, initial_p=beta0, final_p=1.0) else: self.buffer = ReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions) def _random_action(self, n): return np.random.uniform(low=-self.max_u, high=self.max_u, size=(n, self.dimu)) def _preprocess_og(self, o, ag, g): if self.relative_goals: g_shape = g.shape g = g.reshape(-1, self.dimg) ag = ag.reshape(-1, self.dimg) g = self.subtract_goals(g, ag) g = g.reshape(*g_shape) o = np.clip(o, -self.clip_obs, self.clip_obs) g = np.clip(g, -self.clip_obs, self.clip_obs) return o, g def get_actions(self, o, ag, g, noise_eps=0., random_eps=0., use_target_net=False, compute_Q=False): o, g = self._preprocess_og(o, ag, g) policy = self.target if use_target_net else self.main # values to compute vals = [policy.pi_tf] if compute_Q: vals += [policy.Q_pi_tf] # feed feed = { policy.o_tf: o.reshape(-1, self.dimo), policy.g_tf: g.reshape(-1, self.dimg), policy.u_tf: np.zeros((o.size // self.dimo, self.dimu), dtype=np.float32) } ret = self.sess.run(vals, feed_dict=feed) # action postprocessing u = ret[0] noise = noise_eps * self.max_u * np.random.randn( *u.shape) # gaussian noise u += noise u = np.clip(u, -self.max_u, self.max_u) u += np.random.binomial(1, random_eps, u.shape[0]).reshape(-1, 1) * ( self._random_action(u.shape[0]) - u) # eps-greedy if u.shape[0] == 1: u = u[0] u = u.copy() ret[0] = u if len(ret) == 1: return ret[0] else: return ret def get_td_errors(self, o, g, u): o, g = self._preprocess_og(o, g, g) vals = [self.td_error_tf] r = np.ones((o.reshape(-1, self.dimo).shape[0], 1)) feed = { self.target.o_tf: o.reshape(-1, self.dimo), self.target.g_tf: g.reshape(-1, self.dimg), self.bath_tf_r: r, self.main.o_tf: o.reshape(-1, self.dimo), self.main.g_tf: g.reshape(-1, self.dimg), self.main.u_tf: u.reshape(-1, self.dimu) } td_errors = self.sess.run(vals, feed_dict=feed) td_errors = td_errors.copy() return td_errors def fit_density_model(self): self.buffer.fit_density_model() def store_episode(self, episode_batch, dump_buffer, rank_method, epoch, update_stats=True): """ episode_batch: array of batch_size x (T or T+1) x dim_key 'o' is of size T+1, others are of size T """ if self.prioritization == 'tderror': self.buffer.store_episode(episode_batch, dump_buffer) elif self.prioritization == 'entropy': self.buffer.store_episode(episode_batch, rank_method, epoch) else: self.buffer.store_episode(episode_batch) if update_stats: # add transitions to normalizer episode_batch['o_2'] = episode_batch['o'][:, 1:, :] episode_batch['ag_2'] = episode_batch['ag'][:, 1:, :] num_normalizing_transitions = transitions_in_episode_batch( episode_batch) if self.prioritization == 'entropy': if not self.buffer.current_size == 0 and not len( episode_batch['ag']) == 0: transitions = self.sample_transitions( episode_batch, num_normalizing_transitions, 'none', 1.0, True) elif self.prioritization == 'tderror': transitions, weights, episode_idxs = \ self.sample_transitions(self.buffer, episode_batch, num_normalizing_transitions, beta=0) else: transitions = self.sample_transitions( episode_batch, num_normalizing_transitions) o, o_2, g, ag = transitions['o'], transitions['o_2'], transitions[ 'g'], transitions['ag'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) # No need to preprocess the o_2 and g_2 since this is only used for stats self.o_stats.update(transitions['o']) self.g_stats.update(transitions['g']) self.o_stats.recompute_stats() self.g_stats.recompute_stats() def get_current_buffer_size(self): return self.buffer.get_current_size() def dump_buffer(self, epoch): self.buffer.dump_buffer(epoch) def _sync_optimizers(self): self.Q_adam.sync() self.pi_adam.sync() def _grads(self): # Avoid feed_dict here for performance! critic_loss, actor_loss, Q_grad, pi_grad, td_error = self.sess.run([ self.Q_loss_tf, self.main.Q_pi_tf, self.Q_grad_tf, self.pi_grad_tf, self.td_error_tf ]) return critic_loss, actor_loss, Q_grad, pi_grad, td_error def _update(self, Q_grad, pi_grad): self.Q_adam.update(Q_grad, self.Q_lr) self.pi_adam.update(pi_grad, self.pi_lr) def sample_batch(self, t): if self.prioritization == 'entropy': transitions = self.buffer.sample(self.batch_size, self.rank_method, temperature=self.temperature) weights = np.ones_like(transitions['r']).copy() elif self.prioritization == 'tderror': transitions, weights, idxs = self.buffer.sample( self.batch_size, beta=self.beta_schedule.value(t)) else: transitions = self.buffer.sample(self.batch_size) weights = np.ones_like(transitions['r']).copy() o, o_2, g = transitions['o'], transitions['o_2'], transitions['g'] ag, ag_2 = transitions['ag'], transitions['ag_2'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) transitions['o_2'], transitions['g_2'] = self._preprocess_og( o_2, ag_2, g) transitions['w'] = weights.flatten().copy() # note: ordered dict transitions_batch = [ transitions[key] for key in self.stage_shapes.keys() ] if self.prioritization == 'tderror': return (transitions_batch, idxs) else: return transitions_batch def stage_batch(self, t, batch=None): if batch is None: if self.prioritization == 'tderror': batch, idxs = self.sample_batch(t) else: batch = self.sample_batch(t) assert len(self.buffer_ph_tf) == len(batch) self.sess.run(self.stage_op, feed_dict=dict(zip(self.buffer_ph_tf, batch))) if self.prioritization == 'tderror': return idxs def train(self, t, dump_buffer, stage=True): if not self.buffer.current_size == 0: if stage: if self.prioritization == 'tderror': idxs = self.stage_batch(t) else: self.stage_batch(t) critic_loss, actor_loss, Q_grad, pi_grad, td_error = self._grads() if self.prioritization == 'tderror': new_priorities = np.abs(td_error) + self.eps # td_error if dump_buffer: T = self.buffer.buffers['u'].shape[1] episode_idxs = idxs // T t_samples = idxs % T batch_size = td_error.shape[0] with self.buffer.lock: for i in range(batch_size): self.buffer.buffers['td'][episode_idxs[i]][ t_samples[i]] = td_error[i] self.buffer.update_priorities(idxs, new_priorities) self._update(Q_grad, pi_grad) return critic_loss, actor_loss def _init_target_net(self): self.sess.run(self.init_target_net_op) def update_target_net(self): self.sess.run(self.update_target_net_op) def clear_buffer(self): self.buffer.clear_buffer() def _vars(self, scope): res = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.scope + '/' + scope) assert len(res) > 0 return res def _global_vars(self, scope): res = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.scope + '/' + scope) return res def _create_network(self, reuse=False): logger.info("Creating a DDPG agent with action space %d x %s..." % (self.dimu, self.max_u)) self.sess = tf.get_default_session() if self.sess is None: self.sess = tf.InteractiveSession() # running averages with tf.variable_scope('o_stats') as vs: if reuse: vs.reuse_variables() self.o_stats = Normalizer(self.dimo, self.norm_eps, self.norm_clip, sess=self.sess) with tf.variable_scope('g_stats') as vs: if reuse: vs.reuse_variables() self.g_stats = Normalizer(self.dimg, self.norm_eps, self.norm_clip, sess=self.sess) # mini-batch sampling. batch = self.staging_tf.get() batch_tf = OrderedDict([ (key, batch[i]) for i, key in enumerate(self.stage_shapes.keys()) ]) batch_tf['r'] = tf.reshape(batch_tf['r'], [-1, 1]) batch_tf['w'] = tf.reshape(batch_tf['w'], [-1, 1]) # networks with tf.variable_scope('main') as vs: if reuse: vs.reuse_variables() self.main = self.create_actor_critic(batch_tf, net_type='main', **self.__dict__) vs.reuse_variables() with tf.variable_scope('target') as vs: if reuse: vs.reuse_variables() target_batch_tf = batch_tf.copy() target_batch_tf['o'] = batch_tf['o_2'] target_batch_tf['g'] = batch_tf['g_2'] self.target = self.create_actor_critic(target_batch_tf, net_type='target', **self.__dict__) vs.reuse_variables() assert len(self._vars("main")) == len(self._vars("target")) # loss functions target_Q_pi_tf = self.target.Q_pi_tf clip_range = (-self.clip_return, 0. if self.clip_pos_returns else np.inf) target_tf = tf.clip_by_value( batch_tf['r'] + self.gamma * target_Q_pi_tf, *clip_range) self.td_error_tf = tf.stop_gradient(target_tf) - self.main.Q_tf self.errors_tf = tf.square(self.td_error_tf) self.errors_tf = tf.reduce_mean(batch_tf['w'] * self.errors_tf) self.Q_loss_tf = tf.reduce_mean(self.errors_tf) self.pi_loss_tf = -tf.reduce_mean(self.main.Q_pi_tf) self.pi_loss_tf += self.action_l2 * tf.reduce_mean( tf.square(self.main.pi_tf / self.max_u)) Q_grads_tf = tf.gradients(self.Q_loss_tf, self._vars('main/Q')) pi_grads_tf = tf.gradients(self.pi_loss_tf, self._vars('main/pi')) assert len(self._vars('main/Q')) == len(Q_grads_tf) assert len(self._vars('main/pi')) == len(pi_grads_tf) self.Q_grads_vars_tf = zip(Q_grads_tf, self._vars('main/Q')) self.pi_grads_vars_tf = zip(pi_grads_tf, self._vars('main/pi')) self.Q_grad_tf = flatten_grads(grads=Q_grads_tf, var_list=self._vars('main/Q')) self.pi_grad_tf = flatten_grads(grads=pi_grads_tf, var_list=self._vars('main/pi')) # optimizers self.Q_adam = MpiAdam(self._vars('main/Q'), scale_grad_by_procs=False) self.pi_adam = MpiAdam(self._vars('main/pi'), scale_grad_by_procs=False) # polyak averaging self.main_vars = self._vars('main/Q') + self._vars('main/pi') self.target_vars = self._vars('target/Q') + self._vars('target/pi') self.stats_vars = self._global_vars('o_stats') + self._global_vars( 'g_stats') self.init_target_net_op = list( map(lambda v: v[0].assign(v[1]), zip(self.target_vars, self.main_vars))) self.update_target_net_op = list( map( lambda v: v[0].assign(self.polyak * v[0] + (1. - self.polyak) * v[1]), zip(self.target_vars, self.main_vars))) # initialize all variables tf.variables_initializer(self._global_vars('')).run() self._sync_optimizers() self._init_target_net() def logs(self, prefix=''): logs = [] logs += [('stats_o/mean', np.mean(self.sess.run([self.o_stats.mean])))] logs += [('stats_o/std', np.mean(self.sess.run([self.o_stats.std])))] logs += [('stats_g/mean', np.mean(self.sess.run([self.g_stats.mean])))] logs += [('stats_g/std', np.mean(self.sess.run([self.g_stats.std])))] if prefix is not '' and not prefix.endswith('/'): return [(prefix + '/' + key, val) for key, val in logs] else: return logs def __getstate__(self): """Our policies can be loaded from pkl, but after unpickling you cannot continue training. """ excluded_subnames = [ '_tf', '_op', '_vars', '_adam', 'buffer', 'sess', '_stats', 'main', 'target', 'lock', 'env', 'sample_transitions', 'stage_shapes', 'create_actor_critic' ] state = { k: v for k, v in self.__dict__.items() if all([not subname in k for subname in excluded_subnames]) } state['buffer_size'] = self.buffer_size state['tf'] = self.sess.run( [x for x in self._global_vars('') if 'buffer' not in x.name]) return state def __setstate__(self, state): if 'sample_transitions' not in state: # We don't need this for playing the policy. state['sample_transitions'] = None state['env_name'] = None # No need for playing the policy self.__init__(**state) # set up stats (they are overwritten in __init__) for k, v in state.items(): if k[-6:] == '_stats': self.__dict__[k] = v # load TF variables vars = [x for x in self._global_vars('') if 'buffer' not in x.name] assert (len(vars) == len(state["tf"])) node = [tf.assign(var, val) for var, val in zip(vars, state["tf"])] self.sess.run(node)
class DDPG(object): @store_args def __init__(self, input_dims, buffer_size, hidden, layers, network_class, polyak, batch_size, Q_lr, pi_lr, norm_eps, norm_clip, max_u, action_l2, clip_obs, scope, T, rollout_batch_size, subtract_goals, relative_goals, clip_pos_returns, clip_return, sample_transitions, gamma, reuse=False, **kwargs): """Implementation of DDPG that is used in combination with Hindsight Experience Replay (HER). Args: input_dims (dict of ints): dimensions for the observation (o), the goal (g), and the actions (u) buffer_size (int): number of transitions that are stored in the replay buffer hidden (int): number of units in the hidden layers layers (int): number of hidden layers network_class (str): the network class that should be used (e.g. 'baselines.her.ActorCritic') polyak (float): coefficient for Polyak-averaging of the target network batch_size (int): batch size for training Q_lr (float): learning rate for the Q (critic) network pi_lr (float): learning rate for the pi (actor) network norm_eps (float): a small value used in the normalizer to avoid numerical instabilities norm_clip (float): normalized inputs are clipped to be in [-norm_clip, norm_clip] max_u (float): maximum action magnitude, i.e. actions are in [-max_u, max_u] action_l2 (float): coefficient for L2 penalty on the actions clip_obs (float): clip observations before normalization to be in [-clip_obs, clip_obs] scope (str): the scope used for the TensorFlow graph T (int): the time horizon for rollouts rollout_batch_size (int): number of parallel rollouts per DDPG agent subtract_goals (function): function that subtracts goals from each other relative_goals (boolean): whether or not relative goals should be fed into the network clip_pos_returns (boolean): whether or not positive returns should be clipped clip_return (float): clip returns to be in [-clip_return, clip_return] sample_transitions (function) function that samples from the replay buffer gamma (float): gamma used for Q learning updates reuse (boolean): whether or not the networks should be reused """ if self.clip_return is None: self.clip_return = np.inf self.create_actor_critic = import_function(self.network_class) input_shapes = dims_to_shapes(self.input_dims) self.dimo = self.input_dims['o'] self.dimg = self.input_dims['g'] self.dimu = self.input_dims['u'] # Prepare staging area for feeding data to the model. stage_shapes = OrderedDict() for key in sorted(self.input_dims.keys()): if key.startswith('info_'): continue stage_shapes[key] = (None, *input_shapes[key]) for key in ['o', 'g']: stage_shapes[key + '_2'] = stage_shapes[key] stage_shapes['r'] = (None,) self.stage_shapes = stage_shapes # Create network. with tf.variable_scope(self.scope): self.staging_tf = StagingArea( dtypes=[tf.float32 for _ in self.stage_shapes.keys()], shapes=list(self.stage_shapes.values())) self.buffer_ph_tf = [ tf.placeholder(tf.float32, shape=shape) for shape in self.stage_shapes.values()] self.stage_op = self.staging_tf.put(self.buffer_ph_tf) self._create_network(reuse=reuse) # Configure the replay buffer. buffer_shapes = {key: (self.T if key != 'o' else self.T+1, *input_shapes[key]) for key, val in input_shapes.items()} buffer_shapes['g'] = (buffer_shapes['g'][0], self.dimg) buffer_shapes['ag'] = (self.T+1, self.dimg) buffer_size = (self.buffer_size // self.rollout_batch_size) * self.rollout_batch_size self.buffer = ReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions) def _random_action(self, n): return np.random.uniform(low=-self.max_u, high=self.max_u, size=(n, self.dimu)) def _preprocess_og(self, o, ag, g): if self.relative_goals: g_shape = g.shape g = g.reshape(-1, self.dimg) ag = ag.reshape(-1, self.dimg) g = self.subtract_goals(g, ag) g = g.reshape(*g_shape) o = np.clip(o, -self.clip_obs, self.clip_obs) g = np.clip(g, -self.clip_obs, self.clip_obs) return o, g def get_actions(self, o, ag, g, noise_eps=0., random_eps=0., use_target_net=False, compute_Q=False): o, g = self._preprocess_og(o, ag, g) # clip observations and goals policy = self.target if use_target_net else self.main # values to compute vals = [policy.pi_tf] if compute_Q: vals += [policy.Q_pi_tf] # feed feed = { policy.o_tf: o.reshape(-1, self.dimo), policy.g_tf: g.reshape(-1, self.dimg), policy.u_tf: np.zeros((o.size // self.dimo, self.dimu), dtype=np.float32) } # ret = action given by the current policy (eval of NN) ret = self.sess.run(vals, feed_dict=feed) # action postprocessing u = ret[0] noise = noise_eps * self.max_u * np.random.randn(*u.shape) # gaussian noise u += noise u = np.clip(u, -self.max_u, self.max_u) # Below: for each mini-batch we take action u (the one given by the policy) with probability # 1-random_eps, and a random action (u + random_action - u) with probability random_eps u += np.random.binomial(1, random_eps, u.shape[0]).reshape(-1, 1) * (self._random_action(u.shape[0]) - u) # eps-greedy if u.shape[0] == 1: u = u[0] u = u.copy() ret[0] = u if len(ret) == 1: return ret[0] else: return ret def store_episode(self, episode_batch, update_stats=True): """ episode_batch: array of batch_size x (T or T+1) x dim_key 'o' is of size T+1, others are of size T """ self.buffer.store_episode(episode_batch) if update_stats: # add transitions to normalizer episode_batch['o_2'] = episode_batch['o'][:, 1:, :] episode_batch['ag_2'] = episode_batch['ag'][:, 1:, :] num_normalizing_transitions = transitions_in_episode_batch(episode_batch) transitions = self.sample_transitions(episode_batch, num_normalizing_transitions) o, o_2, g, ag = transitions['o'], transitions['o_2'], transitions['g'], transitions['ag'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) # No need to preprocess the o_2 and g_2 since this is only used for stats self.o_stats.update(transitions['o']) self.g_stats.update(transitions['g']) self.o_stats.recompute_stats() self.g_stats.recompute_stats() def get_current_buffer_size(self): return self.buffer.get_current_size() def _sync_optimizers(self): self.Q_adam.sync() self.pi_adam.sync() def _grads(self): # Avoid feed_dict here for performance! critic_loss, actor_loss, Q_grad, pi_grad = self.sess.run([ self.Q_loss_tf, self.main.Q_pi_tf, self.Q_grad_tf, self.pi_grad_tf ]) return critic_loss, actor_loss, Q_grad, pi_grad def _update(self, Q_grad, pi_grad): self.Q_adam.update(Q_grad, self.Q_lr) self.pi_adam.update(pi_grad, self.pi_lr) def sample_batch(self): transitions = self.buffer.sample(self.batch_size) o, o_2, g = transitions['o'], transitions['o_2'], transitions['g'] ag, ag_2 = transitions['ag'], transitions['ag_2'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) transitions['o_2'], transitions['g_2'] = self._preprocess_og(o_2, ag_2, g) transitions_batch = [transitions[key] for key in self.stage_shapes.keys()] return transitions_batch def stage_batch(self, batch=None): if batch is None: batch = self.sample_batch() assert len(self.buffer_ph_tf) == len(batch) self.sess.run(self.stage_op, feed_dict=dict(zip(self.buffer_ph_tf, batch))) def train(self, stage=True): if stage: self.stage_batch() critic_loss, actor_loss, Q_grad, pi_grad = self._grads() self._update(Q_grad, pi_grad) return critic_loss, actor_loss def _init_target_net(self): self.sess.run(self.init_target_net_op) def update_target_net(self): self.sess.run(self.update_target_net_op) def clear_buffer(self): self.buffer.clear_buffer() def _vars(self, scope): res = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.scope + '/' + scope) assert len(res) > 0 return res def _global_vars(self, scope): res = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.scope + '/' + scope) return res def _create_network(self, reuse=False): logger.info("Creating a DDPG agent with action space %d x %s..." % (self.dimu, self.max_u)) self.sess = tf.get_default_session() if self.sess is None: self.sess = tf.InteractiveSession() # running averages with tf.variable_scope('o_stats') as vs: if reuse: vs.reuse_variables() self.o_stats = Normalizer(self.dimo, self.norm_eps, self.norm_clip, sess=self.sess) with tf.variable_scope('g_stats') as vs: if reuse: vs.reuse_variables() self.g_stats = Normalizer(self.dimg, self.norm_eps, self.norm_clip, sess=self.sess) # mini-batch sampling. batch = self.staging_tf.get() batch_tf = OrderedDict([(key, batch[i]) for i, key in enumerate(self.stage_shapes.keys())]) batch_tf['r'] = tf.reshape(batch_tf['r'], [-1, 1]) # networks with tf.variable_scope('main') as vs: if reuse: vs.reuse_variables() self.main = self.create_actor_critic(batch_tf, net_type='main', **self.__dict__) vs.reuse_variables() with tf.variable_scope('target') as vs: if reuse: vs.reuse_variables() target_batch_tf = batch_tf.copy() target_batch_tf['o'] = batch_tf['o_2'] target_batch_tf['g'] = batch_tf['g_2'] self.target = self.create_actor_critic( target_batch_tf, net_type='target', **self.__dict__) vs.reuse_variables() assert len(self._vars("main")) == len(self._vars("target")) # loss functions # self.XX.pi_tf is the action policy we ll use for exploration (TO CONFIRM) # self.XX.Q_pi_tf is the Q network used to train this policy # self.XX.Q_tf target_Q_pi_tf = self.target.Q_pi_tf clip_range = (-self.clip_return, 0. if self.clip_pos_returns else np.inf) # target y_i= r + gamma*Q part of the Bellman equation (with returns clipped if necessary: target_tf = tf.clip_by_value(batch_tf['r'] + self.gamma * target_Q_pi_tf, *clip_range) # loss function for Q_tf where we exclude target_tf from the gradient computation: self.Q_loss_tf = tf.reduce_mean(tf.square(tf.stop_gradient(target_tf) - self.main.Q_tf)) # loss function for the action policy is that of the main Q_pi network: self.pi_loss_tf = -tf.reduce_mean(self.main.Q_pi_tf) # add L2 regularization term from the policy itself: self.pi_loss_tf += self.action_l2 * tf.reduce_mean(tf.square(self.main.pi_tf / self.max_u)) # define the gradients of the Q_loss and pi_loss wrt to their variables respectively Q_grads_tf = tf.gradients(self.Q_loss_tf, self._vars('main/Q')) pi_grads_tf = tf.gradients(self.pi_loss_tf, self._vars('main/pi')) assert len(self._vars('main/Q')) == len(Q_grads_tf) assert len(self._vars('main/pi')) == len(pi_grads_tf) # zip the gradients together with their respective variables self.Q_grads_vars_tf = zip(Q_grads_tf, self._vars('main/Q')) self.pi_grads_vars_tf = zip(pi_grads_tf, self._vars('main/pi')) # flattened gradients and variables self.Q_grad_tf = flatten_grads(grads=Q_grads_tf, var_list=self._vars('main/Q')) self.pi_grad_tf = flatten_grads(grads=pi_grads_tf, var_list=self._vars('main/pi')) # optimizers (using MPI for parralel updates of the network (TO CONFIRM)) self.Q_adam = MpiAdam(self._vars('main/Q'), scale_grad_by_procs=False) self.pi_adam = MpiAdam(self._vars('main/pi'), scale_grad_by_procs=False) # polyak averaging used for the update of the target networks in both pi and Q nets self.main_vars = self._vars('main/Q') + self._vars('main/pi') self.target_vars = self._vars('target/Q') + self._vars('target/pi') self.stats_vars = self._global_vars('o_stats') + self._global_vars('g_stats') # operation to initialize the target nets at the main nets'values self.init_target_net_op = list( map(lambda v: v[0].assign(v[1]), zip(self.target_vars, self.main_vars))) # operation to update the target nets from the main nets using polyak averaging self.update_target_net_op = list( map(lambda v: v[0].assign(self.polyak * v[0] + (1. - self.polyak) * v[1]), zip(self.target_vars, self.main_vars))) # initialize all variables tf.variables_initializer(self._global_vars('')).run() self._sync_optimizers() # CHECK WHAT THIS DOES ???? self._init_target_net() def logs(self, prefix=''): logs = [] logs += [('stats_o/mean', np.mean(self.sess.run([self.o_stats.mean])))] logs += [('stats_o/std', np.mean(self.sess.run([self.o_stats.std])))] logs += [('stats_g/mean', np.mean(self.sess.run([self.g_stats.mean])))] logs += [('stats_g/std', np.mean(self.sess.run([self.g_stats.std])))] if prefix is not '' and not prefix.endswith('/'): return [(prefix + '/' + key, val) for key, val in logs] else: return logs def __getstate__(self): """Our policies can be loaded from pkl, but after unpickling you cannot continue training. """ excluded_subnames = ['_tf', '_op', '_vars', '_adam', 'buffer', 'sess', '_stats', 'main', 'target', 'lock', 'env', 'sample_transitions', 'stage_shapes', 'create_actor_critic'] state = {k: v for k, v in self.__dict__.items() if all([not subname in k for subname in excluded_subnames])} state['buffer_size'] = self.buffer_size state['tf'] = self.sess.run([x for x in self._global_vars('') if 'buffer' not in x.name]) return state def __setstate__(self, state): if 'sample_transitions' not in state: # We don't need this for playing the policy. state['sample_transitions'] = None self.__init__(**state) # set up stats (they are overwritten in __init__) for k, v in state.items(): if k[-6:] == '_stats': self.__dict__[k] = v # load TF variables vars = [x for x in self._global_vars('') if 'buffer' not in x.name] assert(len(vars) == len(state["tf"])) node = [tf.assign(var, val) for var, val in zip(vars, state["tf"])] self.sess.run(node)
class DDPG(object): @store_args def __init__(self, FLAGS, input_dims, buffer_size, hidden, layers, network_class, polyak, batch_size, Q_lr, pi_lr, norm_eps, norm_clip, max_u, action_l2, clip_obs, scope, T, rollout_batch_size, subtract_goals, relative_goals, clip_pos_returns, clip_return, bc_loss, q_filter, num_demo, demo_batch_size, prm_loss_weight, aux_loss_weight, # sample_transitions, gamma, reuse=False, **kwargs): sample_transitions, gamma, td3_policy_freq, td3_policy_noise, td3_noise_clip, reuse=False, *agent_params, **kwargs): ## """Implementation of DDPG that is used in combination with Hindsight Experience Replay (HER). Added functionality to use demonstrations for training to Overcome exploration problem. Args: input_dims (dict of ints): dimensions for the observation (o), the goal (g), and the actions (u) buffer_size (int): number of transitions that are stored in the replay buffer hidden (int): number of units in the hidden layers layers (int): number of hidden layers network_class (str): the network class that should be used (e.g. 'baselines.her.ActorCritic') polyak (float): coefficient for Polyak-averaging of the target network batch_size (int): batch size for training Q_lr (float): learning rate for the Q (critic) network pi_lr (float): learning rate for the pi (actor) network norm_eps (float): a small value used in the normalizer to avoid numerical instabilities norm_clip (float): normalized inputs are clipped to be in [-norm_clip, norm_clip] max_u (float): maximum action magnitude, i.e. actions are in [-max_u, max_u] action_l2 (float): coefficient for L2 penalty on the actions clip_obs (float): clip observations before normalization to be in [-clip_obs, clip_obs] scope (str): the scope used for the TensorFlow graph T (int): the time horizon for rollouts rollout_batch_size (int): number of parallel rollouts per DDPG agent subtract_goals (function): function that subtracts goals from each other relative_goals (boolean): whether or not relative goals should be fed into the network clip_pos_returns (boolean): whether or not positive returns should be clipped clip_return (float): clip returns to be in [-clip_return, clip_return] sample_transitions (function) function that samples from the replay buffer gamma (float): gamma used for Q learning updates reuse (boolean): whether or not the networks should be reused bc_loss: whether or not the behavior cloning loss should be used as an auxilliary loss q_filter: whether or not a filter on the q value update should be used when training with demonstartions num_demo: Number of episodes in to be used in the demonstration buffer demo_batch_size: number of samples to be used from the demonstrations buffer, per mpi thread prm_loss_weight: Weight corresponding to the primary loss aux_loss_weight: Weight corresponding to the auxilliary loss also called the cloning loss agent_params: for HAC agent params """ if self.clip_return is None: self.clip_return = np.inf self.create_actor_critic = import_function(self.network_class) input_shapes = dims_to_shapes(self.input_dims) self.dimo = self.input_dims['o'] # self.dimo1= self.input_dims['o1'] ##A.R add for TD3 (has obs0, obs1) self.dimg = self.input_dims['g'] self.dimu = self.input_dims['u'] #추가된 내용 #parameters for using TD3 variant of DDPG #https://arxiv.org/abs/1802.09477 self.td3_policy_freq = td3_policy_freq self.td3_policy_noise = td3_policy_noise self.td3_noise_clip = td3_noise_clip ## for HAC self.FLAGS = FLAGS # Prepare staging area for feeding data to the model. stage_shapes = OrderedDict() for key in sorted(self.input_dims.keys()): if key.startswith('info_'): continue stage_shapes[key] = (None, *input_shapes[key]) for key in ['o', 'g']: # for key in ['o', 'o1', 'g']: #o1 added by A.R stage_shapes[key + '_2'] = stage_shapes[key] stage_shapes['r'] = (None,) self.stage_shapes = stage_shapes # Create network. with tf.variable_scope(self.scope): self.staging_tf = StagingArea( dtypes=[tf.float32 for _ in self.stage_shapes.keys()], shapes=list(self.stage_shapes.values())) self.buffer_ph_tf = [ tf.placeholder(tf.float32, shape=shape) for shape in self.stage_shapes.values()] self.stage_op = self.staging_tf.put(self.buffer_ph_tf) self._create_network(reuse=reuse) # Configure the replay buffer. buffer_shapes = {key: (self.T-1 if key != 'o' else self.T, *input_shapes[key]) # origin : buffer_shapes = {key: (self.T-1 if key != 'o' else self.T, *input_shapes[key]) # buffer_shapes = {key: (self.T-1 if key != 'o' and key != 'o1' else self.T, *input_shapes[key]) #A.Rㅇ for key, val in input_shapes.items()} buffer_shapes['g'] = (buffer_shapes['g'][0], self.dimg) buffer_shapes['ag'] = (self.T, self.dimg) buffer_size = (self.buffer_size // self.rollout_batch_size) * self.rollout_batch_size self.buffer = ReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions) global DEMO_BUFFER DEMO_BUFFER = ReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions) #initialize the demo buffer; in the same way as the primary data buffer print("@ ddgp.py , buffer={}".format(self.buffer)) # self.meta_controller = DDPG(self.dimo + self.dimg, self.dimo, self.clip_obs) # ## # self.low_replay_buffer = ReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions) # self.high_replay_buffer = ReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions) # ## def _random_action(self, n): return np.random.uniform(low=-self.max_u, high=self.max_u, size=(n, self.dimu)) def _preprocess_og(self, o, ag, g): # def _preprocess_og(self, o, o1, ag, g): #A.R if self.relative_goals: ## goal reshape 해주는 곳. ag vs g..흠 g_shape = g.shape g = g.reshape(-1, self.dimg) ag = ag.reshape(-1, self.dimg) g = self.subtract_goals(g, ag) #상대적인 골로 만들어 주는구나?.. ''' def simple_goal_subtract(a, b): assert a.shape == b.shape return a - b ''' g = g.reshape(*g_shape) o = np.clip(o, -self.clip_obs, self.clip_obs) # o1 = np.clip(o1, -self.clip_obs, self.clip_obs) #A.R g = np.clip(g, -self.clip_obs, self.clip_obs) # return o, o1, g return o, g def step(self, obs): # FLAGS = FLAGS actions = self.get_actions(obs['observation'], obs['achieved_goal'], obs['desired_goal']) # actions = self.get_actions(obs['observation'], obs['achieved_goal'], obs['desired_goal'], FLAGS) # print("for debug, obs : {}".format(obs['observation'])) return actions, None, None, None # def get_actions(self, o, o1, ag, g, noise_eps=0., random_eps=0., use_target_net=False, ##o1이 target 네트워크 def get_actions(self, o, ag, g, noise_eps=0., random_eps=0., use_target_net=False, # def get_actions(self, o, ag, g, FLAGS, noise_eps=0., random_eps=0., use_target_net=False, compute_Q=False): # o, o1, g = self._preprocess_og(o, o1, ag, g) ## o, g = self._preprocess_og(o, ag, g) policy = self.target if use_target_net else self.main # rollout.py에서 넘어온다. # values to compute vals = [policy.pi_tf] if compute_Q: vals += [policy.Q_pi_tf] # feed feed = { policy.o_tf: o.reshape(-1, self.dimo), policy.g_tf: g.reshape(-1, self.dimg), policy.u_tf: np.zeros((o.size // self.dimo, self.dimu), dtype=np.float32) } ret = self.sess.run(vals, feed_dict=feed) # action postprocessing u = ret[0] noise = noise_eps * self.max_u * np.random.randn(*u.shape) # gaussian noise u += noise u = np.clip(u, -self.max_u, self.max_u) u += np.random.binomial(1, random_eps, u.shape[0]).reshape(-1, 1) * (self._random_action(u.shape[0]) - u) # eps-greedy if u.shape[0] == 1: u = u[0] u = u.copy() ret[0] = u if len(ret) == 1: return ret[0] else: return ret def init_demo_buffer(self, demoDataFile, update_stats=True): #function that initializes the demo buffer demoData = np.load(demoDataFile) #load the demonstration data from data file info_keys = [key.replace('info_', '') for key in self.input_dims.keys() if key.startswith('info_')] info_values = [np.empty((self.T - 1, 1, self.input_dims['info_' + key]), np.float32) for key in info_keys] demo_data_obs = demoData['obs'] demo_data_acs = demoData['acs'] demo_data_info = demoData['info'] for epsd in range(self.num_demo): # we initialize the whole demo buffer at the start of the training obs, acts, goals, achieved_goals = [], [] ,[] ,[] i = 0 for transition in range(self.T - 1): obs.append([demo_data_obs[epsd][transition].get('observation')]) acts.append([demo_data_acs[epsd][transition]]) goals.append([demo_data_obs[epsd][transition].get('desired_goal')]) achieved_goals.append([demo_data_obs[epsd][transition].get('achieved_goal')]) for idx, key in enumerate(info_keys): info_values[idx][transition, i] = demo_data_info[epsd][transition][key] obs.append([demo_data_obs[epsd][self.T - 1].get('observation')]) achieved_goals.append([demo_data_obs[epsd][self.T - 1].get('achieved_goal')]) episode = dict(o=obs, u=acts, g=goals, ag=achieved_goals) for key, value in zip(info_keys, info_values): episode['info_{}'.format(key)] = value episode = convert_episode_to_batch_major(episode) global DEMO_BUFFER DEMO_BUFFER.store_episode(episode) # create the observation dict and append them into the demonstration buffer logger.debug("Demo buffer size currently ", DEMO_BUFFER.get_current_size()) #print out the demonstration buffer size if update_stats: # add transitions to normalizer to normalize the demo data as well episode['o_2'] = episode['o'][:, 1:, :] episode['ag_2'] = episode['ag'][:, 1:, :] num_normalizing_transitions = transitions_in_episode_batch(episode) transitions = self.sample_transitions(episode, num_normalizing_transitions) o, g, ag = transitions['o'], transitions['g'], transitions['ag'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) # No need to preprocess the o_2 and g_2 since this is only used for stats self.o_stats.update(transitions['o']) self.g_stats.update(transitions['g']) self.o_stats.recompute_stats() self.g_stats.recompute_stats() episode.clear() logger.info("Demo buffer size: ", DEMO_BUFFER.get_current_size()) #print out the demonstration buffer size def store_episode(self, episode_batch, update_stats=True): """ episode_batch: array of batch_size x (T or T+1) x dim_key 'o' is of size T+1, others are of size T """ self.buffer.store_episode(episode_batch) if update_stats: # add transitions to normalizer episode_batch['o_2'] = episode_batch['o'][:, 1:, :] episode_batch['ag_2'] = episode_batch['ag'][:, 1:, :] num_normalizing_transitions = transitions_in_episode_batch(episode_batch) transitions = self.sample_transitions(episode_batch, num_normalizing_transitions) o, g, ag = transitions['o'], transitions['g'], transitions['ag'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) # No need to preprocess the o_2 and g_2 since this is only used for stats self.o_stats.update(transitions['o']) self.g_stats.update(transitions['g']) self.o_stats.recompute_stats() self.g_stats.recompute_stats() def get_current_buffer_size(self): return self.buffer.get_current_size() def _sync_optimizers(self): self.Q_adam.sync() self.pi_adam.sync() def _grads(self): # Avoid feed_dict here for performance! critic_loss, actor_loss, Q_grad, pi_grad = self.sess.run([ self.Q_loss_tf, self.main.Q_pi_tf, self.Q_grad_tf, self.pi_grad_tf ]) return critic_loss, actor_loss, Q_grad, pi_grad def _update(self, Q_grad, pi_grad): self.Q_adam.update(Q_grad, self.Q_lr) self.pi_adam.update(pi_grad, self.pi_lr) def sample_batch(self): if self.bc_loss: #use demonstration buffer to sample as well if bc_loss flag is set TRUE transitions = self.buffer.sample(self.batch_size - self.demo_batch_size) global DEMO_BUFFER transitions_demo = DEMO_BUFFER.sample(self.demo_batch_size) #sample from the demo buffer for k, values in transitions_demo.items(): rolloutV = transitions[k].tolist() for v in values: rolloutV.append(v.tolist()) transitions[k] = np.array(rolloutV) else: transitions = self.buffer.sample(self.batch_size) #otherwise only sample from primary buffer o, o_2, g = transitions['o'], transitions['o_2'], transitions['g'] # o1, o1_2, g = transitions['o1'], transitions['o1_2'] ## A.R ag, ag_2 = transitions['ag'], transitions['ag_2'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) transitions['o_2'], transitions['g_2'] = self._preprocess_og(o_2, ag_2, g) transitions_batch = [transitions[key] for key in self.stage_shapes.keys()] print("@ ddpg, sample_batch, transitions_batch={}".format(transitions_batch)) return transitions_batch def stage_batch(self, batch=None): if batch is None: batch = self.sample_batch() assert len(self.buffer_ph_tf) == len(batch) self.sess.run(self.stage_op, feed_dict=dict(zip(self.buffer_ph_tf, batch))) def train(self, stage=True): if stage: self.stage_batch() critic_loss, actor_loss, Q_grad, pi_grad = self._grads() ## 현재 loss들 가져오는거 self._update(Q_grad, pi_grad) ## 아담 업데이트 하는거 return critic_loss, actor_loss def _init_target_net(self): self.sess.run(self.init_target1_net_op) self.sess.run(self.init_target2_net_op) def update_target_net(self): # self.sess.run(self.update_target_net_op) self.sess.run(self.update_target1_net_op) self.sess.run(self.update_target2_net_op) def clear_buffer(self): self.buffer.clear_buffer() def _vars(self, scope): res = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.scope + '/' + scope) assert len(res) > 0 #######################이게 왜걸리지? 왜 다시 안걸리지? return res def _global_vars(self, scope): res = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.scope + '/' + scope) # print("DEBUG, {}".format(res)) return res def _create_network(self, reuse=False): ## num_demo 추가 -2 logger.info("Debug : Creating a DDPG agent with action space %d x %s..." % (self.dimu, self.max_u)) self.sess = tf_util.get_session() # self.num_demo = num_demo # running averages with tf.variable_scope('o_stats') as vs: if reuse: vs.reuse_variables() self.o_stats = Normalizer(self.dimo, self.norm_eps, self.norm_clip, sess=self.sess) with tf.variable_scope('g_stats') as vs: if reuse: vs.reuse_variables() self.g_stats = Normalizer(self.dimg, self.norm_eps, self.norm_clip, sess=self.sess) # mini-batch sampling. batch = self.staging_tf.get() ## 그냥 꺼내오는거.. batch_tf = OrderedDict([(key, batch[i]) for i, key in enumerate(self.stage_shapes.keys())]) batch_tf['r'] = tf.reshape(batch_tf['r'], [-1, 1]) #choose only the demo buffer samples mask = np.concatenate((np.zeros(self.batch_size - self.demo_batch_size), np.ones(self.demo_batch_size)), axis = 0) # networks with tf.variable_scope('main') as vs: if reuse: vs.reuse_variables() self.main = self.create_actor_critic(batch_tf, net_type='main', **self.__dict__) vs.reuse_variables() print("tf.variable_scope(main) = {}".format(tf.variable_scope('target1'))) #-1 with tf.variable_scope('target1') as vs: if reuse: vs.reuse_variables() target1_batch_tf = batch_tf.copy() target1_batch_tf['o'] = batch_tf['o_2'] target1_batch_tf['g'] = batch_tf['g_2'] self.target1 = self.create_actor_critic( target1_batch_tf, net_type='target1', **self.__dict__) vs.reuse_variables() print("tf.variable_scope(target1) = {}".format(tf.variable_scope('target1'))) # print("batch= {}".format(target1_batch_tf)) # print(type('target')) #<class 'baselines.her.actor_critic.ActorCritic'> assert len(self._vars("main")) == len(self._vars("target1")) with tf.variable_scope('target2') as vs: if reuse: vs.reuse_variables() target2_batch_tf = batch_tf.copy() target2_batch_tf['o'] = batch_tf['o_2'] target2_batch_tf['g'] = batch_tf['g_2'] self.target2 = self.create_actor_critic( target2_batch_tf, net_type='target2', **self.__dict__) vs.reuse_variables() print("tf.variable_scope(target2) = {}".format(tf.variable_scope('target2'))) print("batch= {}".format(target2_batch_tf)) assert len(self._vars("main")) == len(self._vars("target2")) for nd in range(self.num_demo): ##A.R ##Compute the target Q value, Q1과 Q2중에 min값을 사용한다. target1_Q_pi_tf = self.target1.Q_pi_tf ##A.R policy training target2_Q_pi_tf = self.target2.Q_pi_tf ##A.R # target_Q_pi_tf = tf.minimum(target1_Q_pi_tf, target2_Q_pi_tf) # target1_Q_tf = self.target1.Q_tf ##A.R policy training # target2_Q_tf = self.target2.Q_tf ##A.R # print('target1={}/////target2={}'.format(target1_Q_tf,target2_Q_tf)) target_Q_pi_tf = tf.minimum(target1_Q_pi_tf, target2_Q_pi_tf) # target_Q_tf = tf.minimum(target1_Q_tf, target2_Q_tf) ## 대체 코드 # print("{}///{}///{}".format(target1_Q_pi_tf,target2_Q_pi_tf,tf.minimum(target1_Q_pi_tf, target2_Q_pi_tf))) #### #TD3에서 빠진 코드 :target_Q = reward + (done * discount * target_Q).detach()(L109) ->L428에서 해주고 clip한다 # loss functions # for policy training, Q_pi_tf = nn(input_Q, [self.hidden] * self.layers + [1]) # target_Q_pi_tf = self.target.Q_pi_tf #original code clip_range = (-self.clip_return, 0. if self.clip_pos_returns else np.inf) target_Q_tf = tf.clip_by_value(batch_tf['r'] + self.gamma * target_Q_pi_tf, *clip_range) # target_Q_tf = tf.clip_by_value(batch_tf['r'] + self.gamma * target_Q_tf, *clip_range) ## 대체 코드 # self.Q_loss_tf = tf.reduce_mean(tf.square(tf.stop_gradient(target_tf) - self.main.Q_tf)) ## # current_Q1, current_Q2 = self.critic(state, action) # for critic training, Q_tf = nn(input_Q, [self.hidden] * self.layers + [1], reuse=True) # target_Q_pi_tf = tf.clip_by_value(batch_tf['r'] + self.gamma * target_Q_tf, *clip_range) #original code # self.Q_loss_tf = tf.reduce_mean(tf.square(tf.stop_gradient(target_tf) - self.main.Q_tf)) #critic taining ## Get current Q estimates, for critic Q current_Q1 = self.main.Q_tf ##A.R current_Q2 = self.main.Q_tf # print("Q1={}".format(current_Q1)) ## Compute critic loss ## Torch => critic_loss = F.mse_loss(current_Q1, target_Q) + F.mse_loss(current_Q2, target_Q) self.Q_loss_tf = tf.losses.mean_squared_error(current_Q1, target_Q_tf)+ tf.losses.mean_squared_error(current_Q2,target_Q_tf) # self.Q_loss_tf = tf.losses.mean_squared_error(current_Q1, target_Q_tf)+ tf.losses.mean_squared_error(current_Q2,target_Q_tf) # print("critic_loss ={}".format(self.Q_loss_tf)) Q_grads_tf = tf.gradients(self.Q_loss_tf, self._vars('main/Q')) assert len(self._vars('main/Q')) == len(Q_grads_tf) ## Optimize the critic 아담 옵티마이저 self.Q_adam = MpiAdam(self._vars('main/Q'), scale_grad_by_procs=False) assert len(self._vars('main/Q')) == len(Q_grads_tf) self.Q_grads_vars_tf = zip(Q_grads_tf, self._vars('main/Q')) self.Q_grad_tf = flatten_grads(grads=Q_grads_tf, var_list=self._vars('main/Q')) # ## Delayed policy updates if nd % self.td3_policy_freq == 0: # print("num_demo = {}".format(nd)) target1_Q_pi_tf = self.target1.Q_pi_tf ##A.R policy training target2_Q_pi_tf = self.target2.Q_pi_tf ##A.R tf.print(target1_Q_pi_tf, [target1_Q_pi_tf]) tf.print(target2_Q_pi_tf, [target2_Q_pi_tf]) # print(target2_Q_pi_tf) target_Q_pi_tf = tf.minimum(target1_Q_pi_tf, target2_Q_pi_tf) # target_Q_pi_tf = self.target.Q_pi_tf clip_range = (-self.clip_return, 0. if self.clip_pos_returns else np.inf) target_tf = tf.clip_by_value(batch_tf['r'] + self.gamma * target_Q_pi_tf, *clip_range) self.Q_loss_tf = tf.reduce_mean(tf.square(tf.stop_gradient(target_tf) - self.main.Q_tf)) # Compute actor loss if self.bc_loss ==1 and self.q_filter == 1 : # train with demonstrations and use bc_loss and q_filter both maskMain = tf.reshape(tf.boolean_mask(self.main.Q_tf > self.main.Q_pi_tf, mask), [-1]) #where is the demonstrator action better than actor action according to the critic? choose those samples only #define the cloning loss on the actor's actions only on the samples which adhere to the above masks self.cloning_loss_tf = tf.reduce_sum(tf.square(tf.boolean_mask(tf.boolean_mask((self.main.pi_tf), mask), maskMain, axis=0) - tf.boolean_mask(tf.boolean_mask((batch_tf['u']), mask), maskMain, axis=0))) self.pi_loss_tf = -self.prm_loss_weight * tf.reduce_mean(self.main.Q_pi_tf) #primary loss scaled by it's respective weight prm_loss_weight self.pi_loss_tf += self.prm_loss_weight * self.action_l2 * tf.reduce_mean(tf.square(self.main.pi_tf / self.max_u)) #L2 loss on action values scaled by the same weight prm_loss_weight self.pi_loss_tf += self.aux_loss_weight * self.cloning_loss_tf #adding the cloning loss to the actor loss as an auxilliary loss scaled by its weight aux_loss_weight elif self.bc_loss == 1 and self.q_filter == 0: # train with demonstrations without q_filter self.cloning_loss_tf = tf.reduce_sum(tf.square(tf.boolean_mask((self.main.pi_tf), mask) - tf.boolean_mask((batch_tf['u']), mask))) self.pi_loss_tf = -self.prm_loss_weight * tf.reduce_mean(self.main.Q_pi_tf) self.pi_loss_tf += self.prm_loss_weight * self.action_l2 * tf.reduce_mean(tf.square(self.main.pi_tf / self.max_u)) self.pi_loss_tf += self.aux_loss_weight * self.cloning_loss_tf else: #If not training with demonstrations self.pi_loss_tf = -tf.reduce_mean(self.main.Q_pi_tf) self.pi_loss_tf += self.action_l2 * tf.reduce_mean(tf.square(self.main.pi_tf / self.max_u)) # self.pi_loss_tf = -tf.reduce_mean(self.main.pi_tf) ## what about target1? # self.pi_loss_tf += self.action_l2 * tf.reduce_mean(tf.square(self.main.pi_tf / self.max_u)) # actor_loss = -tf.reduce_mean(self.main.Q_tf) # actor_loss += self.action_l2 * tf.reduce_mean(tf.square(self.main.Q_tf / self.max_u)) pi_grads_tf = tf.gradients(self.pi_loss_tf, self._vars('main/pi')) assert len(self._vars('main/pi')) == len(pi_grads_tf) # Optimize the actor # Q_grads_tf = tf.gradients(self.Q_loss_tf, self._vars('main/Q')) self.pi_adam = MpiAdam(self._vars('main/pi'), scale_grad_by_procs=False) assert len(self._vars('main/pi')) == len(pi_grads_tf) self.pi_grads_vars_tf = zip(pi_grads_tf, self._vars('main/pi')) self.pi_grad_tf = flatten_grads(grads=pi_grads_tf, var_list=self._vars('main/pi')) # Update the frozen target models ## torch code # for param, target_param in zip(self.critic.parameters(), self.critic_target.parameters()): # target_param.data.copy_(tau * param.data + (1 - tau) * target_param.data) self.main_vars = self._vars('main/Q') + self._vars('main/pi') self.target1_vars = self._vars('target1/Q') + self._vars('target1/pi') ##A.R self.target2_vars = self._vars('target2/Q') + self._vars('target2/pi') ##A.R if target_Q_pi_tf == target1_Q_pi_tf: target_vars = self.target1_vars else: target_vars = self.target2_vars # self.target_vars = self._vars('target/Q') + self._vars('target/pi') #original self.stats_vars = self._global_vars('o_stats') + self._global_vars('g_stats') self.init_target1_net_op = list( map(lambda v: v[0].assign(v[1]), zip(self.target1_vars, self.main_vars))) self.init_target2_net_op = list( map(lambda v: v[0].assign(v[1]), zip(self.target2_vars, self.main_vars))) self.update_target_net_op = list( map(lambda v: v[0].assign(self.polyak * v[0] + (1. - self.polyak) * v[1]), zip(target_vars, self.main_vars))) self.update_target1_net_op = list( map(lambda v: v[0].assign(self.polyak * v[0] + (1. - self.polyak) * v[1]), zip(target_vars, self.main_vars))) self.update_target2_net_op = list( map(lambda v: v[0].assign(self.polyak * v[0] + (1. - self.polyak) * v[1]), zip(target_vars, self.main_vars))) tf.variables_initializer(self._global_vars('')).run() self._sync_optimizers() self._init_target_net() # Q_grads_tf = tf.gradients(self.Q_loss_tf, self._vars('main/Q')) # pi_grads_tf = tf.gradients(self.pi_loss_tf, self._vars('main/pi')) # assert len(self._vars('main/Q')) == len(Q_grads_tf) # assert len(self._vars('main/pi')) == len(pi_grads_tf) # self.Q_grads_vars_tf = zip(Q_grads_tf, self._vars('main/Q')) # self.pi_grads_vars_tf = zip(pi_grads_tf, self._vars('main/pi')) # self.Q_grad_tf = flatten_grads(grads=Q_grads_tf, var_list=self._vars('main/Q')) # self.pi_grad_tf = flatten_grads(grads=pi_grads_tf, var_list=self._vars('main/pi')) # optimizers # self.Q_adam = MpiAdam(self._vars('main/Q'), scale_grad_by_procs=False) # self.pi_adam = MpiAdam(self._vars('main/pi'), scale_grad_by_procs=False) # polyak averaging # self.main_vars = self._vars('main/Q') + self._vars('main/pi') # self.target1_vars = self._vars('target1/Q') + self._vars('target1/pi') ##A.R # self.target2_vars = self._vars('target2/Q') + self._vars('target2/pi') ##A.R # # self.target_vars = self._vars('target/Q') + self._vars('target/pi') #original # self.stats_vars = self._global_vars('o_stats') + self._global_vars('g_stats') # self.init_target1_net_op = list( # map(lambda v: v[0].assign(v[1]), zip(self.target1_vars, self.main_vars))) # self.init_target2_net_op = list( # map(lambda v: v[0].assign(v[1]), zip(self.target2_vars, self.main_vars))) # self.update_target_net_op = list( # map(lambda v: v[0].assign(self.polyak * v[0] + (1. - self.polyak) * v[1]), zip(self.target_vars, self.main_vars))) #original # self.init_target_net_op = list( # map(lambda v: v[0].assign(v[1]), zip(self.target_vars, self.main_vars))) # self.update_target_net_op = list( # map(lambda v: v[0].assign(self.polyak * v[0] + (1. - self.polyak) * v[1]), zip(self.target_vars, self.main_vars))) # # initialize all variables # tf.variables_initializer(self._global_vars('')).run() # self._sync_optimizers() # self._init_target_net() def logs(self, prefix=''): logs = [] logs += [('stats_o/mean', np.mean(self.sess.run([self.o_stats.mean])))] logs += [('stats_o/std', np.mean(self.sess.run([self.o_stats.std])))] logs += [('stats_g/mean', np.mean(self.sess.run([self.g_stats.mean])))] logs += [('stats_g/std', np.mean(self.sess.run([self.g_stats.std])))] if prefix is not '' and not prefix.endswith('/'): return [(prefix + '/' + key, val) for key, val in logs] else: return logs def __getstate__(self): """Our policies can be loaded from pkl, but after unpickling you cannot continue training. """ excluded_subnames = ['_tf', '_op', '_vars', '_adam', 'buffer', 'sess', '_stats', # 'main', 'target', 'lock', 'env', 'sample_transitions', #original code 'main', 'target1', 'target2', 'lock', 'env', 'sample_transitions', 'stage_shapes', 'create_actor_critic'] state = {k: v for k, v in self.__dict__.items() if all([not subname in k for subname in excluded_subnames])} state['buffer_size'] = self.buffer_size state['tf'] = self.sess.run([x for x in self._global_vars('') if 'buffer' not in x.name]) return state def __setstate__(self, state): if 'sample_transitions' not in state: # We don't need this for playing the policy. state['sample_transitions'] = None self.__init__(**state) # set up stats (they are overwritten in __init__) for k, v in state.items(): if k[-6:] == '_stats': self.__dict__[k] = v # load TF variables vars = [x for x in self._global_vars('') if 'buffer' not in x.name] assert(len(vars) == len(state["tf"])) node = [tf.assign(var, val) for var, val in zip(vars, state["tf"])] self.sess.run(node) def save(self, save_path): tf_util.save_variables(save_path)
class DDPG(object): @store_args def __init__(self, input_dims, buffer_size, hidden, layers, network_class, polyak, batch_size, Q_lr, pi_lr, norm_eps, norm_clip, max_u, action_l2, clip_obs, scope, T, rollout_batch_size, subtract_goals, relative_goals, clip_pos_returns, clip_return, bc_loss, q_filter, num_demo, demo_batch_size, prm_loss_weight, aux_loss_weight, sample_transitions, gamma, reuse=False, **kwargs): """Implementation of DDPG that is used in combination with Hindsight Experience Replay (HER). Added functionality to use demonstrations for training to Overcome exploration problem. Args: input_dims (dict of ints): dimensions for the observation (o), the goal (g), and the actions (u) buffer_size (int): number of transitions that are stored in the replay buffer hidden (int): number of units in the hidden layers layers (int): number of hidden layers network_class (str): the network class that should be used (e.g. 'baselines.her.ActorCritic') polyak (float): coefficient for Polyak-averaging of the target network batch_size (int): batch size for training Q_lr (float): learning rate for the Q (critic) network pi_lr (float): learning rate for the pi (actor) network norm_eps (float): a small value used in the normalizer to avoid numerical instabilities norm_clip (float): normalized inputs are clipped to be in [-norm_clip, norm_clip] max_u (float): maximum action magnitude, i.e. actions are in [-max_u, max_u] action_l2 (float): coefficient for L2 penalty on the actions clip_obs (float): clip observations before normalization to be in [-clip_obs, clip_obs] scope (str): the scope used for the TensorFlow graph T (int): the time horizon for rollouts rollout_batch_size (int): number of parallel rollouts per DDPG agent subtract_goals (function): function that subtracts goals from each other relative_goals (boolean): whether or not relative goals should be fed into the network clip_pos_returns (boolean): whether or not positive returns should be clipped clip_return (float): clip returns to be in [-clip_return, clip_return] sample_transitions (function) function that samples from the replay buffer gamma (float): gamma used for Q learning updates reuse (boolean): whether or not the networks should be reused bc_loss: whether or not the behavior cloning loss should be used as an auxilliary loss q_filter: whether or not a filter on the q value update should be used when training with demonstartions num_demo: Number of episodes in to be used in the demonstration buffer demo_batch_size: number of samples to be used from the demonstrations buffer, per mpi thread prm_loss_weight: Weight corresponding to the primary loss aux_loss_weight: Weight corresponding to the auxilliary loss also called the cloning loss """ if self.clip_return is None: self.clip_return = np.inf self.create_actor_critic = import_function(self.network_class) input_shapes = dims_to_shapes(self.input_dims) self.dimo = self.input_dims['o'] self.dimg = self.input_dims['g'] self.dimu = self.input_dims['u'] # Prepare staging area for feeding data to the model. stage_shapes = OrderedDict() for key in sorted(self.input_dims.keys()): if key.startswith('info_'): continue stage_shapes[key] = (None, *input_shapes[key]) for key in ['o', 'g']: stage_shapes[key + '_2'] = stage_shapes[key] stage_shapes['r'] = (None, ) self.stage_shapes = stage_shapes # Create network. with tf.variable_scope(self.scope): self.staging_tf = StagingArea( dtypes=[tf.float32 for _ in self.stage_shapes.keys()], shapes=list(self.stage_shapes.values())) self.buffer_ph_tf = [ tf.placeholder(tf.float32, shape=shape) for shape in self.stage_shapes.values() ] self.stage_op = self.staging_tf.put(self.buffer_ph_tf) self._create_network(reuse=reuse) # Configure the replay buffer. buffer_shapes = { key: (self.T - 1 if key != 'o' else self.T, *input_shapes[key]) for key, val in input_shapes.items() } buffer_shapes['g'] = (buffer_shapes['g'][0], self.dimg) buffer_shapes['ag'] = (self.T, self.dimg) buffer_size = (self.buffer_size // self.rollout_batch_size) * self.rollout_batch_size self.buffer = ReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions) global DEMO_BUFFER DEMO_BUFFER = ReplayBuffer( buffer_shapes, buffer_size, self.T, self.sample_transitions ) #initialize the demo buffer; in the same way as the primary data buffer def _random_action(self, n): return np.random.uniform(low=-self.max_u, high=self.max_u, size=(n, self.dimu)) def _preprocess_og(self, o, ag, g): if self.relative_goals: g_shape = g.shape g = g.reshape(-1, self.dimg) ag = ag.reshape(-1, self.dimg) g = self.subtract_goals(g, ag) g = g.reshape(*g_shape) o = np.clip(o, -self.clip_obs, self.clip_obs) g = np.clip(g, -self.clip_obs, self.clip_obs) return o, g def step(self, obs): actions = self.get_actions(obs['observation'], obs['achieved_goal'], obs['desired_goal']) return actions, None, None, None def get_actions(self, o, ag, g, noise_eps=0., random_eps=0., use_target_net=False, compute_Q=False): o, g = self._preprocess_og(o, ag, g) policy = self.target if use_target_net else self.main # values to compute vals = [policy.pi_tf] if compute_Q: vals += [policy.Q_pi_tf] # feed feed = { policy.o_tf: o.reshape(-1, self.dimo), policy.g_tf: g.reshape(-1, self.dimg), policy.u_tf: np.zeros((o.size // self.dimo, self.dimu), dtype=np.float32) } ret = self.sess.run(vals, feed_dict=feed) # action postprocessing u = ret[0] noise = noise_eps * self.max_u * np.random.randn( *u.shape) # gaussian noise u += noise u = np.clip(u, -self.max_u, self.max_u) u += np.random.binomial(1, random_eps, u.shape[0]).reshape(-1, 1) * ( self._random_action(u.shape[0]) - u) # eps-greedy if u.shape[0] == 1: u = u[0] u = u.copy() ret[0] = u if len(ret) == 1: return ret[0] else: return ret def init_demo_buffer( self, demoDataFile, update_stats=True): #function that initializes the demo buffer demoData = np.load( demoDataFile) #load the demonstration data from data file info_keys = [ key.replace('info_', '') for key in self.input_dims.keys() if key.startswith('info_') ] info_values = [ np.empty((self.T - 1, 1, self.input_dims['info_' + key]), np.float32) for key in info_keys ] demo_data_obs = demoData['obs'] demo_data_acs = demoData['acs'] demo_data_info = demoData['info'] for epsd in range( self.num_demo ): # we initialize the whole demo buffer at the start of the training obs, acts, goals, achieved_goals = [], [], [], [] i = 0 for transition in range(self.T - 1): obs.append( [demo_data_obs[epsd][transition].get('observation')]) acts.append([demo_data_acs[epsd][transition]]) goals.append( [demo_data_obs[epsd][transition].get('desired_goal')]) achieved_goals.append( [demo_data_obs[epsd][transition].get('achieved_goal')]) for idx, key in enumerate(info_keys): info_values[idx][transition, i] = demo_data_info[epsd][transition][key] obs.append([demo_data_obs[epsd][self.T - 1].get('observation')]) achieved_goals.append( [demo_data_obs[epsd][self.T - 1].get('achieved_goal')]) episode = dict(o=obs, u=acts, g=goals, ag=achieved_goals) for key, value in zip(info_keys, info_values): episode['info_{}'.format(key)] = value episode = convert_episode_to_batch_major(episode) global DEMO_BUFFER DEMO_BUFFER.store_episode( episode ) # create the observation dict and append them into the demonstration buffer logger.debug("Demo buffer size currently ", DEMO_BUFFER.get_current_size() ) #print out the demonstration buffer size if update_stats: # add transitions to normalizer to normalize the demo data as well episode['o_2'] = episode['o'][:, 1:, :] episode['ag_2'] = episode['ag'][:, 1:, :] num_normalizing_transitions = transitions_in_episode_batch( episode) transitions = self.sample_transitions( episode, num_normalizing_transitions) o, g, ag = transitions['o'], transitions['g'], transitions[ 'ag'] transitions['o'], transitions['g'] = self._preprocess_og( o, ag, g) # No need to preprocess the o_2 and g_2 since this is only used for stats self.o_stats.update(transitions['o']) self.g_stats.update(transitions['g']) self.o_stats.recompute_stats() self.g_stats.recompute_stats() episode.clear() logger.info("Demo buffer size: ", DEMO_BUFFER.get_current_size() ) #print out the demonstration buffer size def store_episode(self, episode_batch, update_stats=True): """ episode_batch: array of batch_size x (T or T+1) x dim_key 'o' is of size T+1, others are of size T """ self.buffer.store_episode(episode_batch) if update_stats: # add transitions to normalizer episode_batch['o_2'] = episode_batch['o'][:, 1:, :] episode_batch['ag_2'] = episode_batch['ag'][:, 1:, :] num_normalizing_transitions = transitions_in_episode_batch( episode_batch) transitions = self.sample_transitions(episode_batch, num_normalizing_transitions) o, g, ag = transitions['o'], transitions['g'], transitions['ag'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) # No need to preprocess the o_2 and g_2 since this is only used for stats self.o_stats.update(transitions['o']) self.g_stats.update(transitions['g']) self.o_stats.recompute_stats() self.g_stats.recompute_stats() def get_current_buffer_size(self): return self.buffer.get_current_size() def _sync_optimizers(self): self.Q_adam.sync() self.pi_adam.sync() def _grads(self): # Avoid feed_dict here for performance! critic_loss, actor_loss, Q_grad, pi_grad = self.sess.run([ self.Q_loss_tf, self.main.Q_pi_tf, self.Q_grad_tf, self.pi_grad_tf ]) return critic_loss, actor_loss, Q_grad, pi_grad def _update(self, Q_grad, pi_grad): self.Q_adam.update(Q_grad, self.Q_lr) self.pi_adam.update(pi_grad, self.pi_lr) def sample_batch(self): if self.bc_loss: #use demonstration buffer to sample as well if bc_loss flag is set TRUE transitions = self.buffer.sample(self.batch_size - self.demo_batch_size) global DEMO_BUFFER transitions_demo = DEMO_BUFFER.sample( self.demo_batch_size) #sample from the demo buffer for k, values in transitions_demo.items(): rolloutV = transitions[k].tolist() for v in values: rolloutV.append(v.tolist()) transitions[k] = np.array(rolloutV) else: transitions = self.buffer.sample( self.batch_size) #otherwise only sample from primary buffer o, o_2, g = transitions['o'], transitions['o_2'], transitions['g'] ag, ag_2 = transitions['ag'], transitions['ag_2'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) transitions['o_2'], transitions['g_2'] = self._preprocess_og( o_2, ag_2, g) assert np.array_equal(transitions['g_2'], transitions['g']) transitions_batch = [ transitions[key] for key in self.stage_shapes.keys() ] return transitions_batch def stage_batch(self, batch=None): if batch is None: batch = self.sample_batch() assert len(self.buffer_ph_tf) == len(batch) self.sess.run(self.stage_op, feed_dict=dict(zip(self.buffer_ph_tf, batch))) def train(self, stage=True): if stage: self.stage_batch() critic_loss, actor_loss, Q_grad, pi_grad = self._grads() self._update(Q_grad, pi_grad) return critic_loss, actor_loss def _init_target_net(self): self.sess.run(self.init_target_net_op) def update_target_net(self): self.sess.run(self.update_target_net_op) def clear_buffer(self): self.buffer.clear_buffer() def _vars(self, scope): res = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.scope + '/' + scope) assert len(res) > 0 return res def _global_vars(self, scope): res = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.scope + '/' + scope) return res def _create_network(self, reuse=False): logger.info("Creating a DDPG agent with action space %d x %s..." % (self.dimu, self.max_u)) self.sess = tf_util.get_session() # running averages with tf.variable_scope('o_stats') as vs: if reuse: vs.reuse_variables() self.o_stats = Normalizer(self.dimo, self.norm_eps, self.norm_clip, sess=self.sess) with tf.variable_scope('g_stats') as vs: if reuse: vs.reuse_variables() self.g_stats = Normalizer(self.dimg, self.norm_eps, self.norm_clip, sess=self.sess) # mini-batch sampling. batch = self.staging_tf.get() batch_tf = OrderedDict([ (key, batch[i]) for i, key in enumerate(self.stage_shapes.keys()) ]) batch_tf['r'] = tf.reshape(batch_tf['r'], [-1, 1]) #choose only the demo buffer samples mask = np.concatenate( (np.zeros(self.batch_size - self.demo_batch_size), np.ones(self.demo_batch_size)), axis=0) # networks with tf.variable_scope('main') as vs: if reuse: vs.reuse_variables() self.main = self.create_actor_critic(batch_tf, net_type='main', **self.__dict__) vs.reuse_variables() with tf.variable_scope('target') as vs: if reuse: vs.reuse_variables() target_batch_tf = batch_tf.copy() target_batch_tf['o'] = batch_tf['o_2'] target_batch_tf['g'] = batch_tf['g_2'] self.target = self.create_actor_critic(target_batch_tf, net_type='target', **self.__dict__) vs.reuse_variables() assert len(self._vars("main")) == len(self._vars("target")) # loss functions target_Q_pi_tf = self.target.Q_pi_tf clip_range = (-self.clip_return, 0. if self.clip_pos_returns else np.inf) target_tf = tf.clip_by_value( batch_tf['r'] + self.gamma * target_Q_pi_tf, *clip_range) self.Q_loss_tf = tf.reduce_mean( tf.square(tf.stop_gradient(target_tf) - self.main.Q_tf)) if self.bc_loss == 1 and self.q_filter == 1: # train with demonstrations and use bc_loss and q_filter both maskMain = tf.reshape( tf.boolean_mask(self.main.Q_tf > self.main.Q_pi_tf, mask), [-1] ) #where is the demonstrator action better than actor action according to the critic? choose those samples only #define the cloning loss on the actor's actions only on the samples which adhere to the above masks self.cloning_loss_tf = tf.reduce_sum( tf.square( tf.boolean_mask(tf.boolean_mask((self.main.pi_tf), mask), maskMain, axis=0) - tf.boolean_mask(tf.boolean_mask((batch_tf['u']), mask), maskMain, axis=0))) self.pi_loss_tf = -self.prm_loss_weight * tf.reduce_mean( self.main.Q_pi_tf ) #primary loss scaled by it's respective weight prm_loss_weight self.pi_loss_tf += self.prm_loss_weight * self.action_l2 * tf.reduce_mean( tf.square(self.main.pi_tf / self.max_u) ) #L2 loss on action values scaled by the same weight prm_loss_weight self.pi_loss_tf += self.aux_loss_weight * self.cloning_loss_tf #adding the cloning loss to the actor loss as an auxilliary loss scaled by its weight aux_loss_weight elif self.bc_loss == 1 and self.q_filter == 0: # train with demonstrations without q_filter self.cloning_loss_tf = tf.reduce_sum( tf.square( tf.boolean_mask((self.main.pi_tf), mask) - tf.boolean_mask((batch_tf['u']), mask))) self.pi_loss_tf = -self.prm_loss_weight * tf.reduce_mean( self.main.Q_pi_tf) self.pi_loss_tf += self.prm_loss_weight * self.action_l2 * tf.reduce_mean( tf.square(self.main.pi_tf / self.max_u)) self.pi_loss_tf += self.aux_loss_weight * self.cloning_loss_tf else: #If not training with demonstrations self.pi_loss_tf = -tf.reduce_mean(self.main.Q_pi_tf) self.pi_loss_tf += self.action_l2 * tf.reduce_mean( tf.square(self.main.pi_tf / self.max_u)) Q_grads_tf = tf.gradients(self.Q_loss_tf, self._vars('main/Q')) pi_grads_tf = tf.gradients(self.pi_loss_tf, self._vars('main/pi')) assert len(self._vars('main/Q')) == len(Q_grads_tf) assert len(self._vars('main/pi')) == len(pi_grads_tf) self.Q_grads_vars_tf = zip(Q_grads_tf, self._vars('main/Q')) self.pi_grads_vars_tf = zip(pi_grads_tf, self._vars('main/pi')) self.Q_grad_tf = flatten_grads(grads=Q_grads_tf, var_list=self._vars('main/Q')) self.pi_grad_tf = flatten_grads(grads=pi_grads_tf, var_list=self._vars('main/pi')) # optimizers self.Q_adam = MpiAdam(self._vars('main/Q'), scale_grad_by_procs=False) self.pi_adam = MpiAdam(self._vars('main/pi'), scale_grad_by_procs=False) # polyak averaging self.main_vars = self._vars('main/Q') + self._vars('main/pi') self.target_vars = self._vars('target/Q') + self._vars('target/pi') self.stats_vars = self._global_vars('o_stats') + self._global_vars( 'g_stats') self.init_target_net_op = list( map(lambda v: v[0].assign(v[1]), zip(self.target_vars, self.main_vars))) self.update_target_net_op = list( map( lambda v: v[0].assign(self.polyak * v[0] + (1. - self.polyak) * v[1]), zip(self.target_vars, self.main_vars))) # initialize all variables tf.variables_initializer(self._global_vars('')).run() self._sync_optimizers() self._init_target_net() def logs(self, prefix=''): logs = [] logs += [('stats_o/mean', np.mean(self.sess.run([self.o_stats.mean])))] logs += [('stats_o/std', np.mean(self.sess.run([self.o_stats.std])))] logs += [('stats_g/mean', np.mean(self.sess.run([self.g_stats.mean])))] logs += [('stats_g/std', np.mean(self.sess.run([self.g_stats.std])))] if prefix != '' and not prefix.endswith('/'): return [(prefix + '/' + key, val) for key, val in logs] else: return logs def __getstate__(self): """Our policies can be loaded from pkl, but after unpickling you cannot continue training. """ excluded_subnames = [ '_tf', '_op', '_vars', '_adam', 'buffer', 'sess', '_stats', 'main', 'target', 'lock', 'env', 'sample_transitions', 'stage_shapes', 'create_actor_critic' ] state = { k: v for k, v in self.__dict__.items() if all([not subname in k for subname in excluded_subnames]) } state['buffer_size'] = self.buffer_size state['tf'] = self.sess.run( [x for x in self._global_vars('') if 'buffer' not in x.name]) return state def __setstate__(self, state): if 'sample_transitions' not in state: # We don't need this for playing the policy. state['sample_transitions'] = None self.__init__(**state) # set up stats (they are overwritten in __init__) for k, v in state.items(): if k[-6:] == '_stats': self.__dict__[k] = v # load TF variables vars = [x for x in self._global_vars('') if 'buffer' not in x.name] assert (len(vars) == len(state["tf"])) node = [tf.assign(var, val) for var, val in zip(vars, state["tf"])] self.sess.run(node) def save(self, save_path): tf_util.save_variables(save_path)
class DDPG(object): @store_args def __init__(self, input_dims, buffer_size, hidden, layers, network_class, polyak, batch_size, Q_lr, pi_lr, norm_eps, norm_clip, action_scale, action_l2, clip_obs, scope, T, rollout_batch_size, subtract_goals, relative_goals, clip_pos_returns, clip_return, bc_loss, q_filter, num_demo, demo_batch_size, prm_loss_weight, aux_loss_weight, sample_transitions, gamma, temperature, prioritization, env_name, alpha, beta0, beta_iters, total_timesteps, rank_method, reuse=False, **kwargs): """ Implementation of DDPG that is used in combination with Hindsight Experience Replay (HER). Added functionality to use demonstrations for training to Overcome exploration problem. Args: :param input_dims (dict of ints): dimensions for the observation (o), the goal (g), and the actions (u) :param buffer_size (int): number of transitions that are stored in the replay buffer :param hidden (int): number of units in the hidden layers :param layers (int): number of hidden layers :param network_class (str): the network class that should be used (e.g. 'baselines.her.ActorCritic') :param polyak (float): coefficient for Polyak-averaging of the target network :param batch_size (int): batch size for training :param Q_lr (float): learning rate for the Q (critic) network :param pi_lr (float): learning rate for the pi (actor) network :param norm_eps (float): a small value used in the normalizer to avoid numerical instabilities :param norm_clip (float): normalized inputs are clipped to be in [-norm_clip, norm_clip] :param action_scale(float): maximum action magnitude, i.e. actions are in [-max_u, max_u] :param action_l2 (float): coefficient for L2 penalty on the actions :param clip_obs (float): clip observations before normalization to be in [-clip_obs, clip_obs] :param scope (str): the scope used for the TensorFlow graph :param T (int): the time horizon for rollouts :param rollout_batch_size (int): number of parallel rollouts per DDPG agent :param subtract_goals (function): function that subtracts goals from each other :param relative_goals (boolean): whether or not relative goals should be fed into the network :param clip_pos_returns (boolean): whether or not positive returns should be clipped :param clip_return (float): clip returns to be in [-clip_return, clip_return] :param sample_transitions (function) function that samples from the replay buffer :param gamma (float): gamma used for Q learning updates :param reuse (boolean): whether or not the networks should be reused :param bc_loss: whether or not the behavior cloning loss should be used as an auxilliary loss :param q_filter: whether or not a filter on the q value update should be used when training with demonstartions :param num_demo: Number of episodes in to be used in the demonstration buffer :param demo_batch_size: number of samples to be used from the demonstrations buffer, per mpi thread :param prm_loss_weight: Weight corresponding to the primary loss :param aux_loss_weight: Weight corresponding to the auxilliary loss also called the cloning loss """ if self.clip_return is None: self.clip_return = np.inf self.create_actor_critic = import_function( self.network_class) # points to actor_critic.py self.input_dims = input_dims input_shapes = dims_to_shapes(input_dims) self.dimo = input_dims['o'] self.dimg = input_dims['g'] self.dimu = input_dims['u'] self.sample_count = 1 self.cycle_count = 1 self.critic_loss_episode = [] self.actor_loss_episode = [] self.critic_loss_avg = [] self.actor_loss_avg = [] # Energy based parameters self.prioritization = prioritization self.env_name = env_name self.temperature = temperature self.rank_method = rank_method # Prepare staging area for feeding data to the model. stage_shapes = OrderedDict() for key in sorted(self.input_dims.keys()): if key.startswith('info_'): continue stage_shapes[key] = (None, *input_shapes[key]) for key in ['o', 'g']: stage_shapes[key + '_2'] = stage_shapes[key] stage_shapes['r'] = (None, ) self.stage_shapes = stage_shapes # Create network. with tf.variable_scope(self.scope): self.staging_tf = StagingArea( dtypes=[tf.float32 for _ in self.stage_shapes.keys()], shapes=list(self.stage_shapes.values())) self.buffer_ph_tf = [ tf.placeholder(tf.float32, shape=shape) for shape in self.stage_shapes.values() ] self.stage_op = self.staging_tf.put(self.buffer_ph_tf) self._create_network(reuse=reuse) # Creates DDPG agent # Configure the replay buffer. buffer_shapes = { key: (self.T - 1 if key != 'o' else self.T, *input_shapes[key]) for key, val in input_shapes.items() } buffer_shapes['g'] = (buffer_shapes['g'][0], self.dimg) buffer_shapes['ag'] = (self.T, self.dimg) buffer_size = (self.buffer_size // self.rollout_batch_size) * self.rollout_batch_size # print("begin init") if self.prioritization == 'energy': self.buffer = ReplayBufferEnergy(buffer_shapes, buffer_size, self.T, self.sample_transitions, self.prioritization, self.env_name) # elif self.prioritization == 'tderror': # self.buffer = PrioritizedReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions, alpha) # if beta_iters is None: # beta_iters = total_timesteps # self.beta_schedule = LinearSchedule(beta_iters, initial_p=beta0, final_p=1.0) else: self.buffer = ReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions) # print("finish init") def _random_action(self, n): return np.random.uniform(low=-self.action_scale, high=self.action_scale, size=(n, self.dimu)) def _preprocess_og(self, o, ag, g): if self.relative_goals: # no self.relative_goals print("self.relative_goals: ", self.relative_goals) g_shape = g.shape g = g.reshape(-1, self.dimg) ag = ag.reshape(-1, self.dimg) g = self.subtract_goals(g, ag) g = g.reshape(*g_shape) # Clip (limit) the values in an array. o = np.clip(o, -self.clip_obs, self.clip_obs) g = np.clip(g, -self.clip_obs, self.clip_obs) return o, g # Not used def step(self, obs): actions = self.get_actions(obs['observation'], obs['achieved_goal'], obs['desired_goal']) return actions, None, None, None def get_actions(self, o, ag, g, noise_eps=0., random_eps=0., use_target_net=False, compute_Q=False): o, g = self._preprocess_og(o, ag, g) # Use target network use main network policy = self.target if use_target_net else self.main # values to compute policy_weights = [policy.actor_tf] if compute_Q: policy_weights += [policy.critic_with_actor_tf] # feeds agent_feed = { policy.obs: o.reshape(-1, self.dimo), policy.goals: g.reshape(-1, self.dimg), policy.actions: np.zeros((o.size // self.dimo, self.dimu), dtype=np.float32) } # Evaluating policy weights with agent information ret = self.sess.run(policy_weights, feed_dict=agent_feed) # print(ret) # action postprocessing action = ret[0] noise = noise_eps * self.action_scale * np.random.randn( *action.shape) # gaussian noise action += noise action = np.clip(action, -self.action_scale, self.action_scale) action += np.random.binomial(1, random_eps, action.shape[0]).reshape( -1, 1) * (self._random_action(action.shape[0]) - action ) # eps-greedy if action.shape[0] == 1: action = action[0] action = action.copy() ret[0] = action if len(ret) == 1: return ret[0] else: return ret # Not used # def init_demo_buffer(self, demoDataFile, update_stats=True): # function that initializes the demo buffer # # demoData = np.load(demoDataFile) # load the demonstration data from data file # info_keys = [key.replace('info_', '') for key in self.input_dims.keys() if key.startswith('info_')] # info_values = [np.empty((self.T - 1, 1, self.input_dims['info_' + key]), np.float32) for key in info_keys] # # demo_data_obs = demoData['obs'] # demo_data_acs = demoData['acs'] # demo_data_info = demoData['info'] # # for epsd in range(self.num_demo): # we initialize the whole demo buffer at the start of the training # obs, acts, goals, achieved_goals = [], [], [], [] # i = 0 # for transition in range(self.T - 1): # obs.append([demo_data_obs[epsd][transition].get('observation')]) # acts.append([demo_data_acs[epsd][transition]]) # goals.append([demo_data_obs[epsd][transition].get('desired_goal')]) # achieved_goals.append([demo_data_obs[epsd][transition].get('achieved_goal')]) # for idx, key in enumerate(info_keys): # info_values[idx][transition, i] = demo_data_info[epsd][transition][key] # # obs.append([demo_data_obs[epsd][self.T - 1].get('observation')]) # achieved_goals.append([demo_data_obs[epsd][self.T - 1].get('achieved_goal')]) # # episode = dict(observations=obs, # u=acts, # g=goals, # ag=achieved_goals) # for key, value in zip(info_keys, info_values): # episode['info_{}'.format(key)] = value # # episode = convert_episode_to_batch_major(episode) # global DEMO_BUFFER # DEMO_BUFFER.ddpg_store_episode( # episode) # create the observation dict and append them into the demonstration buffer # logger.debug("Demo buffer size currently ", # DEMO_BUFFER.get_current_size()) # print out the demonstration buffer size # # if update_stats: # # add transitions to normalizer to normalize the demo data as well # episode['o_2'] = episode['o'][:, 1:, :] # episode['ag_2'] = episode['ag'][:, 1:, :] # num_normalizing_transitions = transitions_in_episode_batch(episode) # transitions = self.sample_transitions(episode, num_normalizing_transitions) # # o, g, ag = transitions['o'], transitions['g'], transitions['ag'] # transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) # # No need to preprocess the o_2 and g_2 since this is only used for stats # # self.o_stats.update(transitions['o']) # self.g_stats.update(transitions['g']) # # self.o_stats.recompute_stats() # self.g_stats.recompute_stats() # episode.clear() # # logger.info("Demo buffer size: ", DEMO_BUFFER.get_current_size()) # print out the demonstration buffer size def ddpg_store_episode(self, episode_batch, dump_buffer, w_potential, w_linear, w_rotational, rank_method, clip_energy, update_stats=True): """ episode_batch: array of batch_size x (T or T+1) x dim_key 'o' is of size T+1, others are of size T """ # if self.prioritization == 'tderror': # self.buffer.store_episode(episode_batch, dump_buffer) # print("DDPG BEGIN STORE episode") if self.prioritization == 'energy': self.buffer.store_episode(episode_batch, w_potential, w_linear, w_rotational, rank_method, clip_energy) else: self.buffer.store_episode(episode_batch) # print("DDPG END STORE episode") if update_stats: # add transitions to normalizer episode_batch['o_2'] = episode_batch['o'][:, 1:, :] episode_batch['ag_2'] = episode_batch['ag'][:, 1:, :] num_normalizing_transitions = transitions_in_episode_batch( episode_batch) # print("START ddpg sample transition") # n_cycles calls HER sampler if self.prioritization == 'energy': if not self.buffer.current_size == 0 and not len( episode_batch['ag']) == 0: transitions = self.sample_transitions( episode_batch, num_normalizing_transitions, 'none', 1.0, self.sample_count, self.cycle_count, True) # elif self.prioritization == 'tderror': # transitions, weights, episode_idxs = \ # self.sample_transitions(self.buffer, episode_batch, num_normalizing_transitions, beta=0) else: transitions = self.sample_transitions( episode_batch, num_normalizing_transitions) # print("END ddpg sample transition") # print("DDPG END STORE episode 2") o, g, ag = transitions['o'], transitions['g'], transitions['ag'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) # No need to preprocess the o_2 and g_2 since this is only used for stats self.o_stats.update(transitions['o']) self.g_stats.update(transitions['g']) self.o_stats.recompute_stats() self.g_stats.recompute_stats() def get_current_buffer_size(self): return self.buffer.get_current_size() def _sync_optimizers(self): self.critic_optimiser.sync() self.actor_optimiser.sync() def _grads(self): # Avoid feed_dict here for performance! critic_loss, actor_loss, critic_grad, actor_grad, td_error = self.sess.run( [ self.critic_loss_tf, # MSE of target_tf - main.critic_tf self.main.critic_with_actor_tf, # actor_loss self.critic_grads, self.actor_grads, self.td_error_tf ]) return critic_loss, actor_loss, critic_grad, actor_grad, td_error def _update(self, critic_grads, actor_grads): self.critic_optimiser.update(critic_grads, self.Q_lr) self.actor_optimiser.update(actor_grads, self.pi_lr) def sample_batch(self, t): # print("Begin Sample batch") if self.prioritization == 'energy': transitions = self.buffer.sample(self.batch_size, self.rank_method, temperature=self.temperature) weights = np.ones_like(transitions['r']).copy() # print("reach?") # elif self.prioritization == 'tderror': # transitions, weights, idxs = self.buffer.sample(self.batch_size, beta=self.beta_schedule.value(t)) else: transitions = self.buffer.sample(self.batch_size) weights = np.ones_like(transitions['r']).copy() o, o_2, g = transitions['o'], transitions['o_2'], transitions['g'] ag, ag_2 = transitions['ag'], transitions['ag_2'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) transitions['o_2'], transitions['g_2'] = self._preprocess_og( o_2, ag_2, g) transitions['w'] = weights.flatten().copy() # note: ordered dict transitions_batch = [ transitions[key] for key in self.stage_shapes.keys() ] # if self.prioritization == 'tderror': # return (transitions_batch, idxs) # else: # print("End sample batch") return transitions_batch def stage_batch(self, t, batch=None): if batch is None: # if self.prioritization == 'tderror': # batch, idxs = self.sample_batch(t) # else: batch = self.sample_batch(t) assert len(self.buffer_ph_tf) == len(batch) self.sess.run(self.stage_op, feed_dict=dict(zip(self.buffer_ph_tf, batch))) # if self.prioritization == 'tderror': # return idxs def ddpg_train(self, t, dump_buffer, stage=True): if stage: # if self.prioritization == 'tderror': # idxs = self.stage_batch(t) # else: self.stage_batch(t) self.critic_loss, self.actor_loss, Q_grad, pi_grad, td_error = self._grads( ) # if self.prioritization == 'tderror': # new_priorities = np.abs(td_error) + self.eps # td_error # if dump_buffer: # T = self.buffer.buffers['u'].shape[1] # episode_idxs = idxs // T # t_samples = idxs % T # batch_size = td_error.shape[0] # with self.buffer.lock: # for i in range(batch_size): # self.buffer.buffers['td'][episode_idxs[i]][t_samples[i]] = td_error[i] # # self.buffer.update_priorities(idxs, new_priorities) # Update gradients for actor and critic networks self._update(Q_grad, pi_grad) # My variables self.visual_actor_loss = 1 - self.actor_loss self.critic_loss_episode.append(self.critic_loss) self.actor_loss_episode.append(self.visual_actor_loss) # print("Critic loss: ", self.critic_loss, " Actor loss: ", self.actor_loss) return self.critic_loss, np.mean(self.actor_loss) def _init_target_net(self): self.sess.run(self.init_target_net_op) def ddpg_update_target_net(self): # print("ddpg_cycle", self.cycle_count) self.cycle_count += 1 self.critic_loss_avg = np.mean(self.critic_loss_episode) self.actor_loss_avg = np.mean(self.actor_loss_episode) self.sess.run(self.update_target_net_op) def clear_buffer(self): self.buffer.clear_buffer() def _vars(self, scope): res = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.scope + '/' + scope) assert len(res) > 0 return res def _global_vars(self, scope): res = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.scope + '/' + scope) return res def _create_network(self, reuse=False): logger.info("Creating a DDPG agent with action space %d x %s..." % (self.dimu, self.action_scale)) self.sess = tf_util.get_session() # running averages with tf.variable_scope('o_stats') as variable_scope: if reuse: variable_scope.reuse_variables() self.o_stats = Normalizer(self.dimo, self.norm_eps, self.norm_clip, sess=self.sess) with tf.variable_scope('g_stats') as variable_scope: if reuse: variable_scope.reuse_variables() self.g_stats = Normalizer(self.dimg, self.norm_eps, self.norm_clip, sess=self.sess) # mini-batch sampling. batch = self.staging_tf.get() batch_tf = OrderedDict([ (key, batch[i]) for i, key in enumerate(self.stage_shapes.keys()) ]) batch_tf['r'] = tf.reshape(batch_tf['r'], [-1, 1]) # choose only the demo buffer samples mask = np.concatenate( (np.zeros(self.batch_size - self.demo_batch_size), np.ones(self.demo_batch_size)), axis=0) # networks with tf.variable_scope('main') as variable_scope: if reuse: variable_scope.reuse_variables() # Create actor critic network self.main = self.create_actor_critic(batch_tf, net_type='main', **self.__dict__) variable_scope.reuse_variables() with tf.variable_scope('target') as variable_scope: if reuse: variable_scope.reuse_variables() target_batch_tf = batch_tf.copy() target_batch_tf['o'] = batch_tf['o_2'] target_batch_tf['g'] = batch_tf['g_2'] self.target = self.create_actor_critic(target_batch_tf, net_type='target', **self.__dict__) variable_scope.reuse_variables() assert len(self._vars("main")) == len(self._vars("target")) # loss functions target_critic_actor_tf = self.target.critic_with_actor_tf clip_range = (-self.clip_return, 0. if self.clip_pos_returns else np.inf) target_tf = tf.clip_by_value( batch_tf['r'] + self.gamma * target_critic_actor_tf, *clip_range) # MSE of target_tf - critic_tf. This is the TD Learning step self.td_error_tf = tf.stop_gradient(target_tf) - self.main.critic_tf self.critic_loss_tf = tf.reduce_mean( tf.square(tf.stop_gradient(target_tf) - self.main.critic_tf)) # self.actor_loss_tf = -tf.reduce_mean(self.main.critic_with_actor_tf) self.actor_loss_tf += self.action_l2 * tf.reduce_mean( tf.square(self.main.actor_tf / self.action_scale)) # Constructs symbolic derivatives of sum of critic_loss_tf vs _vars('main/Q') critic_grads_tf = tf.gradients(self.critic_loss_tf, self._vars('main/Q')) actor_grads_tf = tf.gradients(self.actor_loss_tf, self._vars('main/pi')) assert len(self._vars('main/Q')) == len(critic_grads_tf) assert len(self._vars('main/pi')) == len(actor_grads_tf) self.critic_grads_vars_tf = zip(critic_grads_tf, self._vars('main/Q')) self.actor_grads_vars_tf = zip(actor_grads_tf, self._vars('main/pi')) # Flattens variables and their gradients. self.critic_grads = flatten_grads(grads=critic_grads_tf, var_list=self._vars('main/Q')) self.actor_grads = flatten_grads(grads=actor_grads_tf, var_list=self._vars('main/pi')) # optimizers self.critic_optimiser = MpiAdam(self._vars('main/Q'), scale_grad_by_procs=False) self.actor_optimiser = MpiAdam(self._vars('main/pi'), scale_grad_by_procs=False) # polyak averaging used to update target network self.main_vars = self._vars('main/Q') + self._vars('main/pi') self.target_vars = self._vars('target/Q') + self._vars('target/pi') self.stats_vars = self._global_vars('o_stats') + self._global_vars( 'g_stats') # list( map( lambda( assign() ), zip())) self.init_target_net_op = list( map( # Apply lambda to each item item in the zipped list lambda v: v[0].assign(v[1]), zip(self.target_vars, self.main_vars))) # Polyak-Ruppert averaging where most recent iterations are weighted more than past iterations. self.update_target_net_op = list( map( # Apply lambda to each item item in the zipped list lambda v: v[0].assign(self.polyak * v[0] + (1. - self.polyak) * v[1]), # polyak averaging zip(self.target_vars, self.main_vars)) # [(target_vars, main_vars), (), ...] ) # initialize all variables tf.variables_initializer(self._global_vars('')).run() self._sync_optimizers() self._init_target_net() def logs(self, prefix=''): logs = [] logs += [('actor_critic/critic_loss', self.critic_loss_avg)] logs += [('actor_critic/actor_loss', self.actor_loss_avg)] logs += [('stats_o/mean', np.mean(self.sess.run([self.o_stats.mean])))] logs += [('stats_o/std', np.mean(self.sess.run([self.o_stats.std])))] logs += [('stats_g/mean', np.mean(self.sess.run([self.g_stats.mean])))] logs += [('stats_g/std', np.mean(self.sess.run([self.g_stats.std])))] # logs += [('critic_loss', np.mean(self.sess.run([self.critic_loss])))] # logs += [('actor_loss', np.mean(self.sess.run([self.actor_loss])))] if prefix != '' and not prefix.endswith('/'): return [(prefix + '/' + key, val) for key, val in logs] else: return logs def __getstate__(self): """Our policies can be loaded from pkl, but after unpickling you cannot continue training. """ excluded_subnames = [ '_tf', '_op', '_vars', '_adam', 'buffer', 'sess', '_stats', 'main', 'target', 'lock', 'env', 'sample_transitions', 'stage_shapes', 'create_actor_critic' ] state = { k: v for k, v in self.__dict__.items() if all([not subname in k for subname in excluded_subnames]) } state['buffer_size'] = self.buffer_size state['tf'] = self.sess.run( [x for x in self._global_vars('') if 'buffer' not in x.name]) return state def __setstate__(self, state): if 'sample_transitions' not in state: # We don't need this for playing the policy. state['sample_transitions'] = None self.__init__(**state) # set up stats (they are overwritten in __init__) for k, v in state.items(): if k[-6:] == '_stats': self.__dict__[k] = v # load TF variables vars = [x for x in self._global_vars('') if 'buffer' not in x.name] assert (len(vars) == len(state["tf"])) node = [tf.assign(var, val) for var, val in zip(vars, state["tf"])] self.sess.run(node) def save(self, save_path): tf_util.save_variables(save_path)
class DDPG(object): def __init__(self, input_dims, buffer_size, hidden, layers, network_class, polyak, batch_size, q_lr, pi_lr, norm_eps, norm_clip, max_u, action_l2, clip_obs, scope, time_horizon, rollout_batch_size, subtract_goals, relative_goals, clip_pos_returns, clip_return, sample_transitions, gamma, reuse=False): """ Implementation of DDPG that is used in combination with Hindsight Experience Replay (HER). :param input_dims: ({str: int}) dimensions for the observation (o), the goal (g), and the actions (u) :param buffer_size: (int) number of transitions that are stored in the replay buffer :param hidden: (int) number of units in the hidden layers :param layers: (int) number of hidden layers :param network_class: (str) the network class that should be used (e.g. 'baselines.her.ActorCritic') :param polyak: (float) coefficient for Polyak-averaging of the target network :param batch_size: (int) batch size for training :param q_lr: (float) learning rate for the Q (critic) network :param pi_lr: (float) learning rate for the pi (actor) network :param norm_eps: (float) a small value used in the normalizer to avoid numerical instabilities :param norm_clip: (float) normalized inputs are clipped to be in [-norm_clip, norm_clip] :param max_u: (float) maximum action magnitude, i.e. actions are in [-max_u, max_u] :param action_l2: (float) coefficient for L2 penalty on the actions :param clip_obs: (float) clip observations before normalization to be in [-clip_obs, clip_obs] :param scope: (str) the scope used for the TensorFlow graph :param time_horizon: (int) the time horizon for rollouts :param rollout_batch_size: (int) number of parallel rollouts per DDPG agent :param subtract_goals: (function (numpy Number, numpy Number): numpy Number) function that subtracts goals from each other :param relative_goals: (boolean) whether or not relative goals should be fed into the network :param clip_pos_returns: (boolean) whether or not positive returns should be clipped :param clip_return: (float) clip returns to be in [-clip_return, clip_return] :param sample_transitions: (function (dict, int): dict) function that samples from the replay buffer :param gamma: (float) gamma used for Q learning updates :param reuse: (boolean) whether or not the networks should be reused """ # Updated in experiments/config.py self.input_dims = input_dims self.buffer_size = buffer_size self.hidden = hidden self.layers = layers self.network_class = network_class self.polyak = polyak self.batch_size = batch_size self.q_lr = q_lr self.pi_lr = pi_lr self.norm_eps = norm_eps self.norm_clip = norm_clip self.max_u = max_u self.action_l2 = action_l2 self.clip_obs = clip_obs self.scope = scope self.time_horizon = time_horizon self.rollout_batch_size = rollout_batch_size self.subtract_goals = subtract_goals self.relative_goals = relative_goals self.clip_pos_returns = clip_pos_returns self.clip_return = clip_return self.sample_transitions = sample_transitions self.gamma = gamma self.reuse = reuse if self.clip_return is None: self.clip_return = np.inf self.create_actor_critic = import_function(self.network_class) input_shapes = dims_to_shapes(self.input_dims) self.dim_obs = self.input_dims['o'] self.dim_goal = self.input_dims['g'] self.dim_action = self.input_dims['u'] # Prepare staging area for feeding data to the model. stage_shapes = OrderedDict() for key in sorted(self.input_dims.keys()): if key.startswith('info_'): continue stage_shapes[key] = (None, *input_shapes[key]) for key in ['o', 'g']: stage_shapes[key + '_2'] = stage_shapes[key] stage_shapes['r'] = (None, ) self.stage_shapes = stage_shapes # Create network. with tf.variable_scope(self.scope): self.staging_tf = StagingArea( dtypes=[tf.float32 for _ in self.stage_shapes.keys()], shapes=list(self.stage_shapes.values())) self.buffer_ph_tf = [ tf.placeholder(tf.float32, shape=shape) for shape in self.stage_shapes.values() ] self.stage_op = self.staging_tf.put(self.buffer_ph_tf) self._create_network(reuse=reuse) # Configure the replay buffer. buffer_shapes = { key: (self.time_horizon if key != 'o' else self.time_horizon + 1, *input_shapes[key]) for key, val in input_shapes.items() } buffer_shapes['g'] = (buffer_shapes['g'][0], self.dim_goal) buffer_shapes['ag'] = (self.time_horizon + 1, self.dim_goal) buffer_size = (self.buffer_size // self.rollout_batch_size) * self.rollout_batch_size self.buffer = ReplayBuffer(buffer_shapes, buffer_size, self.time_horizon, self.sample_transitions) def _random_action(self, num): return np.random.uniform(low=-self.max_u, high=self.max_u, size=(num, self.dim_action)) def _preprocess_obs_goal(self, obs, achieved_goal, goal): if self.relative_goals: g_shape = goal.shape goal = goal.reshape(-1, self.dim_goal) achieved_goal = achieved_goal.reshape(-1, self.dim_goal) goal = self.subtract_goals(goal, achieved_goal) goal = goal.reshape(*g_shape) obs = np.clip(obs, -self.clip_obs, self.clip_obs) goal = np.clip(goal, -self.clip_obs, self.clip_obs) return obs, goal def get_actions(self, obs, achieved_goal, goal, noise_eps=0., random_eps=0., use_target_net=False, compute_q=False): """ return the action from an observation and goal :param obs: (numpy Number) the observation :param achieved_goal: (numpy Number) the achieved goal :param goal: (numpy Number) the goal :param noise_eps: (float) the noise epsilon :param random_eps: (float) the random epsilon :param use_target_net: (bool) whether or not to use the target network :param compute_q: (bool) whether or not to compute Q value :return: (numpy float or float) the actions """ obs, goal = self._preprocess_obs_goal(obs, achieved_goal, goal) policy = self.target if use_target_net else self.main # values to compute vals = [policy.pi_tf] if compute_q: vals += [policy.q_pi_tf] # feed feed = { policy.o_tf: obs.reshape(-1, self.dim_obs), policy.g_tf: goal.reshape(-1, self.dim_goal), policy.u_tf: np.zeros((obs.size // self.dim_obs, self.dim_action), dtype=np.float32) } ret = self.sess.run(vals, feed_dict=feed) # action postprocessing action = ret[0] noise = noise_eps * self.max_u * np.random.randn( *action.shape) # gaussian noise action += noise action = np.clip(action, -self.max_u, self.max_u) # eps-greedy n_ac = action.shape[0] action += np.random.binomial(1, random_eps, n_ac).reshape( -1, 1) * (self._random_action(n_ac) - action) if action.shape[0] == 1: action = action[0] action = action.copy() ret[0] = action if len(ret) == 1: return ret[0] else: return ret def store_episode(self, episode_batch, update_stats=True): """ Story the episode transitions :param episode_batch: (numpy Number) array of batch_size x (T or T+1) x dim_key 'o' is of size T+1, others are of size T :param update_stats: (bool) whether to update stats or not """ self.buffer.store_episode(episode_batch) if update_stats: # add transitions to normalizer episode_batch['o_2'] = episode_batch['o'][:, 1:, :] episode_batch['ag_2'] = episode_batch['ag'][:, 1:, :] num_normalizing_transitions = transitions_in_episode_batch( episode_batch) transitions = self.sample_transitions(episode_batch, num_normalizing_transitions) obs, _, goal, achieved_goal = transitions['o'], transitions[ 'o_2'], transitions['g'], transitions['ag'] transitions['o'], transitions['g'] = self._preprocess_obs_goal( obs, achieved_goal, goal) # No need to preprocess the o_2 and g_2 since this is only used for stats self.o_stats.update(transitions['o']) self.g_stats.update(transitions['g']) self.o_stats.recompute_stats() self.g_stats.recompute_stats() def get_current_buffer_size(self): """ returns the current buffer size :return: (int) buffer size """ return self.buffer.get_current_size() def _sync_optimizers(self): self.q_adam.sync() self.pi_adam.sync() def _grads(self): # Avoid feed_dict here for performance! critic_loss, actor_loss, q_grad, pi_grad = self.sess.run([ self.q_loss_tf, self.main.q_pi_tf, self.q_grad_tf, self.pi_grad_tf ]) return critic_loss, actor_loss, q_grad, pi_grad def _update(self, q_grad, pi_grad): self.q_adam.update(q_grad, self.q_lr) self.pi_adam.update(pi_grad, self.pi_lr) def sample_batch(self): """ sample a batch :return: (dict) the batch """ transitions = self.buffer.sample(self.batch_size) obs, obs_2, goal = transitions['o'], transitions['o_2'], transitions[ 'g'] achieved_goal, achieved_goal_2 = transitions['ag'], transitions['ag_2'] transitions['o'], transitions['g'] = self._preprocess_obs_goal( obs, achieved_goal, goal) transitions['o_2'], transitions['g_2'] = self._preprocess_obs_goal( obs_2, achieved_goal_2, goal) transitions_batch = [ transitions[key] for key in self.stage_shapes.keys() ] return transitions_batch def stage_batch(self, batch=None): """ apply a batch to staging :param batch: (dict) the batch to add to staging, if None: self.sample_batch() """ if batch is None: batch = self.sample_batch() assert len(self.buffer_ph_tf) == len(batch) self.sess.run(self.stage_op, feed_dict=dict(zip(self.buffer_ph_tf, batch))) def train(self, stage=True): """ train DDPG :param stage: (bool) enable staging :return: (float, float) critic loss, actor loss """ if stage: self.stage_batch() critic_loss, actor_loss, q_grad, pi_grad = self._grads() self._update(q_grad, pi_grad) return critic_loss, actor_loss def _init_target_net(self): self.sess.run(self.init_target_net_op) def update_target_net(self): """ update the target network """ self.sess.run(self.update_target_net_op) def clear_buffer(self): """ clears the replay buffer """ self.buffer.clear_buffer() def _vars(self, scope): res = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.scope + '/' + scope) assert len(res) > 0 return res def _global_vars(self, scope): res = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.scope + '/' + scope) return res def _create_network(self, reuse=False): logger.info("Creating a DDPG agent with action space %d x %s..." % (self.dim_action, self.max_u)) self.sess = tf.get_default_session() if self.sess is None: self.sess = tf.InteractiveSession() # running averages with tf.variable_scope('o_stats') as scope: if reuse: scope.reuse_variables() self.o_stats = Normalizer(self.dim_obs, self.norm_eps, self.norm_clip, sess=self.sess) with tf.variable_scope('g_stats') as scope: if reuse: scope.reuse_variables() self.g_stats = Normalizer(self.dim_goal, self.norm_eps, self.norm_clip, sess=self.sess) # mini-batch sampling. batch = self.staging_tf.get() batch_tf = OrderedDict([ (key, batch[i]) for i, key in enumerate(self.stage_shapes.keys()) ]) batch_tf['r'] = tf.reshape(batch_tf['r'], [-1, 1]) # networks with tf.variable_scope('main') as scope: if reuse: scope.reuse_variables() self.main = self.create_actor_critic(batch_tf, net_type='main', **self.__dict__) scope.reuse_variables() with tf.variable_scope('target') as scope: if reuse: scope.reuse_variables() target_batch_tf = batch_tf.copy() target_batch_tf['o'] = batch_tf['o_2'] target_batch_tf['g'] = batch_tf['g_2'] self.target = self.create_actor_critic(target_batch_tf, net_type='target', **self.__dict__) scope.reuse_variables() assert len(self._vars("main")) == len(self._vars("target")) # loss functions target_q_pi_tf = self.target.q_pi_tf clip_range = (-self.clip_return, 0. if self.clip_pos_returns else np.inf) target_tf = tf.clip_by_value( batch_tf['r'] + self.gamma * target_q_pi_tf, *clip_range) self.q_loss_tf = tf.reduce_mean( tf.square(tf.stop_gradient(target_tf) - self.main.q_tf)) self.pi_loss_tf = -tf.reduce_mean(self.main.q_pi_tf) self.pi_loss_tf += self.action_l2 * tf.reduce_mean( tf.square(self.main.pi_tf / self.max_u)) q_grads_tf = tf.gradients(self.q_loss_tf, self._vars('main/Q')) pi_grads_tf = tf.gradients(self.pi_loss_tf, self._vars('main/pi')) assert len(self._vars('main/Q')) == len(q_grads_tf) assert len(self._vars('main/pi')) == len(pi_grads_tf) self.q_grads_vars_tf = zip(q_grads_tf, self._vars('main/Q')) self.pi_grads_vars_tf = zip(pi_grads_tf, self._vars('main/pi')) self.q_grad_tf = flatten_grads(grads=q_grads_tf, var_list=self._vars('main/Q')) self.pi_grad_tf = flatten_grads(grads=pi_grads_tf, var_list=self._vars('main/pi')) # optimizers self.q_adam = MpiAdam(self._vars('main/Q'), scale_grad_by_procs=False) self.pi_adam = MpiAdam(self._vars('main/pi'), scale_grad_by_procs=False) # polyak averaging self.main_vars = self._vars('main/Q') + self._vars('main/pi') self.target_vars = self._vars('target/Q') + self._vars('target/pi') self.stats_vars = self._global_vars('o_stats') + self._global_vars( 'g_stats') self.init_target_net_op = list( map(lambda v: v[0].assign(v[1]), zip(self.target_vars, self.main_vars))) self.update_target_net_op = list( map( lambda v: v[0].assign(self.polyak * v[0] + (1. - self.polyak) * v[1]), zip(self.target_vars, self.main_vars))) # initialize all variables tf.variables_initializer(self._global_vars('')).run() self._sync_optimizers() self._init_target_net() def logs(self, prefix=''): """ create a log dictionary :param prefix: (str) the prefix for evey index :return: ({str: Any}) the log """ logs = [] logs += [('stats_o/mean', np.mean(self.sess.run([self.o_stats.mean])))] logs += [('stats_o/std', np.mean(self.sess.run([self.o_stats.std])))] logs += [('stats_g/mean', np.mean(self.sess.run([self.g_stats.mean])))] logs += [('stats_g/std', np.mean(self.sess.run([self.g_stats.std])))] if prefix is not '' and not prefix.endswith('/'): return [(prefix + '/' + key, val) for key, val in logs] else: return logs def __getstate__(self): """Our policies can be loaded from pkl, but after unpickling you cannot continue training. """ excluded_subnames = [ '_tf', '_op', '_vars', '_adam', 'buffer', 'sess', '_stats', 'main', 'target', 'lock', 'env', 'sample_transitions', 'stage_shapes', 'create_actor_critic' ] state = { k: v for k, v in self.__dict__.items() if all([subname not in k for subname in excluded_subnames]) } state['buffer_size'] = self.buffer_size state['tf'] = self.sess.run( [x for x in self._global_vars('') if 'buffer' not in x.name]) return state def __setstate__(self, state): if 'sample_transitions' not in state: # We don't need this for playing the policy. state['sample_transitions'] = None self.__init__(**state) # set up stats (they are overwritten in __init__) for key, value in state.items(): if key[-6:] == '_stats': self.__dict__[key] = value # load TF variables _vars = [x for x in self._global_vars('') if 'buffer' not in x.name] assert len(_vars) == len(state["tf"]) node = [tf.assign(var, val) for var, val in zip(_vars, state["tf"])] self.sess.run(node)
class DDPG(object): @store_args def __init__(self, input_dims, buffer_size, hidden, layers, network_class, polyak, batch_size, Q_lr, pi_lr, norm_eps, norm_clip, max_u, action_l2, clip_obs, scope, T, rollout_batch_size, subtract_goals, relative_goals, clip_pos_returns, clip_return, bc_loss, q_filter, num_demo, demo_batch_size, prm_loss_weight, aux_loss_weight, sample_transitions, gamma, reuse=False, **kwargs): """Implementation of DDPG that is used in combination with Hindsight Experience Replay (HER). Added functionality to use demonstrations for training to Overcome exploration problem. Args: input_dims (dict of ints): dimensions for the observation (o), the goal (g), and the actions (u) buffer_size (int): number of transitions that are stored in the replay buffer hidden (int): number of units in the hidden layers layers (int): number of hidden layers network_class (str): the network class that should be used (e.g. 'baselines.her.ActorCritic') polyak (float): coefficient for Polyak-averaging of the target network batch_size (int): batch size for training Q_lr (float): learning rate for the Q (critic) network pi_lr (float): learning rate for the pi (actor) network norm_eps (float): a small value used in the normalizer to avoid numerical instabilities norm_clip (float): normalized inputs are clipped to be in [-norm_clip, norm_clip] max_u (float): maximum action magnitude, i.e. actions are in [-max_u, max_u] action_l2 (float): coefficient for L2 penalty on the actions clip_obs (float): clip observations before normalization to be in [-clip_obs, clip_obs] scope (str): the scope used for the TensorFlow graph T (int): the time horizon for rollouts rollout_batch_size (int): number of parallel rollouts per DDPG agent subtract_goals (function): function that subtracts goals from each other relative_goals (boolean): whether or not relative goals should be fed into the network clip_pos_returns (boolean): whether or not positive returns should be clipped clip_return (float): clip returns to be in [-clip_return, clip_return] sample_transitions (function) function that samples from the replay buffer gamma (float): gamma used for Q learning updates reuse (boolean): whether or not the networks should be reused bc_loss: whether or not the behavior cloning loss should be used as an auxilliary loss q_filter: whether or not a filter on the q value update should be used when training with demonstartions num_demo: Number of episodes in to be used in the demonstration buffer demo_batch_size: number of samples to be used from the demonstrations buffer, per mpi thread prm_loss_weight: Weight corresponding to the primary loss aux_loss_weight: Weight corresponding to the auxilliary loss also called the cloning loss """ if self.clip_return is None: self.clip_return = np.inf self.create_actor_critic = import_function(self.network_class) input_shapes = dims_to_shapes(self.input_dims) self.dimo = self.input_dims['o'] self.dimg = self.input_dims['g'] self.dimu = self.input_dims['u'] # Prepare staging area for feeding data to the model. stage_shapes = OrderedDict() for key in sorted(self.input_dims.keys()): if key.startswith('info_'): continue stage_shapes[key] = (None, *input_shapes[key]) for key in ['o', 'g']: stage_shapes[key + '_2'] = stage_shapes[key] stage_shapes['r'] = (None,) self.stage_shapes = stage_shapes # Create network. with tf.variable_scope(self.scope): self.staging_tf = StagingArea( dtypes=[tf.float32 for _ in self.stage_shapes.keys()], shapes=list(self.stage_shapes.values())) self.buffer_ph_tf = [ tf.placeholder(tf.float32, shape=shape) for shape in self.stage_shapes.values()] self.stage_op = self.staging_tf.put(self.buffer_ph_tf) self._create_network(reuse=reuse) # Configure the replay buffer. buffer_shapes = {key: (self.T-1 if key != 'o' else self.T, *input_shapes[key]) for key, val in input_shapes.items()} buffer_shapes['g'] = (buffer_shapes['g'][0], self.dimg) buffer_shapes['ag'] = (self.T, self.dimg) buffer_size = (self.buffer_size // self.rollout_batch_size) * self.rollout_batch_size self.buffer = ReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions) global DEMO_BUFFER DEMO_BUFFER = ReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions) #initialize the demo buffer; in the same way as the primary data buffer def _random_action(self, n): return np.random.uniform(low=-self.max_u, high=self.max_u, size=(n, self.dimu)) def _preprocess_og(self, o, ag, g): if self.relative_goals: g_shape = g.shape g = g.reshape(-1, self.dimg) ag = ag.reshape(-1, self.dimg) g = self.subtract_goals(g, ag) g = g.reshape(*g_shape) o = np.clip(o, -self.clip_obs, self.clip_obs) g = np.clip(g, -self.clip_obs, self.clip_obs) return o, g def step(self, obs): actions = self.get_actions(obs['observation'], obs['achieved_goal'], obs['desired_goal']) return actions, None, None, None def get_actions(self, o, ag, g, noise_eps=0., random_eps=0., use_target_net=False, compute_Q=False): o, g = self._preprocess_og(o, ag, g) policy = self.target if use_target_net else self.main # values to compute vals = [policy.pi_tf] if compute_Q: vals += [policy.Q_pi_tf] # feed feed = { policy.o_tf: o.reshape(-1, self.dimo), policy.g_tf: g.reshape(-1, self.dimg), policy.u_tf: np.zeros((o.size // self.dimo, self.dimu), dtype=np.float32) } ret = self.sess.run(vals, feed_dict=feed) # action postprocessing u = ret[0] noise = noise_eps * self.max_u * np.random.randn(*u.shape) # gaussian noise u += noise u = np.clip(u, -self.max_u, self.max_u) u += np.random.binomial(1, random_eps, u.shape[0]).reshape(-1, 1) * (self._random_action(u.shape[0]) - u) # eps-greedy if u.shape[0] == 1: u = u[0] u = u.copy() ret[0] = u if len(ret) == 1: return ret[0] else: return ret def init_demo_buffer(self, demoDataFile, update_stats=True): #function that initializes the demo buffer demoData = np.load(demoDataFile) #load the demonstration data from data file info_keys = [key.replace('info_', '') for key in self.input_dims.keys() if key.startswith('info_')] info_values = [np.empty((self.T - 1, 1, self.input_dims['info_' + key]), np.float32) for key in info_keys] demo_data_obs = demoData['obs'] demo_data_acs = demoData['acs'] demo_data_info = demoData['info'] for epsd in range(self.num_demo): # we initialize the whole demo buffer at the start of the training obs, acts, goals, achieved_goals = [], [] ,[] ,[] i = 0 for transition in range(self.T - 1): obs.append([demo_data_obs[epsd][transition].get('observation')]) acts.append([demo_data_acs[epsd][transition]]) goals.append([demo_data_obs[epsd][transition].get('desired_goal')]) achieved_goals.append([demo_data_obs[epsd][transition].get('achieved_goal')]) for idx, key in enumerate(info_keys): info_values[idx][transition, i] = demo_data_info[epsd][transition][key] obs.append([demo_data_obs[epsd][self.T - 1].get('observation')]) achieved_goals.append([demo_data_obs[epsd][self.T - 1].get('achieved_goal')]) episode = dict(o=obs, u=acts, g=goals, ag=achieved_goals) for key, value in zip(info_keys, info_values): episode['info_{}'.format(key)] = value episode = convert_episode_to_batch_major(episode) global DEMO_BUFFER DEMO_BUFFER.store_episode(episode) # create the observation dict and append them into the demonstration buffer logger.debug("Demo buffer size currently ", DEMO_BUFFER.get_current_size()) #print out the demonstration buffer size if update_stats: # add transitions to normalizer to normalize the demo data as well episode['o_2'] = episode['o'][:, 1:, :] episode['ag_2'] = episode['ag'][:, 1:, :] num_normalizing_transitions = transitions_in_episode_batch(episode) transitions = self.sample_transitions(episode, num_normalizing_transitions) o, g, ag = transitions['o'], transitions['g'], transitions['ag'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) # No need to preprocess the o_2 and g_2 since this is only used for stats self.o_stats.update(transitions['o']) self.g_stats.update(transitions['g']) self.o_stats.recompute_stats() self.g_stats.recompute_stats() episode.clear() logger.info("Demo buffer size: ", DEMO_BUFFER.get_current_size()) #print out the demonstration buffer size def store_episode(self, episode_batch, update_stats=True): """ episode_batch: array of batch_size x (T or T+1) x dim_key 'o' is of size T+1, others are of size T """ self.buffer.store_episode(episode_batch) if update_stats: # add transitions to normalizer episode_batch['o_2'] = episode_batch['o'][:, 1:, :] episode_batch['ag_2'] = episode_batch['ag'][:, 1:, :] num_normalizing_transitions = transitions_in_episode_batch(episode_batch) transitions = self.sample_transitions(episode_batch, num_normalizing_transitions) o, g, ag = transitions['o'], transitions['g'], transitions['ag'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) # No need to preprocess the o_2 and g_2 since this is only used for stats self.o_stats.update(transitions['o']) self.g_stats.update(transitions['g']) self.o_stats.recompute_stats() self.g_stats.recompute_stats() def get_current_buffer_size(self): return self.buffer.get_current_size() def _sync_optimizers(self): self.Q_adam.sync() self.pi_adam.sync() def _grads(self): # Avoid feed_dict here for performance! critic_loss, actor_loss, Q_grad, pi_grad = self.sess.run([ self.Q_loss_tf, self.main.Q_pi_tf, self.Q_grad_tf, self.pi_grad_tf ]) return critic_loss, actor_loss, Q_grad, pi_grad def _update(self, Q_grad, pi_grad): self.Q_adam.update(Q_grad, self.Q_lr) self.pi_adam.update(pi_grad, self.pi_lr) def sample_batch(self): if self.bc_loss: #use demonstration buffer to sample as well if bc_loss flag is set TRUE transitions = self.buffer.sample(self.batch_size - self.demo_batch_size) global DEMO_BUFFER transitions_demo = DEMO_BUFFER.sample(self.demo_batch_size) #sample from the demo buffer for k, values in transitions_demo.items(): rolloutV = transitions[k].tolist() for v in values: rolloutV.append(v.tolist()) transitions[k] = np.array(rolloutV) else: transitions = self.buffer.sample(self.batch_size) #otherwise only sample from primary buffer o, o_2, g = transitions['o'], transitions['o_2'], transitions['g'] ag, ag_2 = transitions['ag'], transitions['ag_2'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) transitions['o_2'], transitions['g_2'] = self._preprocess_og(o_2, ag_2, g) transitions_batch = [transitions[key] for key in self.stage_shapes.keys()] return transitions_batch def stage_batch(self, batch=None): if batch is None: batch = self.sample_batch() assert len(self.buffer_ph_tf) == len(batch) self.sess.run(self.stage_op, feed_dict=dict(zip(self.buffer_ph_tf, batch))) def train(self, stage=True): if stage: self.stage_batch() critic_loss, actor_loss, Q_grad, pi_grad = self._grads() self._update(Q_grad, pi_grad) return critic_loss, actor_loss def _init_target_net(self): self.sess.run(self.init_target_net_op) def update_target_net(self): self.sess.run(self.update_target_net_op) def clear_buffer(self): self.buffer.clear_buffer() def _vars(self, scope): res = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.scope + '/' + scope) assert len(res) > 0 return res def _global_vars(self, scope): res = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.scope + '/' + scope) return res def _create_network(self, reuse=False): logger.info("Creating a DDPG agent with action space %d x %s..." % (self.dimu, self.max_u)) self.sess = tf_util.get_session() # running averages with tf.variable_scope('o_stats') as vs: if reuse: vs.reuse_variables() self.o_stats = Normalizer(self.dimo, self.norm_eps, self.norm_clip, sess=self.sess) with tf.variable_scope('g_stats') as vs: if reuse: vs.reuse_variables() self.g_stats = Normalizer(self.dimg, self.norm_eps, self.norm_clip, sess=self.sess) # mini-batch sampling. batch = self.staging_tf.get() batch_tf = OrderedDict([(key, batch[i]) for i, key in enumerate(self.stage_shapes.keys())]) batch_tf['r'] = tf.reshape(batch_tf['r'], [-1, 1]) #choose only the demo buffer samples mask = np.concatenate((np.zeros(self.batch_size - self.demo_batch_size), np.ones(self.demo_batch_size)), axis = 0) # networks with tf.variable_scope('main') as vs: if reuse: vs.reuse_variables() self.main = self.create_actor_critic(batch_tf, net_type='main', **self.__dict__) vs.reuse_variables() with tf.variable_scope('target') as vs: if reuse: vs.reuse_variables() target_batch_tf = batch_tf.copy() target_batch_tf['o'] = batch_tf['o_2'] target_batch_tf['g'] = batch_tf['g_2'] self.target = self.create_actor_critic( target_batch_tf, net_type='target', **self.__dict__) vs.reuse_variables() assert len(self._vars("main")) == len(self._vars("target")) # loss functions target_Q_pi_tf = self.target.Q_pi_tf clip_range = (-self.clip_return, 0. if self.clip_pos_returns else np.inf) target_tf = tf.clip_by_value(batch_tf['r'] + self.gamma * target_Q_pi_tf, *clip_range) self.Q_loss_tf = tf.reduce_mean(tf.square(tf.stop_gradient(target_tf) - self.main.Q_tf)) if self.bc_loss ==1 and self.q_filter == 1 : # train with demonstrations and use bc_loss and q_filter both maskMain = tf.reshape(tf.boolean_mask(self.main.Q_tf > self.main.Q_pi_tf, mask), [-1]) #where is the demonstrator action better than actor action according to the critic? choose those samples only #define the cloning loss on the actor's actions only on the samples which adhere to the above masks self.cloning_loss_tf = tf.reduce_sum(tf.square(tf.boolean_mask(tf.boolean_mask((self.main.pi_tf), mask), maskMain, axis=0) - tf.boolean_mask(tf.boolean_mask((batch_tf['u']), mask), maskMain, axis=0))) self.pi_loss_tf = -self.prm_loss_weight * tf.reduce_mean(self.main.Q_pi_tf) #primary loss scaled by it's respective weight prm_loss_weight self.pi_loss_tf += self.prm_loss_weight * self.action_l2 * tf.reduce_mean(tf.square(self.main.pi_tf / self.max_u)) #L2 loss on action values scaled by the same weight prm_loss_weight self.pi_loss_tf += self.aux_loss_weight * self.cloning_loss_tf #adding the cloning loss to the actor loss as an auxilliary loss scaled by its weight aux_loss_weight elif self.bc_loss == 1 and self.q_filter == 0: # train with demonstrations without q_filter self.cloning_loss_tf = tf.reduce_sum(tf.square(tf.boolean_mask((self.main.pi_tf), mask) - tf.boolean_mask((batch_tf['u']), mask))) self.pi_loss_tf = -self.prm_loss_weight * tf.reduce_mean(self.main.Q_pi_tf) self.pi_loss_tf += self.prm_loss_weight * self.action_l2 * tf.reduce_mean(tf.square(self.main.pi_tf / self.max_u)) self.pi_loss_tf += self.aux_loss_weight * self.cloning_loss_tf else: #If not training with demonstrations self.pi_loss_tf = -tf.reduce_mean(self.main.Q_pi_tf) self.pi_loss_tf += self.action_l2 * tf.reduce_mean(tf.square(self.main.pi_tf / self.max_u)) Q_grads_tf = tf.gradients(self.Q_loss_tf, self._vars('main/Q')) pi_grads_tf = tf.gradients(self.pi_loss_tf, self._vars('main/pi')) assert len(self._vars('main/Q')) == len(Q_grads_tf) assert len(self._vars('main/pi')) == len(pi_grads_tf) self.Q_grads_vars_tf = zip(Q_grads_tf, self._vars('main/Q')) self.pi_grads_vars_tf = zip(pi_grads_tf, self._vars('main/pi')) self.Q_grad_tf = flatten_grads(grads=Q_grads_tf, var_list=self._vars('main/Q')) self.pi_grad_tf = flatten_grads(grads=pi_grads_tf, var_list=self._vars('main/pi')) # optimizers self.Q_adam = MpiAdam(self._vars('main/Q'), scale_grad_by_procs=False) self.pi_adam = MpiAdam(self._vars('main/pi'), scale_grad_by_procs=False) # polyak averaging self.main_vars = self._vars('main/Q') + self._vars('main/pi') self.target_vars = self._vars('target/Q') + self._vars('target/pi') self.stats_vars = self._global_vars('o_stats') + self._global_vars('g_stats') self.init_target_net_op = list( map(lambda v: v[0].assign(v[1]), zip(self.target_vars, self.main_vars))) self.update_target_net_op = list( map(lambda v: v[0].assign(self.polyak * v[0] + (1. - self.polyak) * v[1]), zip(self.target_vars, self.main_vars))) # initialize all variables tf.variables_initializer(self._global_vars('')).run() self._sync_optimizers() self._init_target_net() def logs(self, prefix=''): logs = [] logs += [('stats_o/mean', np.mean(self.sess.run([self.o_stats.mean])))] logs += [('stats_o/std', np.mean(self.sess.run([self.o_stats.std])))] logs += [('stats_g/mean', np.mean(self.sess.run([self.g_stats.mean])))] logs += [('stats_g/std', np.mean(self.sess.run([self.g_stats.std])))] if prefix != '' and not prefix.endswith('/'): return [(prefix + '/' + key, val) for key, val in logs] else: return logs def __getstate__(self): """Our policies can be loaded from pkl, but after unpickling you cannot continue training. """ excluded_subnames = ['_tf', '_op', '_vars', '_adam', 'buffer', 'sess', '_stats', 'main', 'target', 'lock', 'env', 'sample_transitions', 'stage_shapes', 'create_actor_critic'] state = {k: v for k, v in self.__dict__.items() if all([not subname in k for subname in excluded_subnames])} state['buffer_size'] = self.buffer_size state['tf'] = self.sess.run([x for x in self._global_vars('') if 'buffer' not in x.name]) return state def __setstate__(self, state): if 'sample_transitions' not in state: # We don't need this for playing the policy. state['sample_transitions'] = None self.__init__(**state) # set up stats (they are overwritten in __init__) for k, v in state.items(): if k[-6:] == '_stats': self.__dict__[k] = v # load TF variables vars = [x for x in self._global_vars('') if 'buffer' not in x.name] assert(len(vars) == len(state["tf"])) node = [tf.assign(var, val) for var, val in zip(vars, state["tf"])] self.sess.run(node) def save(self, save_path): tf_util.save_variables(save_path)
class DDPG(object): @store_args def __init__(self, input_dims, buffer_size, hidden, layers, network_class, polyak, batch_size, Q_lr, pi_lr, norm_eps, norm_clip, max_u, action_l2, clip_obs, scope, T, rollout_batch_size, subtract_goals, relative_goals, clip_pos_returns, clip_return, sample_transitions, gamma, replay_k, reward_fun=None, reuse=False, **kwargs): """Implementation of DDPG that is used in combination with Hindsight Experience Replay (HER). Args: input_dims (dict of ints): dimensions for the observation (o), the goal (g), and the actions (u) buffer_size (int): number of transitions that are stored in the replay buffer hidden (int): number of units in the hidden layers layers (int): number of hidden layers network_class (str): the network class that should be used (e.g. 'baselines.her.ActorCritic') polyak (float): coefficient for Polyak-averaging of the target network batch_size (int): batch size for training Q_lr (float): learning rate for the Q (critic) network pi_lr (float): learning rate for the pi (actor) network norm_eps (float): a small value used in the normalizer to avoid numerical instabilities norm_clip (float): normalized inputs are clipped to be in [-norm_clip, norm_clip] max_u (float): maximum action magnitude, i.e. actions are in [-max_u, max_u] action_l2 (float): coefficient for L2 penalty on the actions clip_obs (float): clip observations before normalization to be in [-clip_obs, clip_obs] scope (str): the scope used for the TensorFlow graph T (int): the time horizon for rollouts rollout_batch_size (int): number of parallel rollouts per DDPG agent subtract_goals (function): function that subtracts goals from each other relative_goals (boolean): whether or not relative goals should be fed into the network clip_pos_returns (boolean): whether or not positive returns should be clipped clip_return (float): clip returns to be in [-clip_return, clip_return] sample_transitions (function) function that samples from the replay buffer gamma (float): gamma used for Q learning updates reuse (boolean): whether or not the networks should be reused """ if self.clip_return is None: self.clip_return = np.inf # Create the actor critic networks. network_class is defined in actor_critic.py # This class is assigned to network_class when DDPG objest is created self.create_actor_critic = import_function(self.network_class) input_shapes = dims_to_shapes(self.input_dims) self.dimo = self.input_dims['o'] self.dimg = self.input_dims['g'] self.dimu = self.input_dims['u'] # Prepare staging area for feeding data to the model. stage_shapes = OrderedDict() for key in sorted(self.input_dims.keys()): if key.startswith('info_'): continue stage_shapes[key] = (None, *input_shapes[key]) # Next state (o_2) and goal at next state (g_2) for key in ['o', 'g']: stage_shapes[key + '_2'] = stage_shapes[key] stage_shapes['r'] = (None,) self.stage_shapes = stage_shapes # Adding variable for correcting bias - Ameet self.stage_shapes_new = OrderedDict() self.stage_shapes_new['bias'] = (None,) ############################################## # Create network # Staging area is a datatype in tf to input data into GPUs with tf.variable_scope(self.scope): self.staging_tf = StagingArea( dtypes=[tf.float32 for _ in self.stage_shapes.keys()], shapes=list(self.stage_shapes.values())) self.buffer_ph_tf = [ tf.placeholder(tf.float32, shape=shape) for shape in self.stage_shapes.values()] self.stage_op = self.staging_tf.put(self.buffer_ph_tf) # Adding bias term from section 3.4 - Ameet self.staging_tf_new = StagingArea( dtypes=[tf.float32 for _ in self.stage_shapes_new.keys()], shapes=list(self.stage_shapes_new.values())) self.buffer_ph_tf_new = [ tf.placeholder(tf.float32, shape=shape) for shape in self.stage_shapes_new.values()] self.stage_op_new = self.staging_tf_new.put(self.buffer_ph_tf_new) ############################################ self._create_network(reuse=reuse) # Configure the replay buffer buffer_shapes = {key: (self.T if key != 'o' else self.T+1, *input_shapes[key]) for key, val in input_shapes.items()} buffer_shapes['g'] = (buffer_shapes['g'][0], self.dimg) buffer_shapes['ag'] = (self.T+1, self.dimg) buffer_size = (self.buffer_size // self.rollout_batch_size) * self.rollout_batch_size # conf represents the parameters required for initializing the priority_queue # Remember: The bias gets annealed only conf.total_steps number of times conf = {'size': self.buffer_size, 'learn_start': self.batch_size, 'batch_size': self.batch_size, # Using some heuristic to set the partition_num as it matters only when the buffer is not full (unlikely) 'partition_size': (self.replay_k)*100} self.buffer = ReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions, conf, self.replay_k) # global_steps represents the number of batches used for updates self.global_step = 0 self.debug = {} def _random_action(self, n): return np.random.uniform(low=-self.max_u, high=self.max_u, size=(n, self.dimu)) # Preprocessing by clipping the goal and state variables # Not sure about the relative_goal part def _preprocess_og(self, o, ag, g): if self.relative_goals: g_shape = g.shape g = g.reshape(-1, self.dimg) ag = ag.reshape(-1, self.dimg) g = self.subtract_goals(g, ag) g = g.reshape(*g_shape) o = np.clip(o, -self.clip_obs, self.clip_obs) g = np.clip(g, -self.clip_obs, self.clip_obs) return o, g # target is the target policy network and main is the one which is updated # target is updated by moving the parameters towards that of the main # pi_tf is the output of the policy network, Q_pi_tf is the output of the Q network used for training pi_tf # i.e., Q_pi_tf uses the pi_tf's action to evaluate the value # While just Q_tf uses the action which was actually taken def get_actions(self, o, ag, g, noise_eps=0., random_eps=0., use_target_net=False, compute_Q=False): o, g = self._preprocess_og(o, ag, g) policy = self.target if use_target_net else self.main # values to compute vals = [policy.pi_tf] if compute_Q: vals += [policy.Q_pi_tf] # feed feed = { policy.o_tf: o.reshape(-1, self.dimo), policy.g_tf: g.reshape(-1, self.dimg), policy.u_tf: np.zeros((o.size // self.dimo, self.dimu), dtype=np.float32) } ret = self.sess.run(vals, feed_dict=feed) # action postprocessing u = ret[0] noise = noise_eps * self.max_u * np.random.randn(*u.shape) # gaussian noise u += noise u = np.clip(u, -self.max_u, self.max_u) u += np.random.binomial(1, random_eps, u.shape[0]).reshape(-1, 1) * (self._random_action(u.shape[0]) - u) # eps-greedy if u.shape[0] == 1: u = u[0] u = u.copy() ret[0] = u if len(ret) == 1: return ret[0] else: return ret def store_episode(self, episode_batch, update_stats=True): """ episode_batch: array of batch_size x (T or T+1) x dim_key 'o' is of size T+1, others are of size T """ ###### Remove the l value - Supposed to be a list of length 2 # First entry consists of transitions with actual goals and second is alternate goals self.buffer.store_episode(episode_batch) # ###### Debug # # This functions was used to check the hypothesis that if TD error is high # # for a state with some goal, it is high for that states with all other goals # self.debug_td_error_alternate_actual(debug_transitions) # Updating stats ## Change this-------------- update_stats = False ###-------------------------- if update_stats: # add transitions to normalizer episode_batch['o_2'] = episode_batch['o'][:, 1:, :] episode_batch['ag_2'] = episode_batch['ag'][:, 1:, :] num_normalizing_transitions = transitions_in_episode_batch(episode_batch) transitions = self.sample_transitions(episode_batch, num_normalizing_transitions) o, o_2, g, ag = transitions['o'], transitions['o_2'], transitions['g'], transitions['ag'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) # No need to preprocess the o_2 and g_2 since this is only used for stats self.o_stats.update(transitions['o']) self.g_stats.update(transitions['g']) self.o_stats.recompute_stats() self.g_stats.recompute_stats() # This function is purely for Debugging purposes def debug_td_error_alternate_actual(self, debug_transitions): actual_transitions, alternate_transitions = debug_transitions[0], debug_transitions[1] actual_transitions, alternate_transitions = self.td_error_convert_to_format(actual_transitions),\ self.td_error_convert_to_format(alternate_transitions) # Calculated priorities priorities = [] priorities.append(self.get_priorities(actual_transitions)) priorities.append(self.get_priorities(alternate_transitions)) f = open('act_alt_goals.txt', 'a') # Length of priorities[0] is 100 and priorities[1] is 400 for i in range(len(priorities[0])): f.write(str(priorities[0][i])+" : ") for k in range(4): f.write(str(priorities[1][i*self.replay_k+k])+" : ") f.write('\n') f.write("Done Storing One Rollout\n\n\n") # f.write('The number of transitions are: '+str(len(priorities[0]))+" :: "+str(len(priorities[1]))+"\n") # This function is purely for Debugging purposes def td_error_convert_to_format(self, sample_transitions): # sample_transitions is now a list of transitions, convert it to the usual {key: batch X dim_key} keys = sample_transitions[0].keys() # print("Keys in _sample_her_transitions are: "+str(keys)) transitions = {} for key in keys: # Initialize for all the keys transitions[key] = [] # Add transitions one by one to the list for single_transition in range(len(sample_transitions)): transitions[key].append(sample_transitions[single_transition][key]) transitions[key] = np.array(transitions[key]) # Reconstruct info dictionary for reward computation. info = {} for key, value in transitions.items(): if key.startswith('info_'): info[key.replace('info_', '')] = value # print("The keys in transitions are: "+str(transitions.keys())) reward_params = {k: transitions[k] for k in ['ag_2', 'g']} reward_params['info'] = info transitions['r'] = self.reward_fun(**reward_params) # transitions = {k: transitions[k].reshape(batch_size, *transitions[k].shape[1:]) # for k in transitions.keys()} return transitions def get_current_buffer_size(self): return self.buffer.get_current_size() def _sync_optimizers(self): self.Q_adam.sync() self.pi_adam.sync() def _grads(self): # Avoid feed_dict here for performance! critic_loss, actor_loss, Q_grad, pi_grad = self.sess.run([ self.Q_loss_tf, self.main.Q_pi_tf, self.Q_grad_tf, self.pi_grad_tf ]) return critic_loss, actor_loss, Q_grad, pi_grad # Adam update for Q and pi networks def _update(self, Q_grad, pi_grad): self.Q_adam.update(Q_grad, self.Q_lr) self.pi_adam.update(pi_grad, self.pi_lr) # Sample a batch for mini batch gradient descent, already defined in replay_buffer.py def sample_batch(self): # Increment the global step self.global_step += 1 transitions, w, rank_e_id = self.buffer.sample(self.batch_size, self.global_step, self.uniform_priority) priorities = self.get_priorities(transitions) # ##### Debug function # self.debug_td_error(transitions, priorities) # ##### o, o_2, g = transitions['o'], transitions['o_2'], transitions['g'] ag, ag_2 = transitions['ag'], transitions['ag_2'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) transitions['o_2'], transitions['g_2'] = self._preprocess_og(o_2, ag_2, g) # # Remove # print("Stage Shape keys in sample_batch are: "+str(self.stage_shapes.keys())) transitions_batch = [transitions[key] for key in self.stage_shapes.keys()] # Updates the priorities of the sampled transitions in the priority queue self.buffer.update_priority(rank_e_id, priorities) return transitions_batch, [w] # This function is purely for debugging purposes def debug_td_error(self, transitions, priorities): f = open('td_error_debug.txt', 'a') self.debug['actual_goals'] = 0 self.debug['alternate_goals'] = 0 trans = transitions['is_actual_goal'] for t in range(trans.shape[0]): if trans[t]: self.debug['actual_goals'] += 1 # f.write('Actual goal transition: '+str(priorities[t])+'\n') else: self.debug['alternate_goals'] += 1 # f.write('Alternate goal transition: '+str(priorities[t])+'\n') f.write('Ratio is: '+str(float(self.debug['alternate_goals'])/self.debug['actual_goals'])+'\n') del transitions['is_actual_goal'] ###### Debug End def get_priorities(self, transitions): pi_target = self.target.pi_tf Q_pi_target = self.target.Q_pi_tf Q_main = self.main.Q_tf o = transitions['o'] o_2 = transitions['o_2'] u = transitions['u'] g = transitions['g'] r = transitions['r'] # Check this with Srikanth ag = transitions['ag'] priorities = np.zeros(o.shape[0]) # file_obj = open("priorities_print","a") for i in range(o.shape[0]): o_2_i = np.clip(o_2[i], -self.clip_obs, self.clip_obs) o_i, g_i = self._preprocess_og(o[i], ag[i], g[i]) u_i = u[i] # Not sure about the o_2_i.size // self.dimo. I guess we need not pass one at a time feed_target = { self.target.o_tf: o_2_i.reshape(-1, self.dimo), self.target.g_tf: g_i.reshape(-1, self.dimg), self.target.u_tf: np.zeros((o_2_i.size // self.dimo, self.dimu), dtype=np.float32) } # u_tf for main network is just the action taken at that state feed_main = { self.main.o_tf: o_i.reshape(-1, self.dimo), self.main.g_tf: g_i.reshape(-1, self.dimg), self.main.u_tf: u_i.reshape(-1, self.dimu) } TD = r[i] + self.gamma*self.sess.run(Q_pi_target, feed_dict=feed_target) - self.sess.run(Q_main, feed_dict=feed_main) priorities[i] = abs(TD) text = str(TD) # file_obj.write(text) # file_obj.close() return priorities def stage_batch(self, batch=None): if batch is None: batch, bias = self.sample_batch() # print("Batch type is: "+str(type(batch))) # print("Batch Shape is: "+str(len(batch))) # print(str(type(batch[0]))) assert len(self.buffer_ph_tf) == len(batch), "Expected: "+str(len(self.buffer_ph_tf))+" Got: "+str(len(batch)) self.sess.run(self.stage_op, feed_dict=dict(zip(self.buffer_ph_tf, batch))) ##### Adding for bias - Ameet assert len(self.buffer_ph_tf_new) == len(bias), "Expected: "+str(len(self.buffer_ph_tf_new))+" Got: "+str(len(bias)) self.sess.run(self.stage_op_new, feed_dict=dict(zip(self.buffer_ph_tf_new, bias))) ##### # print("Completed stage batch") def train(self, stage=True): if stage: self.stage_batch() critic_loss, actor_loss, Q_grad, pi_grad = self._grads() # print("In ddpg priority:: The shapes of Q_grad and pi_grad are: "+str(Q_grad.shape)+"::"+str(pi_grad.shape)) # print("Their types are::"+str(type(Q_grad))) self._update(Q_grad, pi_grad) return critic_loss, actor_loss def _init_target_net(self): self.sess.run(self.init_target_net_op) def update_target_net(self): self.sess.run(self.update_target_net_op) def clear_buffer(self): self.buffer.clear_buffer() def _vars(self, scope): res = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.scope + '/' + scope) assert len(res) > 0 return res def _global_vars(self, scope): res = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.scope + '/' + scope) return res def _create_network(self, reuse=False): logger.info("Creating a DDPG agent with action space %d x %s..." % (self.dimu, self.max_u)) self.sess = tf.get_default_session() if self.sess is None: self.sess = tf.InteractiveSession() # running averages with tf.variable_scope('o_stats') as vs: if reuse: vs.reuse_variables() self.o_stats = Normalizer(self.dimo, self.norm_eps, self.norm_clip, sess=self.sess) with tf.variable_scope('g_stats') as vs: if reuse: vs.reuse_variables() self.g_stats = Normalizer(self.dimg, self.norm_eps, self.norm_clip, sess=self.sess) # mini-batch sampling. batch = self.staging_tf.get() batch_tf = OrderedDict([(key, batch[i]) for i, key in enumerate(self.stage_shapes.keys())]) batch_tf['r'] = tf.reshape(batch_tf['r'], [-1, 1]) ########### Getting the bias terms - Ameet bias = self.staging_tf_new.get() bias_tf = OrderedDict([(key, bias[i]) for i, key in enumerate(self.stage_shapes_new.keys())]) bias_tf['bias'] = tf.reshape(bias_tf['bias'], [-1, 1]) ####################################### # Create main and target networks, each will have a pi_tf, Q_tf and Q_pi_tf with tf.variable_scope('main') as vs: if reuse: vs.reuse_variables() self.main = self.create_actor_critic(batch_tf, net_type='main', **self.__dict__) vs.reuse_variables() with tf.variable_scope('target') as vs: if reuse: vs.reuse_variables() target_batch_tf = batch_tf.copy() target_batch_tf['o'] = batch_tf['o_2'] target_batch_tf['g'] = batch_tf['g_2'] self.target = self.create_actor_critic( target_batch_tf, net_type='target', **self.__dict__) vs.reuse_variables() assert len(self._vars("main")) == len(self._vars("target")) # loss functions target_Q_pi_tf = self.target.Q_pi_tf clip_range = (-self.clip_return, 0. if self.clip_pos_returns else np.inf) target_tf = tf.clip_by_value(batch_tf['r'] + self.gamma * target_Q_pi_tf, *clip_range) ############## Added for bias - Ameet error = (tf.stop_gradient(target_tf) - self.main.Q_tf) * bias_tf['bias'] self.Q_loss_tf = tf.reduce_mean(tf.square(error)) # self.pi_loss_tf = -tf.reduce_mean(self.main.Q_pi_tf * bias_tf['bias']) # Note that the following statement does not include bias because of the remark in the IEEE paper self.pi_loss_tf = -tf.reduce_mean(self.main.Q_pi_tf) ############## # Regularization - L2 - Check - Penalty for taking the best action self.pi_loss_tf += self.action_l2 * tf.reduce_mean(tf.square(self.main.pi_tf / self.max_u)) Q_grads_tf = tf.gradients(self.Q_loss_tf, self._vars('main/Q')) pi_grads_tf = tf.gradients(self.pi_loss_tf, self._vars('main/pi')) assert len(self._vars('main/Q')) == len(Q_grads_tf) assert len(self._vars('main/pi')) == len(pi_grads_tf) self.Q_grads_vars_tf = zip(Q_grads_tf, self._vars('main/Q')) self.pi_grads_vars_tf = zip(pi_grads_tf, self._vars('main/pi')) ################### Shape Info ####Shape of Q_grads_tf is: 8 ####Shape of Q_grads_tf[0] is: (17, 256) self.Q_grad_tf = flatten_grads(grads=Q_grads_tf, var_list=self._vars('main/Q')) self.pi_grad_tf = flatten_grads(grads=pi_grads_tf, var_list=self._vars('main/pi')) # optimizers self.Q_adam = MpiAdam(self._vars('main/Q'), scale_grad_by_procs=False) self.pi_adam = MpiAdam(self._vars('main/pi'), scale_grad_by_procs=False) # polyak averaging # 'main/Q' is a way of communicating the scope of the variables # _vars has a way to understand this self.main_vars = self._vars('main/Q') + self._vars('main/pi') self.target_vars = self._vars('target/Q') + self._vars('target/pi') self.stats_vars = self._global_vars('o_stats') + self._global_vars('g_stats') # Update the networks # target net is updated by using polyak averaging # target net is initialized by just copying the main net self.init_target_net_op = list( map(lambda v: v[0].assign(v[1]), zip(self.target_vars, self.main_vars))) self.update_target_net_op = list( map(lambda v: v[0].assign(self.polyak * v[0] + (1. - self.polyak) * v[1]), zip(self.target_vars, self.main_vars))) # initialize all variables tf.variables_initializer(self._global_vars('')).run() self._sync_optimizers() self._init_target_net() def logs(self, prefix=''): logs = [] logs += [('stats_o/mean', np.mean(self.sess.run([self.o_stats.mean])))] logs += [('stats_o/std', np.mean(self.sess.run([self.o_stats.std])))] logs += [('stats_g/mean', np.mean(self.sess.run([self.g_stats.mean])))] logs += [('stats_g/std', np.mean(self.sess.run([self.g_stats.std])))] if prefix is not '' and not prefix.endswith('/'): return [(prefix + '/' + key, val) for key, val in logs] else: return logs def __getstate__(self): """Our policies can be loaded from pkl, but after unpickling you cannot continue training. """ excluded_subnames = ['_tf', '_op', '_vars', '_adam', 'buffer', 'sess', '_stats', 'main', 'target', 'lock', 'env', 'sample_transitions', 'stage_shapes', 'create_actor_critic'] state = {k: v for k, v in self.__dict__.items() if all([not subname in k for subname in excluded_subnames])} state['buffer_size'] = self.buffer_size state['tf'] = self.sess.run([x for x in self._global_vars('') if 'buffer' not in x.name]) return state def __setstate__(self, state): if 'sample_transitions' not in state: # We don't need this for playing the policy. state['sample_transitions'] = None self.__init__(**state) # set up stats (they are overwritten in __init__) for k, v in state.items(): if k[-6:] == '_stats': self.__dict__[k] = v # load TF variables vars = [x for x in self._global_vars('') if 'buffer' not in x.name] assert(len(vars) == len(state["tf"])) node = [tf.assign(var, val) for var, val in zip(vars, state["tf"])] self.sess.run(node)
class ValueEnsemble: @store_args def __init__(self, *, input_dims, size_ensemble, use_Q, use_double_network, buffer_size, hidden, layers, batch_size, lr, norm_eps, norm_clip, polyak, max_u, clip_obs, scope, T, rollout_batch_size, subtract_goals, relative_goals, clip_pos_returns, clip_return, sample_transitions, gamma, reuse=False, **kwargs): """Implementation of value function ensemble. Args: input_dims (dict of ints): dimensions for the observation (o), the goal (g), and the actions (u) buffer_size (int): number of transitions that are stored in the replay buffer size_ensemble (int): number of value functions in the ensemble hidden (int): number of units in the hidden layers layers (int): number of hidden layers batch_size (int): batch size for training lr (float): learning rate for the Q (critic) network norm_eps (float): a small value used in the normalizer to avoid numerical instabilities norm_clip (float): normalized inputs are clipped to be in [-norm_clip, norm_clip] max_u (float): maximum action magnitude, i.e. actions are in [-max_u, max_u] action_l2 (float): coefficient for L2 penalty on the actions scope (str): the scope used for the TensorFlow graph T (int): the time horizon for rollouts rollout_batch_size (int): number of parallel rollouts per DDPG agent subtract_goals (function): function that subtracts goals from each other relative_goals (boolean): whether or not relative goals should be fed into the network clip_pos_returns (boolean): whether or not positive returns should be clipped in Bellman update inference_clip_pos_returns (boolean): whether or not output of the value output used for disagreement should be clipped clip_return (float): clip returns to be in [-clip_return, clip_return] sample_transitions (function) function that samples from the replay buffer gamma (float): gamma used for Q learning updates reuse (boolean): whether or not the networks should be reused """ if self.use_double_network: self.use_Q = True self.create_v_function = DoubleQFunction elif self.use_Q: self.create_v_function = QFunction else: self.create_v_function = VFunction if self.clip_return is None: self.clip_return = np.inf # self.inference_clip_range = (-self.clip_return, 0. if inference_clip_pos_returns else self.clip_return) input_shapes = dims_to_shapes(self.input_dims) self.dimo = self.input_dims['o'] self.dimg = self.input_dims['g'] self.dimu = self.input_dims['u'] # Prepare staging area for feeding data to the model. stage_shapes = OrderedDict() for key in sorted(self.input_dims.keys()): if key.startswith('info_'): continue stage_shapes[key] = (None, *input_shapes[key]) for key in ['o', 'g']: stage_shapes[key + '_2'] = stage_shapes[key] if self.use_Q: stage_shapes['u_2'] = stage_shapes['u'] stage_shapes['r'] = (None, ) self.stage_shapes = stage_shapes # Create network. with tf.variable_scope(self.scope): self.staging_tf = [None] * self.size_ensemble self.stage_ops = [None] * self.size_ensemble self.buffer_ph_tf = [] for e in range(self.size_ensemble): staging_tf = StagingArea( dtypes=[tf.float32 for _ in self.stage_shapes.keys()], shapes=list(self.stage_shapes.values())) buffer_ph_tf = [ tf.placeholder(tf.float32, shape=shape) for shape in self.stage_shapes.values() ] stage_op = staging_tf.put(buffer_ph_tf) # store in attribute list self.staging_tf[e] = staging_tf self.buffer_ph_tf.extend(buffer_ph_tf) self.stage_ops[e] = stage_op if self.use_double_network: self._create_double_network(reuse=reuse) else: self._create_network(reuse=reuse) # Configure the replay buffer. buffer_shapes = { key: (self.T - 1 if key != 'o' else self.T, *input_shapes[key]) for key, val in input_shapes.items() } buffer_shapes['ag'] = (self.T, self.dimg) # if self.use_Q: # buffer_shapes['u_2'] = (self.T-1, self.dimu) buffer_size = (self.buffer_size // self.rollout_batch_size) * self.rollout_batch_size self.buffer = ReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions) # @property # def buffer_full(self): # return self.buffer.full # def buffer_get_transitions_stored(self): # return self.buffer.get_transitions_stored() def get_values(self, o, ag, g, u=None): if self.size_ensemble == 0: return None if u is not None: assert self.use_Q u = self._preprocess_u(u) o, g = self._preprocess_og(o, ag, g) # values to compute vars = [v_function.V_tf for v_function in self.V_fun] # feed feed = {} for e in range(self.size_ensemble): feed[self.V_fun[e].o_tf] = o.reshape(-1, self.dimo) feed[self.V_fun[e].g_tf] = g.reshape(-1, self.dimg) if self.use_Q: feed[self.V_fun[e].u_tf] = u.reshape(-1, self.dimu) ret = self.sess.run(vars, feed_dict=feed) # value prediction postprocessing # ret = np.clip(ret, -self.clip_return, 0. if self.clip_pos_returns else self.clip_return) ret = np.clip(ret, -self.clip_return, 0. if self.clip_pos_returns else np.inf) return ret def _sample_batch(self, policy): batch_size_in_transitions = self.batch_size * self.size_ensemble transitions = self.buffer.sample(batch_size_in_transitions) # label policy if self.use_Q: u = transitions['u'] u_2 = policy.get_actions(o=transitions['o_2'], ag=transitions['ag_2'], g=transitions['g']) transitions['u'] = self._preprocess_u(u) transitions['u_2'] = self._preprocess_u(u_2) o, o_2, g = transitions['o'], transitions['o_2'], transitions['g'] ag, ag_2 = transitions['ag'], transitions['ag_2'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) transitions['o_2'], transitions['g_2'] = self._preprocess_og( o_2, ag_2, g) transitions_batches = [ transitions[key][e * self.batch_size:(e + 1) * self.batch_size] for e in range(self.size_ensemble) for key in self.stage_shapes.keys() ] return transitions_batches def _stage_batch(self, policy): batches = self._sample_batch(policy=policy) assert len(self.buffer_ph_tf) == len(batches) self.sess.run(self.stage_ops, feed_dict=dict(zip(self.buffer_ph_tf, batches))) def logs(self, prefix=''): logs = [] logs += [('stats_o/mean', np.mean(self.sess.run([self.o_stats.mean])))] logs += [('stats_o/std', np.mean(self.sess.run([self.o_stats.std])))] logs += [('stats_g/mean', np.mean(self.sess.run([self.g_stats.mean])))] logs += [('stats_g/std', np.mean(self.sess.run([self.g_stats.std])))] if prefix != '' and not prefix.endswith('/'): return [(prefix + '/' + key, val) for key, val in logs] else: return logs def train(self, policy): self._stage_batch(policy=policy) V_loss, V_grad = self._grads() self._update(V_grad) assert len(V_loss) == self.size_ensemble return np.mean(V_loss) def _update(self, V_grad): for e in range(self.size_ensemble): self.V_adam[e].update(V_grad[e], self.lr) def _create_network(self, reuse=False): # logger.info("Creating a q function ensemble with action space %d x %s..." % (self.dimu, self.max_u)) # self.sess = tf_util.get_session() self.sess = tf.get_default_session() assert self.sess is not None # running averages, separate from alg (this is within a different scope) # assume reuse is False with tf.variable_scope('o_stats') as vs: if reuse: vs.reuse_variables() self.o_stats = Normalizer(self.dimo, self.norm_eps, self.norm_clip, sess=self.sess) with tf.variable_scope('g_stats'): if reuse: vs.reuse_variables() self.g_stats = Normalizer(self.dimg, self.norm_eps, self.norm_clip, sess=self.sess) self.V_loss_tf = [None] * self.size_ensemble self.V_fun = [None] * self.size_ensemble self.V_grads_vars_tf = [None] * self.size_ensemble self.V_grad_tf = [None] * self.size_ensemble self.V_adam = [None] * self.size_ensemble clip_range = (-self.clip_return, 0. if self.clip_pos_returns else self.clip_return) for e in range(self.size_ensemble): # mini-batch sampling batch = self.staging_tf[e].get() batch_tf = OrderedDict([ (key, batch[i]) for i, key in enumerate(self.stage_shapes.keys()) ]) batch_tf['r'] = tf.reshape(batch_tf['r'], [-1, 1]) # networks (no target network for now) with tf.variable_scope("ve_{}".format(e)) as vs: if reuse: vs.reuse_variables() v_function = self.create_v_function(batch_tf, **self.__dict__) vs.reuse_variables() # loss functions V_2_tf = v_function.V_2_tf target_tf = tf.clip_by_value(batch_tf['r'] + self.gamma * V_2_tf, *clip_range) V_loss_tf = tf.reduce_mean( tf.square(tf.stop_gradient(target_tf) - v_function.V_tf)) V_scope = 've_{}/V'.format(e) V_grads_tf = tf.gradients(V_loss_tf, self._vars(V_scope)) assert len(self._vars(V_scope)) == len(V_grads_tf) V_grads_vars_tf = zip(V_grads_tf, self._vars(V_scope)) V_grad_tf = flatten_grads(grads=V_grads_tf, var_list=self._vars(V_scope)) # optimizers V_adam = MpiAdam(self._vars(V_scope), scale_grad_by_procs=False) # store in attribute lists self.V_loss_tf[e] = V_loss_tf self.V_fun[e] = v_function self.V_grads_vars_tf[e] = V_grads_vars_tf self.V_grad_tf[e] = V_grad_tf self.V_adam[e] = V_adam n_vars = [ len(self._vars("ve_{}".format(e))) for e in range(self.size_ensemble) ] assert np.all(np.asarray(n_vars) == n_vars[0]), n_vars # report loss as the average of value function loss over the ensemble # self.V_loss_tf = tf.reduce_mean(self.V_loss_tf) # initialize all variables tf.variables_initializer(self._global_vars('')).run() self._sync_optimizers() def _create_double_network(self, reuse=False): # logger.info("Creating a q function ensemble with action space %d x %s..." % (self.dimu, self.max_u)) self.sess = tf_util.get_session() # running averages, separate from alg (this is within a different scope) # assume reuse is False with tf.variable_scope('o_stats') as vs: if reuse: vs.reuse_variables() self.o_stats = Normalizer(self.dimo, self.norm_eps, self.norm_clip, sess=self.sess) with tf.variable_scope('g_stats'): if reuse: vs.reuse_variables() self.g_stats = Normalizer(self.dimg, self.norm_eps, self.norm_clip, sess=self.sess) self.V_loss_tf = [None] * self.size_ensemble self.V_fun = [None] * self.size_ensemble self.V_target_fun = [None] * self.size_ensemble self.V_grads_vars_tf = [None] * self.size_ensemble self.V_grad_tf = [None] * self.size_ensemble self.V_adam = [None] * self.size_ensemble self.init_target_net_op = [None] * self.size_ensemble self.update_target_net_op = [None] * self.size_ensemble clip_range = (-self.clip_return, 0. if self.clip_pos_returns else self.clip_return) for e in range(self.size_ensemble): # mini-batch sampling batch = self.staging_tf[e].get() batch_tf = OrderedDict([ (key, batch[i]) for i, key in enumerate(self.stage_shapes.keys()) ]) batch_tf['r'] = tf.reshape(batch_tf['r'], [-1, 1]) # networks (no target network for now) with tf.variable_scope(f've_{e}') as vs: if reuse: vs.reuse_variables() v_function = self.create_v_function(batch_tf, **self.__dict__) vs.reuse_variables() with tf.variable_scope(f've_{e}_target') as vs: if reuse: vs.reuse_variables() target_batch_tf = batch_tf.copy() target_batch_tf['o'] = batch_tf['o_2'] target_batch_tf['g'] = batch_tf['g_2'] target_batch_tf['u'] = batch_tf['u_2'] v_target_function = self.create_v_function( target_batch_tf, **self.__dict__) vs.reuse_variables() # loss functions target_tf = tf.clip_by_value( batch_tf['r'] + self.gamma * v_target_function.V_tf, *clip_range) V_loss_tf = tf.reduce_mean( tf.square(tf.stop_gradient(target_tf) - v_function.V_tf)) V_scope = f've_{e}/V' V_grads_tf = tf.gradients(V_loss_tf, self._vars(V_scope)) assert len(self._vars(V_scope)) == len(V_grads_tf) V_grads_vars_tf = zip(V_grads_tf, self._vars(V_scope)) V_grad_tf = flatten_grads(grads=V_grads_tf, var_list=self._vars(V_scope)) # optimizers V_adam = MpiAdam(self._vars(V_scope), scale_grad_by_procs=False) # store in attribute lists self.V_loss_tf[e] = V_loss_tf self.V_fun[e] = v_function self.V_target_fun[e] = v_target_function self.V_grads_vars_tf[e] = V_grads_vars_tf self.V_grad_tf[e] = V_grad_tf self.V_adam[e] = V_adam # polyak averaging main_vars = sum( [self._vars(f've_{e}/V') for e in range(self.size_ensemble)], []) target_vars = sum([ self._vars(f've_{e}_target/V') for e in range(self.size_ensemble) ], []) self.init_target_net_op = list( map(lambda v: v[0].assign(v[1]), zip(target_vars, main_vars))) self.update_target_net_op = list( map( lambda v: v[0].assign(self.polyak * v[0] + (1. - self.polyak) * v[1]), zip(target_vars, main_vars))) assert len(main_vars) == len(target_vars) # report loss as the average of value function loss over the ensemble # self.V_loss_tf = tf.reduce_mean(self.V_loss_tf) # initialize all variables tf.variables_initializer(self._global_vars('')).run() self._sync_optimizers() self._init_target_net() def _init_target_net(self): self.sess.run(self.init_target_net_op) def update_target_net(self): if self.use_double_network: self.sess.run(self.update_target_net_op) else: pass def _vars(self, scope): res = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.scope + '/' + scope) assert len(res) > 0 return res def _global_vars(self, scope): res = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.scope + '/' + scope) return res def _sync_optimizers(self): for e in range(self.size_ensemble): self.V_adam[e].sync() def _grads(self): """ returns: V_loss (scalar) V_grad (list) """ V_loss, V_grad = self.sess.run([ self.V_loss_tf, self.V_grad_tf, ]) return V_loss, V_grad def get_current_buffer_size(self): return self.buffer.get_current_size() def store_episode(self, episode_batch, update_stats=True): """ episode_batch: array of batch_size x (T or T+1) x dim_key 'o' is of size T+1, others are of size T """ # if self.use_Q: # u_2 = policy.get_actions(o=episode_batch['o'][:, 1:, :], ag=episode_batch['ag'][:, 1:, :], g=episode_batch['g']) # (batch_size x t x dimu) # self.buffer.store_episode({**episode_batch, 'u_2': u_2.reshape(episode_batch['u'].shape)}) # else: # self.buffer.store_episode(episode_batch) self.buffer.store_episode(episode_batch) if update_stats: # add transitions to normalizer # # flatten episode batch # o = episode_batch['o']#[:, :-1, :] # g = episode_batch['g']#[:, :-1, :] # ag = episode_batch['ag']#[:, :-1, :] # o = np.reshape(o, (-1, self.dimo)) # g = np.reshape(g, (-1, self.dimg)) # ag = np.reshape(ag, (-1, self.dimg)) # o, g = self._preprocess_og(o, ag, g) # # self.o_stats.update(o) # self.g_stats.update(g) # # self.o_stats.recompute_stats() # self.g_stats.recompute_stats() episode_batch['o_2'] = episode_batch['o'][:, 1:, :] episode_batch['ag_2'] = episode_batch['ag'][:, 1:, :] num_normalizing_transitions = transitions_in_episode_batch( episode_batch) transitions = self.sample_transitions(episode_batch, num_normalizing_transitions) o, g, ag = transitions['o'], transitions['g'], transitions['ag'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) # No need to preprocess the o_2 and g_2 since this is only used for stats self.o_stats.update(transitions['o']) self.g_stats.update(transitions['g']) self.o_stats.recompute_stats() self.g_stats.recompute_stats() def _preprocess_og(self, o, ag, g): if self.relative_goals: g_shape = g.shape g = g.reshape(-1, self.dimg) ag = ag.reshape(-1, self.dimg) g = self.subtract_goals(g, ag) g = g.reshape(*g_shape) o = np.clip(o, -self.clip_obs, self.clip_obs) g = np.clip(g, -self.clip_obs, self.clip_obs) return o, g def _preprocess_u(self, u): return np.clip(u, -self.max_u, self.max_u) def __getstate__(self): """Our policies can be loaded from pkl, but after unpickling you cannot continue training. """ excluded_subnames = [ '_tf', '_op', '_vars', '_adam', 'buffer', 'sess', '_stats', 'V_fun', 'V_target_fun', 'lock', 'env', 'sample_transitions', 'stage_shapes', 'create_v_function' ] state = { k: v for k, v in self.__dict__.items() if all([not subname in k for subname in excluded_subnames]) } state['buffer_size'] = self.buffer_size state['tf'] = self.sess.run( [x for x in self._global_vars('') if 'buffer' not in x.name]) return state def __setstate__(self, state): if 'sample_transitions' not in state: # We don't need this for playing the policy. state['sample_transitions'] = None if 'use_Q' not in state: state['use_Q'] = False # a hack to accomendate old data if 'create_v_function' in state: del state['create_v_function'] self.__init__(**state) # set up stats (they are overwritten in __init__) for k, v in state.items(): if k[-6:] == '_stats': self.__dict__[k] = v # load TF variables vars = [x for x in self._global_vars('') if 'buffer' not in x.name] assert (len(vars) == len(state["tf"])) node = [tf.assign(var, val) for var, val in zip(vars, state["tf"])] self.sess.run(node) def save(self, save_path): tf_util.save_variables(save_path)
class PGGD(object): DIMO = 0 @store_args def __init__(self, input_dims, buffer_size, hidden, layers, network_class, polyak, batch_size, Q_lr, pi_lr, norm_eps, norm_clip, max_u, action_l2, clip_obs, scope, T, rollout_batch_size, subtract_goals, relative_goals, clip_pos_returns, clip_return, sample_transitions, gamma, reuse=False, **kwargs): """Implementation of PGGD that is used in combination with Hindsight Experience Replay (HER). Args: input_dims (dict of ints): dimensions for the observation (o), the goal (g), and the actions (u) buffer_size (int): number of transitions that are stored in the replay buffer hidden (int): number of units in the hidden layers layers (int): number of hidden layers network_class (str): the network class that should be used (e.g. 'baselines.her.ActorCritic') polyak (float): coefficient for Polyak-averaging of the target network batch_size (int): batch size for training Q_lr (float): learning rate for the Q (critic) network pi_lr (float): learning rate for the pi (actor) network norm_eps (float): a small value used in the normalizer to avoid numerical instabilities norm_clip (float): normalized inputs are clipped to be in [-norm_clip, norm_clip] max_u (float): maximum action magnitude, i.e. actions are in [-max_u, max_u] action_l2 (float): coefficient for L2 penalty on the actions clip_obs (float): clip observations before normalization to be in [-clip_obs, clip_obs] scope (str): the scope used for the TensorFlow graph T (int): the time horizon for rollouts rollout_batch_size (int): number of parallel rollouts per PGGD agent subtract_goals (function): function that subtracts goals from each other relative_goals (boolean): whether or not relative goals should be fed into the network clip_pos_returns (boolean): whether or not positive returns should be clipped clip_return (float): clip returns to be in [-clip_return, clip_return] sample_transitions (function) function that samples from the replay buffer gamma (float): gamma used for Q learning updates reuse (boolean): whether or not the networks should be reused """ # ------------------ # To access information of environment name and stuff self.kwargs = kwargs # ------------------ if self.clip_return is None: self.clip_return = np.inf self.create_actor_critic = import_function(self.network_class) input_shapes = dims_to_shapes(self.input_dims) self.dimo = self.input_dims['o'] self.dimg = self.input_dims['g'] self.dimu = self.input_dims['u'] # ---------------------- input_shapes['o'] = (None, ) # ---------------------- # Prepare staging area for feeding data to the model. stage_shapes = OrderedDict() for key in sorted(self.input_dims.keys()): if key.startswith('info_'): continue stage_shapes[key] = (None, *input_shapes[key]) for key in ['o', 'g']: stage_shapes[key + '_2'] = stage_shapes[key] stage_shapes['r'] = (None, ) # ---------------------- stage_shapes['G'] = (None, ) # ---------------------- self.stage_shapes = stage_shapes # Create network. with tf.variable_scope(self.scope): self.staging_tf = StagingArea( dtypes=[tf.float32 for _ in self.stage_shapes.keys()], shapes=list(self.stage_shapes.values())) self.buffer_ph_tf = [ tf.placeholder(tf.float32, shape=shape) for shape in self.stage_shapes.values() ] self.stage_op = self.staging_tf.put(self.buffer_ph_tf) self._create_network(reuse=reuse) # Configure the replay buffer. buffer_shapes = { key: (self.T, *input_shapes[key]) if key != 'o' else (self.T + 1, PGGD.DIMO) for key, val in input_shapes.items() } buffer_shapes['g'] = (buffer_shapes['g'][0], self.dimg) buffer_shapes['ag'] = (self.T + 1, self.dimg) # ------------------- buffer_shapes['G'] = (self.T, ) buffer_shapes['sigma'] = (self.T, self.dimu) self.weight_path = None # ------------------- buffer_size = (self.buffer_size // self.rollout_batch_size) * self.rollout_batch_size self.buffer = ReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions) def _random_action(self, n): return np.random.uniform(low=-self.max_u, high=self.max_u, size=(n, self.dimu)) def _preprocess_og(self, o, ag, g): if self.relative_goals: g_shape = g.shape g = g.reshape(-1, self.dimg) ag = ag.reshape(-1, self.dimg) g = self.subtract_goals(g, ag) g = g.reshape(*g_shape) o = np.clip(o, -self.clip_obs, self.clip_obs) g = np.clip(g, -self.clip_obs, self.clip_obs) return o, g # ------------------------------- # If observation has more dimensions than what the policy takes in # then just truncate it. def get_actions(self, o, ag, g, exploit=False): # if len(o.shape) == 1: # o = o[:self.dimo] # g = g[:self.dimg] # ag = ag[:self.dimg] # else: # o = o[:,:self.dimo] # g = g[:,:self.dimg] # ag = ag[:,:self.dimg] o, g = self._preprocess_og(o, ag, g) policy = self.main # values to compute if exploit: vals = [policy.da_tf] else: vals = [policy.a_tf] vals += [policy.raw_tf, policy.sigma_tf] # feed feed = { policy.o_tf: o.reshape(-1, self.dimo), policy.g_tf: g.reshape(-1, self.dimg), policy.u_tf: np.zeros((o.size // self.dimo, self.dimu), dtype=np.float32) } ret = self.sess.run(vals, feed_dict=feed) # action postprocessing u, raw, sigma = ret if u.shape[0] == 1: u = u[0] raw = raw[0] sigma = sigma[0] u = u.copy() raw = raw.copy() sigma = sigma.copy() return u, raw, sigma # ------------------------------- def store_episode(self, episode_batch, update_stats=True): """ episode_batch: array of batch_size x (T or T+1) x dim_key 'o' is of size T+1, others are of size T """ self.buffer.store_episode(episode_batch) if update_stats: # add transitions to normalizer episode_batch['o_2'] = episode_batch['o'][:, 1:, :] episode_batch['ag_2'] = episode_batch['ag'][:, 1:, :] num_normalizing_transitions = transitions_in_episode_batch( episode_batch) transitions = self.sample_transitions(episode_batch, num_normalizing_transitions) o, o_2, g, ag = transitions['o'], transitions['o_2'], transitions[ 'g'], transitions['ag'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) # No need to preprocess the o_2 and g_2 since this is only used for stats if 'Variation' in self.kwargs['info']['env_name']: o = transitions['o'][:, 1:] # o = np.concatenate([transitions['o'][:,:ENV_FEATURES], # transitions['o'][:,ENV_FEATURES+1:]], axis=1) else: o = transitions['o'] self.o_stats.update(o) self.G_stats.update(transitions['G']) self.sigma_stats.update(transitions['sigma']) # self.g_stats.update(transitions['g']) self.o_stats.recompute_stats() # self.g_stats.recompute_stats() self.G_stats.recompute_stats() self.sigma_stats.recompute_stats() def get_current_buffer_size(self): return self.buffer.get_current_size() def _sync_optimizers(self): self.pi_adam.sync() def _grads(self): # Avoid feed_dict here for performance! pi_loss, pi_grad, mu = self.sess.run( [self.pi_loss_tf, self.pi_grad_tf, self.main.mu_tf]) # print(np.mean(mu), np.mean(pi_grad), np.mean(pi_loss)) return pi_loss, pi_grad def _update(self, pi_grad): self.pi_adam.update(pi_grad, self.pi_lr) def sample_batch(self): transitions = self.buffer.sample(self.batch_size) o, o_2, g = transitions['o'], transitions['o_2'], transitions['g'] ag, ag_2 = transitions['ag'], transitions['ag_2'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) transitions['o_2'], transitions['g_2'] = self._preprocess_og( o_2, ag_2, g) transitions_batch = [ transitions[key] for key in self.stage_shapes.keys() ] # print(transitions['G']) return transitions_batch def stage_batch(self, batch=None): if batch is None: batch = self.sample_batch() assert len(self.buffer_ph_tf) == len(batch) self.sess.run(self.stage_op, feed_dict=dict(zip(self.buffer_ph_tf, batch))) def train(self, stage=True): if stage: self.stage_batch() pi_loss, pi_grad = self._grads() self._update(pi_grad) # print(np.mean(pi_grad)) return pi_loss def _init_target_net(self): self.sess.run(self.init_target_net_op) def update_target_net(self): self.sess.run(self.update_target_net_op) def clear_buffer(self): self.buffer.clear_buffer() def _vars(self, scope): res = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.scope + '/' + scope) assert len(res) > 0 return res def _global_vars(self, scope): res = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.scope + '/' + scope) return res def _create_network(self, reuse=False): logger.info("Creating a PGGD agent with action space %d x %s..." % (self.dimu, self.max_u)) self.sess = tf.get_default_session() if self.sess is None: self.sess = tf.InteractiveSession() # running averages with tf.variable_scope('o_stats') as vs: if reuse: vs.reuse_variables() o_stats_dim = self.dimo if 'Variation' in self.kwargs['info']['env_name']: print("Found Variation in env name") o_stats_dim -= 1 self.o_stats = Normalizer(o_stats_dim, self.norm_eps, self.norm_clip, sess=self.sess) # -------------- with tf.variable_scope('G_stats') as vs: if reuse: vs.reuse_variables() self.G_stats = Normalizer(1, self.norm_eps, self.norm_clip, sess=self.sess) with tf.variable_scope('sigma_stats') as vs: if reuse: vs.reuse_variables() self.sigma_stats = Normalizer(self.dimu, self.norm_eps, self.norm_clip, sess=self.sess) # -------------- with tf.variable_scope('g_stats') as vs: if reuse: vs.reuse_variables() self.g_stats = Normalizer(self.dimg, self.norm_eps, self.norm_clip, sess=self.sess) # mini-batch sampling. batch = self.staging_tf.get() batch_tf = OrderedDict([ (key, batch[i]) for i, key in enumerate(self.stage_shapes.keys()) ]) batch_tf['r'] = tf.reshape(batch_tf['r'], [-1, 1]) # ------------ batch_tf['G'] = tf.reshape(batch_tf['G'], [ -1, ]) # ------------ # networks with tf.variable_scope('main') as vs: if reuse: vs.reuse_variables() self.main = self.create_actor_critic(batch_tf, net_type='main', **self.__dict__) vs.reuse_variables() with tf.variable_scope('target') as vs: if reuse: vs.reuse_variables() target_batch_tf = batch_tf.copy() target_batch_tf['o'] = batch_tf['o_2'] target_batch_tf['g'] = batch_tf['g_2'] self.target = self.create_actor_critic(target_batch_tf, net_type='target', **self.__dict__) vs.reuse_variables() assert len(self._vars("main")) == len(self._vars("target")) # --------------------------- # loss functions log_prob = tf.reduce_sum(tf.log( tf.clip_by_value(self.main.a_prob_tf, 1e-10, 1.0)), axis=1) neg_weighted_log_prob = -tf.multiply(batch_tf['G'], log_prob) self.pi_loss_tf = tf.reduce_mean(neg_weighted_log_prob) # https://github.com/tensorflow/tensorflow/issues/783 def replace_none_with_zero(grads, var_list): return [ grad if grad is not None else tf.zeros_like(var) for var, grad in zip(var_list, grads) ] pi_grads_tf = replace_none_with_zero( tf.gradients(self.pi_loss_tf, self._vars('main/pi')), self._vars('main/pi')) assert len(self._vars('main/pi')) == len(pi_grads_tf) self.pi_grads_vars_tf = zip(pi_grads_tf, self._vars('main/pi')) self.pi_grad_tf = flatten_grads(grads=pi_grads_tf, var_list=self._vars('main/pi')) # --------------------------- # optimizers self.pi_adam = MpiAdam(self._vars('main/pi'), scale_grad_by_procs=False) # polyak averaging # self.main_vars = self._vars('main/Q') + self._vars('main/pi') # self.target_vars = self._vars('target/Q') + self._vars('target/pi') self.stats_vars = self._global_vars('o_stats') + self._global_vars( 'g_stats') + self._global_vars('G_stats') + self._global_vars( 'sigma_stats') # self.init_target_net_op = list( # map(lambda v: v[0].assign(v[1]), zip(self.target_vars, self.main_vars))) # self.update_target_net_op = list( # map(lambda v: v[0].assign(self.polyak * v[0] + (1. - self.polyak) * v[1]), zip(self.target_vars, self.main_vars))) # initialize all variables tf.variables_initializer(self._global_vars('')).run() self._sync_optimizers() # self._init_target_net() def logs(self, prefix=''): logs = [] logs += [('stats_o/mean', np.mean(self.sess.run([self.o_stats.mean])))] logs += [('stats_o/std', np.mean(self.sess.run([self.o_stats.std])))] logs += [('stats_g/mean', np.mean(self.sess.run([self.g_stats.mean])))] logs += [('stats_g/std', np.mean(self.sess.run([self.g_stats.std])))] logs += [('stats_G/mean', np.mean(self.sess.run([self.G_stats.mean])))] logs += [('stats_G/std', np.mean(self.sess.run([self.G_stats.std])))] logs += [('stats_stddev/mean', np.mean(self.sess.run([self.sigma_stats.mean])))] if prefix is not '' and not prefix.endswith('/'): return [(prefix + '/' + key, val) for key, val in logs] else: return logs def __getstate__(self): """Our policies can be loaded from pkl, but after unpickling you cannot continue training. """ excluded_subnames = [ '_tf', '_op', '_vars', '_adam', 'buffer', 'sess', '_stats', 'main', 'target', 'lock', 'env', 'sample_transitions', 'stage_shapes', 'create_actor_critic' ] state = { k: v for k, v in self.__dict__.items() if all([not subname in k for subname in excluded_subnames]) } state['buffer_size'] = self.buffer_size state['tf'] = self.sess.run( [x for x in self._global_vars('') if 'buffer' not in x.name]) return state def set_sample_transitions(self, fn): self.sample_transitions = fn self.buffer.sample_transitions = fn def set_obs_size(self, dims): self.input_dims = dims self.dimo = dims['o'] self.dimg = dims['g'] self.dimu = dims['u'] def save_weights(self, path): self.main.save_weights(self.sess, path) self.weight_path = path def __setstate__(self, state): if 'sample_transitions' not in state: # We don't need this for playing the policy. state['sample_transitions'] = None self.__init__(**state) # set up stats (they are overwritten in __init__) for k, v in state.items(): if k[-6:] == '_stats': self.__dict__[k] = v self.weight_path = state['weight_path'] # Hard override... # This is due to the fact that the directory that the weights are saved to # might not be the same when it is loaded again # TODO: Delete this!!!! self.weight_path = "/Users/matt/RL/Results/5-3blocks-GPGGD-3-256/weights" # load TF variables vars = [x for x in self._global_vars('') if 'buffer' not in x.name] assert (len(vars) == len(state["tf"])) node = [ tf.no_op() if 'o_stats' in var.name else tf.assign(var, val) for var, val in zip(vars, state["tf"]) ] self.sess.run(node) if self.weight_path != None: print("Reading weights for sure this time!") print(self.weight_path) print(tf.train.latest_checkpoint(self.weight_path)) self.main.load_weights(self.sess, self.weight_path)
class DDPG(object): @store_args def __init__(self, input_dims, buffer_size, hidden, layers, network_class, polyak, batch_size, Q_lr, pi_lr, norm_eps, norm_clip, max_u, action_l2, clip_obs, scope, T, rollout_batch_size, subtract_goals, relative_goals, clip_pos_returns, clip_return, sample_transitions, gamma, reuse=False, **kwargs): """Implementation of DDPG that is used in combination with Hindsight Experience Replay (HER). Args: input_dims (dict of ints): dimensions for the observation (o), the goal (g), and the actions (u) buffer_size (int): number of transitions that are stored in the replay buffer hidden (int): number of units in the hidden layers layers (int): number of hidden layers network_class (str): the network class that should be used (e.g. 'baselines.her.ActorCritic') polyak (float): coefficient for Polyak-averaging of the target network batch_size (int): batch size for training Q_lr (float): learning rate for the Q (critic) network pi_lr (float): learning rate for the pi (actor) network norm_eps (float): a small value used in the normalizer to avoid numerical instabilities norm_clip (float): normalized inputs are clipped to be in [-norm_clip, norm_clip] max_u (float): maximum action magnitude, i.e. actions are in [-max_u, max_u] action_l2 (float): coefficient for L2 penalty on the actions clip_obs (float): clip observations before normalization to be in [-clip_obs, clip_obs] scope (str): the scope used for the TensorFlow graph T (int): the time horizon for rollouts rollout_batch_size (int): number of parallel rollouts per DDPG agent subtract_goals (function): function that subtracts goals from each other relative_goals (boolean): whether or not relative goals should be fed into the network clip_pos_returns (boolean): whether or not positive returns should be clipped clip_return (float): clip returns to be in [-clip_return, clip_return] sample_transitions (function) function that samples from the replay buffer gamma (float): gamma used for Q learning updates reuse (boolean): whether or not the networks should be reused """ if self.clip_return is None: self.clip_return = np.inf self.create_actor_critic = import_function(self.network_class) input_shapes = dims_to_shapes(self.input_dims) self.dimo = self.input_dims['o'] self.dimg = self.input_dims['g'] self.dimu = self.input_dims['u'] # Prepare staging area for feeding data to the model. stage_shapes = OrderedDict() for key in sorted(self.input_dims.keys()): if key.startswith('info_'): continue stage_shapes[key] = (None, *input_shapes[key]) for key in ['o', 'g']: stage_shapes[key + '_2'] = stage_shapes[key] stage_shapes['r'] = (None,) self.stage_shapes = stage_shapes # Create network. with tf.variable_scope(self.scope): self.staging_tf = StagingArea( dtypes=[tf.float32 for _ in self.stage_shapes.keys()], shapes=list(self.stage_shapes.values())) self.buffer_ph_tf = [ tf.placeholder(tf.float32, shape=shape) for shape in self.stage_shapes.values()] self.stage_op = self.staging_tf.put(self.buffer_ph_tf) self._create_network(reuse=reuse) # Configure the replay buffer. buffer_shapes = {key: (self.T if key != 'o' else self.T+1, *input_shapes[key]) for key, val in input_shapes.items()} buffer_shapes['g'] = (buffer_shapes['g'][0], self.dimg) buffer_shapes['ag'] = (self.T+1, self.dimg) buffer_size = (self.buffer_size // self.rollout_batch_size) * self.rollout_batch_size self.buffer = ReplayBuffer(buffer_shapes, buffer_size, self.T, self.sample_transitions) def _random_action(self, n): return np.random.uniform(low=-self.max_u, high=self.max_u, size=(n, self.dimu)) def _preprocess_og(self, o, ag, g): if self.relative_goals: g_shape = g.shape g = g.reshape(-1, self.dimg) ag = ag.reshape(-1, self.dimg) g = self.subtract_goals(g, ag) g = g.reshape(*g_shape) o = np.clip(o, -self.clip_obs, self.clip_obs) g = np.clip(g, -self.clip_obs, self.clip_obs) return o, g def get_actions(self, o, ag, g, noise_eps=0., random_eps=0., use_target_net=False, compute_Q=False): o, g = self._preprocess_og(o, ag, g) policy = self.target if use_target_net else self.main # values to compute vals = [policy.pi_tf] if compute_Q: vals += [policy.Q_pi_tf] # feed feed = { policy.o_tf: o.reshape(-1, self.dimo), policy.g_tf: g.reshape(-1, self.dimg), policy.u_tf: np.zeros((o.size // self.dimo, self.dimu), dtype=np.float32) } ret = self.sess.run(vals, feed_dict=feed) # action postprocessing u = ret[0] noise = noise_eps * self.max_u * np.random.randn(*u.shape) # gaussian noise u += noise u = np.clip(u, -self.max_u, self.max_u) u += np.random.binomial(1, random_eps, u.shape[0]).reshape(-1, 1) * (self._random_action(u.shape[0]) - u) # eps-greedy if u.shape[0] == 1: u = u[0] u = u.copy() ret[0] = u if len(ret) == 1: return ret[0] else: return ret def store_episode(self, episode_batch, update_stats=True): """ episode_batch: array of batch_size x (T or T+1) x dim_key 'o' is of size T+1, others are of size T """ self.buffer.store_episode(episode_batch) if update_stats: # add transitions to normalizer episode_batch['o_2'] = episode_batch['o'][:, 1:, :] episode_batch['ag_2'] = episode_batch['ag'][:, 1:, :] num_normalizing_transitions = transitions_in_episode_batch(episode_batch) transitions = self.sample_transitions(episode_batch, num_normalizing_transitions) o, o_2, g, ag = transitions['o'], transitions['o_2'], transitions['g'], transitions['ag'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) # No need to preprocess the o_2 and g_2 since this is only used for stats self.o_stats.update(transitions['o']) self.g_stats.update(transitions['g']) self.o_stats.recompute_stats() self.g_stats.recompute_stats() def get_current_buffer_size(self): return self.buffer.get_current_size() def _sync_optimizers(self): self.Q_adam.sync() self.pi_adam.sync() def _grads(self): # Avoid feed_dict here for performance! critic_loss, actor_loss, Q_grad, pi_grad = self.sess.run([ self.Q_loss_tf, self.main.Q_pi_tf, self.Q_grad_tf, self.pi_grad_tf ]) return critic_loss, actor_loss, Q_grad, pi_grad def _update(self, Q_grad, pi_grad): self.Q_adam.update(Q_grad, self.Q_lr) self.pi_adam.update(pi_grad, self.pi_lr) def sample_batch(self): transitions = self.buffer.sample(self.batch_size) o, o_2, g = transitions['o'], transitions['o_2'], transitions['g'] ag, ag_2 = transitions['ag'], transitions['ag_2'] transitions['o'], transitions['g'] = self._preprocess_og(o, ag, g) transitions['o_2'], transitions['g_2'] = self._preprocess_og(o_2, ag_2, g) transitions_batch = [transitions[key] for key in self.stage_shapes.keys()] return transitions_batch def stage_batch(self, batch=None): if batch is None: batch = self.sample_batch() assert len(self.buffer_ph_tf) == len(batch) self.sess.run(self.stage_op, feed_dict=dict(zip(self.buffer_ph_tf, batch))) def train(self, stage=True): if stage: self.stage_batch() critic_loss, actor_loss, Q_grad, pi_grad = self._grads() self._update(Q_grad, pi_grad) return critic_loss, actor_loss def _init_target_net(self): self.sess.run(self.init_target_net_op) def update_target_net(self): self.sess.run(self.update_target_net_op) def clear_buffer(self): self.buffer.clear_buffer() def _vars(self, scope): res = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.scope + '/' + scope) assert len(res) > 0 return res def _global_vars(self, scope): res = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.scope + '/' + scope) return res def _create_network(self, reuse=False): logger.info("Creating a DDPG agent with action space %d x %s..." % (self.dimu, self.max_u)) self.sess = tf.get_default_session() if self.sess is None: self.sess = tf.InteractiveSession() # running averages with tf.variable_scope('o_stats') as vs: if reuse: vs.reuse_variables() self.o_stats = Normalizer(self.dimo, self.norm_eps, self.norm_clip, sess=self.sess) with tf.variable_scope('g_stats') as vs: if reuse: vs.reuse_variables() self.g_stats = Normalizer(self.dimg, self.norm_eps, self.norm_clip, sess=self.sess) # mini-batch sampling. batch = self.staging_tf.get() batch_tf = OrderedDict([(key, batch[i]) for i, key in enumerate(self.stage_shapes.keys())]) batch_tf['r'] = tf.reshape(batch_tf['r'], [-1, 1]) # networks with tf.variable_scope('main') as vs: if reuse: vs.reuse_variables() self.main = self.create_actor_critic(batch_tf, net_type='main', **self.__dict__) vs.reuse_variables() with tf.variable_scope('target') as vs: if reuse: vs.reuse_variables() target_batch_tf = batch_tf.copy() target_batch_tf['o'] = batch_tf['o_2'] target_batch_tf['g'] = batch_tf['g_2'] self.target = self.create_actor_critic( target_batch_tf, net_type='target', **self.__dict__) vs.reuse_variables() assert len(self._vars("main")) == len(self._vars("target")) # loss functions target_Q_pi_tf = self.target.Q_pi_tf clip_range = (-self.clip_return, 0. if self.clip_pos_returns else np.inf) target_tf = tf.clip_by_value(batch_tf['r'] + self.gamma * target_Q_pi_tf, *clip_range) self.Q_loss_tf = tf.reduce_mean(tf.square(tf.stop_gradient(target_tf) - self.main.Q_tf)) self.pi_loss_tf = -tf.reduce_mean(self.main.Q_pi_tf) self.pi_loss_tf += self.action_l2 * tf.reduce_mean(tf.square(self.main.pi_tf / self.max_u)) Q_grads_tf = tf.gradients(self.Q_loss_tf, self._vars('main/Q')) pi_grads_tf = tf.gradients(self.pi_loss_tf, self._vars('main/pi')) assert len(self._vars('main/Q')) == len(Q_grads_tf) assert len(self._vars('main/pi')) == len(pi_grads_tf) self.Q_grads_vars_tf = zip(Q_grads_tf, self._vars('main/Q')) self.pi_grads_vars_tf = zip(pi_grads_tf, self._vars('main/pi')) self.Q_grad_tf = flatten_grads(grads=Q_grads_tf, var_list=self._vars('main/Q')) self.pi_grad_tf = flatten_grads(grads=pi_grads_tf, var_list=self._vars('main/pi')) # optimizers self.Q_adam = MpiAdam(self._vars('main/Q'), scale_grad_by_procs=False) self.pi_adam = MpiAdam(self._vars('main/pi'), scale_grad_by_procs=False) # polyak averaging self.main_vars = self._vars('main/Q') + self._vars('main/pi') self.target_vars = self._vars('target/Q') + self._vars('target/pi') self.stats_vars = self._global_vars('o_stats') + self._global_vars('g_stats') self.init_target_net_op = list( map(lambda v: v[0].assign(v[1]), zip(self.target_vars, self.main_vars))) self.update_target_net_op = list( map(lambda v: v[0].assign(self.polyak * v[0] + (1. - self.polyak) * v[1]), zip(self.target_vars, self.main_vars))) # initialize all variables tf.variables_initializer(self._global_vars('')).run() self._sync_optimizers() self._init_target_net() def logs(self, prefix=''): logs = [] logs += [('stats_o/mean', np.mean(self.sess.run([self.o_stats.mean])))] logs += [('stats_o/std', np.mean(self.sess.run([self.o_stats.std])))] logs += [('stats_g/mean', np.mean(self.sess.run([self.g_stats.mean])))] logs += [('stats_g/std', np.mean(self.sess.run([self.g_stats.std])))] if prefix is not '' and not prefix.endswith('/'): return [(prefix + '/' + key, val) for key, val in logs] else: return logs def __getstate__(self): """Our policies can be loaded from pkl, but after unpickling you cannot continue training. """ excluded_subnames = ['_tf', '_op', '_vars', '_adam', 'buffer', 'sess', '_stats', 'main', 'target', 'lock', 'env', 'sample_transitions', 'stage_shapes', 'create_actor_critic'] state = {k: v for k, v in self.__dict__.items() if all([not subname in k for subname in excluded_subnames])} state['buffer_size'] = self.buffer_size state['tf'] = self.sess.run([x for x in self._global_vars('') if 'buffer' not in x.name]) return state def __setstate__(self, state): if 'sample_transitions' not in state: # We don't need this for playing the policy. state['sample_transitions'] = None self.__init__(**state) # set up stats (they are overwritten in __init__) for k, v in state.items(): if k[-6:] == '_stats': self.__dict__[k] = v # load TF variables vars = [x for x in self._global_vars('') if 'buffer' not in x.name] assert(len(vars) == len(state["tf"])) node = [tf.assign(var, val) for var, val in zip(vars, state["tf"])] self.sess.run(node)