def outofsample_extensions(method='linear-regression'): # Load the data and init seeds train_data, train_labels, test_data, test_labels = load_mnist(dataset_path='data') np.random.seed(1) sklearn.utils.check_random_state(1) n_train_samples = 5000 # Learn a new space using Isomap isomap = Isomap(n_components=10, n_neighbors=20) train_data_isomap = np.float32(isomap.fit_transform(train_data[:n_train_samples, :])) if method == 'linear-regression': # Use linear regression to provide baseline out-of-sample extensions proj = LinearRegression() proj.fit(np.float64(train_data[:n_train_samples, :]), np.float64(train_data_isomap)) acc = evaluate_svm(proj.predict(train_data[:n_train_samples, :]), train_labels[:n_train_samples], proj.predict(test_data), test_labels) elif method == 'c-ISOMAP-10d' or method == 'c-ISOMAP-20d': # Use the SEF to provide out-of-sample extensions if method == 'c-ISOMAP-10d': proj = LinearSEF(train_data.shape[1], output_dimensionality=10) proj.cuda() else: proj = LinearSEF(train_data.shape[1], output_dimensionality=20) proj.cuda() loss = proj.fit(data=train_data[:n_train_samples, :], target_data=train_data_isomap, target='copy', epochs=50, batch_size=128, verbose=True, learning_rate=0.001, regularizer_weight=1) acc = evaluate_svm(proj.transform(train_data[:n_train_samples, :]), train_labels[:n_train_samples], proj.transform(test_data), test_labels) print("Method: ", method, " Test accuracy: ", 100 * acc, " %")
def outofsample_extensions(method=None, dataset=None): np.random.seed(1) sklearn.utils.check_random_state(1) dataset_path = 'data' train_data, train_labels, test_data, test_labels = dataset_loader(dataset_path, dataset, seed=1) # Learn a new space using Isomap isomap = Isomap(n_components=10, n_neighbors=20) train_data_isomap = np.float32(isomap.fit_transform(train_data)) if method == 'linear-regression': from sklearn.preprocessing import StandardScaler std = StandardScaler() train_data = std.fit_transform(train_data) test_data = std.transform(test_data) # Use linear regression to provide baseline out-of-sample extensions proj = LinearRegression() proj.fit(np.float64(train_data), np.float64(train_data_isomap)) acc = evaluate_svm(proj.predict(train_data), train_labels, proj.predict(test_data), test_labels) elif method == 'c-ISOMAP-10d' or method == 'c-ISOMAP-20d': # Use the SEF to provide out-of-sample extensions if method == 'c-ISOMAP-10d': proj = LinearSEF(train_data.shape[1], output_dimensionality=10) proj.cuda() else: proj = LinearSEF(train_data.shape[1], output_dimensionality=20) proj.cuda() loss = proj.fit(data=train_data, target_data=train_data_isomap, target='copy', epochs=50, batch_size=1024, verbose=False, learning_rate=0.001, regularizer_weight=1) acc = evaluate_svm(proj.transform(train_data), train_labels, proj.transform(test_data), test_labels) print("Method: ", method, " Test accuracy: ", 100 * acc, " %")
def run(args): if args.use_gpu: try: output = subprocess.check_output("free-gpu", shell=True) output = int(output.decode('utf-8')) gpu = output print("claiming gpu {}".format(gpu)) torch.cuda.set_device(int(gpu)) a = torch.tensor([1]).cuda() device = int(gpu) except (IndexError, subprocess.CalledProcessError) as e: device = None else: device = None print(f"device is : {device}") train_loader = DataLoader("../../data/raw/xkcd_colordata", "../../data/raw", "train", device=device) dev_generator = DataLoader("../../data/raw/xkcd_colordata", "../../data/raw/", "dev", device=device) model = LinearRegression(128, 3) if device is not None: model = model.cuda(device) criterion = nn.MSELoss() optimizer = torch.optim.SGD(model.parameters(), lr=args.lr) for i in range(args.n_epochs): for batch in train_loader: X, Y = prepare_batch(batch, device) optimizer.zero_grad() Y_pred = model.forward(X) loss = criterion(Y_pred, Y) loss.backward() optimizer.step() print(f"epoch: {i}, loss: {loss.data[0]}") torch.save(model, "../../models/baseline/{args.lr}_linear_{i}")