Esempio n. 1
0
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 = net.Network([64, 64], opt.lr, opt.epochs)
    opt.pi.load_weights('meta/Pusher3-v0/weights_1.0-0.1-0.5_5.0.txt',
                        'meta/Pusher3-v0/stats_1.0-0.1-0.5_5.0.txt')
    opt.sess = None
    opt.sup = Supervisor(opt.pi)
    opt.misc = Options()
    opt.misc.num_evaluations = 1

    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=float, 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)

    ap.add_argument('--weights',
                    required=True,
                    nargs='+',
                    type=float,
                    default=[1.0, .1, .5])
    ap.add_argument('--ufact', required=True, default=4.0, type=float)
    ap.add_argument('--id', required=True, default=4.0, type=int)

    ap.add_argument('--nu', required=True, type=float)
    ap.add_argument('--gamma', required=True, type=float)

    opt = Options()
    args = ap.parse_args()
    opt.load_args(args)
    args = vars(args)

    opt.envname = opt.env
    opt.env = gym.envs.make(opt.envname).env
    opt.sim = gym.envs.make(opt.envname).env
    exp_id = args['id']
    opt.env.my_weights = args['weights']
    opt.env.ufact = args['ufact']
    opt.pi = net.Network([64, 64], .01, 300)
    suffix = '_' + utils.stringify(args['weights']) + '_' + str(args['ufact'])
    weights_path = 'meta/' + 'test' + '/' + opt.envname + '_' + str(
        exp_id) + '_weights' + suffix + '.txt'
    stats_path = 'meta/' + 'test' + '/' + opt.envname + '_' + str(
        exp_id) + '_stats' + suffix + '.txt'
    opt.pi.load_weights(weights_path, stats_path)
    opt.sup = Supervisor(opt.pi)
    opt.misc = Options()
    opt.misc.num_evaluations = 1

    opt.misc.samples = 5
    rec_results = {}
    lnr_results = {}

    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(data_dir + '/opt'):
        os.makedirs(data_dir + '/opt')
    if not os.path.exists(plot_dir + '/opt'):
        os.makedirs(plot_dir + '/opt')

    opt.data_dir = data_dir
    opt.plot_dir = plot_dir

    opt.env.reset()
    opt.env.render()

    trials_data = run_trial(opt)
def main():
    ap = argparse.ArgumentParser()

    ap.add_argument('--arch', required=True, nargs='+', type=int)
    ap.add_argument('--lr', required=False, type=float, 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)

    ap.add_argument('--weights',
                    required=True,
                    nargs='+',
                    type=float,
                    default=[1.0, .1, .5])
    ap.add_argument('--ufact', required=True, default=4.0, type=float)
    ap.add_argument('--id', required=True, default=4.0, type=int)

    ap.add_argument('--nu', required=True, type=float)
    ap.add_argument('--gamma', required=True, type=float)

    opt = Options()
    args = ap.parse_args()
    opt.load_args(args)
    args = vars(args)

    opt.envname = opt.env
    opt.env = gym.envs.make(opt.envname).env
    opt.sim = gym.envs.make(opt.envname).env
    exp_id = args['id']
    opt.env.my_weights = args['weights']
    opt.env.ufact = args['ufact']
    opt.pi = net.Network([64, 64], .01, 300)
    suffix = '_' + utils.stringify(args['weights']) + '_' + str(args['ufact'])
    weights_path = 'meta/' + 'test' + '/' + opt.envname + '_' + str(
        exp_id) + '_weights' + suffix + '.txt'
    stats_path = 'meta/' + 'test' + '/' + opt.envname + '_' + str(
        exp_id) + '_stats' + suffix + '.txt'
    opt.pi.load_weights(weights_path, stats_path)
    opt.sup = Supervisor(opt.pi)
    opt.misc = Options()
    opt.misc.num_evaluations = 1

    opt.misc.samples = 200
    rec_results = {}
    lnr_results = {}

    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')

    try:
        for t in range(opt.trials):
            start_time = timer.time()
            results = run_trial(opt)

            for key in results['rec'].keys():
                if key in rec_results:
                    rec_results[key].append(results['rec'][key])
                else:
                    rec_results[key] = [results['rec'][key]]

            for key in results['lnr'].keys():
                if key in lnr_results:
                    lnr_results[key].append(results['lnr'][key])
                else:
                    lnr_results[key] = [results['lnr'][key]]

    except KeyboardInterrupt:
        pass

    labels = sorted(list(rec_results.keys()))
    for key in rec_results.keys():
        rec_results[key] = np.array(rec_results[key]) / float(opt.misc.samples)
        lnr_results[key] = np.array(lnr_results[key]) / float(opt.misc.samples)

    rec_means = [
        np.mean(rec_results[key]) for key in sorted(rec_results.keys())
    ]
    rec_sems = [
        scipy.stats.sem(rec_results[key]) for key in sorted(rec_results.keys())
    ]
    lnr_means = [
        np.mean(lnr_results[key]) for key in sorted(lnr_results.keys())
    ]
    lnr_sems = [
        scipy.stats.sem(lnr_results[key]) for key in sorted(lnr_results.keys())
    ]
def main():
    ap = argparse.ArgumentParser()

    ap.add_argument('--arch', required=True, nargs='+', type=int)
    ap.add_argument('--lr', required=False, type=float, 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)

    ap.add_argument('--weights', required=True, nargs='+', type=float, default=[1.0, .1, .5])
    ap.add_argument('--ufact', required=True, default=4.0, type=float)
    ap.add_argument('--id', required=True, default=4.0, type=int)

    ap.add_argument('--nu', required=True, type=float)
    ap.add_argument('--gamma', required=True, type=float)

    opt = Options()
    args = ap.parse_args()
    opt.load_args(args)
    args = vars(args)

    opt.envname = opt.env
    opt.env = gym.envs.make(opt.envname).env
    opt.sim = gym.envs.make(opt.envname).env
    exp_id = args['id']
    opt.env.my_weights = args['weights']
    opt.env.ufact = args['ufact']
    opt.pi = net.Network([64, 64], .01, 300)
    suffix = '_' + utils.stringify(args['weights']) + '_' + str(args['ufact'])
    weights_path = 'meta/' + 'test' + '/' + opt.envname + '_' + str(exp_id) + '_weights' + suffix + '.txt'
    stats_path = 'meta/' + 'test' + '/' + opt.envname + '_' + str(exp_id) + '_stats' + suffix + '.txt'
    opt.pi.load_weights(weights_path, stats_path) 
    opt.sup = Supervisor(opt.pi)
    opt.misc = Options()
    opt.misc.num_evaluations = 1


    opt.misc.samples = 30
    rec_results = {}
    lnr_results = {}

    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')

    trials_data = []
    try:
        for t in range(opt.trials):
            start_time = timer.time()
            results = run_trial(opt)

            trials_data.append(results)
            pickle.dump(trials_data, open('./results/trials_data.pkl', 'w'))
    except KeyboardInterrupt:
        pass
Esempio n. 5
0
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)

    ap.add_argument('--weights',
                    required=True,
                    nargs='+',
                    type=float,
                    default=[1.0, .1, .5])
    ap.add_argument('--ufact', required=True, default=4.0, type=float)
    ap.add_argument('--id', required=True, default=4.0, type=int)

    opt = Options()
    args = ap.parse_args()
    opt.load_args(args)
    args = vars(args)

    opt.envname = opt.env
    opt.env = gym.envs.make(opt.envname).env
    opt.sim = gym.envs.make(opt.envname).env
    exp_id = args['id']
    opt.env.my_weights = args['weights']
    opt.env.ufact = args['ufact']
    opt.pi = net.Network([64, 64], .01, 300)
    suffix = '_' + utils.stringify(args['weights']) + '_' + str(args['ufact'])
    weights_path = 'meta/' + 'test' + '/' + opt.envname + '_' + str(
        exp_id) + '_weights' + suffix + '.txt'
    stats_path = 'meta/' + 'test' + '/' + opt.envname + '_' + str(
        exp_id) + '_stats' + suffix + '.txt'
    opt.pi.load_weights(weights_path, stats_path)
    opt.sup = Supervisor(opt.pi)
    opt.misc = Options()
    opt.misc.num_evaluations = 1

    opt.misc.samples = 500
    rec_results = {}
    lnr_results = {}

    try:
        for t in range(opt.trials):
            start_time = timer.time()
            results = run_trial(opt)

            for key in results['rec'].keys():
                if key in rec_results:
                    rec_results[key].append(results['rec'][key])
                else:
                    rec_results[key] = [results['rec'][key]]

            for key in results['lnr'].keys():
                if key in lnr_results:
                    lnr_results[key].append(results['lnr'][key])
                else:
                    lnr_results[key] = [results['lnr'][key]]

    except KeyboardInterrupt:
        pass

    labels = sorted(list(rec_results.keys()))
    for key in rec_results.keys():
        rec_results[key] = np.array(rec_results[key]) / float(opt.misc.samples)
        lnr_results[key] = np.array(lnr_results[key]) / float(opt.misc.samples)

    rec_means = [
        np.mean(rec_results[key]) for key in sorted(rec_results.keys())
    ]
    rec_sems = [
        scipy.stats.sem(rec_results[key]) for key in sorted(rec_results.keys())
    ]
    lnr_means = [
        np.mean(lnr_results[key]) for key in sorted(lnr_results.keys())
    ]
    lnr_sems = [
        scipy.stats.sem(lnr_results[key]) for key in sorted(lnr_results.keys())
    ]

    plt.style.use('ggplot')
    width = .4
    index = np.arange(len(rec_means))

    plt.bar(index + 0 * width,
            rec_means,
            width,
            label='recovery',
            yerr=rec_sems)
    plt.bar(index + 1 * width,
            lnr_means,
            width,
            label='no recovery',
            yerr=lnr_sems)
    plt.legend()
    plt.xticks(index, labels)
    plt.ylim(0, 1)
    plt.show()
Esempio n. 6
0
def main():
    ap = argparse.ArgumentParser()

    ap.add_argument('--arch', required=True, nargs='+', type=int)
    ap.add_argument('--lr', required=False, type=float, 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)

    ap.add_argument('--weights',
                    required=True,
                    nargs='+',
                    type=float,
                    default=[1.0, .1, .5])
    ap.add_argument('--ufact', required=True, default=4.0, type=float)
    ap.add_argument('--id', required=True, default=4.0, type=int)

    ap.add_argument('--nu', required=True, type=float)
    ap.add_argument('--gamma', required=True, type=float)

    opt = Options()
    args = ap.parse_args()
    opt.load_args(args)
    args = vars(args)

    opt.envname = opt.env
    opt.env = gym.envs.make(opt.envname).env
    opt.sim = gym.envs.make(opt.envname).env
    exp_id = args['id']
    opt.env.my_weights = args['weights']
    opt.env.ufact = args['ufact']
    opt.pi = net.Network([64, 64], .01, opt.epochs)
    suffix = '_' + utils.stringify(args['weights']) + '_' + str(args['ufact'])
    weights_path = 'meta/' + 'test' + '/' + opt.envname + '_' + str(
        exp_id) + '_weights' + suffix + '.txt'
    stats_path = 'meta/' + 'test' + '/' + opt.envname + '_' + str(
        exp_id) + '_stats' + suffix + '.txt'
    opt.pi.load_weights(weights_path, stats_path)
    opt.sup = Supervisor(opt.pi)
    opt.misc = Options()
    opt.misc.num_evaluations = 1

    opt.misc.samples = 1
    rec_results = {}
    lnr_results = {}

    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(data_dir + '/multiple_trials'):
        os.makedirs(data_dir + '/multiple_trials')
    if not os.path.exists(plot_dir + '/multiple_trials'):
        os.makedirs(plot_dir + '/multiple_trials')

    opt.data_dir = data_dir
    opt.plot_dir = plot_dir

    trials_data = []
    rec_scores = []
    rec_cutoffs = []
    rec_mo_scores = []
    rec_mo_cutoffs = []
    for t in range(opt.trials):
        print "\n\nTrial: " + str(t) + "\n\n"
        opt.t_value = t
        trial_data, info = run_trial(opt)

        trials_data.append(trial_data)
        rec_scores += info['rec_scores']
        rec_cutoffs += info['rec_cutoffs']
        rec_mo_scores += info['rec_mo_scores']
        rec_mo_cutoffs += info['rec_mo_cutoffs']

        print "Saving to: " + str(opt.data_dir +
                                  'multiple_trials/trials_data.pkl')
        pickle.dump(
            trials_data,
            open(opt.data_dir + 'multiple_trials/trials_data.pkl', 'w'))
        pickle.dump(rec_scores,
                    open(opt.data_dir + 'multiple_trials/rec_scores.pkl', 'w'))
        pickle.dump(
            rec_cutoffs,
            open(opt.data_dir + 'multiple_trials/rec_cutoffs.pkl', 'w'))
        pickle.dump(
            rec_mo_scores,
            open(opt.data_dir + 'multiple_trials/rec_mo_scores.pkl', 'w'))
        pickle.dump(
            rec_mo_cutoffs,
            open(opt.data_dir + 'multiple_trials/rec_mo_cutoffs.pkl', 'w'))