def setup_models(io, do_load, cuda, trainer=True): models = dict() if trainer: name = "training" else: name = "best" if do_load: models["emb"] = load_model("emb_{}".format(name), io) models["readout"] = load_model("readout_{}".format(name), io) models["policy"] = load_model("policy_{}".format(name), io) models["backup"] = load_model("backup_{}".format(name), io) else: models["emb"] = None models["readout"] = None models["policy"] = None models["backup"] = None for model_name, model in models.items(): if model is None: models[model_name] = create_net(model_name) train_mode(models) if cuda: activate_cuda(models) return models
def train(datasetType, dataset_path, modelType, dataLoaderType, trainingType, max_epochs, max_epochs_without_improvement, learning_rate, momentum, batch_size, device, printProgress=False): # Load Data path = dataset_path + datasetType.name + ".hdf5" trainLoader, testLoader, classes = datasets.create_dataloaders( dataLoaderType, path, batch_size, device, dataset=datasetType) # Create net and move to correct device in_channels = 3 if datasetType is datasets.DataSet.CIFAR10 else 1 num_classes = 10 net = models.create_net(modelType, in_channels, num_classes) net.to(device) # Training criterion = torch.nn.CrossEntropyLoss() optimizer = torch.optim.SGD(net.parameters(), lr=learning_rate, momentum=momentum) trainObj = training.create_training(trainingType, net, criterion, optimizer, trainLoader, testLoader, max_epochs, max_epochs_without_improvement, printProgress) trainObj.train() return trainObj
cosine_annealing = DictConfig([cosine_annealing,cosine_annealing_ops]) plateau = NamedConfig(("scheduler",optim.lr_scheduler.ReduceLROnPlateau)) ca_T_max = NamedConfig(("patience",5)) plateau_ops = NamedConfig(("scheduler_params",DictConfig([ca_T_max]))) plateau = DictConfig([plateau,plateau_ops]) schedulers = ExclusiveConfigs([cosine_annealing,plateau]) evaluator_obj = Evaluator("./models/mnist_classifier",inference_fn=debug_fn) training_modes = NamedConfig(("modes",[1])) max_epochs = NamedConfig(("max_epochs",40)) training_objectives = NamedConfig((1,SupervisedMetricList([[nn.CrossEntropyLoss()]],[[1.0]]))) validation_objectives = NamedConfig((0,SupervisedMetricList([[Accuracy()]],[[1.0]]))) objective_fns = NamedConfig(("objective_fns",DictConfig([training_objectives,validation_objectives]))) constants = DictConfig([objective_fns,training_modes,max_epochs]) network = NamedConfig(("network",create_net(CustomNetClassification))) growth_factor = NamedConfig(("growth_factor",ExclusiveConfigs([10]))) input_dim = NamedConfig(("input_dim",28)) final_dim = NamedConfig(("final_conv_dim",8)) initial_channels = NamedConfig(("initial_channels",1)) num_classes = NamedConfig(("num_classes",10)) conv_module = NamedConfig(("conv_module",ExclusiveConfigs([DoubleConvLayer,InceptionLayer]))) network_params = NamedConfig(("network_params",DictConfig([growth_factor,input_dim,final_dim,initial_channels,num_classes,conv_module]))) train_loader_options = NamedConfig((1,{"shuffle":True,"batch_size":1000,"num_workers":4})) val_loader_options = NamedConfig((0,{"batch_size":1000,"num_workers":4})) loader_options = NamedConfig(("loader_options",DictConfig( [train_loader_options,val_loader_options]))) network_loader_set1 = DictConfig([network_params,loader_options])
schedulers = ExclusiveConfigs([cosine_annealing, plateau]) evaluator_obj = Evaluator("./models/mnist_classifier", inference_fn=debug_fn) training_modes = NamedConfig(("modes", [1])) max_epochs = NamedConfig(("max_epochs", 1)) training_objectives = NamedConfig( (1, SupervisedMetricList([[nn.CrossEntropyLoss()]], [[1.0]]))) validation_objectives = NamedConfig( (0, SupervisedMetricList([[nn.CrossEntropyLoss()]], [[1.0]]))) objective_fns = NamedConfig( ("objective_fns", DictConfig([training_objectives, validation_objectives]))) constants = DictConfig([objective_fns, training_modes, max_epochs]) network = NamedConfig(("network", create_net(PreTrainedClassifier))) num_classes = NamedConfig(("num_classes", 120)) model_class = NamedConfig(("model_class", DensenetModels)) model = NamedConfig(("model", models.densenet121)) network_params = NamedConfig( ("network_params", DictConfig([model, model_class, num_classes]))) train_loader_options = NamedConfig((1, { "shuffle": True, "batch_size": 20, "num_workers": 4 })) val_loader_options = NamedConfig((0, {"batch_size": 20, "num_workers": 4})) loader_options = NamedConfig( ("loader_options",