def random_input(self, test_size=1): if not self.one_hot: X = np.zeros((test_size, len(self.input_features))) else: X = np.empty((test_size, len(self.input_features)), dtype=object) for i, f in enumerate(self.input_features): if self.feature_types[i] == "CATEGORICAL": # choose random values for the categorical features X[:, i] = np.random.choice(self.label_encoders[i].classes_, size=test_size) else: if self.binning: # choose uniformly random values for the continuous features X[:, i] = [self.bins[f][j].unif_val() for j in np.random.choice(len(self.bins[f]), size=test_size)] else: X[:, i] = utils.gen_query_set(1, test_size)[:, 0] return X
def extract(self, budget, steps=[], adaptive_oracle=False, baseline=False, epsilon=1e-2, reg_lambda=1e-16, eps_factor=0.99, epoch=100, print_epoch=10, batch_size=1, num_passes=1000, random_seed=0): numpy.random.seed(random_seed) assert not (adaptive_oracle and steps) if steps: step = steps[0] else: step = budget if not adaptive_oracle: X_ext = utils.gen_query_set(self.num_features(), step) else: X_ext = utils.line_search_oracle(self.num_features(), step, self.query) model = None idx = 0 while budget > 0: idx += 1 budget -= step y_ext = self.query(X_ext) if not baseline: y_ext_p = self.query_probas(X_ext) else: num_classes = len(self.get_classes()) y_ext_p = numpy.zeros((len(y_ext), num_classes)) y_ext_p[numpy.arange(len(y_ext)), y_ext] = 1 print y_ext_p print '{}'.format(-numpy.mean(y_ext_p * numpy.log(y_ext_p))) model = build_model(X_ext, y_ext_p, epsilon=epsilon, reg_lambda=reg_lambda, num_passes=num_passes, eps_factor=eps_factor, epoch=epoch, print_epoch=print_epoch, batch_size=batch_size, warm_start=model) mtype = "base" if baseline else "extr" mode = "adapt-local" if steps \ else "adapt-oracle" if adaptive_oracle \ else "passive" save( model, 'experiments/inversion/{}/models/{}_{}_{}.pkl'.format( self.dataset, mode, mtype, len(X_ext))) if budget > 0: step = steps[idx] - steps[idx - 1] X_local = utils.gen_query_set(n=X_repr.shape[1], test_size=1000) Y_local = predict(model, X_local) assert len(pd.Series(Y_local).unique()) != 1 adaptive_budget = (min(step, budget) * 3) / 4 adaptive_budget += adaptive_budget % 2 random_budget = min(step, budget) - adaptive_budget predict_func = lambda x: predict(model, x) samples = utils.line_search(X_local, Y_local, adaptive_budget / 2, predict_func) X_random = utils.gen_query_set(X_ext.shape[1], random_budget) X_ext = numpy.vstack((samples, X_random, X_ext))
def main(): parser = argparse.ArgumentParser() parser.add_argument('model_file', type=str, help='a pickled model file') parser.add_argument('action', type=str, help='action to perform') parser.add_argument('budget', type=str, help='query budget') parser.add_argument('--num_passes', type=int, help='number of passes', default=1000) parser.add_argument('--epsilon', type=float, help='learning rate', default=0.1) parser.add_argument('--rounding', type=int, help='rounding digits') parser.add_argument('--steps', type=str, nargs='+', default=[], help='adaptive active learning') parser.add_argument('--adaptive_oracle', dest='adaptive_oracle', action='store_true', help='adaptive active learning from oracle') parser.add_argument('--force_reg', dest='force_reg', action='store_true', help='train a regression layer only') parser.add_argument('--batch_size', type=int, help='batch size', default=1) parser.add_argument('--seed', type=int, default=0, help='random seed') args = parser.parse_args() model_file = args.model_file action = args.action budget = args.budget num_passes = args.num_passes rounding = args.rounding steps = args.steps adaptive_oracle = args.adaptive_oracle epsilon = args.epsilon batch_size = args.batch_size force_reg = args.force_reg seed = args.seed np.random.seed(0) X_train, y_train, X_test, y_test, _ = utils.prepare_data('att_faces') ext = TheanoMLInversionExtractor(model_file) num_unknowns = len(ext.get_classes()) * ext.num_features() try: budget = int(budget) except ValueError: budget = int(float(budget) * num_unknowns) try: steps = map(int, steps) except ValueError: steps = map(lambda x: int(float(x) * num_unknowns), steps) print >> sys.stderr, 'Data: {}, Action: {}, Budget:{}, Seed: {}'.\ format(model_file, action, budget, seed) print >> sys.stderr, 'Number of unknowns: {}'.format(num_unknowns) if action == "extract": ext.extract(budget, steps=steps, print_epoch=1, adaptive_oracle=adaptive_oracle, num_passes=num_passes, epsilon=epsilon, batch_size=batch_size, random_seed=seed) elif action == "compare": X_test_u = utils.gen_query_set(X_test.shape[1], 1000) ext.compare(X_test, X_test_u) else: raise ValueError('Unknown action')
def gen_query_set(self, n, test_size, force_input_space=True): return utils.gen_query_set(n, test_size)
def extract(self, X_train, y_train, num_repr, budget, steps=[], adaptive_oracle=False, use_labels_only=False, gamma=1, epsilon=1e-2, reg_lambda=1e-16, eps_factor=0.99, epoch=100, print_epoch=10, batch_size=1, num_passes=1000, random_seed=0): """ Extracts a logistic regression multilabl classifier with RBF kernel for self.query :param X_train: For plotting :param y_train: For plotting :param num_repr: amount of vectors in the kernel base. :param budget: total amount of queries to the oracle. :param steps: monotoincally increasing list of queries amounts. :param adaptive_oracle: whether to query the oracle using line search, or use only random inputs. :param use_labels_only: Whether to use only the labels in training. :param gamma: coefficient for the RBF kernel :param epsilon: initial learining rate. :param reg_lambda: regularization coefficient. :param eps_factor: update factor for the learning rate. Once in every epoch: learning_rate *= eps_factor :param epoch: how many iterations over the whole training data count as a n epoch. :param print_epoch: :param batch_size: :param num_passes: how many iterations over the whole data should we do in each training. :param random_seed: :return: """ numpy.random.seed(random_seed) assert not (adaptive_oracle and steps) if steps: step = steps[0] else: step = budget if not adaptive_oracle: X_ext = utils.gen_query_set(self.num_features(), step) else: X_ext = utils.line_search_oracle(self.num_features(), step, self.query) X_repr = utils.gen_query_set(self.num_features(), num_repr) model = None idx = 0 while budget > 0: idx += 1 budget -= step y_ext = self.query(X_ext) if not use_labels_only: y_ext_p = self.query_probas(X_ext) else: # When this a baseline, take the labels from the query result # as one-hot vectors to be the probability vectors. num_classes = len(self.get_classes()) y_ext_p = numpy.zeros((len(y_ext), num_classes)) y_ext_p[numpy.arange(len(y_ext)), y_ext] = 1 print y_ext_p print '{} ({})'.format( self.calculate_loss(X_ext, y_ext_p, reg_lambda), self.calculate_loss(X_ext, y_ext_p, 0)) print >> sys.stderr, self.calculate_loss(X_ext, y_ext_p, reg_lambda) model = build_model(X_repr, False, X_ext, y_ext_p, gamma=gamma, epsilon=epsilon, reg_lambda=reg_lambda, num_passes=num_passes, eps_factor=eps_factor, epoch=epoch, print_epoch=print_epoch, batch_size=batch_size, warm_start=model) mtype = "base" if use_labels_only else "extr" mode = "adapt-local" if steps \ else "adapt-oracle" if adaptive_oracle \ else "passive" save( model, 'experiments/KLR/{}/models/{}_{}_{}.pkl'.format( self.dataset, mode, mtype, len(X_ext))) if X_train is not None and X_train.shape[1] == 2: bounds = [-1.1, 1.1, -1.1, 1.1] filename = 'experiments/KLR/{}/plots/{}_{}_{}_{}_boundary'.\ format(self.dataset, mode, mtype, len(X_ext), random_seed) utils.plot_decision_boundary(lambda x: predict(model, x), X_train.values, y_train, bounds, filename) filename = 'experiments/KLR/{}/plots/{}_{}_{}_{}_boundary_ext'.\ format(self.dataset, mode, mtype, len(X_ext), random_seed) utils.plot_decision_boundary(lambda x: predict(model, x), X_ext, y_ext, bounds, filename) if budget > 0: step = steps[idx] - steps[idx - 1] X_local = utils.gen_query_set(n=X_repr.shape[1], test_size=1000) Y_local = predict(model, X_local) assert len(pd.Series(Y_local).unique()) != 1 adaptive_budget = (min(step, budget) * 3) / 4 adaptive_budget += adaptive_budget % 2 random_budget = min(step, budget) - adaptive_budget predict_func = lambda x: predict(model, x) samples = utils.line_search(X_local, Y_local, adaptive_budget / 2, predict_func) X_random = utils.gen_query_set(X_ext.shape[1], random_budget) X_ext = numpy.vstack((samples, X_random, X_ext))
def main(): parser = argparse.ArgumentParser() parser.add_argument('data', type=str, help='a dataset') parser.add_argument('action', type=str, help='action to perform') parser.add_argument('num_repr', type=int, help='number of representers') parser.add_argument('budget', type=str, help='query budget') parser.add_argument('--num_passes', type=int, help='number of passes', default=1000) parser.add_argument('--rounding', type=int, help='rounding digits') parser.add_argument('--steps', type=str, nargs='+', default=[], help='adaptive active learning') parser.add_argument('--adaptive_oracle', dest='adaptive_oracle', action='store_true', help='adaptive active learning from oracle') parser.add_argument('--gamma', type=float, help='RBF kernel hyper-parameter') parser.add_argument('--epsilon', type=float, help='learning rate', default=0.1) parser.add_argument('--seed', type=int, default=0, help='random seed') parser.add_argument('--batch_size', type=int, help='batch size') args = parser.parse_args() dataset = args.data action = args.action num_repr = args.num_repr budget = args.budget num_passes = args.num_passes rounding = args.rounding steps = args.steps adaptive_oracle = args.adaptive_oracle gamma = args.gamma epsilon = args.epsilon seed = args.seed batch_size = args.batch_size np.random.seed(0) X_train, y_train, X_test, y_test, scaler = utils.prepare_data(dataset) X_test_u = utils.gen_query_set(X_test.shape[1], 1000) ext = LocalKernelExtractor(dataset, X_train, y_train, rounding=rounding) num_unknowns = num_repr * X_train.shape[1] + \ len(ext.get_classes()) * (num_repr + 1) try: budget = int(budget) except ValueError: budget = int(float(budget) * num_unknowns) try: steps = map(int, steps) except ValueError: steps = map(lambda x: int(float(x) * num_unknowns), steps) print >> sys.stderr, 'Data: {}, Action: {}, Budget:{}, Seed: {}'.\ format(dataset, action, budget, seed) print >> sys.stderr, 'Number of unknowns: {}'.format(num_unknowns) if action == "train": ext.train(num_repr, X_test, y_test) elif action == "extract": if gamma is None: gamma = ext.get_gamma() print gamma ext.extract(X_train, y_train, num_repr, budget, gamma=gamma, steps=steps, adaptive_oracle=adaptive_oracle, num_passes=num_passes, epsilon=epsilon, random_seed=seed, batch_size=batch_size) elif action == "baseline": if gamma is None: gamma = ext.get_gamma() ext.extract(X_train, y_train, num_repr, budget, gamma=gamma, steps=steps, adaptive_oracle=adaptive_oracle, baseline=True, num_passes=num_passes, epsilon=epsilon, random_seed=seed, batch_size=batch_size, reg_lambda=1e-40) elif action == "compare": ext.compare(X_test, X_test_u, scaler=None)
def train(self, num_repr, X_test, y_test): X_train = self.X_train y_train = self.y_train y_train_p = np.zeros((len(y_train), len(self.classes))) y_train_p[np.arange(len(y_train)), y_train] = 1 """ assert num_repr >= len(self.get_classes()) X_repr_bb = [] class_counter = Counter(y_train) repr_per_class \ = (num_repr + len(self.get_classes()) - 1) / len(self.get_classes()) for (c, count) in sorted(class_counter.iteritems(), key=lambda _: _[1]): print c, count, repr_per_class reprs = X_train.values[np.where(y_train == c)[0][0:repr_per_class]] X_repr_bb.append(reprs) X_repr_bb = np.vstack(X_repr_bb)[0:num_repr] print '{} representers'.format(len(X_repr_bb)) """ X_repr_bb = X_train[0:num_repr].values """ import math import matplotlib.pyplot as plt side = math.sqrt(X_repr_bb.shape[1]) plt.figure() for i in range(len(X_repr_bb)): plt.subplot(2, len(X_repr_bb)/2, i + 1) plt.axis('off') image = X_repr_bb[i, :].reshape((side, side)) plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest') plt.show() """ """ model_bb = krs.build_model(X_repr_bb, False, X_train, y_train_p, epsilon=epsilon, reg_lambda=1e-4, num_passes=num_passes, eps_factor=0.99, epoch=100, print_epoch=10, batch_size=1, gamma=gamma) y_pred_bb = krs.predict(model_bb, X_test) """ best_model = None best_acc = 0 best_gamma = None for gamma in [10**x for x in range(-10, 10)]: from sklearn.metrics.pairwise import rbf_kernel from sklearn.linear_model import LogisticRegression X_train_ker = rbf_kernel(X_train, X_repr_bb, gamma=gamma) model = LogisticRegression(multi_class='multinomial', solver='lbfgs').\ fit(X_train_ker, y_train) y_pred = model.predict(rbf_kernel(X_test, X_repr_bb, gamma=gamma)) acc = accuracy_score(y_test, y_pred) if acc > best_acc: best_acc = acc best_gamma = gamma best_model = model W = best_model.coef_.T b = best_model.intercept_ if len(self.classes) == 2: W = np.hstack((np.zeros((len(X_repr_bb), 1)), W)) b = np.hstack((0, b)) W = theano.shared( value=W, name='W', borrow=True ) b = theano.shared( value=b, name='b', borrow=True ) model_bb = krs.KernelLog(T.matrix('x'), best_gamma, len(self.classes), X_repr_bb, learn_Y=False, W=W, b=b) y_pred_bb = krs.predict(model_bb, X_test) print 'Best Gamma: {}'.format(best_gamma) print 'Y_test: {}'.format(Counter(y_test)) print 'Y_pred: {}'.format(Counter(y_pred_bb)) acc = accuracy_score(y_test, y_pred_bb) print 'Training accuracy: {}'.format(acc) print >> sys.stderr, 'Training accuracy: {}'.format(acc) X_test_u = utils.gen_query_set(X_test.shape[1], 10000) y_pred_u = krs.predict(model_bb, X_test_u) print 'Y_pred_u: {}'.format(Counter(y_pred_u)) if X_train.shape[1] == 2: bounds = [-1.1, 1.1, -1.1, 1.1] X_train = X_train.values utils.plot_decision_boundary( lambda x: krs.predict(model_bb, x), X_train, y_train, bounds) krs.save(model_bb, 'experiments/KLR/{}/models/oracle.pkl'.format(self.dataset))
def train(self, num_repr, X_test, y_test): X_train = self.X_train y_train = self.y_train y_train_p = np.zeros((len(y_train), len(self.classes))) y_train_p[np.arange(len(y_train)), y_train] = 1 """ assert num_repr >= len(self.get_classes()) X_repr_bb = [] class_counter = Counter(y_train) repr_per_class \ = (num_repr + len(self.get_classes()) - 1) / len(self.get_classes()) for (c, count) in sorted(class_counter.iteritems(), key=lambda _: _[1]): print c, count, repr_per_class reprs = X_train.values[np.where(y_train == c)[0][0:repr_per_class]] X_repr_bb.append(reprs) X_repr_bb = np.vstack(X_repr_bb)[0:num_repr] print '{} representers'.format(len(X_repr_bb)) """ X_repr_bb = X_train[0:num_repr].values """ import math import matplotlib.pyplot as plt side = math.sqrt(X_repr_bb.shape[1]) plt.figure() for i in range(len(X_repr_bb)): plt.subplot(2, len(X_repr_bb)/2, i + 1) plt.axis('off') image = X_repr_bb[i, :].reshape((side, side)) plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest') plt.show() """ """ model_bb = krs.build_model(X_repr_bb, False, X_train, y_train_p, epsilon=epsilon, reg_lambda=1e-4, num_passes=num_passes, eps_factor=0.99, epoch=100, print_epoch=10, batch_size=1, gamma=gamma) y_pred_bb = krs.predict(model_bb, X_test) """ best_model = None best_acc = 0 best_gamma = None for gamma in [10**x for x in range(-10, 10)]: from sklearn.metrics.pairwise import rbf_kernel from sklearn.linear_model import LogisticRegression X_train_ker = rbf_kernel(X_train, X_repr_bb, gamma=gamma) model = LogisticRegression(multi_class='multinomial', solver='lbfgs').\ fit(X_train_ker, y_train) y_pred = model.predict(rbf_kernel(X_test, X_repr_bb, gamma=gamma)) acc = accuracy_score(y_test, y_pred) if acc > best_acc: best_acc = acc best_gamma = gamma best_model = model W = best_model.coef_.T b = best_model.intercept_ if len(self.classes) == 2: W = np.hstack((np.zeros((len(X_repr_bb), 1)), W)) b = np.hstack((0, b)) W = theano.shared( value=W, name='W', borrow=True ) b = theano.shared( value=b, name='b', borrow=True ) model_bb = krs.KernelLog(T.matrix('x'), best_gamma, len(self.classes), X_repr_bb, learn_kernel_base=False, W=W, b=b) y_pred_bb = krs.predict(model_bb, X_test) print 'Best Gamma: {}'.format(best_gamma) print 'Y_test: {}'.format(Counter(y_test)) print 'Y_pred: {}'.format(Counter(y_pred_bb)) acc = accuracy_score(y_test, y_pred_bb) print 'Training accuracy: {}'.format(acc) print >> sys.stderr, 'Training accuracy: {}'.format(acc) X_test_u = utils.gen_query_set(X_test.shape[1], 10000) y_pred_u = krs.predict(model_bb, X_test_u) print 'Y_pred_u: {}'.format(Counter(y_pred_u)) if X_train.shape[1] == 2: bounds = [-1.1, 1.1, -1.1, 1.1] X_train = X_train.values utils.plot_decision_boundary( lambda x: krs.predict(model_bb, x), X_train, y_train, bounds) krs.save(model_bb, 'experiments/KLR/{}/models/oracle.pkl'.format(self.dataset))
def main(): parser = argparse.ArgumentParser() parser.add_argument('data', type=str, help='a dataset') parser.add_argument('action', type=str, help='action to perform') parser.add_argument('num_repr', type=int, help='number of representers') parser.add_argument('budget', type=str, help='query budget') parser.add_argument('--num_passes', type=int, help='number of passes', default=1000) parser.add_argument('--rounding', type=int, help='rounding digits') parser.add_argument('--steps', type=str, nargs='+', default=[], help='adaptive active learning') parser.add_argument('--adaptive_oracle', dest='adaptive_oracle', action='store_true', help='adaptive active learning from oracle') parser.add_argument('--gamma', type=float, help='RBF kernel hyper-parameter') parser.add_argument('--epsilon', type=float, help='learning rate', default=0.1) parser.add_argument('--seed', type=int, default=0, help='random seed') parser.add_argument('--batch_size', type=int, help='batch size') args = parser.parse_args() dataset = args.data action = args.action num_repr = args.num_repr budget = args.budget num_passes = args.num_passes rounding = args.rounding steps = args.steps adaptive_oracle = args.adaptive_oracle gamma = args.gamma epsilon = args.epsilon seed = args.seed batch_size = args.batch_size np.random.seed(0) X_train, y_train, X_test, y_test, scaler = utils.prepare_data(dataset) X_test_u = utils.gen_query_set(X_test.shape[1], 1000) ext = LocalKernelExtractor(dataset, X_train, y_train, rounding=rounding) num_unknowns = num_repr * X_train.shape[1] + \ len(ext.get_classes()) * (num_repr + 1) try: budget = int(budget) except ValueError: budget = int(float(budget) * num_unknowns) try: steps = map(int, steps) except ValueError: steps = map(lambda x: int(float(x) * num_unknowns), steps) print >> sys.stderr, 'Data: {}, Action: {}, Budget:{}, Seed: {}'.\ format(dataset, action, budget, seed) print >> sys.stderr, 'Number of unknowns: {}'.format(num_unknowns) if action == "train": ext.train(num_repr, X_test, y_test) elif action == "extract": if gamma is None: gamma = ext.get_gamma() print gamma ext.extract(X_train, y_train, num_repr, budget, gamma=gamma, steps=steps, adaptive_oracle=adaptive_oracle, num_passes=num_passes, epsilon=epsilon, random_seed=seed, batch_size=batch_size) elif action == "baseline": if gamma is None: gamma = ext.get_gamma() ext.extract(X_train, y_train, num_repr, budget, gamma=gamma, steps=steps, adaptive_oracle=adaptive_oracle, use_labels_only=True, num_passes=num_passes, epsilon=epsilon, random_seed=seed, batch_size=batch_size, reg_lambda=1e-40) elif action == "compare": ext.compare(X_test, X_test_u, scaler=None)
def extract(self, X_train, y_train, num_repr, budget, steps=[], adaptive_oracle=False, baseline=False, gamma=1, epsilon=1e-2, reg_lambda=1e-16, eps_factor=0.99, epoch=100, print_epoch=10, batch_size=1, num_passes=1000, random_seed=0): numpy.random.seed(random_seed) assert not (adaptive_oracle and steps) if steps: step = steps[0] else: step = budget if not adaptive_oracle: X_ext = utils.gen_query_set(self.num_features(), step) else: X_ext = utils.line_search_oracle(self.num_features(), step, self.query) X_repr = utils.gen_query_set(self.num_features(), num_repr) model = None idx = 0 while budget > 0: idx += 1 budget -= step y_ext = self.query(X_ext) if not baseline: y_ext_p = self.query_probas(X_ext) else: num_classes = len(self.get_classes()) y_ext_p = numpy.zeros((len(y_ext), num_classes)) y_ext_p[numpy.arange(len(y_ext)), y_ext] = 1 print y_ext_p print '{} ({})'.format( self.calculate_loss(X_ext, y_ext_p, reg_lambda), self.calculate_loss(X_ext, y_ext_p, 0)) print >> sys.stderr, self.calculate_loss(X_ext, y_ext_p, reg_lambda) model = build_model(X_repr, False, X_ext, y_ext_p, gamma=gamma, epsilon=epsilon, reg_lambda=reg_lambda, num_passes=num_passes, eps_factor=eps_factor, epoch=epoch, print_epoch=print_epoch, batch_size=batch_size, warm_start=model) mtype = "base" if baseline else "extr" mode = "adapt-local" if steps \ else "adapt-oracle" if adaptive_oracle \ else "passive" save( model, 'experiments/KLR/{}/models/{}_{}_{}.pkl'.format( self.dataset, mode, mtype, len(X_ext))) if X_train is not None and X_train.shape[1] == 2: bounds = [-1.1, 1.1, -1.1, 1.1] filename = 'experiments/KLR/{}/plots/{}_{}_{}_{}_boundary'.\ format(self.dataset, mode, mtype, len(X_ext), random_seed) utils.plot_decision_boundary(lambda x: predict(model, x), X_train.values, y_train, bounds, filename) filename = 'experiments/KLR/{}/plots/{}_{}_{}_{}_boundary_ext'.\ format(self.dataset, mode, mtype, len(X_ext), random_seed) utils.plot_decision_boundary(lambda x: predict(model, x), X_ext, y_ext, bounds, filename) if budget > 0: step = steps[idx] - steps[idx - 1] X_local = utils.gen_query_set(n=X_repr.shape[1], test_size=1000) Y_local = predict(model, X_local) assert len(pd.Series(Y_local).unique()) != 1 adaptive_budget = (min(step, budget) * 3) / 4 adaptive_budget += adaptive_budget % 2 random_budget = min(step, budget) - adaptive_budget predict_func = lambda x: predict(model, x) samples = utils.line_search(X_local, Y_local, adaptive_budget / 2, predict_func) X_random = utils.gen_query_set(X_ext.shape[1], random_budget) X_ext = numpy.vstack((samples, X_random, X_ext))
def extract(self, X_train, y_train, budget, steps=[], adaptive_oracle=False, baseline=False, epsilon=1e-1, num_passes=1000, reg_lambda=1e-8, eps_factor=0.99, epoch=100, print_loss=True, print_epoch=10, batch_size=20, random_seed=0): numpy.random.seed(random_seed) assert not (adaptive_oracle and steps) if steps: step = steps[0] else: step = budget if not adaptive_oracle: X_ext = utils.gen_query_set(X_train.shape[1], step) else: X_ext = utils.line_search_oracle(X_train.shape[1], step, self.query) idx = 0 while budget > 0: idx += 1 budget -= step y_ext = self.query(X_ext) if not baseline: y_ext_p = self.query_probas(X_ext) else: num_classes = len(self.get_classes()) y_ext_p = numpy.zeros((len(y_ext), num_classes)) y_ext_p[numpy.arange(len(y_ext)), y_ext] = 1 # Loss with correct parameters: print self.calculate_loss(X_ext, y_ext_p, reg_lambda) print >> sys.stderr, self.calculate_loss(X_ext, y_ext_p, reg_lambda) model = build_model(self.hidden_nodes, X_ext, y_ext_p, epsilon=epsilon, num_passes=num_passes, reg_lambda=reg_lambda, epoch=epoch, eps_factor=eps_factor, print_loss=print_loss, print_epoch=print_epoch, batch_size=batch_size) m_type = "base" if baseline else "extr" mode = "adapt-local" if steps \ else "adapt-oracle" if adaptive_oracle \ else "passive" save(model, 'experiments/{}/models/{}_{}_{}_{}_{}.pkl'. format(self.dataset, mode, m_type, self.hidden_nodes, len(X_ext), random_seed)) if X_train is not None and X_train.shape[1] == 2: bounds = [-1.1, 1.1, -1.1, 1.1] filename = 'experiments/{}/plots/{}_{}_{}_{}_{}_boundary'.\ format(self.dataset, mode, m_type, self.hidden_nodes, len(X_ext), random_seed) utils.plot_decision_boundary(lambda x: predict(model, x), X_train.values, y_train, bounds, filename) filename = 'experiments/{}/plots/{}_{}_{}_{}_{}_boundary_ext'.\ format(self.dataset, mode, m_type, self.hidden_nodes, len(X_ext), random_seed) utils.plot_decision_boundary(lambda x: predict(model, x), X_ext, y_ext, bounds, filename) if budget > 0: step = steps[idx] - steps[idx-1] X_local = utils.gen_query_set(n=X_ext.shape[1], test_size=1000) Y_local = predict(model, X_local) assert len(pd.Series(Y_local).unique()) != 1 adaptive_budget = (min(step, budget)*3)/4 adaptive_budget += adaptive_budget % 2 random_budget = min(step, budget) - adaptive_budget predict_func = lambda x: predict(model, x) samples = utils.line_search(X_local, Y_local, adaptive_budget/2, predict_func) X_random = utils.gen_query_set(X_ext.shape[1], random_budget) X_ext = numpy.vstack((samples, X_random, X_ext))
def main(): parser = argparse.ArgumentParser() parser.add_argument('data', type=str, help='a dataset') parser.add_argument('hidden_nodes', type=int, help='number of hidden nodes') parser.add_argument('action', type=str, help='action to perform') parser.add_argument('budget', type=str, help='query budget') parser.add_argument('--num_passes', type=int, help='number of passes', default=1000) parser.add_argument('--epsilon', type=float, help='learning rate', default=0.1) parser.add_argument('--rounding', type=int, help='rounding digits') parser.add_argument('--steps', type=str, nargs='+', default=[], help='adaptive active learning') parser.add_argument('--adaptive_oracle', dest='adaptive_oracle', action='store_true', help='adaptive active learning from oracle') parser.add_argument('--force_reg', dest='force_reg', action='store_true', help='train a regression layer only') parser.add_argument('--batch_size', type=int, help='batch size', default=1) parser.add_argument('--seed', type=int, default=0, help='random seed') args = parser.parse_args() dataset = args.data action = args.action hidden_nodes = args.hidden_nodes budget = args.budget num_passes = args.num_passes rounding = args.rounding steps = args.steps adaptive_oracle = args.adaptive_oracle epsilon = args.epsilon batch_size = args.batch_size force_reg = args.force_reg seed = args.seed np.random.seed(0) X_train, y_train, X_test, y_test, scaler = utils.prepare_data(dataset) if force_reg: dataset += "_reg" ext = LocalPerceptronExtractor(dataset, hidden_nodes, X_train, y_train, rounding=rounding, force_reg=force_reg) num_unknowns = hidden_nodes * (X_train.shape[1] + 1) + \ len(ext.get_classes()) * (hidden_nodes + 1) try: budget = int(budget) except ValueError: budget = int(float(budget) * num_unknowns) try: steps = map(int, steps) except ValueError: steps = map(lambda x: int(float(x) * num_unknowns), steps) print >> sys.stderr, 'Data: {}, Action: {}, Budget:{}, Seed: {}'.\ format(dataset, action, budget, seed) print >> sys.stderr, 'Number of unknowns: {}'.format(num_unknowns) if action == "train": ext.train(X_test, y_test, num_passes=num_passes) elif action == "extract": ext.extract(X_train, y_train, budget, steps=steps, adaptive_oracle=adaptive_oracle, num_passes=num_passes, epsilon=epsilon, batch_size=batch_size, random_seed=seed) elif action == "baseline": ext.extract(X_train, y_train, budget, steps=steps, adaptive_oracle=adaptive_oracle, baseline=True, num_passes=num_passes, epsilon=epsilon, batch_size=batch_size, random_seed=seed, reg_lambda=1e-40) elif action == "compare": X_test_u = utils.gen_query_set(X_test.shape[1], 10000) ext.compare(X_test, X_test_u, force_reg=force_reg) else: raise ValueError('Unknown action')