Beispiel #1
0
    def __call__(self , i_particle):

        rho = 1.e100
        prior_obj = Prior(self.prior_dict)

        while np.all(rho < self.eps_t)==False:
        
           theta_star = utils.weighted_sampling(self.theta_t, self.w_t)
           theta_starstar = np.random.multivariate_normal(theta_star, self.sig_t, 1)[0]
        
           while np.all((prior_range[:,0] < theta_starstar)&(theta_starstar < prior_range[:,1]))==False:

              theta_star = utils.weighted_sampling(self.theta_t, self.w_t)
              theta_starstar = np.random.multivariate_normal(theta_star, self.sig_t).rvs(size=1)
        
           model_starstar = self.model(theta_starstar)
           rho = self.distance(self.data, model_starstar)

        p_theta = prior_obj.pi_priors(theta_starstar)
        w_starstar = p_theta / np.sum(self.w_t * \
                                      utils.better_multinorm(theta_starstar,
                                                             self.theta_t,
                                                             self.sig_t) )

        pool_list = [np.int(i_particle)]
        theta_starstar = np.atleast_1d(theta_starstar)
        for i_param in xrange(self.n_params):
           pool_list.append(theta_starstar[i_param])
        pool_list.append(w_starstar)
        for r in rho:
            pool_list.append(r)

        return np.array(pool_list)
Beispiel #2
0
    def __call__(self, i_particle):
        
        rho = 1.e100
        prior_obj = Prior(self.prior_dict)
        while np.all(rho < self.eps_0)==False:

            theta_star = prior_obj.sampler()
            model_theta = self.model(theta_star)
            rho = self.distance(self.data, model_theta)

        pool_list = [np.int(i_particle)]
        for i_param in xrange(self.n_params):
             pool_list.append(theta_star[i_param])
        pool_list.append(1.)
        for r in rho:
            pool_list.append(r)
        
        return np.array(pool_list)
Beispiel #3
0
def _engine(ckpt_loc: str = 'ckpt/prior',
            db_train_loc: str = 'data-center/train.smi',
            db_test_loc: str = 'data-center/test.smi',
            num_workers: int = 1,
            batch_size: int = 128,
            batch_size_test: int = 256,
            device_id: int = 0,
            num_embeddings: int = 8,
            casual_hidden_sizes: t.Iterable = (16, 32),
            num_dense_bottlenec_feat: int = 48,
            num_k: int = 12,
            num_dense_layers: int = 10,
            num_dense_out_features: int = 64,
            num_nice_blocks: int = 24,
            nice_hidden_size: int = 128,
            activation: str = 'elu',
            lr: float = 1e-3,
            final_lr: float = 0.1,
            clip_grad: float = 3.0,
            num_iterations: int = int(1e4),
            summary_steps: int = 200,
            num_post_samples: int = 10,
            ):
    """Script to start model training

    Args:
        ckpt_loc (str, optional):
            Checkpoint location. Defaults to 'ckpt/prior'.
        db_train_loc (str, optional):
            The location of training dataset.
            Defaults to 'data-center/train.smi'.
        db_test_loc (str, optional):
            The location of test dataset. Defaults to 'data-center/test.smi'.
        num_workers (int, optional):
            The number of worker used for loading training set. Defaults to 1.
        batch_size (int, optional):
            The size of mini-batch for training set. Defaults to 128.
        batch_size_test (int, optional):
            The size of mini-batch for test set. Defaults to 256.
        device_id (int, optional):
            Which device should the model be put to. Defaults to 0.
        num_embeddings (int, optional):
            The embedding size for nodes. Defaults to 8.
        casual_hidden_sizes (t.Iterable, optional):
            The hidden sizes for casual layers. Defaults to (16, 32).
        num_dense_bottlenec_feat (int, optional):
            The size of bottlenec layer for densenet. Defaults to 48.
        num_k (int, optional):
            The growth rate. Defaults to 12.
        num_dense_layers (int, optional):
            The number of layers in densenet. Defaults to 10.
        num_dense_out_features (int, optional):
            The number of output features for densenet. Defaults to 64.
        num_nice_blocks (int, optional):
            The number of nice blocks. Defaults to 10.
        nice_hidden_size (int, optional):
            The size of hidden layers in each nice block. Defaults to 128.
        activation (str, optional):
            The type of activation used. Defaults to 'elu'.. Defaults to 'elu'.
        lr (float, optional):
            Initial learning rate for AdaBound. Defaults to 1e-3.
        final_lr (float, optional):
            The final learning rate AdaBound should converge to.
            Defaults to 0.1.
        clip_grad (float, optional):
            The scale of gradient clipping. Defaults to 3.0.
        num_iterations (int, optional):
            How many iterations should the training be performed.
            Defaults to 1e4.
        summary_steps (int, optional):
            Create summary for each `summary_steps` steps. Defaults to 200.
    """
    device = torch.device(f'cuda:{device_id}')
    # Get training and test set loaders
    train_loader, test_loader = \
        _get_loader(db_train_loc,
                    db_test_loc,
                    num_workers,
                    batch_size,
                    batch_size_test)
    train_iter, test_iter = iter(train_loader), iter(test_loader)
    # Load VAE
    model_vae = _load_vae(ckpt_loc)
    # Move model to GPU
    model_vae = model_vae.to(device).eval()
    # Disable gradient
    p: nn.Parameter
    for p in model_vae.parameters():
        p.requires_grad_(False)

    # Initialize model and optimizer
    model_prior = Prior(model_vae.num_c_feat,
                        num_embeddings,
                        casual_hidden_sizes,
                        num_dense_bottlenec_feat,
                        num_k,
                        num_dense_layers,
                        num_dense_out_features,
                        model_vae.num_z_feat,
                        num_nice_blocks,
                        nice_hidden_size,
                        activation)
    model_prior.to(device)
    # optim = AdaBound(model_prior.parameters(),
    #                  lr=lr,
    #                  final_lr=final_lr)
    optim = torch.optim.SGD(model_prior.parameters(), lr=final_lr * 0.1)

    with open(os.path.join(ckpt_loc, 'log.out'), 'w') as f:
        f.write('Global step\t'
                'Time\t'
                'Training loss\t'
                'Test loss\n')
        t0 = time.time()
        for step_id in range(num_iterations):
            train_loss = _train_step(optim,
                               train_iter,
                               train_loader,
                               model_prior,
                               model_vae,
                               clip_grad,
                               num_post_samples)
            print(train_loss)
            if step_id % summary_steps == 0:
                test_loss = _test_step(model_prior,
                                  model_vae,
                                  test_iter,
                                  test_loader,
                                  num_post_samples)
                f.write(f'{step_id}\t'
                        f'{float(time.time() - t0) / 60}\t'
                        f'{train_loss}\t'
                        f'{test_loss}\n')
                f.flush()
                _save(model_prior, ckpt_loc, optim)
        test_loss = _test_step(model_prior,
                          model_vae,
                          test_iter,
                          test_loader,
                          num_post_samples)
        f.write(f'{step_id}\t'
                f'{float(time.time() - t0) / 60}\t'
                f'{train_loss}\t'
                f'{test_loss}\n')
        f.flush()
        _save(model_prior, ckpt_loc, optim)