Пример #1
0
    def test_time_as_feature_equals_true(self):
        torch.manual_seed(0)
        batchsize = 2
        ts_dim = 1
        sequencelength = 5
        model = ConvShapeletModel(n_shapelets_per_size={
            10: 50,
            20: 50
        },
                                  ts_dim=ts_dim,
                                  n_classes=2,
                                  use_time_as_feature=True)
        x = torch.zeros([batchsize, ts_dim, sequencelength])
        if torch.cuda.is_available():
            x = x.cuda()
            model = model.cuda()

        logits, deltas, pts, budget = model._logits(x=x)

        logits_sum_reference = np.array(-1.0345266, dtype=np.float32)
        self.assertAlmostEqual(logits.sum().cpu().detach().numpy(),
                               logits_sum_reference,
                               places=4)

        pts_sum_reference = np.array(2., dtype=np.float32)
        self.assertAlmostEqual(pts.sum().cpu().detach().numpy(),
                               pts_sum_reference,
                               places=4)
Пример #2
0
    def test_Trainer_Conv1D_TwoPatterns(self):
        cleanup()

        try:
            traindataset = UCRDataset("TwoPatterns",
                                      partition="train",
                                      ratio=.75,
                                      randomstate=0,
                                      augment_data_noise=.1)
            validdataset = UCRDataset("TwoPatterns",
                                      partition="valid",
                                      ratio=.75,
                                      randomstate=0)
            nclasses = traindataset.nclasses
            traindataloader = torch.utils.data.DataLoader(traindataset,
                                                          batch_size=8,
                                                          shuffle=True,
                                                          num_workers=0,
                                                          pin_memory=True)

            validdataloader = torch.utils.data.DataLoader(validdataset,
                                                          batch_size=8,
                                                          shuffle=False,
                                                          num_workers=0,
                                                          pin_memory=True)

            model = ConvShapeletModel(num_layers=3,
                                      hidden_dims=50,
                                      ts_dim=1,
                                      n_classes=nclasses)

            if torch.cuda.is_available():
                model = model.cuda()

            config = dict(epochs=2,
                          learning_rate=1e-3,
                          earliness_factor=.75,
                          visdomenv="unittest",
                          switch_epoch=1,
                          loss_mode="loss_cross_entropy",
                          show_n_samples=0,
                          store="/tmp")

            trainer = Trainer(model, traindataloader, validdataloader,
                              **config)
            trainer.fit()

        except Exception as e:
            self.fail(logging.exception(e))

        self.assertEqual(trainer.get_phase(), EARLINESS_PHASE_NAME)

        # should have written two model files
        self.assertTrue(
            os.path.exists(
                "/tmp/model_{}.pth".format(CLASSIFICATION_PHASE_NAME)))
        self.assertTrue(
            os.path.exists("/tmp/model_{}.pth".format(EARLINESS_PHASE_NAME)))
Пример #3
0
class RayTrainerConv1D(ray.tune.Trainable):
    def _setup(self, config):

        traindataset = UCRDataset(config["dataset"],
                                  partition="train",
                                  ratio=.8,
                                  randomstate=config["fold"],
                                  silent=True,
                                  augment_data_noise=0)

        validdataset = UCRDataset(config["dataset"],
                                  partition="valid",
                                  ratio=.8,
                                  randomstate=config["fold"],
                                  silent=True)

        self.epochs = config["epochs"]

        nclasses = traindataset.nclasses

        # handles multitxhreaded batching andconfig shuffling
        self.traindataloader = torch.utils.data.DataLoader(
            traindataset,
            batch_size=config["batchsize"],
            shuffle=True,
            num_workers=config["workers"],
            pin_memory=False)
        self.validdataloader = torch.utils.data.DataLoader(
            validdataset,
            batch_size=config["batchsize"],
            shuffle=False,
            num_workers=config["workers"],
            pin_memory=False)

        self.model = ConvShapeletModel(
            num_layers=config["num_layers"],
            hidden_dims=config["hidden_dims"],
            ts_dim=1,
            n_classes=nclasses,
            use_time_as_feature=True,
            drop_probability=config["drop_probability"],
            scaleshapeletsize=False,
            shapelet_width_increment=config["shapelet_width_increment"])

        if torch.cuda.is_available():
            self.model = self.model.cuda()

        self.trainer = Trainer(self.model, self.traindataloader,
                               self.validdataloader, **config)

    def _train(self):
        # epoch is used to distinguish training phases. epoch=None will default to (first) cross entropy phase

        # train epochs and then infer once. to avoid overhead on these small datasets
        for i in range(self.epochs):
            self.trainer.train_epoch(epoch=None)

        return self.trainer.test_epoch(dataloader=self.validdataloader)

    def _save(self, path):
        path = path + ".pth"
        torch.save(self.model.state_dict(), path)
        return path

    def _restore(self, path):
        state_dict = torch.load(path, map_location="cpu")
        self.model.load_state_dict(state_dict)