def roomTemperature():
	exp = Experiment()
	plotting = Plotter()
	plotting.initialize(filt = "median, movingAverage", skip = 500)

	dimension = 20

	exp.run_experiments_multiple(RoomTemperature(), {},\
						 [VAR(),\
						  ECVARMAOGD(),\
						  OnlineWaveFilteringParameterFree(),\
						  OnlineWaveFilteringParameterFree(),\
						  OnlineWaveFilteringParameterFree()
						  ],\
						 [{'p' : 8, 'dim': dimension},\
						  {'p' : 8, 'dim': dimension, 'lr': 1},\
						  {'max_k' : 10, 'action_dim': dimension, 'out_dim': dimension, 'opt': Hedge(), 'optForSubPredictors': FTRL()},\
						  {'max_k' : 30, 'action_dim': dimension, 'out_dim': dimension, 'opt': Hedge(), 'optForSubPredictors': FTRL()},\
						  {'max_k' : 50, 'action_dim': dimension, 'out_dim': dimension, 'opt': Hedge(), 'optForSubPredictors': FTRL()}
						  ],\
						 ['VAR_8',\
						  'ECVARMA_OGD16',\
						  'OnlineWaveFilteringParameterFree10',\
						  'OnlineWaveFilteringParameterFree30',\
						  'OnlineWaveFilteringParameterFree50'
						  ],\
						 n_runs = 20,\
						 plotter = plotting,\
						 verbose = True,
						 action_generator = ProblemBasedAction(RoomTemperature()))
Beispiel #2
0
def artif_exp_4():
    exp = Experiment()
    plotting = Plotter()
    plotting.initialize(yscale='log',
                        filt="median, movingAverage",
                        skip=100,
                        ylabel="Average Squared Error",
                        col=['c', 'r', 'g', 'orange', 'k'])

    T = 1000

    ag = RandomAction(mu=0, sigma=0.3)

    exp.run_experiments_multiple(Setting4(), {"timesteps" : T},\
          [OnlineWaveFilteringParameterFree(),\
           OnlineWaveFiltering(),\
           EMKalmanFilter(),\
           SSIDKalmanFilter(),\
           Consistency()],\
          [{'timesteps': T, 'max_k' : 30, 'action_dim': 1, 'out_dim': 1, 'opt': Hedge(), 'optForSubPredictors': FTRL()},\
           {'timesteps': T, 'k' : 10, 'lr': 1e-2, 'action_dim': 1, 'out_dim': 1, 'R_m': 3.0},\
           {'timesteps': T, 'order': 2, 'data': 100, 'iter': 500},\
           {'timesteps': T, 'order': 2, 'data': 100},\
           {'timesteps': T, 'out_dim': 1}],\
          ['OnlineWaveFilteringParameterFree',\
           'OnlineWaveFiltering',\
           'EM',\
           '4SID',\
           'Consistency'],\
          n_runs = 20,\
          plotter = plotting,\
          action_generator = ag,\
          verbose = True)
def sp500():
	exp = Experiment()
	plotting = Plotter()
	plotting.initialize(filt = "movingAverage", skip = 100)

	exp.run_experiments_multiple(SP500(), {},\
						 [ARMA(),\
						  ARIMA(),\
						  OnlineWaveFilteringParameterFree(),\
						  OnlineWaveFilteringParameterFree(),\
						  OnlineWaveFilteringParameterFree()
						  ],\
						 [{'p' : 64, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 16, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'max_k' : 20, 'action_dim': 1, 'out_dim': 1, 'opt': Hedge(), 'optForSubPredictors': FTRL()},\
						  {'max_k' : 50, 'action_dim': 1, 'out_dim': 1, 'opt': Hedge(), 'optForSubPredictors': FTRL()},\
						  {'max_k' : 100, 'action_dim': 1, 'out_dim': 1, 'opt': Hedge(), 'optForSubPredictors': FTRL()}
						  ],\
						 ['ARMA_OGD',\
						  'ARIMA_OGD',\
						  'OnlineWaveFilteringParameterFree20',\
						  'OnlineWaveFilteringParameterFree50',\
						  'OnlineWaveFilteringParameterFree100'
						  ],\
						 n_runs = 20,\
						 plotter = plotting,\
						 verbose = True,
						 action_generator = ProblemBasedAction(SP500()))
    def __init__(self, config: 'IdealGroundedConfig',
                 data: 'LabeledSentences'):
        super().__init__()
        self.device = config.device

        self.pretrained_embed = Experiment.load_txt_embedding(
            config.pretrained_embed_path, data.word_to_id)
        self.pretrained_embed.weight.requires_grad = False

        self.deno_space = Decomposer(
            preserve='deno',
            initial_space=self.pretrained_embed.weight,
            deno_probe=config.deno_probe,
            cono_probe=config.cono_probe,
            id_to_word=data.id_to_word,
            ground=data.ground,
            device=self.device)

        self.cono_space = Decomposer(
            preserve='cono',
            initial_space=self.pretrained_embed.weight,
            deno_probe=config.deno_probe,
            cono_probe=config.cono_probe,
            id_to_word=data.id_to_word,
            ground=data.ground,
            device=self.device)

        # Recomposer
        self.recomposer = nn.Linear(600, 300)
        self.rho = config.recomposer_rho
        self.to(self.device)

        self.word_to_id = data.word_to_id
        self.id_to_word = data.id_to_word
        self.ground = data.ground
Beispiel #5
0
def artif_exp_2():
    exp = Experiment()
    plotting = Plotter()
    plotting.initialize(yscale='log',
                        filt="median, movingAverage",
                        skip=100,
                        ylabel="Average Squared Error",
                        col=['c', 'r', 'g', 'orange', 'k'])

    A = np.array([[0.999, 0], [0, 0.5]])
    B = np.array([[1.0], [1.0]])
    C = np.array([[1.0, 1.0]])
    D = np.array([[0.0]])

    T = 1000

    ag = RandomAction(mu=0, sigma=0.3)

    exp.run_experiments_multiple(LDS(), {"timesteps" : T, 'action_dim': 1, 'hidden_dim': 2, 'out_dim': 1, 'partially_observable': True,\
            'system_params': {'A': A, 'B' : B, 'C': C, 'D': D, 'noise_distribution': 'normal'}},\
          [OnlineWaveFilteringParameterFree(),\
           OnlineWaveFiltering(),\
           EMKalmanFilter(),\
           SSIDKalmanFilter(),\
           Consistency()],\
          [{'timesteps': T, 'max_k' : 30, 'action_dim': 1, 'out_dim': 1, 'opt': Hedge(), 'optForSubPredictors': FTRL()},\
           {'timesteps': T, 'k' : 10, 'lr': 1e-3, 'action_dim': 1, 'out_dim': 1, 'R_m': 1.0},\
           {'timesteps': T, 'order': 2, 'data': 100, 'iter': 500},\
           {'timesteps': T, 'order': 2, 'data': 100},\
           {'timesteps': T, 'out_dim': 1}],\
          ['OnlineWaveFilteringParameterFree',\
           'OnlineWaveFiltering',\
           'EM',\
           '4SID',\
           'Consistency'],\
          n_runs = 20,\
          plotter = plotting,\
          action_generator = ag,\
          verbose = True)
Beispiel #6
0
def run():
    start = time.time()

    parser = argparse.ArgumentParser(description="run_file")
    parser.add_argument('--idx',
                        default=0,
                        type=int,
                        help='identifies run number and configuration')
    parser.add_argument('--config-file',
                        default='config_files/actor_critic.json')

    args = parser.parse_args()
    project_root = os.path.abspath(os.path.join(os.path.dirname(__file__)))
    sweeper = Sweeper(os.path.join(project_root, args.config_file))
    cfg = sweeper.parse(args.idx)

    cfg.env_instance = MountainCar(cfg)
    agent_class = getattr(agents.agent, cfg.agent_class)
    agent = agent_class(cfg)

    log_dir = cfg.get_logdir()
    ensure_dirs([log_dir])
    steps_log = os.path.join(log_dir, 'steps_log')
    steps_logger = setup_logger(steps_log, stdout=True)
    cfg.log_config(steps_logger)
    ep_log = os.path.join(log_dir, 'ep_log')
    ep_logger = setup_logger(ep_log, stdout=False)
    cfg.log_config(ep_logger)

    exp = Experiment(agent,
                     cfg.env_instance,
                     max_steps=cfg.max_steps,
                     seed=args.idx,
                     steps_log=steps_log,
                     ep_log=ep_log)
    exp.run_step_mode()

    print("Memory used: {:5} MB".format(memory_usage_psutil()))
    print("Time elapsed: {:5.2} minutes".format((time.time() - start) / 60))
Beispiel #7
0
def artif_exp_5():
    exp = Experiment()
    plotting = Plotter()
    plotting.initialize(yscale='log',
                        filt="median, movingAverage",
                        skip=100,
                        ylabel="Average Squared Error",
                        col=['c', 'r', 'g', 'k'])

    A = np.diag([0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
    B = np.eye(10)
    C = np.random.normal(size=(10, 10)) * 0.3
    D = np.zeros((10, 10))

    T = 1000

    ag = BlockAction(prob_repeat=0.8, sigma=0.3)

    exp.run_experiments_multiple(LDS(), {'action_dim': 10, 'hidden_dim': 10, 'out_dim': 10, 'partially_observable': True,\
            'system_params': {'A': A, 'B' : B, 'C': C, 'D': D}},\
          [OnlineWaveFilteringParameterFree(),\
           OnlineWaveFiltering(),\
           EMKalmanFilter(),\
           Consistency()],\
          [{'timesteps': T, 'max_k' : 30, 'action_dim': 10, 'out_dim': 10, 'opt': Hedge(), 'optForSubPredictors': FTRL()},\
           {'timesteps': T, 'k' : 30, 'lr': 1e-4, 'action_dim': 10, 'out_dim': 10, 'R_m': 5},\
           {'timesteps': T, 'order': 10, 'data': 100, 'iter': 500},\
           {'timesteps': T, 'out_dim': 10}],\
          ['OnlineWaveFilteringParameterFree',\
           'OnlineWaveFiltering',\
           'EM',\
           'Consistency'],\
          action_generator = ag,\
          n_runs = 20,\
          plotter = plotting,\
          verbose = True)
    def __init__(self, config: 'ProxyGroundedConfig',
                 data: 'LabeledDocuments'):
        super(Recomposer, self).__init__()
        self.device = config.device

        self.pretrained_embed = Experiment.load_txt_embedding(
            config.pretrained_embed_path, data.word_to_id)
        self.pretrained_embed.weight.requires_grad = False

        self.deno_space = ProxyGroundedDecomposer(
            preserve='deno',
            initial_space=self.pretrained_embed.weight,
            cono_probe=config.cono_probe,
            id_to_word=data.id_to_word,
            ground=data.ground,
            num_negative_samples=config.num_negative_samples,
            negative_sampling_probs=data.negative_sampling_probs,
            device=self.device)

        self.cono_space = ProxyGroundedDecomposer(
            preserve='cono',
            initial_space=self.pretrained_embed.weight,
            cono_probe=config.cono_probe,
            id_to_word=data.id_to_word,
            ground=data.ground,
            num_negative_samples=config.num_negative_samples,
            negative_sampling_probs=data.negative_sampling_probs,
            device=self.device)

        # Recomposer
        self.recomposer = nn.Linear(600, 300)
        self.rho = config.recomposer_rho
        self.to(self.device)

        self.word_to_id = data.word_to_id
        self.id_to_word = data.id_to_word
        self.ground = data.ground
        self.eval_deno = hasattr(config, "extra_grounding")
Beispiel #9
0
    if not os.path.isdir(log_dir):
        os.mkdir(log_dir)

    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(levelname)s - %(message)s',
                        handlers=[
                            logging.FileHandler(
                                os.path.join(
                                    log_dir,
                                    '%s-%d.log' % (args.name, time.time()))),
                            logging.StreamHandler()
                        ])
    logger = logging.getLogger()

    writer = MyWriter(hp, log_dir)

    assert hp.data.path != '', \
        'hp.data.path cannot be empty: please fill out your dataset\'s path in configuration yaml file.'

    if args.bucket:
        preload_dataset(args.bucket, hp.data.path, args.sample_threshold)

    trainloader = create_dataloader(hp, args, train=True)
    testloader = create_dataloader(hp, args, train=False)

    experiment = Experiment(api_key=args.comet_key, project_name='MelNet')
    experiment.log_parameters(hp)

    train(args, pt_dir, args.checkpoint_path, trainloader, testloader, writer,
          logger, hp, hp_str, experiment)
Beispiel #10
0
def sp500Tuning():
	exp = Experiment()
	plotting = Plotter()
	plotting.initialize(filt = "movingAverage", skip = 100)
	exp.run_experiments_multiple(SP500(), {},\
						 [ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ],\
						 [{'p' : 8, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 8, 'optimizer': RealOGD(hyperparameters={'lr':1.0})},\
						  {'p' : 8, 'optimizer': RealOGD(hyperparameters={'lr':0.1})},\
						  {'p' : 8, 'optimizer': RealOGD(hyperparameters={'lr':0.01})},\
						  {'p' : 8, 'optimizer': RealOGD(hyperparameters={'lr':0.001})},\
						  {'p' : 8, 'optimizer': RealOGD(hyperparameters={'lr':0.0001})},\
						  {'p' : 16, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 16, 'optimizer': RealOGD(hyperparameters={'lr':1.0})},\
						  {'p' : 16, 'optimizer': RealOGD(hyperparameters={'lr':0.1})},\
						  {'p' : 16, 'optimizer': RealOGD(hyperparameters={'lr':0.01})},\
						  {'p' : 16, 'optimizer': RealOGD(hyperparameters={'lr':0.001})},\
						  {'p' : 16, 'optimizer': RealOGD(hyperparameters={'lr':0.0001})},\
						  {'p' : 32, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 32, 'optimizer': RealOGD(hyperparameters={'lr':1.0})},\
						  {'p' : 32, 'optimizer': RealOGD(hyperparameters={'lr':0.1})},\
						  {'p' : 32, 'optimizer': RealOGD(hyperparameters={'lr':0.01})},\
						  {'p' : 32, 'optimizer': RealOGD(hyperparameters={'lr':0.001})},\
						  {'p' : 32, 'optimizer': RealOGD(hyperparameters={'lr':0.0001})},\
						  {'p' : 64, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 64, 'optimizer': RealOGD(hyperparameters={'lr':1.0})},\
						  {'p' : 64, 'optimizer': RealOGD(hyperparameters={'lr':0.1})},\
						  {'p' : 64, 'optimizer': RealOGD(hyperparameters={'lr':0.01})},\
						  {'p' : 64, 'optimizer': RealOGD(hyperparameters={'lr':0.001})},\
						  {'p' : 64, 'optimizer': RealOGD(hyperparameters={'lr':0.0001})},\
						  {'p' : 8, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 8, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':1.0})},\
						  {'p' : 8, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.1})},\
						  {'p' : 8, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.01})},\
						  {'p' : 8, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.001})},\
						  {'p' : 8, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.0001})},\
						  {'p' : 16, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 16, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':1.0})},\
						  {'p' : 16, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.1})},\
						  {'p' : 16, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.01})},\
						  {'p' : 16, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.001})},\
						  {'p' : 16, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.0001})},\
						  {'p' : 32, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 32, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':1.0})},\
						  {'p' : 32, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.1})},\
						  {'p' : 32, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.01})},\
						  {'p' : 32, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.001})},\
						  {'p' : 32, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.0001})},\
						  {'p' : 64, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 64, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':1.0})},\
						  {'p' : 64, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.1})},\
						  {'p' : 64, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.01})},\
						  {'p' : 64, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.001})},\
						  {'p' : 64, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.0001})}
						  ],\
						 ['ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD'
						  ],\
						 n_runs = 5,\
						 plotter = plotting,\
						 verbose = True,
						 action_generator = ProblemBasedAction(SP500()))
Beispiel #11
0
def roomTemperatureTuning():
	exp = Experiment()
	plotting = Plotter()
	plotting.initialize(filt = "median, movingAverage", skip = 0, printLast = True)

	dimension = 20

	exp.run_experiments_multiple(RoomTemperature(), {},\
						 [VAR(),\
						  VAR(),\
						  VAR(),\
						  VAR(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD()
						 ],\
						 [{'p' : 8, 'dim': dimension},\
						  {'p' : 16, 'dim': dimension},\
						  {'p' : 32, 'dim': dimension},\
						  {'p' : 64, 'dim': dimension},\
						  {'p' : 8, 'dim': dimension, 'lr': 1},\
						  {'p' : 16, 'dim': dimension, 'lr': 1},\
						  {'p' : 32, 'dim': dimension, 'lr': 1},\
						  {'p' : 64, 'dim': dimension, 'lr': 1},\
						  {'p' : 8, 'dim': dimension, 'lr': 0.1},\
						  {'p' : 16, 'dim': dimension, 'lr': 0.1},\
						  {'p' : 32, 'dim': dimension, 'lr': 0.1},\
						  {'p' : 64, 'dim': dimension, 'lr': 0.1},\
						  {'p' : 8, 'dim': dimension, 'lr': 0.01},\
						  {'p' : 16, 'dim': dimension, 'lr': 0.01},\
						  {'p' : 32, 'dim': dimension, 'lr': 0.01},\
						  {'p' : 64, 'dim': dimension, 'lr': 0.01},\
						  {'p' : 8, 'dim': dimension, 'lr': 0.001},\
						  {'p' : 16, 'dim': dimension, 'lr': 0.001},\
						  {'p' : 32, 'dim': dimension, 'lr': 0.001},\
						  {'p' : 64, 'dim': dimension, 'lr': 0.001}
						  ],\
						 ['VAR_8',\
						  'VAR_16',\
						  'VAR_32',\
						  'VAR_64',\
						  'ECVARMA_OGD8_1',\
						  'ECVARMA_OGD16_1',\
						  'ECVARMA_OGD32_1',\
						  'ECVARMA_OGD64_1',\
						  'ECVARMA_OGD8_01',\
						  'ECVARMA_OGD16_01',\
						  'ECVARMA_OGD32_01',\
						  'ECVARMA_OGD64_01',\
						  'ECVARMA_OGD8_001',\
						  'ECVARMA_OGD16_001',\
						  'ECVARMA_OGD32_001',\
						  'ECVARMA_OGD64_001',\
						  'ECVARMA_OGD8_0001',\
						  'ECVARMA_OGD16_0001',\
						  'ECVARMA_OGD32_0001',\
						  'ECVARMA_OGD64_0001'
						  ],\
						 n_runs = 5,\
						 plotter = plotting,\
						 verbose = True,
						 action_generator = ProblemBasedAction(RoomTemperature()))
def main():
    # set manual seed for random number generation
    if args.retrain:
        SEED = 2345
    else:
        SEED = 4325
    torch.manual_seed(SEED)
    if args.cuda:
        torch.cuda.manual_seed(SEED)

    np.random.seed(SEED)
    exper = Experiment(args, config)
    # Initialize EVERYTHING? i.e. if necessary load the validation functions
    val_funcs = exper.start()
    # print the argument flags
    print_flags(exper)

    if not exper.args.learner == 'manual':
        if exper.args.problem == "mlp":
            num_of_inputs = 3
        else:
            num_of_inputs = 1
        meta_optimizer = get_model(exper,
                                   num_of_inputs,
                                   retrain=exper.args.retrain)
        exper.optimizer = OPTIMIZER_DICT[exper.args.optimizer](
            meta_optimizer.parameters(), lr=exper.args.lr)
    else:
        # we're using one of the standard optimizers, initialized per function below
        meta_optimizer = None
        exper.optimizer = None

    batch_handler_class = None if exper.batch_handler_class is None else \
        getattr(utils.batch_handler, exper.batch_handler_class)

    if exper.args.learner == "meta" and exper.args.version == "V7":
        curriculum_schedule = load_curriculum("curriculum.dll")
    else:
        curriculum_schedule = None
    for epoch in range(exper.args.max_epoch):
        # use binary_switch to alternative between 1 and 2 layer MLPs. Not used for any other optimization problem!
        exper.epoch += 1
        batch_handler_class.id = 0
        exper.init_epoch_stats()
        epoch_obj = Epoch()
        epoch_obj.start(exper)
        exper.meta_logger.info("Epoch {}: Num of batches {}".format(
            exper.epoch, epoch_obj.num_of_batches))
        if exper.args.learner == "meta" and exper.args.version == "V7":
            global_curriculum = curriculum_schedule[exper.epoch - 1]

        for i in range(epoch_obj.num_of_batches):
            if exper.args.learner in ['meta', 'act']:
                optimizees = get_batch_functions(exper)
                if exper.args.learner == "meta" and exper.args.version == "V7":
                    exper.inc_learning_schedule[exper.epoch -
                                                1] = global_curriculum[i]
                execute_batch(exper,
                              optimizees,
                              meta_optimizer,
                              exper.optimizer,
                              epoch_obj,
                              final_batch=True if i +
                              1 == epoch_obj.num_of_batches else False)

            elif exper.args.learner[0:6] in [
                    'act_sb'
            ] or exper.args.learner == "meta_act":
                loss_sum = Variable(torch.DoubleTensor([0.]))
                kl_sum = 0.
                penalty_sum = 0.
                if exper.args.cuda:
                    loss_sum = loss_sum.cuda()
                for _ in np.arange(exper.args.samples_per_batch):
                    batch = batch_handler_class(exper, is_train=True)
                    batch_handler_class.id += 1
                    # final_batch parameter does nothing else than printing the accuracy for the last batch in the
                    # MLP experiment. Not used for regression_(T)
                    batch(exper,
                          epoch_obj,
                          meta_optimizer,
                          final_batch=True if i +
                          1 == epoch_obj.num_of_batches else False)
                    # only calculate batch loss if we're not using truncated BPTT. Otherwise this is done in batchHandler
                    if not exper.args.trunc_bptt:
                        batch.compute_batch_loss(epoch_obj.weight_regularizer)
                    loss_sum += batch.loss_sum
                    kl_sum += batch.kl_term
                    penalty_sum += batch.penalty_term

                loss_sum = loss_sum * 1. / float(exper.args.samples_per_batch)
                # only execute backward if we're not using truncated BPTT. Otherwise this is done in batchHandler
                if not exper.args.trunc_bptt:
                    act_loss, sum_grads = batch.backward(epoch_obj,
                                                         meta_optimizer,
                                                         exper.optimizer,
                                                         loss_sum=loss_sum)
                else:
                    act_loss = batch.total_opt_loss
                    sum_grads = batch.total_sum_grads
                    kl_sum = batch.total_kl_term

                epoch_obj.model_grads.append(sum_grads)
                epoch_obj.add_kl_term(
                    kl_sum * 1. / float(exper.args.samples_per_batch),
                    penalty_sum * 1. / float(exper.args.samples_per_batch))
                epoch_obj.add_act_loss(act_loss)
            else:
                raise ValueError(
                    "args.learner {} not supported by this implementation".
                    format(exper.args.learner))

        # END OF EPOCH, calculate average final loss/error and print summary
        # we computed the average loss per function in the batch! and added those losses to the final loss
        # therefore we only need to divide through the number of batches to end up with the average loss per function

        exper.scale_step_statistics()
        epoch_obj.end(exper)
        # check whether we need to adjust the learning rate
        if exper.args.lr_step_decay != 0 \
                and (epoch_obj.loss_optimizer <= exper.loss_threshold_lr_decay
                     or exper.lr_decay_last_epoch != 0):
            exper.check_lr_decay(meta_optimizer,
                                 epoch_obj.loss_optimizer,
                                 decay_type="lr_step_decay")
        # execute a checkpoint (save model) if necessary
        if exper.args.checkpoint_eval is not None and exper.epoch % exper.args.checkpoint_eval == 0:
            epoch_obj.execute_checkpoint(exper, meta_optimizer)

        # if applicable, VALIDATE model performance
        if exper.run_validation and (exper.epoch % exper.args.eval_freq == 0
                                     or epoch + 1 == exper.args.max_epoch):
            if exper.args.problem == "mlp":
                if epoch_obj.loss_optimizer < exper.config.loss_threshold_lr_decay:
                    exper.eval(epoch_obj,
                               meta_optimizer,
                               val_funcs,
                               save_model=True,
                               save_run=None)
            else:
                exper.eval(epoch_obj,
                           meta_optimizer,
                           val_funcs,
                           save_model=True,
                           save_run=None)
            # check if we need to lower learning rate, only when we didn't enable lr_step_decay already via args
            if exper.args.lr_step_decay == 0:
                # we start checking the learning rate if we already evaluated for at least 3 times
                exper.check_lr_decay(meta_optimizer,
                                     decay_type="compare_val_loss")
        # per epoch collect the statistics w.r.t q(t|x, T) distribution for training and validation
        if exper.args.learner == 'act':

            exper.epoch_stats['qt_hist'][exper.epoch] = meta_optimizer.qt_hist
            exper.epoch_stats['opt_step_hist'][
                exper.epoch] = meta_optimizer.opt_step_hist

            meta_optimizer.init_qt_statistics(exper.config)
        if hasattr(meta_optimizer, "epochs_trained"):
            meta_optimizer.epochs_trained += 1

    exper.end(meta_optimizer)
Beispiel #13
0
def create_experiment(config):
    """Creates an experiment based on config."""

    device = torch.device(config.device)
    logging.info("using {}".format(config.device))

    experiment = Experiment(config.name, config.save_dir)
    experiment.register_config(config)

    logger = None
    if config.use_tflogger:
        logger = Logger(config.tflog_dir)
        experiment.register_logger(logger)

    torch.manual_seed(config.rseed)

    model = NRU(device,
                config.input_size,
                config.output_size,
                num_layers=config.num_layers,
                layer_size=config.layer_size,
                output_activation="linear",
                layer_norm=config.layer_norm,
                use_relu=config.use_relu,
                memory_size=config.memory_size,
                k=config.k).to(device)
    experiment.register_model(model)

    data_iterator = get_data_iterator(config)
    experiment.register_data_iterator(data_iterator)

    optimizer = get_optimizer(model.parameters(), config)
    model.register_optimizer(optimizer)

    tr = MyContainer()
    tr.updates_done = 0
    tr.epochs_done = 0
    tr.ce = {}
    tr.ce["train"] = []
    tr.accuracy = {}
    tr.accuracy["valid"] = []
    tr.accuracy["test"] = []
    tr.grad_norm = []

    experiment.register_train_statistics(tr)

    return experiment, model, data_iterator, tr, logger, device
Beispiel #14
0
    parser.add_argument(
        '--qual_experiment_dir',
        help='The directory where the quality experiment is saved.',
        dest='qual_experiment_dir',
        default='parameters')
    parser.add_argument('--qual_experiment_name',
                        help='The name of the experiment.',
                        dest='qual_experiment_name',
                        default='wsqual')

    args = parser.parse_args()

    ###########################################################################
    #                                Quality                                  #
    ###########################################################################
    qual_experiment = Experiment(args.qual_experiment_dir,
                                 args.qual_experiment_name)

    qual_model_path = os.path.join(qual_experiment._full_dir(), 'model.hdf5')
    qual_hype = qual_experiment.hyperparams

    global wsqual
    wsqual = models_qual.MyModel(qual_hype['layer'],
                                 pooling=qual_hype['pooling'],
                                 architecture=qual_hype['architecture'],
                                 n_features=qual_hype['n_features'],
                                 pretrain=qual_hype['pretrain'])
    wsqual.model.load_weights(qual_model_path)
    wsqual.architecture = qual_hype['architecture']
    wsqual.layer = qual_hype['layer']

    ###########################################################################
Beispiel #15
0
import matplotlib.pyplot as plt
import logging
from utils.arg_extract import get_args
from utils.experiment_runner import ExperimentRunner
from utils.experiment import Experiment
# from utils.experiment_evaluator import evaluate_experiment

plt.ioff()
logging.basicConfig(format='%(message)s', level=logging.INFO)

args = get_args()
experiment = Experiment(args)

if args.continue_experiment:
    experiment.load_from_disk(test=False)
else:
    experiment.create_new()

experiment_runner = ExperimentRunner(experiment)
experiment_runner.run_experiment()

# No need cause it timeouts
# evaluate_experiment(experiment, args)
import logging
import matplotlib.pyplot as plt
from utils.arg_extract import get_args
from utils.experiment_evaluator import evaluate_experiment
from utils.experiment import Experiment

plt.ioff()
logging.basicConfig(format='%(message)s', level=logging.INFO)

args_new = get_args()
experiment = Experiment(args_new)
experiment.load_from_disk(test=True)

evaluate_experiment(experiment, args_new)