Ejemplo n.º 1
0
    def __init__(self, configs=MethodConfigs()):
        super(SupervisedInstanceSelectionGreedy, self).__init__(configs)
        self.cv_params = {
            'sigma_p': np.logspace(-3, 3, 10),
            'sigma_y': self.create_cv_params(-5, 5),
            'C': self.create_cv_params(-3, 3),
        }
        self.use_l1_loss = getattr(configs, 'use_l1_loss', False)
        self.C = 0
        self.sigma_y = 1
        self.sigma_p = 1
        self.no_f_x = getattr(configs, 'no_f_x', False)
        #self.fixed_sigma_x = getattr(configs, 'fixed_sigma_x', False)
        #self.no_spectral_kernel = getattr(configs, 'no_spectral_kernel', False)
        self.use_p_x = getattr(configs, 'use_p_x', True)

        # Just use p(x)
        if self.no_f_x:
            del self.cv_params['sigma_y']
            del self.cv_params['C']
            self.C = 1

        # Just use f(x)
        if not self.use_p_x:
            #self.cv_params['sigma_p'] = np.asarray([1], dtype=np.float)
            del self.cv_params['sigma_p']
            if 'C' in self.cv_params:
                del self.cv_params['C']
Ejemplo n.º 2
0
 def __init__(self, configs=MethodConfigs()):
     super(RelativeActiveOEDMethod, self).__init__(configs)
     self.use_grad = True
     #self.oed_method = 'E'
     self.oed_method = None
     self.use_labeled = True
     self.use_true_y = getattr(configs, 'use_true_y', False)
Ejemplo n.º 3
0
    def __init__(self, configs=MethodConfigs()):
        super(StackingTransfer, self).__init__(configs)
        #from far_transfer_methods import GraphTransferNW
        self.base_learner = method.SKLRidgeRegression(deepcopy(configs))
        self.source_learner = method.NadarayaWatsonMethod(deepcopy(configs))
        self.target_learner = method.NadarayaWatsonMethod(deepcopy(configs))
        self.joint_cv = getattr(configs, 'joint_cv', False)
        self.only_use_source_prediction = False
        self.use_all_source = True
        self.source_only = False
        self.target_only = False
        self.just_bias = False
        self.linear_source = False
        if self.target_only or self.source_only or self.linear_source:
            self.joint_cv = False
        if self.just_bias:
            self.base_learner.cv_params = {'alpha': [1e16]}
            self.joint_cv = False
        if self.linear_source:
            self.target_learner.cv_params = {'sigma': [1]}
        if self.joint_cv:
            self.cv_params = self.base_learner.cv_params.copy()
            self.cv_params.update(self.target_learner.cv_params)
            self.base_learner.cv_params = None
            self.target_learner.cv_params = None

        sub_configs = deepcopy(configs)

        #self.source_learner = method.NadarayaWatsonKNNMethod(deepcopy(sub_configs))
        #self.target_learner = method.NadarayaWatsonKNNMethod(deepcopy(sub_configs))
        self.use_validation = configs.use_validation
Ejemplo n.º 4
0
 def __init__(self, configs=MethodConfigs()):
     super(ClusterActiveMethod, self).__init__(configs)
     self.transform = StandardScaler()
     self.use_target_variance = True
     self.use_density = False
     self.use_instance_selection = True
     self.use_greedy_instance_selection = getattr(
         configs, 'use_greedy_instance_selection', False)
     self.use_p_x = False
     self.cluster_select_singleton = getattr(configs,
                                             'cluster_select_singleton',
                                             True)
     self.transfer_hyperparameters = getattr(configs,
                                             'transfer_hyperparameters',
                                             False)
     if self.use_greedy_instance_selection:
         configs.use_p_x = self.use_p_x
         self.instance_selector = SupervisedInstanceSelectionGreedy(
             deepcopy(configs))
     else:
         self.instance_selector = SupervisedInstanceSelectionClusterGraph(
             deepcopy(configs))
     self.instance_selector.quiet = False
     self.use_warm_start = False
     self.use_oracle_labels = False
     self.use_oracle_target = False
     self.max_items_for_instance_selection = None
Ejemplo n.º 5
0
 def __init__(self, configs=MethodConfigs()):
     super(SupervisedInstanceSelectionClusterSplit, self).__init__(configs)
     self.mixture_reg = None
     self.cv_params = {}
     self.k_means = KMeans()
     self.max_std = .4
     self.sub_cluster_size = 2
     self.impure_cluster_samples = 2
     self.original_cluster_inds = None
Ejemplo n.º 6
0
 def __init__(self, configs=MethodConfigs()):
     super(ReweightedTransfer, self).__init__(configs)
     self.target_kde = None
     self.source_kde = None
     self.kde_bandwidths = 10**np.asarray(range(-6, 6), dtype='float64')
     c = deepcopy(configs)
     c.temp_dir = None
     self.base_learner = method.NadarayaWatsonMethod(configs)
     self.cv_params = {'B': np.asarray([2, 4, 8, 16, 32])}
     self.base_learner_cv_keys = []
Ejemplo n.º 7
0
 def __init__(self, configs=MethodConfigs()):
     super(KDE, self).__init__(configs)
     self.cv_params = {'sigma': np.asarray(10.0**np.asarray(range(-4, 5)))}
     self.cv_params = {}
     self.is_classifier = False
     self._estimated_error = None
     self.quiet = True
     self.best_params = None
     self.model = None
     self.configs.loss_function = loss_function.MeanSquaredError()
     self.configs.cv_loss_function = loss_function.MeanSquaredError()
Ejemplo n.º 8
0
 def __init__(self, configs=MethodConfigs()):
     super(SupervisedInstanceSelectionSubmodular, self).__init__(configs)
     self.mixture_reg = None
     self.cv_params = {
         'sigma_x': self.create_cv_params(-5, 5),
         'sigma_y': self.create_cv_params(-5, 5),
         'C': self.create_cv_params(-3, 3),
     }
     self.original_cluster_inds = None
     self.configs.use_saved_cv_output = True
     self.no_f_x = getattr(configs, 'no_f_x', False)
     self.num_class_splits = getattr(configs, 'num_class_splits', None)
     if self.no_f_x:
         del self.cv_params['sigma_y']
Ejemplo n.º 9
0
 def __init__(self, configs=MethodConfigs()):
     super(SupervisedInstanceSelectionClusterGraph, self).__init__(configs)
     self.mixture_reg = None
     self.cv_params = {
         'sigma_x': self.create_cv_params(-5, 5),
         'sigma_y': self.create_cv_params(-5, 5),
     }
     self.spectral_cluster = SpectralClustering()
     self.original_cluster_inds = None
     self.configs.use_saved_cv_output = True
     self.no_f_x = getattr(configs, 'no_f_x', False)
     self.fixed_sigma_x = getattr(configs, 'fixed_sigma_x', False)
     self.no_spectral_kernel = getattr(configs, 'no_spectral_kernel', False)
     self.cluster_select_singleton = getattr(configs, 'cluster_select_singleton', True)
     if self.no_f_x:
         del self.cv_params['sigma_y']
     if self.fixed_sigma_x or self.no_spectral_kernel:
         self.cv_params['sigma_x'] = np.asarray([1], dtype=np.float)
Ejemplo n.º 10
0
    def __init__(self, configs=MethodConfigs()):
        super(SupervisedInstanceSelection, self).__init__(configs)
        self.cv_params = dict()
        self.is_classifier = False
        self.p_s = None
        self.p_x = None
        self.f_s = None
        self.f_x = None
        self.f_x_estimate = None
        self.learned_distribution = None
        self.optimization_value = None
        self.use_linear = False
        self.quiet = False

        self.selected_data = None
        self.full_data = None

        configs = deepcopy(self.configs)
        self.target_learner = method.NadarayaWatsonMethod(deepcopy(configs))
        self.target_learner.configs.use_validation = True
        self.target_learner.configs.results_features = ['y', 'true_y']
        self.target_learner.quiet = True
        self.subset_learner = deepcopy(self.target_learner)
        self.mixture_reg = 1
        self.subset_size = 10
        self.density_reg = .1
        self.num_samples = 5
        self.subset_density_reg = .1
        self.learner_reg = 100
        self.pca = None
        self.output = None

        self.no_f_x = False

        self.is_noisy = None

        if self.use_linear:
            self.supervised_loss_func = compute_f_linear
        else:
            self.supervised_loss_func = compute_f_nw
            self.cv_params['subset_size'] = self.create_cv_params(-5, 5)
Ejemplo n.º 11
0
    def __init__(self, configs=MethodConfigs()):
        super(HypothesisTransfer, self).__init__(configs)
        self.cv_params = {
            'C': self.create_cv_params(-5, 5),
            'C2': self.create_cv_params(-5, 5),
            'C3': self.create_cv_params(-5, 5),
        }
        self.w = None
        self.b = None

        #self.base_source_learner = method.SKLRidgeClassification(deepcopy(configs))
        self.base_source_learner = None
        self.label_transform = None

        self.source_w = []
        self.transform = StandardScaler()
        #self.transform = None
        self.use_oracle = False
        self.tune_C = False
        #self.weight_type = HypothesisTransfer.WEIGHTS_ALL
        #self.weight_type = HypothesisTransfer.WEIGHTS_JUST_TARGET
        #self.weight_type = HypothesisTransfer.WEIGHTS_JUST_OPTIMAL
        self.weight_type = HypothesisTransfer.WEIGHTS_JUST_FIRST
        if hasattr(configs, 'weight_type'):
            self.weight_type = configs.weight_type
        self.oracle_data_set_ids = configs.oracle_data_set_ids
        self.c_value = None
        self.use_test_error_for_model_selection = configs.use_test_error_for_model_selection
        if self.weight_type == HypothesisTransfer.WEIGHTS_JUST_TARGET:
            del self.cv_params['C2']
            del self.cv_params['C3']
            self.C2 = 0
            self.C3 = 0
        elif not getattr(self, 'tune_C', True):
            del self.cv_params['C']
            self.C = 0
Ejemplo n.º 12
0
 def __init__(self, configs=MethodConfigs()):
     super(RelativeActiveUncertaintyMethod, self).__init__(configs)
     self.use_oracle = False
     self.use_largest_delta = False
Ejemplo n.º 13
0
 def __init__(self, configs=MethodConfigs()):
     super(RelativeActiveDensityMethod, self).__init__(configs)
Ejemplo n.º 14
0
 def __init__(self, configs=MethodConfigs()):
     super(OEDLinearActiveMethod, self).__init__(configs)
     self.transform = StandardScaler()
     self.use_labeled = True
Ejemplo n.º 15
0
 def __init__(self, configs=MethodConfigs()):
     super(SupervisedInstanceSelectionCluster, self).__init__(configs)
     self.mixture_reg = None
     self.cv_params = {}
     self.k_means = KMeans()
     self.original_cluster_inds = None
Ejemplo n.º 16
0
 def __init__(self, configs=MethodConfigs()):
     super(IGRelativeActiveMethod, self).__init__(configs)
 def __init__(self, configs=MethodConfigs()):
     super(MixedFeatureGuidanceMethod, self).__init__(configs)
     #self.cv_params['C'] = self.create_cv_params(-5, 5, append_zero=True)
     self.cv_params['C'] = self.create_cv_params(-5, 5, append_zero=False)
     #self.cv_params['C2'] = self.create_cv_params(-8, 8, append_zero=True, prepend_inf=True)
     self.cv_params['C2'] = self.create_cv_params(-8,
                                                  8,
                                                  append_zero=True,
                                                  prepend_inf=False)
     #self.cv_params['C2'] = np.asarray([np.inf])
     self.cv_params['C3'] = self.create_cv_params(-5, 5, append_zero=True)
     self.transform = StandardScaler()
     #self.preprocessor = preprocessing.BasisQuadraticFewPreprocessor()
     if hasattr(configs, 'method'):
         self.method = configs.method
     else:
         self.method = MixedFeatureGuidanceMethod.METHOD_RELATIVE
         #self.method = MixedFeatureGuidanceMethod.METHOD_RIDGE
         #self.method = MixedFeatureGuidanceMethod.METHOD_ORACLE
         #self.method = MixedFeatureGuidanceMethod.METHOD_ORACLE_SPARSITY
     self.use_sign = getattr(configs, 'use_sign', True)
     self.use_corr = getattr(configs, 'use_corr', False)
     self.use_training_corr = getattr(configs, 'use_training_corr', False)
     self.use_oracle = getattr(configs, 'use_oracle', False)
     self.use_nonneg = getattr(configs, 'use_nonneg', False)
     self.use_stacking = getattr(configs, 'use_stacking', False)
     self.can_use_test_error_for_model_selection = True
     self.use_test_error_for_model_selection = configs.use_test_error_for_model_selection
     self.use_validation = configs.use_validation
     self.num_random_pairs = getattr(configs, 'num_random_pairs', 0)
     self.num_random_signs = getattr(configs, 'num_random_signs', 0)
     self.disable_relaxed_guidance = getattr(configs,
                                             'disable_relaxed_guidance',
                                             False)
     self.disable_tikhonov = getattr(configs, 'disable_tikhonov', False)
     self.random_guidance = getattr(configs, 'random_guidance', False)
     self.use_transfer = getattr(configs, 'use_transfer', False)
     self.w = None
     self.b = None
     self.stacking_method = method.NadarayaWatsonMethod(configs)
     self.trained_stacked_methods = list()
     self.cvx_method = getattr(configs, 'cvx_method', 'SCS')
     self.num_features = getattr(configs, 'num_features', -1)
     self.use_l1 = getattr(configs, 'use_l1', False)
     self.solve_dual = getattr(configs, 'solve_dual', False)
     self.mean_b = getattr(configs, 'mean_b', False)
     self.solve_scipy = getattr(configs, 'solve_scipy', False)
     self.use_pairwise_same_signs = getattr(configs,
                                            'use_pairwise_same_signs',
                                            False)
     self.use_hinge_primal = getattr(configs, 'use_hinge_primal', False)
     self.fix_C2 = getattr(configs, 'fix_C2', False)
     self.fix_C3 = getattr(configs, 'fix_C3', False)
     if self.method == MixedFeatureGuidanceMethod.METHOD_HARD_CONSTRAINT:
         self.configs.scipy_opt_method = 'SLSQP'
     if self.method in MixedFeatureGuidanceMethod.METHODS_UNIFORM_C:
         self.C = 1
         del self.cv_params['C']
     if (self.method in MixedFeatureGuidanceMethod.METHODS_NO_C3
             and not self.use_hinge_primal) or self.fix_C3:
         self.C3 = 1
         del self.cv_params['C3']
     if self.method in MixedFeatureGuidanceMethod.METHODS_NO_C2 or self.fix_C2:
         self.C2 = 1
         del self.cv_params['C2']
     if self.disable_relaxed_guidance:
         self.C2 = np.inf
         if 'C2' in self.cv_params:
             del self.cv_params['C2']
     if self.disable_tikhonov:
         self.C = 0
         if 'C' in self.cv_params:
             del self.cv_params['C']
     self.quiet = False
     self.pairs = None
     self.feats_to_constrain = None
     self.learner_lasso = Lasso()
Ejemplo n.º 18
0
 def __init__(self, configs=MethodConfigs()):
     super(TargetTranfer, self).__init__(configs)
     self.base_learner = method.SKLLogisticRegression(configs)
     self.cv_params = {}
     self.base_learner.experiment_results_class = self.experiment_results_class
Ejemplo n.º 19
0
 def __init__(self, configs=MethodConfigs()):
     super(ClusterActiveMethod, self).__init__(configs)
     self.transform = StandardScaler()
     self.use_labeled = True
     self.cluster_scale = 10
Ejemplo n.º 20
0
 def __init__(self, configs=MethodConfigs()):
     super(ModelSelectionTransfer, self).__init__(configs)
     self.methods.append(TargetTranfer(configs))
     self.methods.append(FuseTransfer(configs))
     for m in self.methods:
         m.base_learner = method.NadarayaWatsonMethod(configs)
Ejemplo n.º 21
0
 def __init__(self, configs=MethodConfigs()):
     super(ActiveMethod, self).__init__(configs)
     self.base_learner = method.SKLRidgeRegression(configs)
     self.fix_model = False
Ejemplo n.º 22
0
 def __init__(self, configs=MethodConfigs()):
     super(FuseTransfer, self).__init__(configs)
     self.use_oracle = False
     #self.target_weight_scale = None
     self.target_weight_scale = .75
     self.label_transform = NanLabelBinarizer()
Ejemplo n.º 23
0
def vis_data():
    s = '../data_sets/' + data_file_dir + '/raw_data.pkl'
    data = helper_functions.load_object(s)
    x = data.x
    y = data.y
    c = MethodConfigs()
    x_mat = vec_to_matrix(x, y)
    #self.set_data_set_defaults('taxi3', source_labels=[1], target_labels=[0], is_regression=True)
    c.source_labels = np.asarray([1])
    c.target_labels = np.asarray([0])
    c.use_validation = True
    I_target = (c.target_labels[0] == data.data_set_ids).nonzero()[0]
    I_to_use = np.random.choice(I_target, 40, replace=False)
    data.y[I_target] = np.nan
    data.y[I_to_use] = data.true_y[I_to_use]
    learner = local_transfer_methods.LocalTransferDeltaNew(c)
    v = 1
    learner.cv_params['sigma_target'] = learner.create_cv_params(-v, v)
    learner.cv_params['sigma_b'] = learner.create_cv_params(-v, v)
    learner.cv_params['sigma_alpha'] = learner.create_cv_params(-v, v)
    #learner.transform = None
    output = learner.train_and_test(data).prediction

    fig = plt.figure(0)
    plt.title('TODO')
    plt.axis('off')

    I_target = data.get_transfer_inds(c.target_labels)
    vals_to_plot = [
        np.abs(output.ft - output.true_y)**1,
        np.abs(output.y_s + output.b - output.true_y)**1,
        output.alpha,
        np.abs(output.y - output.true_y)**1,
    ]
    titles = [
        'Target Function \nError',
        'Adapted Source \nFunction Error',
        'Mixture Function \n',
        'Final Prediction \nError',
    ]
    min_error = min([vals_to_plot[i].min() for i in [0, 1, 3]])
    max_error = max([vals_to_plot[i].max() for i in [0, 1, 3]])
    print output.b
    print output.alpha
    for i, vals in enumerate(vals_to_plot):
        ax = plt.subplot(1, len(vals_to_plot), i + 1)

        ax.set_title(titles[i], fontsize=15)
        #array_functions.plot_heatmap(data.x[I_target], vals, fig=fig, make_subplot=False, sizes=20)
        #vals -= min_error
        #vals /= max_error
        vals -= vals.min()
        vals /= vals.max()
        vals_reshaped = np.reshape(vals, (40, 40))
        plt.pcolormesh(vals_reshaped, cmap=cm.gray, shading='flat', norm=None)
        ax.set_xlabel('Latitude')
        if i == 0:
            ax.set_ylabel('Longitude')
        else:
            ax.set_ylabel('')
        plt.xticks([], [])
        plt.yticks([], [])
    array_functions.move_fig(fig, 1200, 400)
    #plt.tight_layout(pad=0, h_pad=0, w_pad=0)
    #plt.wsp
    plt.subplots_adjust(left=0.05, right=0.95, top=0.8, bottom=0.1)
    plt.show(block=True)
    print ''