def main(self, seed=10):

        np.random.seed(seed)
        torch.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)

        x1_sp = np.arange(-5, 5, 0.1)
        x2_sp = np.arange(-5, 5, 0.1)

        self.input_vectors = [x1_sp, x2_sp]

        assert x1_sp.shape == x2_sp.shape, ValueError(
            'the cardinality of dimensions cannot be '
            'different')

        D = self.dim
        self.model: nn.Module = MADE(D,
                                     self.hiddens,
                                     D * x1_sp.shape[-1],
                                     natural_ordering=True,
                                     seed=seed)
        self.model.to(self.device)

        self.opt = optim.Adam(self.model.parameters(),
                              lr=self.lr,
                              weight_decay=0)

        self.buffer = BufferNumpy(mode=self.mode,
                                  goal=self.goal,
                                  cut_off=self.cut_off)

        self.train(self.n_init_samples, x1_sp, x2_sp, iter_cnt=0)
        xdata, _, probs = self.sample_model(2000, x1_sp, x2_sp)

        self.plt_hist2D(xdata.to(self.cpu).data.numpy())
        name = self.get_full_name('dist', prefix='training', suffix='0_after')
        plt.savefig(f'search_figs/{name}.png')
        plt.close()

        # pdb.set_trace()
        for iter_cnt in range(self.niter):
            self.train(self.nsamples, x1_sp, x2_sp, iter_cnt=iter_cnt + 1)
            self.model.eval()
            xdata, _, _ = self.sample_model(2000, x1_sp, x2_sp)
            self.plt_hist2D(xdata.to(self.cpu).data.numpy())
            name = self.get_full_name('dist',
                                      prefix='training',
                                      suffix=f'{iter_cnt+1}_after')
            plt.savefig(f'search_figs/{name}.png')
            plt.close()

            self.plt_hist2D(self.buffer.data_arr)
            name = self.get_full_name('buffer_dist',
                                      prefix='training',
                                      suffix=f'{iter_cnt+1}_after')
            plt.savefig(f'search_figs/{name}.png')
            plt.close()
Example #2
0
    def main(self, seed=10):
        np.random.seed(seed)
        torch.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)

        data = sample_data(self.nsample)
        fval = ackley(data[:, 0, :])
        weights = weight(fval, self.goal, 4, mode='le')
        xtr, xte, wtr, wte = split_data(data, label=weights)

        D = self.dim
        self.model: nn.Module = MADE(D, self.hiddens, D * 100, seed=seed)
        self.model.to(self.device)
        self.opt = torch.optim.Adam(self.model.parameters(),
                                    self.lr,
                                    weight_decay=0)
        B = self.bsize
        N, D, _ = xtr.shape
        # per epoch
        tr_nll, te_nll = [], []
        for epoch_id in range(self.nepoch):
            nstep = N // B
            # per batch
            tr_nll_per_b, te_nll_per_b = 0, 0
            for step in range(nstep):
                self.model.train()
                xb = xtr[step * B:step * B + B]
                wb = wtr[step * B:step * B + B]
                xb_tens = torch.from_numpy(xb).to(self.device)
                wb_tens = torch.from_numpy(wb).to(self.device)

                xin = xb_tens[:, 0, :]
                xin_ind = xb_tens[:, 1, :].long()
                loss = self.get_nll(xin, xin_ind, weights=wb_tens)
                self.opt.zero_grad()
                loss.backward()
                self.opt.step()

                # print(loss)
                # for name, param in self.model.named_parameters():
                #     print(f'{name} = {param.grad}')
                # import pdb
                # pdb.set_trace()

                tr_nll_per_b += loss.to(self.cpu).item() / nstep

            self.model.eval()
            xte_tens = torch.from_numpy(xte).to(self.device)
            wte_tens = torch.from_numpy(wte).to(self.device)
            xin_te = xte_tens[:, 0, :]
            xin_ind_te = xte_tens[:, 1, :].long()
            te_loss = self.get_nll(xin_te, xin_ind_te, weights=wte_tens)
            te_nll.append(te_loss)

            print(f'epoch = {epoch_id}, tr_nll = {tr_nll_per_b}')
            print(f'epoch = {epoch_id}, te_nll = {te_loss}')
            tr_nll.append(tr_nll_per_b)

        self.plot_learning(tr_nll, te_nll)

        x1 = np.linspace(start=-5, stop=5, num=100)
        x2 = np.linspace(start=-5, stop=5, num=100)
        samples, _ = self.sample_model(self.nsample, x1, x2)
        samples = samples.to(self.cpu).data.numpy()
        plot_data(samples,
                  scatter_loc='figs/test_model3_scatter.png',
                  hist_loc='figs/test_model3_hist2D.png')
    def main(self, seed=10):
        np.random.seed(seed)
        torch.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)

        data, weights = sample_data(self.nsample)
        plot_data(data[:, 0, :], label=weights)
        xtr, xte, wtr, wte = split_data(data, label=weights)

        D = self.dim
        self.model: nn.Module = MADE(D, self.hiddens, D * 100, seed=seed)
        self.model.to(self.device)
        self.opt = torch.optim.Adam(self.model.parameters(),
                                    self.lr,
                                    weight_decay=0)
        B = self.bsize
        N, D, _ = xtr.shape
        # per epoch
        tr_nll, te_nll = [], []
        for epoch_id in range(self.nepoch):
            nstep = N // B
            # per batch
            tr_nll_per_b, te_nll_per_b = 0, 0
            for step in range(nstep):
                self.model.train()
                xb = xtr[step * B:step * B + B]
                wb = wtr[step * B:step * B + B]
                xb_tens = torch.from_numpy(xb).to(self.device)
                wb_tens = torch.from_numpy(wb).to(self.device)

                xin = xb_tens[:, 0, :]
                xin_ind = xb_tens[:, 1, :].long()
                loss = self.get_nll(xin, xin_ind, weights=wb_tens, debug=False)
                self.opt.zero_grad()
                loss.backward()
                self.opt.step()

                tr_nll_per_b += loss.to(self.cpu).item() / nstep

            self.model.eval()
            xte_tens = torch.from_numpy(xte).to(self.device)
            wte_tens = torch.from_numpy(wte).to(self.device)
            xin_te = xte_tens[:, 0, :]
            xin_ind_te = xte_tens[:, 1, :].long()
            te_loss = self.get_nll(xin_te, xin_ind_te, weights=wte_tens)
            te_nll.append(te_loss)

            print(f'epoch = {epoch_id}, tr_nll = {tr_nll_per_b}')
            print(f'epoch = {epoch_id}, te_nll = {te_loss}')
            tr_nll.append(tr_nll_per_b)

        #     x1 = np.linspace(start=-5, stop=5, num=100)
        #     x2 = np.linspace(start=-5, stop=5, num=100)
        #     samples, _ = self.sample_model(self.nsample, x1, x2)
        #     samples = samples.to(self.cpu).data.numpy()
        #
        #     ax = plt.subplot(5, 5, epoch_id + 1)
        #     plot_data(samples, scatter_loc='figs/test_model_4_scatter.png',
        #               hist_loc='figs/test_model_4_hist2D.png', ax=ax)
        #     plt.tight_layout()
        # plt.savefig('figs/test_model_4_hist2D.png')

        self.plot_learning(tr_nll, te_nll)
        # pdb.set_trace()

        x1 = np.linspace(start=-5, stop=5, num=100)
        x2 = np.linspace(start=-5, stop=5, num=100)
        samples, _ = self.sample_model(10000, x1, x2)
        samples = samples.to(self.cpu).data.numpy()
        plot_data(samples,
                  scatter_loc='figs/test_model_4_scatter.png',
                  hist_loc='figs/test_model_4_hist2D.png')
    def main(self, seed=10):

        np.random.seed(seed)
        torch.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)

        D = self.dim
        self.model: nn.Module = MADE(D,
                                     self.hiddens,
                                     D * 3 * self.nr_mix,
                                     seed=seed,
                                     natural_ordering=True)
        self.model.to(self.device)

        self.opt = optim.Adam(self.model.parameters(),
                              lr=self.lr,
                              weight_decay=0)

        self.buffer = CacheBuffer(self.mode, self.goal, self.cut_off)

        # collect samples using the random initial model (probably a bad initialization)
        self.model.eval()
        _, xdata_ind = self.sample_model(200)
        self.plt_hist2D(xdata_ind.to(self.cpu).data.numpy().astype('int'))
        name = self.get_full_name('dist', suffix='init')
        plt.savefig(self.dir / f'{name}.png')
        plt.close()

        self.collect_samples(self.n_init_samples, uniform=True)
        # train the init model
        self.model.train()
        self.train(0, self.init_nepochs)
        # visualize empirical distribution after training
        self.model.eval()
        _, xdata_ind = self.sample_model(200)

        self.plt_hist2D(xdata_ind.to(self.cpu).data.numpy().astype('int'))
        name = self.get_full_name('dist', prefix='training', suffix='0_after')
        plt.savefig(self.dir / f'{name}.png')
        plt.close()

        tr_losses = []
        for iter_cnt in range(self.niter):
            self.collect_samples(self.nsamples)
            if iter_cnt == self.niter - 1 and self.full_training:
                tr_loss, tr_loss_list = self.train(iter_cnt + 1, 1000)
            else:
                tr_loss, tr_loss_list = self.train(iter_cnt + 1, self.nepochs)

            tr_losses.append(tr_loss_list)

            if (iter_cnt + 1) % 10 == 0:
                _, xdata_ind = self.sample_model(200)
                self.plt_hist2D(
                    xdata_ind.to(self.cpu).data.numpy().astype('int'))
                name = self.get_full_name('dist',
                                          prefix='training',
                                          suffix=f'{iter_cnt+1}_after')
                plt.savefig(self.dir / f'{name}.png')
                plt.close()

        self.plot_learning_with_epochs(training=tr_losses)
Example #5
0
    def main(self, seed=10):
        np.random.seed(seed)
        torch.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)

        data, delta, weights = self.sample_data(self.nsample)
        self.plot_data(data[:, 1, :].astype('int'))
        xtr, xte, wtr, wte = split_data(data, label=weights)

        D = self.dim
        self.model: nn.Module = MADE(D,
                                     self.hiddens,
                                     D * 3 * self.nr_mix,
                                     seed=seed)
        self.model.to(self.device)
        self.opt = torch.optim.Adam(self.model.parameters(),
                                    self.lr,
                                    weight_decay=0)
        self.lr_sch = StepLR(self.opt, step_size=50, gamma=0.9)
        B = self.bsize
        N, D, _ = xtr.shape
        # per epoch
        tr_nll, te_nll = [], []
        plt.figure(figsize=(15, 8))
        for epoch_id in range(self.nepoch):
            nstep = N // B
            # per batch
            tr_nll_per_b, te_nll_per_b = 0, 0
            for step in range(nstep):
                self.model.train()
                xb = xtr[step * B:step * B + B]
                wb = wtr[step * B:step * B + B]

                xb_tens = torch.from_numpy(xb).to(self.device)
                wb_tens = torch.from_numpy(wb).to(self.device)

                xin = xb_tens[:, 0, :]
                loss = self.get_nll(xin, delta, weights=wb_tens, debug=False)
                self.opt.zero_grad()
                loss.backward()
                self.opt.step()
                self.lr_sch.step(epoch_id)

                tr_nll_per_b += loss.to(self.cpu).item() / nstep

            self.model.eval()
            xte_tens = torch.from_numpy(xte).to(self.device)
            wte_tens = torch.from_numpy(wte).to(self.device)
            xin_te = xte_tens[:, 0, :]
            te_loss = self.get_nll(xin_te,
                                   delta,
                                   weights=wte_tens,
                                   debug=False)
            te_nll.append(te_loss)

            print(f'epoch = {epoch_id}, tr_nll = {tr_nll_per_b}')
            print(f'epoch = {epoch_id}, te_nll = {te_loss}')
            tr_nll.append(tr_nll_per_b)

        #     if (epoch_id + 1) % 20 == 0 and epoch_id <= 100:
        #         _, samples_ind = self.sample_model(1, delta)
        #         samples_ind = samples_ind.to(self.cpu).data.numpy().astype('int')
        #
        #         ax = plt.subplot(1, 5, epoch_id // 20 + 1, adjustable='box', aspect=1)
        #         self.plot_data(samples_ind, scatter_loc='figs/test_model_6_sub_scatter.png',
        #                        hist_loc='figs/test_model_6_sub_hist2D.png', ax=ax)
        #         plt.tight_layout()
        # plt.savefig('figs/test_model_6_sub_hist2D.png')

        self.plot_learning(tr_nll, te_nll)
        # pdb.set_trace()

        samples, samples_ind = self.sample_model(1000, delta)
        samples_ind = samples_ind.to(self.cpu).data.numpy().astype('int')
        self.plot_data(samples_ind,
                       scatter_loc='figs/test_model_6_scatter.png',
                       hist_loc='figs/test_model_6_hist2D.png')