Exemplo n.º 1
0
Arquivo: worker.py Projeto: csala/ATM
    def load_selector(self):
        """
        Load and initialize the BTB class which will be responsible for
        selecting hyperpartitions.
        """
        # selector will either be a key into SELECTORS_MAP or a path to
        # a file that defines a class called CustomSelector.
        if self.datarun.selector in SELECTORS_MAP:
            Selector = SELECTORS_MAP[self.datarun.selector]
        else:
            path, classname = re.match(CUSTOM_CLASS_REGEX,
                                       self.datarun.selector).groups()
            mod = imp.load_source('btb.selection.custom', path)
            Selector = getattr(mod, classname)

        logger.info('Selector: %s' % Selector)

        # generate the arguments we need to initialize the selector
        hyperpartitions = self.db.get_hyperpartitions(
            datarun_id=self.datarun.id)
        hp_by_method = defaultdict(list)
        for hp in hyperpartitions:
            hp_by_method[hp.method].append(hp.id)

        hyperpartition_ids = [hp.id for hp in hyperpartitions]

        # Selector classes support passing in redundant arguments
        self.selector = get_instance(Selector,
                                     choices=hyperpartition_ids,
                                     k=self.datarun.k_window,
                                     by_algorithm=dict(hp_by_method))
Exemplo n.º 2
0
def test_make_selector():
    kwargs = {
        'choices': [1, 2, 3],
        'k': 3,
        'by_algorithm': {'svm': [1, 2], 'rf': [3, 4]}
    }

    for selector_class in SELECTORS.values():
        selector = utilities.get_instance(selector_class, **kwargs)
        assert isinstance(selector, Selector)
Exemplo n.º 3
0
Arquivo: worker.py Projeto: csala/ATM
    def tune_hyperparameters(self, hyperpartition):
        """
        Use the hyperparameter tuning method specified by our datarun to choose
        a set of hyperparameters from the potential space.
        """
        # Get parameter metadata for this hyperpartition
        tunables = hyperpartition.tunables

        # If there aren't any tunable parameters, we're done. Return the vector
        # of values in the hyperpartition and mark the set as finished.
        if not len(tunables):
            logger.warning('No tunables for hyperpartition %d' %
                           hyperpartition.id)
            self.db.mark_hyperpartition_gridding_done(hyperpartition.id)
            return update_params(params=[],
                                 tunables=tunables,
                                 categoricals=hyperpartition.categoricals,
                                 constants=hyperpartition.constants)

        # Get previously-used parameters: every classifier should either be
        # completed or have thrown an error
        all_clfs = self.db.get_classifiers(hyperpartition_id=hyperpartition.id)
        classifiers = [
            c for c in all_clfs if c.status == ClassifierStatus.COMPLETE
        ]

        X = [c.hyperparameter_values for c in classifiers]
        y = np.array([
            float(getattr(c, self.datarun.score_target)) for c in classifiers
        ])

        # Initialize the tuner and propose a new set of parameters
        # this has to be initialized with information from the hyperpartition, so we
        # need to do it fresh for each classifier (not in load_tuner)
        tuner = get_instance(self.Tuner,
                             tunables=tunables,
                             gridding=self.datarun.gridding,
                             r_minimum=self.datarun.r_minimum)
        if len(X) > 0:
            tuner.add(X, y)

        params = tuner.propose()
        if params is None and self.datarun.gridding:
            logger.info('Gridding done for hyperpartition %d' %
                        hyperpartition.id)
            self.db.mark_hyperpartition_gridding_done(hyperpartition.id)
            return None

        # Append categorical and constants to the params.
        return update_params(params=params,
                             categoricals=hyperpartition.categoricals,
                             constants=hyperpartition.constants)