def main(): parser = argparse.ArgumentParser() #parser.add_argument('--name', type=str, default='name') #parser.add_argument('--figure_dir', type=str, default='') parser.add_argument('--n_bootstraps', type=int, default=100) parser.add_argument('--weakness', type=float, default=.8) parser.add_argument('--perm', type=str, choices=['original', 'permuted'], default='original') args = parser.parse_args() print(sys.argv) print(args) train, test = pickle.load(open('../pickle_files/mice_data.pickle', 'rb')) X_train, y_train = train n_bootstraps = args.n_bootstraps weakness = args.weakness n, p = X_train.shape X_train_perm = X_train.copy() y_train_perm = y_train.copy() if args.perm == 'permuted': np.random.shuffle(y_train_perm) m = int(np.floor(n / 2.)) figure_dir = str(datetime.datetime.now()).replace(' ', ',') + '_' + str( uuid.uuid4()) if not os.path.exists(figure_dir): os.makedirs(figure_dir) for _ in range(n_bootstraps): start = time.time() X_train_perm, y_train_perm = unison_shuffled_copies( X_train_perm, y_train_perm) weights = 1. - (1. - weakness) * np.random.randint(2, size=p) lassonet_trainer(args.perm + '_' + str(uuid.uuid4()), (weights * X_train_perm[:m], y_train_perm[:m]), test, utils={'figure_dir': figure_dir}) end = time.time() print('elapsed time:', end - start)
return (X[:X.shape[0] * 4 // 5], Y[:X.shape[0] * 4 // 5]), (X[X.shape[0] * 4 // 5:], Y[X.shape[0] * 4 // 5:]) name = 'mice' train, test = load_mice() # LassoNet natively convers the data into PyTorch Tensors, and is GPU-compatible. If the machine has a GPU, it will be used for training. Otherwise, it defaults to CPU. # # LassoNet uses a training loop typical of PyTorch. We begin by training an initial fully dense, and use it as warm start over the entire regularization path. The entire training loop takes less than 5 min on a Tesla K80 GPU. # In[4]: lassonet_results = lassonet_trainer(name, train, test) # The classification accuracy is plotted below: # In[10]: nselected = lassonet_results['nselected'] accuracy = lassonet_results['test_accuracy'] import matplotlib.pyplot as plt fig = plt.figure(figsize=(9, 6)) plt.grid(True) plt.plot(nselected, accuracy, 'o-') plt.xlabel("number of selected features") plt.ylabel("classification accuracy") plt.title("Classification accuracy")
train, test = load_fashion() print("Using {} dataset".format(name)) utils = { 'M': 30, 'LR': 1e-3, 'hidden_dims': [500], 'n_epochs': 3000, 'criterion': 'CrossEntropy', 'patience': 50, #'device':torch.device("cuda:2"), 'figure_dir': 'figures' } # run LassoNet and save the results to a file lassonet_trainer(name, train, test, utils) # run the other feature selection methods and save the results to a file feature_sizes = [25, 50, 75, 100, 125] alternative_methods(name, train, test, [fisher, hsic, pfa_transform], feature_sizes) # run the downstream supervised learners and save the results to a file eval_methods(name, train, test, [lassonet_trainer, fisher, hsic, pfa_transform], feature_sizes, utils) # print the results indices = pickle.load(open(join(figure_dir, name + '-indices.pkl'), 'rb')) results = pickle.load(open(join(figure_dir, name + '-results.pkl'), 'rb')) algs = {
def main(): parser = argparse.ArgumentParser() parser.add_argument('--n', type=int, default=200) parser.add_argument('--p', type=int, default=50) parser.add_argument('--n_bootstraps', type=int, default=100) parser.add_argument('--n_b', type=int, default=1) parser.add_argument('--noise_var', type=float, default=.6) parser.add_argument('--weakness', type=float, default=.8) parser.add_argument('--perm', type=str, choices=['original', 'permuted'], default='original') args = parser.parse_args() print(sys.argv) print(args) n = args.n p = args.p n_bootstraps = args.n_bootstraps n_b = args.n_b noise_var = args.noise_var weakness = args.weakness test_size = 10 X, y, b = generate_data(n + test_size, p, n_b=n_b, noise_var=noise_var, seed=123) y = np.expand_dims(y, axis=-1) X_test = X[n:n + test_size] y_test = y[n:n + test_size] X_train = X[:n] y_train = y[:n] X_train_perm = X_train.copy() y_train_perm = y_train.copy() if args.perm == 'permuted': np.random.shuffle(y_train_perm) m = int(np.floor(n / 2.)) figure_dir = str(datetime.datetime.now()).replace(' ', ',') + '_' + str( uuid.uuid4()) if not os.path.exists(figure_dir): os.makedirs(figure_dir) for _ in range(n_bootstraps): start = time.time() X_train_perm, y_train_perm = unison_shuffled_copies( X_train_perm, y_train_perm) weights = 1. - (1. - weakness) * np.random.randint(2, size=p) lassonet_trainer('toy_' + args.perm + '_' + str(uuid.uuid4()), (weights * X_train_perm[:m], y_train_perm[:m]), (X_test, y_test), utils={ 'figure_dir': figure_dir, 'criterion': 'MSE' }) end = time.time() print('elapsed time:', end - start)