def test_Trainer_TwoPatterns(self): 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 = DualOutputRNN(input_dim=1, nclasses=nclasses, hidden_dims=20, num_rnn_layers=1, dropout=.2) 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="twophase_linear_loss", show_n_samples=0, store="/tmp", overwrite=True, ) trainer = Trainer(model, traindataloader, validdataloader, **config) trainer.fit() except Exception as e: self.fail(logging.exception(e))
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 RayTrainerDualOutputRNN(ray.tune.Trainable): def _setup(self, config): if config["dataset"] == "BavarianCrops": region = "HOLL_2018_MT_pilot" root = "/home/marc/data/BavarianCrops" nsamples = None traindataset = BavarianCropsDataset(root=root, region=region, partition="train", nsamples=nsamples) validdataset = BavarianCropsDataset(root=root, region=region, partition="valid", nsamples=nsamples) else: traindataset = UCRDataset(config["dataset"], partition="train", ratio=.8, randomstate=config["fold"], silent=False, augment_data_noise=0) validdataset = UCRDataset(config["dataset"], partition="valid", ratio=.8, randomstate=config["fold"], silent=False) nclasses = traindataset.nclasses self.epochs = config["epochs"] # 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 = DualOutputRNN(input_dim=traindataset.ndims, nclasses=nclasses, hidden_dims=config["hidden_dims"], num_rnn_layers=config["num_layers"]) 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 five 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 eval(dataset, batchsize, workers, num_rnn_layers, dropout, hidden_dims, store="/tmp", epochs=30, switch_epoch=30, learning_rate=1e-3, visdomenv="run", earliness_factor=.75, show_n_samples=1, modelname="DualOutputRNN", loss_mode=None, load_weights=None, entropy_factor=0): if dataset == "synthetic": traindataset = SyntheticDataset(num_samples=2000, T=100) validdataset = SyntheticDataset(num_samples=1000, T=100) else: traindataset = UCRDataset(dataset, partition="trainvalid") validdataset = UCRDataset(dataset, partition="test") nclasses = traindataset.nclasses np.random.seed(0) torch.random.manual_seed(0) traindataloader = torch.utils.data.DataLoader(traindataset, batch_size=batchsize, shuffle=True, num_workers=workers, pin_memory=True) np.random.seed(1) torch.random.manual_seed(1) validdataloader = torch.utils.data.DataLoader(validdataset, batch_size=batchsize, shuffle=False, num_workers=workers, pin_memory=True) if modelname == "DualOutputRNN": model = DualOutputRNN(input_dim=1, nclasses=nclasses, hidden_dim=hidden_dims, num_rnn_layers=num_rnn_layers, dropout=dropout) else: raise ValueError( "Invalid Model, Please insert either 'DualOutputRNN' or 'AttentionRNN'" ) if load_weights is not None: model.load(load_weights) # parse epoch 29 from filename e.g., 'models/TwoPatterns/run/model_29.pth' start_epoch = int( os.path.basename(load_weights).split("_")[-1].split(".")[0]) else: start_epoch = 0 if torch.cuda.is_available(): model = model.cuda() #if run is None: # visdomenv = "{}_{}_{}".format(args.experiment, dataset,args.loss_mode.replace("_","-")) # storepath = store #else: # visdomenv = run #storepath = os.path.join(store, dataset) if switch_epoch is None: switch_epoch = int(epochs / 2) config = dict(epochs=epochs, learning_rate=learning_rate, earliness_factor=earliness_factor, visdomenv=visdomenv, switch_epoch=switch_epoch, loss_mode=loss_mode, show_n_samples=show_n_samples, store=store, entropy_factor=entropy_factor) trainer = Trainer(model, traindataloader, validdataloader, config=config) logged_data = trainer.fit(start_epoch=start_epoch) return logged_data