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)
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)))
def test_save_load(self): model = ConvShapeletModel() try: model.save("/tmp/model.pth", testarg=1, secondtestarg=dict(a=1, b="c")) snapshot = model.load("/tmp/model.pth") except Exception as e: self.fail(logging.error(e)) # test if custom kwargs are saved and loaded as intended... self.assertEqual(snapshot["testarg"], 1) self.assertEqual(snapshot["secondtestarg"]["a"], 1) self.assertEqual(snapshot["secondtestarg"]["b"], "c")
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 getModel(args): # Get Model if args.model == "DualOutputRNN": model = DualOutputRNN(input_dim=args.input_dims, nclasses=args.nclasses, hidden_dims=args.hidden_dims, num_rnn_layers=args.num_layers, dropout=args.dropout, init_late=True) elif args.model == "WaveNet": model = WaveNetModel(layers=5, blocks=4, dilation_channels=32, residual_channels=32, skip_channels=256, end_channels=args.nclasses, classes=args.nclasses, output_length=1, kernel_size=2, dtype=torch.FloatTensor, input_dims=args.input_dims, bias=False) elif args.model == "Conv1D": model = ConvShapeletModel( num_layers=args.num_layers, hidden_dims=args.hidden_dims, ts_dim=args.input_dims, n_classes=args.nclasses, use_time_as_feature=False, seqlength=args.seqlength, scaleshapeletsize=args.shapelet_width_in_percent, drop_probability=args.dropout, shapelet_width_increment=args.shapelet_width_increment) else: raise ValueError( "Invalid Model, Please insert either 'DualOutputRNN', or 'Conv1D'") if torch.cuda.is_available(): model = model.cuda() return model
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)
def prepare_model_and_optimizer(args, input_dims, sequence_length, num_classes): if args.model == "DualOutputRNN": hparams = pd.read_csv(config.DUALOUTPUTRNN_HYPERPARAMETER_CSV, index_col=0) if args.dataset not in hparams.index: raise ValueError( f"No dataset {args.dataset} found in {config.DUALOUTPUTRNN_HYPERPARAMETER_CSV}" ) hparam = hparams.loc[args.dataset] model = DualOutputRNN(input_dim=int(input_dims), nclasses=num_classes, hidden_dims=int(hparam.hidden_dims), num_rnn_layers=int(hparam.num_layers), dropout=hparam.dropout, init_late=True) optimizer = torch.optim.Adam(model.parameters(), lr=hparam.learning_rate, weight_decay=hparam.weight_decay) #optimizer = ScheduledOptim(optimizer, d_model=model.d_model, n_warmup_steps=500) return model, optimizer elif args.model == "WaveNet": model = WaveNetModel(layers=5, blocks=4, dilation_channels=32, residual_channels=32, skip_channels=256, end_channels=args.nclasses, classes=args.nclasses, output_length=1, kernel_size=2, dtype=torch.FloatTensor, input_dims=args.input_dims, bias=False) elif args.model == "Conv1D": hparams = pd.read_csv(config.CONV1D_HYPERPARAMETER_CSV, index_col=0) if args.dataset not in hparams.index: raise ValueError( f"No dataset {args.dataset} found in {config.CONV1D_HYPERPARAMETER_CSV}" ) hparam = hparams.loc[args.dataset] model = ConvShapeletModel(num_layers=int(hparam.num_layers), hidden_dims=int(hparam.hidden_dims), ts_dim=int(input_dims), n_classes=int(num_classes), drop_probability=hparam.dropout, scaleshapeletsize=False, seqlength=int(sequence_length), shapelet_width_increment=int( hparam.shapelet_width_increment)) optimizer = torch.optim.Adam(model.parameters(), lr=hparam.learning_rate, weight_decay=hparam.weight_decay) return model, optimizer else: raise ValueError( "Invalid Model, Please insert either 'DualOutputRNN', or 'Conv1D'")
def test_defaults_constructor_arguments(self): try: ConvShapeletModel() except Exception as e: self.fail(logging.error(e))