def __init__(self, env, master_policy,old_master_policy, sub_policies, old_sub_policies, comm, clip_param=0.2, entcoeff=0, optim_epochs=10, optim_stepsize=3e-4, optim_batchsize=64): self.clip_param = clip_param self.entcoeff = entcoeff self.optim_epochs = optim_epochs self.optim_stepsize = optim_stepsize self.optim_batchsize = optim_batchsize self.num_subpolicies = len(sub_policies) self.sub_policies = sub_policies self.master_policy = master_policy ob_space = env.observation_space ac_space = env.action_space self.sp_ac = sub_policies[0].pdtype.sample_placeholder([None]) atarg = tf.placeholder(dtype=tf.float32, shape=[None]) ret = tf.placeholder(dtype=tf.float32, shape=[None]) # Empirical return # for training theta # inputs for training theta ob = U.get_placeholder_cached(name="ob") ob_master = U.get_placeholder_cached(name="adv_ob") ac_master = master_policy.pdtype.sample_placeholder([None]) loss_master = self.policy_loss_master(master_policy, old_master_policy, ob_master, ac_master, atarg, ret, clip_param) self.master_policy_var_list = master_policy.get_trainable_variables() self.master_loss = U.function([ob_master, ac_master, atarg, ret], U.flatgrad(loss_master, self.master_policy_var_list)) self.master_adam = MpiAdam(self.master_policy_var_list, comm=comm) self.assign_old_eq_new = U.function([],[], updates=[tf.assign(oldv, newv) for (oldv, newv) in zipsame(old_master_policy.get_variables(), master_policy.get_variables())]) self.assign_subs = [] self.change_subs = [] self.adams = [] self.losses = [] for i in range(self.num_subpolicies): varlist = sub_policies[i].get_trainable_variables() self.adams.append(MpiAdam(varlist)) # loss for test loss = self.policy_loss(sub_policies[i], sub_policies[(i-1)%2], old_sub_policies[i], ob, self.sp_ac, atarg, ret, clip_param) self.losses.append(U.function([ob, self.sp_ac, atarg, ret], U.flatgrad(loss, varlist))) self.assign_subs.append(U.function([],[], updates=[tf.assign(oldv, newv) for (oldv, newv) in zipsame(old_sub_policies[i].get_variables(), sub_policies[i].get_variables())])) self.zerograd = U.function([], self.nograd(varlist)) U.initialize() self.master_adam.sync() for i in range(self.num_subpolicies): self.adams[i].sync()
def __init__(self, env, policy, old_policy, sub_policies, old_sub_policies, comm, clip_param=0.2, entcoeff=0, optim_epochs=10, optim_stepsize=3e-4, optim_batchsize=64): self.policy = policy self.clip_param = clip_param self.entcoeff = entcoeff self.optim_epochs = optim_epochs self.optim_stepsize = optim_stepsize self.optim_batchsize = optim_batchsize self.num_subpolicies = len(sub_policies) self.sub_policies = sub_policies ob_space = env.observation_space ac_space = env.action_space if WRITE_SCALAR: self.scalar_writer = tf.summary.FileWriter(osp.join("savedir/",'checkpoints', 'scalar%d' % time.time())) # for training theta # inputs for training theta ob = U.get_placeholder_cached(name="ob") ac = policy.pdtype.sample_placeholder([None]) atarg = tf.placeholder(dtype=tf.float32, shape=[None]) # Target advantage function (if applicable) ret = tf.placeholder(dtype=tf.float32, shape=[None]) # Empirical return total_loss = self.policy_loss(policy, old_policy, ob, ac, atarg, ret, clip_param) self.master_policy_var_list = policy.get_trainable_variables() self.master_loss = U.function([ob, ac, atarg, ret], U.flatgrad(total_loss, self.master_policy_var_list)) self.master_adam = MpiAdam(self.master_policy_var_list, comm=comm) summ = tf.summary.scalar("total_loss", total_loss) self.calc_summary = U.function([ob, ac, atarg, ret],[summ]) self.assign_old_eq_new = U.function([],[], updates=[tf.assign(oldv, newv) for (oldv, newv) in zipsame(old_policy.get_variables(), policy.get_variables())]) self.assign_subs = [] self.change_subs = [] self.adams = [] self.losses = [] self.sp_ac = sub_policies[0].pdtype.sample_placeholder([None]) for i in range(self.num_subpolicies): varlist = sub_policies[i].get_trainable_variables() self.adams.append(MpiAdam(varlist)) # loss for test loss = self.policy_loss(sub_policies[i], old_sub_policies[i], ob, self.sp_ac, atarg, ret, clip_param) self.losses.append(U.function([ob, self.sp_ac, atarg, ret], U.flatgrad(loss, varlist))) self.assign_subs.append(U.function([],[], updates=[tf.assign(oldv, newv) for (oldv, newv) in zipsame(old_sub_policies[i].get_variables(), sub_policies[i].get_variables())])) self.zerograd = U.function([], self.nograd(varlist)) U.initialize() self.master_adam.sync() for i in range(self.num_subpolicies): self.adams[i].sync()
def test_MpiAdam(): np.random.seed(0) tf.set_random_seed(0) a = tf.Variable(np.random.randn(3).astype('float32')) b = tf.Variable(np.random.randn(2, 5).astype('float32')) loss = tf.reduce_sum(tf.square(a)) + tf.reduce_sum(tf.sin(b)) stepsize = 1e-2 update_op = tf.train.AdamOptimizer(stepsize).minimize(loss) do_update = U.function([], loss, updates=[update_op]) tf.get_default_session().run(tf.global_variables_initializer()) for i in range(10): print(i, do_update()) tf.set_random_seed(0) tf.get_default_session().run(tf.global_variables_initializer()) var_list = [a, b] lossandgrad = U.function([], [loss, U.flatgrad(loss, var_list)], updates=[update_op]) adam = MpiAdam(var_list) for i in range(10): l, g = lossandgrad() adam.update(g, stepsize) print(i, l)
def __init__(self, env, sub_policy, old_sub_policy, comm, clip_param=0.2, entcoeff=0, optim_epochs=10, optim_stepsize=3e-4, optim_batchsize=64, args=None): # self.policy = policy self.clip_param = clip_param self.entcoeff = entcoeff self.optim_epochs = optim_epochs self.optim_stepsize = optim_stepsize self.optim_batchsize = optim_batchsize # self.num_subpolicies = len(sub_policies) self.sub_policy = sub_policy self.args = args ob_space = env.observation_space ac_space = env.action_space # for training theta # inputs for training theta ob = U.get_placeholder_cached(name="ob") # ac = policy.pdtype.sample_placeholder([None]) atarg = tf.placeholder( dtype=tf.float32, shape=[None]) # Target advantage function (if applicable) ret = tf.placeholder(dtype=tf.float32, shape=[None]) # Empirical return entcoeff = tf.placeholder(dtype=tf.float32, name="entcoef") # total_loss = self.policy_loss(policy, old_policy, ob, ac, atarg, ret, clip_param, entcoeff) # self.master_policy_var_list = policy.get_trainable_variables() # self.master_loss = U.function([ob, ac, atarg, ret, entcoeff], U.flatgrad(total_loss, self.master_policy_var_list)) # self.master_adam = MpiAdam(self.master_policy_var_list, comm=comm) # self.assign_old_eq_new = U.function([],[], updates=[tf.assign(oldv, newv) # for (oldv, newv) in zipsame(old_policy.get_variables(), policy.get_variables())]) self.assign_subs = [] self.change_subs = [] self.adams = [] self.losses = [] self.sp_ac = sub_policy.pdtype.sample_placeholder([None]) # for i in range(self.num_subpolicies): varlist = sub_policy.get_trainable_variables() self.adams.append(MpiAdam(varlist)) # loss for test loss = self.policy_loss(sub_policy, old_sub_policy, ob, self.sp_ac, atarg, ret, clip_param, entcoeff) self.losses.append( U.function([ob, self.sp_ac, atarg, ret, entcoeff], U.flatgrad(loss, varlist))) self.assign_subs.append( U.function( [], [], updates=[ tf.assign(oldv, newv) for (oldv, newv) in zipsame(old_sub_policy.get_variables(), sub_policy.get_variables()) ])) self.zerograd = U.function([], self.nograd(varlist)) U.initialize() # self.master_adam.sync() # for i in range(self.num_subpolicies): self.adams[0].sync()