def main(): # Get the arguments args = parse_arguments() # Get the training parameters class_size = args.class_size if args.class_size is not None else DEFAULT_CLASS_SIZE # Get the train and dev feature files train_files = args.train_files dev_files = args.dev_files # Create the list of models to train models = [] if args.svm: models.append(svm.SVM()) if args.mlp: models.append(mlp.MLP(10)) # If there no development files, perform cross-validation if dev_files == None: train_data, train_labels = utils.read_features(train_files) models = train_cross_validation(train_data, train_labels, models, class_size) # Otherwise use the development files else: train_data, train_labels = utils.read_features(train_files) dev_data, dev_labels = utils.read_features(dev_files) train(train_data, train_labels, dev_data, dev_labels)
def __init__(self, args): self.generator = resunet.ResUNet(args.in_ch, args.out_ch, args.base_kernel, args.is_leaky) self.discriminator = mlp.MLP(args.in_ch + args.out_ch, args.patch_layers, args.base_kernel) # Initialize weights self.generator.apply(weights_init_normal) self.discriminator.apply(weights_init_normal)
def main(): # Read the arguments args = parse_arguments() # Get the class size class_size = args.class_size if args.class_size is not None else DEFAULT_CLASS_SIZE # Read the features from the test files test_files = args.test_files # Ensure at least 1 test file is passed in if test_files is None: print 'Error. Please provide testing feature files' exit(1) test_data, test_labels = utils.read_features(test_files) test_data, test_labels, map = utils.partition(test_data, test_labels, class_size) # Read and load the model if args.svm: model = svm.SVM() model.load(args.model) if args.mlp: model = mlp.MLP(10) model.load(args.model) # Ensure a model was created if model is None: print 'Error. Model invalid' exit(1) # Test the model predictions = model.predict(test_data) accuracy = 1.0 * sum([ 1 for label, predict in zip(test_labels, predictions) if label == predict ]) / len(predictions) # Output results print 'Accuracy is: ', accuracy
if __name__ == '__main__': env_name = 'CartPole-v0' environment = Environment( environment=gym.make(env_name), agent=None, verbose=True, max_steps=200, capacity=500, # representation_method='one_hot_encoding', representation_method='observation', ) model_mlp = mlps.MLP(input_dimension=environment.get_input_dimension(), hidden_dimension=100, n_hidden_layers=1, n_actions=environment.n_actions, dropout=0.) model = model_mlp agent = agents.DQNAgent(model=model, optimiser=eligibility.EligibilitySGD( model.parameters(), lr=1e-3, gamma=0.99, lambd=0.9), gamma=.99, temperature=3., algorithm='qlearning', n_actions=environment.n_actions, terminal_state=environment.max_obs,
def load_model(model, dataset, actfun, k, p, g, num_params, perm_method, device, resnet_ver, resnet_width, verbose): model_params = [] if dataset == 'mnist' or dataset == 'fashion_mnist': input_channels, input_dim, output_dim = 1, 28, 10 elif dataset == 'cifar10' or dataset == 'svhn': input_channels, input_dim, output_dim = 3, 32, 10 elif dataset == 'cifar100': input_channels, input_dim, output_dim = 3, 32, 100 if model == 'nn' or model == 'mlp': if dataset == 'mnist' or dataset == 'fashion_mnist': input_dim = 784 elif dataset == 'cifar10' or dataset == 'svhn': input_dim = 3072 elif dataset == 'cifar100': input_dim = 3072 elif dataset == 'iris': input_dim, output_dim = 4, 3 model = mlp.MLP(actfun=actfun, input_dim=input_dim, output_dim=output_dim, k=k, p=p, g=g, num_params=num_params, permute_type=perm_method).to(device) model_params.append({'params': model.batch_norms.parameters(), 'weight_decay': 0}) model_params.append({'params': model.linear_layers.parameters()}) if actfun == 'combinact': model_params.append({'params': model.all_alpha_primes.parameters(), 'weight_decay': 0}) elif model == 'cnn': model = cnn.CNN(actfun=actfun, num_input_channels=input_channels, input_dim=input_dim, num_outputs=output_dim, k=k, p=p, g=g, num_params=num_params, permute_type=perm_method).to(device) model_params.append({'params': model.conv_layers.parameters()}) model_params.append({'params': model.pooling.parameters()}) model_params.append({'params': model.batch_norms.parameters(), 'weight_decay': 0}) model_params.append({'params': model.linear_layers.parameters()}) if actfun == 'combinact': model_params.append({'params': model.all_alpha_primes.parameters(), 'weight_decay': 0}) elif model == 'resnet': model = preact_resnet.PreActResNet(resnet_ver=resnet_ver, actfun=actfun, in_channels=input_channels, out_channels=output_dim, k=k, p=p, g=g, permute_type=perm_method, width=resnet_width, verbose=verbose).to(device) model_params = model.parameters() return model, model_params
def __do_train(device, train_samples, dev_samples, dev_true_labels_dict, test_samples, test_true_labels_dict, type_vocab_train, type_vocab_test, type_infer_train, type_infer_test, use_vr, use_hr, n_labelers, n_mlp_layers, mlp_hdim, dropout, margin, learning_rate, batch_size, n_iter, lr_gamma): mlp_input_dim = 2 if use_vr: mlp_input_dim += 2 if use_hr: mlp_input_dim += 1 model = mlp.MLP(n_mlp_layers, mlp_input_dim, n_labelers, mlp_hdim, dropout=dropout) model.to(device) param_optimizer = list(model.named_parameters()) no_decay = ['bias', 'lin1_bn', 'lin2_bn'] optimizer_grouped_parameters = [ {'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.1}, {'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0} ] loss_obj = exputils.BinMaxMarginLoss(pos_margin=margin, neg_margin=margin) # optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) optimizer = torch.optim.Adam(optimizer_grouped_parameters, lr=learning_rate) n_batches = (len(train_samples) + batch_size - 1) // batch_size if len(train_samples) == batch_size * (n_batches - 1) + 1: n_batches -= 1 scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=n_batches * 10, gamma=lr_gamma) n_steps = n_batches * n_iter losses = list() n_types_train = len(type_vocab_train) best_dev_loss, best_dev_acc = 1e5, 0 best_test_acc, best_test_maf1, best_test_mif1 = 0, 0, 0 for step in range(n_steps): bidx = step % n_batches bbeg, bend = bidx * batch_size, min((bidx + 1) * batch_size, len(train_samples)) batch = train_samples[bbeg:bend] cur_batch_size = bend - bbeg (pred_logits_tensor, max_logits_tensor, true_label_vecs ) = __get_batch_input(device, n_types_train, batch, use_vr, use_hr) feats = max_logits_tensor model.train() ens_logits = model(feats) final_logits = ens_labeler_logits(ens_logits, pred_logits_tensor, cur_batch_size) # print(ens_logits) loss = loss_obj.loss(true_label_vecs, final_logits) optimizer.zero_grad() loss.backward() optimizer.step() scheduler.step() losses.append(loss.data.cpu().numpy()) if (step + 1) % n_batches == 0: acc, loss_dev = 0, 0 if dev_samples is not None: acc, maf1, mif1, _, loss_dev, avg_weights = __eval_ens( device, loss_obj, type_vocab_train, model, type_infer_train, use_vr, use_hr, dev_samples, dev_true_labels_dict) best_tag = '*' if acc > best_dev_acc or (acc == best_dev_acc and loss_dev < best_dev_loss) else '' # print((step + 1) // n_batches, sum(losses), loss_dev, acc, maf1, mif1, avg_weights, best_tag) print('{} {:.4f} {:.4f} {:.4f} {:.4f} {:.4f} {} {}'.format( (step + 1) // n_batches, sum(losses), loss_dev, acc, maf1, mif1, avg_weights, best_tag)) if acc > best_dev_acc or (acc == best_dev_acc and loss_dev < best_dev_loss): acct, maf1t, mif1t, result_objs, _, _ = __eval_ens( device, None, type_vocab_test, model, type_infer_test, use_vr, use_hr, test_samples, test_true_labels_dict) print('TEST {} {} ACC={:.4f} {:.4f} {:.4f}'.format( (step + 1) // n_batches, sum(losses), acct, maf1t, mif1t)) best_test_acc, best_test_maf1, best_test_mif1 = acct, maf1t, mif1t best_dev_acc = acc best_dev_loss = loss_dev losses = list() # random.shuffle(train_samples) print('test acc={:.4f} maf1={:.4f} mif1={:.4f}'.format(best_test_acc, best_test_maf1, best_test_mif1)) return best_test_acc, best_test_maf1, best_test_mif1
test_set = AIRBNB(path='../data/', data_set='test_' + dataset_name + '.csv') test_loader = DataLoader(test_set, batch_size=len(test_set), shuffle=False, num_workers=0, pin_memory=True) n_features = test_set.get_n_features() # ########## Net Init ########## print('Network initialization...') device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') print('Running on ' + device.type) model = mlp.MLP(n_features, n_hidden_units=args['hidden'], activation_function=args['activation']) # MultiGPU # if torch.cuda.device_count() > 1: # print('We are using', torch.cuda.device_count(), 'GPUs') # model = nn.DataParallell(model) model = model.to(device) # ########## TRAIN VAL LOOP ########## if train_mode: print('Trainer initialization...') trainer = Trainer(device, model, train_loader, val_loader, args) trainer.train_model() # ########## TEST ##########