def main(): ap = argparse.ArgumentParser() ap.add_argument('--arch', required=True, nargs='+', type=int) ap.add_argument('--lr', required=False, type=int, default=.01) ap.add_argument('--epochs', required=False, type=int, default=100) ap.add_argument('--iters', required=True, type=int) ap.add_argument('--trials', required=True, type=int) ap.add_argument('--env', required=True) ap.add_argument('--t', required=True, type=int) args = vars(ap.parse_args()) opt = Options() opt.envname = args['env'] opt.trials = args['trials'] opt.iters = args['iters'] opt.epochs = args['epochs'] opt.lr = args['lr'] opt.arch = args['arch'] opt.t = args['t'] opt.filename = '/Users/JonathanLee/experts/' + opt.envname + '.pkl' opt.env = gym.envs.make(opt.envname).env opt.sim = gym.envs.make(opt.envname).env opt.pi = load_policy.load_policy(opt.filename) opt.sess = tf.Session() opt.sup = NetSupervisor(opt.pi, opt.sess) run_trial(opt)
def load_env(envname): filename = '/Users/JonathanLee/experts/' + envname + '.pkl' env = gym.envs.make(envname).env pi = load_policy.load_policy(filename) sess = tf.Session() sup = NetSupervisor(pi, sess) return env, sup
def main(): ap = argparse.ArgumentParser() ap.add_argument('--arch', required=True, nargs='+', type=int) ap.add_argument('--lr', required=False, type=int, default=.01) ap.add_argument('--epochs', required=False, type=int, default=100) ap.add_argument('--iters', required=True, type=int) ap.add_argument('--trials', required=True, type=int) ap.add_argument('--env', required=True) ap.add_argument('--t', required=True, type=int) opt = Options() opt.load_args(ap.parse_args()) opt.envname = opt.env opt.filename = '/Users/JonathanLee/experts/' + opt.envname + '.pkl' opt.env = gym.envs.make(opt.envname).env opt.sim = gym.envs.make(opt.envname).env opt.pi = load_policy.load_policy(opt.filename) opt.sess = tf.Session() opt.sup = NetSupervisor(opt.pi, opt.sess) plot_dir = utils.generate_plot_dir('initial', 'experts', vars(opt)) data_dir = utils.generate_data_dir('initial', 'experts', vars(opt)) if not os.path.exists(plot_dir): os.makedirs(plot_dir) if not os.path.exists(data_dir): os.makedirs(data_dir) opt.plot_dir = plot_dir opt.data_dir = data_dir train_errs = np.zeros((opt.trials, opt.t)) valid_errs = np.zeros((opt.trials, opt.t)) test_errs = np.zeros((opt.trials, opt.t)) for t in range(opt.trials): train_errs[t, :], valid_errs[t, :], test_errs[t, :] = run_trial(opt, t) train_err = np.mean(train_errs, axis=0) valid_err = np.mean(valid_errs, axis=0) test_err = np.mean(test_errs, axis=0) errs = [train_err, valid_err, test_err] labels = ['train', 'valid', 'test'] width = .2 index = np.arange(opt.t) for i, (err, label) in enumerate(zip(errs, labels)): plt.bar(index + i * width, err, width, label=label) plt.legend() plt.ylim(0, .75) plt.savefig('/Users/JonathanLee/Desktop/bar_original_avg.png') utils.clear()
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import gym import matplotlib.pyplot as plt from icra_tools.supervisor import NetSupervisor import tensorflow as tf import numpy as np from icra_tools.expert import load_policy from icra_tools import statistics import IPython from sklearn import svm from icra_tools import utils envname = 'Walker2d-v1' filename = '/Users/JonathanLee/experts/' + envname + '.pkl' env = gym.envs.make(envname) pi = load_policy.load_policy(filename) sess = tf.Session() sup = NetSupervisor(pi, sess) T = 1000 ITERATIONS = 10 trajs_train = [] trajs_test = [] for i in range(7): print "{ Iteration: " + str(i) + " }" s = env.reset() reward = 0.0 traj = [] for t in range(T): # env.render()
def main(): ap = argparse.ArgumentParser() ap.add_argument('--arch', required=True, nargs='+', type=int) ap.add_argument('--lr', required=False, type=int, default=.01) ap.add_argument('--epochs', required=False, type=int, default=100) ap.add_argument('--iters', required=True, type=int) ap.add_argument('--trials', required=True, type=int) ap.add_argument('--env', required=True) ap.add_argument('--t', required=True, type=int) ap.add_argument('--grads', required=True, type=int) opt = Options() opt.load_args(ap.parse_args()) opt.envname = opt.env opt.filename = '/Users/JonathanLee/experts/' + opt.envname + '.pkl' opt.env = gym.envs.make(opt.envname).env opt.sim = gym.envs.make(opt.envname).env opt.pi = load_policy.load_policy(opt.filename) opt.sess = tf.Session() opt.sup = NetSupervisor(opt.pi, opt.sess) opt.misc = Options() opt.misc.num_evaluations = 10 plot_dir = utils.generate_plot_dir('initial', 'experts', vars(opt)) data_dir = utils.generate_data_dir('initial', 'experts', vars(opt)) if not os.path.exists(plot_dir): os.makedirs(plot_dir) if not os.path.exists(data_dir): os.makedirs(data_dir) if not os.path.exists(plot_dir + '/scores'): os.makedirs(plot_dir + '/scores') if not os.path.exists(plot_dir + '/mags'): os.makedirs(plot_dir + '/mags') opt.plot_dir = plot_dir opt.data_dir = data_dir sup_rewards = np.zeros((opt.trials, opt.misc.num_evaluations)) lnr_rewards = np.zeros((opt.trials, opt.misc.num_evaluations)) rob_rewards = np.zeros((opt.trials, opt.misc.num_evaluations)) train_err = np.zeros((opt.trials, opt.misc.num_evaluations, opt.t)) valid_err = np.zeros((opt.trials, opt.misc.num_evaluations, opt.t)) test_err = np.zeros((opt.trials, opt.misc.num_evaluations, opt.t)) robust_err = np.zeros((opt.trials, opt.misc.num_evaluations, opt.t)) freq = np.zeros((opt.trials, opt.misc.num_evaluations)) train_bar_errs = np.zeros((opt.trials, opt.t)) valid_bar_errs = np.zeros((opt.trials, opt.t)) test_bar_errs = np.zeros((opt.trials, opt.t)) print "Running Trials:\n\n" try: for t in range(opt.trials): start_time = timer.time() results = run_trial(opt) sup_rewards[t, :] = results['sup_reward'] lnr_rewards[t, :] = results['lnr_reward'] rob_rewards[t, :] = results['rob_reward'] train_err[t, :, :] = results['train_err'] valid_err[t, :, :] = results['valid_err'] test_err[t, :, :] = results['test_err'] robust_err[t, :, :] = results['robust_err'] freq[t, :] = results['correction_freq'] train_bar_errs[t], valid_bar_errs[t], test_bar_errs[t] = results[ 'bar_errs'] sup_rewards_save, lnr_rewards_save, rob_rewards_save = sup_rewards[: t + 1, :], lnr_rewards[: t + 1, :], rob_rewards[: t + 1, :] train_err_save, valid_err_save, test_err_save, robust_err_save = train_err[: t + 1, :, :], valid_err[: t + 1, :, :], test_err[: t + 1, :, :], robust_err[: t + 1, :, :] freq_save = freq[:t + 1, :] pd.DataFrame(sup_rewards_save).to_csv(opt.data_dir + 'sup_rewards.csv', index=False) pd.DataFrame(lnr_rewards_save).to_csv(opt.data_dir + 'lnr_rewards.csv', index=False) pd.DataFrame(rob_rewards_save).to_csv(opt.data_dir + 'rob_rewards.csv', index=False) for tau in range(opt.t): pd.DataFrame(train_err_save[:, :, tau]).to_csv( opt.data_dir + 'train_err_t' + str(tau) + '.csv', index=False) pd.DataFrame(valid_err_save[:, :, tau]).to_csv( opt.data_dir + 'valid_err_t' + str(tau) + '.csv', index=False) pd.DataFrame(test_err_save[:, :, tau]).to_csv( opt.data_dir + 'test_err_t' + str(tau) + '.csv', index=False) pd.DataFrame(robust_err_save[:, :, tau]).to_csv( opt.data_dir + 'robust_err_t' + str(tau) + '.csv', index=False) pd.DataFrame(freq_save).to_csv(opt.data_dir + 'freq.csv', index=False) train_err_avg = np.mean(train_err_save, axis=2) valid_err_avg = np.mean(valid_err_save, axis=2) test_err_avg = np.mean(test_err_save, axis=2) robust_err_avg = np.mean(robust_err_save, axis=2) utils.plot([sup_rewards_save, lnr_rewards_save, rob_rewards_save], ['Supervisor', 'Learner', 'Robust Learner'], opt, "Reward", colors=['red', 'blue', 'green']) utils.plot( [train_err_avg, valid_err_avg, test_err_avg, robust_err_avg], ['Training', 'Validation', 'Learner', 'Robust Learner'], opt, "Error", colors=['red', 'orange', 'blue', 'green']) utils.plot([freq_save], ['Frequency'], opt, 'Correction Frequency', colors=['green']) bar_errs = [ np.mean(train_bar_errs, axis=0), np.mean(valid_bar_errs, axis=0), np.mean(test_bar_errs, axis=0) ] labels = ['train', 'valid', 'test'] width = .2 index = np.arange(opt.t) for i, (err, label) in enumerate(zip(bar_errs, labels)): plt.bar(index + i * width, err, width, label=label) plt.legend() plt.ylim(0, .75) plt.savefig('/Users/JonathanLee/Desktop/bar_new_avg.png') utils.clear() end_time = timer.time() print "Trial time: " + str(end_time - start_time) except KeyboardInterrupt: pass
def main(): ap = argparse.ArgumentParser() ap.add_argument('--arch', required=True, nargs='+', type=int) ap.add_argument('--lr', required=False, type=int, default=.01) ap.add_argument('--epochs', required=False, type=int, default=100) ap.add_argument('--iters', required=True, type=int) ap.add_argument('--trials', required=True, type=int) ap.add_argument('--env', required=True) ap.add_argument('--t', required=True, type=int) opt = Options() opt.load_args(ap.parse_args()) opt.envname = opt.env opt.filename = '/Users/JonathanLee/experts/' + opt.envname + '.pkl' opt.env = gym.envs.make(opt.envname).env opt.sim = gym.envs.make(opt.envname).env opt.pi = load_policy.load_policy(opt.filename) opt.sess = tf.Session() opt.sup = NetSupervisor(opt.pi, opt.sess) plot_dir = utils.generate_plot_dir('initial', 'experts', vars(opt)) data_dir = utils.generate_data_dir('initial', 'experts', vars(opt)) if not os.path.exists(plot_dir): os.makedirs(plot_dir) if not os.path.exists(data_dir): os.makedirs(data_dir) opt.plot_dir = plot_dir opt.data_dir = data_dir sup_rewards = np.zeros((opt.trials, opt.iters)) lnr_rewards = np.zeros((opt.trials, opt.iters)) rob_rewards = np.zeros((opt.trials, opt.iters)) train_err = np.zeros((opt.trials, opt.iters)) valid_err = np.zeros((opt.trials, opt.iters)) test_err = np.zeros((opt.trials, opt.iters)) robust_err = np.zeros((opt.trials, opt.iters)) freq = np.zeros((opt.trials, opt.iters)) for t in range(opt.trials): results = run_trial(opt) sup_rewards[t, :] = results['sup_reward'] lnr_rewards[t, :] = results['lnr_reward'] rob_rewards[t, :] = results['rob_reward'] train_err[t, :] = results['train_err'] valid_err[t, :] = results['valid_err'] test_err[t, :] = results['test_err'] robust_err[t, :] = results['robust_err'] freq[t, :] = results['correction_freq'] pd.DataFrame(sup_rewards).to_csv(opt.data_dir + 'sup_rewards.csv', index=False) pd.DataFrame(lnr_rewards).to_csv(opt.data_dir + 'lnr_rewards.csv', index=False) pd.DataFrame(rob_rewards).to_csv(opt.data_dir + 'rob_rewards.csv', index=False) pd.DataFrame(train_err).to_csv(opt.data_dir + 'train_err.csv', index=False) pd.DataFrame(valid_err).to_csv(opt.data_dir + 'valid_err.csv', index=False) pd.DataFrame(test_err).to_csv(opt.data_dir + 'test_err.csv', index=False) pd.DataFrame(robust_err).to_csv(opt.data_dir + 'robust_err.csv', index=False) pd.DataFrame(freq).to_csv(opt.data_dir + 'freq.csv', index=False) utils.plot([sup_rewards, lnr_rewards, rob_rewards], ['Supervisor', 'Learner', 'Robust Learner'], opt, "Reward", colors=['red', 'blue', 'green']) utils.plot([train_err, valid_err, test_err, robust_err], ['Training', 'Validation', 'Learner', 'Robust Learner'], opt, "Error", colors=['red', 'orange', 'blue', 'green']) utils.plot([freq], ['Frequency'], opt, 'Correction Frequency', colors=['green'])