Beispiel #1
0
    def fit(self, features, labels):
        self.features, self.labels = torch.FloatTensor(
            features), torch.FloatTensor(labels)

        # construct data loader
        self.data_loader = DataLoader(TensorDataset(self.features,
                                                    self.labels),
                                      batch_size=self.batch_size,
                                      shuffle=True)

        # now train with pre-specified options
        result = self.train(model=self.model,
                            loader=self.data_loader,
                            optimizer=self.optimizer,
                            criterion=self.criterion,
                            lr_init=self.lr_init,
                            use_cuda=self.use_cuda,
                            epochs=self.epochs,
                            const_lr=self.const_lr)

        if self.criterion.noise_var is not None:
            # another forwards pass through network to estimate noise variance
            preds, targets = utils.predictions(model=self.model,
                                               test_loader=self.data_loader,
                                               regression=True,
                                               cuda=self.use_cuda)
            self.var = np.power(np.linalg.norm(preds - targets),
                                2.0) / targets.shape[0]
            print(self.var)

        if self.use_cuda:
            self.features = self.features.cuda()
        self.net_features = self.model(self.features, output_features=True)
        return result
Beispiel #2
0
    def fit(self, features, labels):
        self.features, self.labels = torch.FloatTensor(features), torch.FloatTensor(labels)

        # construct data loader
	# may want to turn shuffle = False for the very smallest datasets (e.g. uci small) 
        self.data_loader = DataLoader(TensorDataset(self.features, self.labels), batch_size = self.batch_size, shuffle = True)

        # now train with pre-specified options
        result = self.train(model=self.model, loader=self.data_loader, optimizer=self.optimizer, criterion=self.criterion, 
                lr_init=self.lr_init, swag_model=self.swag_model, swag=self.use_swag, swag_start=self.swag_start,
                swag_freq=self.swag_freq, swag_lr=self.swag_lr, use_cuda=self.use_cuda, epochs=self.epochs, const_lr=self.const_lr)

        if self.criterion.noise_var is not None:
            # another forwards pass through network to estimate noise variance
            preds, targets = utils.predictions(model=self.model, test_loader=self.data_loader, regression=True,cuda=self.use_cuda)
            self.var = np.power(np.linalg.norm(preds - targets), 2.0) / targets.shape[0]
            print(self.var)

        return result
Beispiel #3
0
        lr = args.lr_init
    
    if (args.swag and (epoch + 1) > args.swag_start) and args.cov_mat:
        train_res = utils.train_epoch(train_loader, model, criterion, optimizer, epoch)
    else:
        train_res = utils.train_epoch(train_loader, model, criterion, optimizer, epoch)

    if epoch == 0 or epoch % args.eval_freq == args.eval_freq - 1 or epoch == args.epochs - 1:
        test_res = utils.eval(val_loader, model, criterion)
        utils.mata_eval(model, meta_valloader, meta_testloader, 'BASELINE')

    else:
        test_res = {'loss': None, 'accuracy': None}

    if args.swag and (epoch + 1) > args.swag_start and (epoch + 1 - args.swag_start) % args.swag_c_epochs == 0:
        sgd_preds, sgd_targets = utils.predictions(val_loader, model)
        # sgd_res = utils.predict(val_loader, model)
        # sgd_preds = sgd_res["predictions"]
        # sgd_targets = sgd_res["targets"]
        # print("updating sgd_ens")
        if sgd_ens_preds is None:
            sgd_ens_preds = sgd_preds.copy()
        else:
            #TODO: rewrite in a numerically stable way
            sgd_ens_preds = sgd_ens_preds * n_ensembled / (n_ensembled + 1) + sgd_preds/ (n_ensembled + 1)
        n_ensembled += 1
        swag_model.collect_model(model)
        if epoch == 0 or epoch % args.eval_freq == args.eval_freq - 1 or epoch == args.epochs - 1:
            # swag_model.set_swa()
            swag_model.sample(0.0)
            utils.bn_update(train_loader, swag_model)