Exemplo n.º 1
0
    def get_space_with_dynamic(self):

        space = HyperSpace()
        with space.as_default():
            input1 = HyperInput()
            input2 = HyperInput()

            id1 = Identity(p1=Choice([1, 2]), p2=Int(1, 100))
            id2 = Identity(p3=Real(0, 1))
            id3 = Identity(p4=Dynamic(lambda p5: p5 * 3, p5=Choice([2, 4, 8])))

            def new_module(m):
                dm1 = Identity(dp1=Choice(['a', 'b']))
                return dm1, dm1

            id4 = ConnectionSpace(dynamic_fn=new_module, p6=Choice(['f', 'g']))
            id5 = Identity()
            id6 = Identity()
            id7 = Identity()

            id1([input1, id7])
            id2([input1, input2])
            id3([input2, id1, id2])
            id4([input1, id2])
            id5(input2)
            id6(id3)
            id7([id4, id5])
        return space
Exemplo n.º 2
0
    def test_basic_ops(self):
        with HyperSpace().as_default() as space:
            id1 = HyperInput()
            id2 = HyperInput()
            id3 = Identity()
            id3([id1, id2])
            id4 = Identity()
            id4(id3)
            id5 = Identity()
            id5(id3)

            graph_inputs = space.get_inputs()
            id3_inputs = space.get_inputs(id3)
            id4_inputs = space.get_inputs(id4)

            assert graph_inputs == [id1, id2]
            assert id3_inputs == [id1, id2]
            assert id4_inputs == [id3]

            graph_outputs = space.get_outputs()
            id1_outputs = space.get_outputs(id1)
            id3_outputs = space.get_outputs(id3)

            assert graph_outputs == [id4, id5]
            assert id1_outputs == [id3]
            assert id3_outputs == [id4, id5]

            space.disconnect(id3, id5)
            id3_outputs = space.get_outputs(id3)
            assert id3_outputs == [id4]

            assert len(space.modules) == 5
            assert len(space.edges) == 3
Exemplo n.º 3
0
def get_space_numeric_pipeline_complex():
    space = HyperSpace()
    with space.as_default():
        input = HyperInput(name='input1')
        p1 = numeric_pipeline_complex()(input)
        p3 = DataFrameMapper(input_df=True, df_out=True)([p1])  # passthrough
        est = LightGBMEstimator(task='binary', fit_kwargs={})(p3)
        space.set_inputs(input)
    return space
Exemplo n.º 4
0
def get_space_num_cat_pipeline(default=False):
    space = HyperSpace()
    with space.as_default():
        input = HyperInput(name='input1')
        p1 = numeric_pipeline_simple()(input)
        p2 = categorical_pipeline_simple()(input)
        p3 = DataFrameMapper(default=default, input_df=True,
                             df_out=True)([p1, p2])  # passthrough
        est = LightGBMEstimator(task='binary', fit_kwargs={})(p3)
        space.set_inputs(input)
    return space
Exemplo n.º 5
0
    def get_space(self):
        space = HyperSpace()
        with space.as_default():
            input1 = HyperInput()
            input2 = HyperInput()

            id1 = Identity(p1=Choice([1, 2]), p2=Int(1, 100))
            id2 = Identity(p3=Real(0, 1))
            id3 = Identity(p4=Dynamic(lambda p5: p5 * 3, p5=Choice([2, 4, 8])))
            id4 = Identity()
            id5 = Identity()
            id6 = Identity()
            id7 = Identity()

            id1([input1, id7])
            id2([input1, input2])
            id3([input2, id1, id2])
            id4([input1, id2])
            id5(input2)
            id6(id3)
            id7([id4, id5])
        return space
Exemplo n.º 6
0
def func_space(func):
    space = HyperSpace()
    with space.as_default():
        params = {
            name: copy.copy(v)
            for name, v in zip(func.__code__.co_varnames, func.__defaults__)
            if isinstance(v, ParameterSpace)
        }
        for _, v in params.items():
            v.attach_to_space(space, v.name)
        input = HyperInput()
        id1 = Identity(**params)(input)
        space.set_outputs([id1])
    return space
Exemplo n.º 7
0
def get_space_p_in_p():
    space = HyperSpace()
    with space.as_default():
        p1 = Pipeline(
            [SimpleImputer(name='imputer1'),
             StandardScaler(name='scaler1')],
            name='p1')
        p2 = Pipeline(
            [SimpleImputer(name='imputer2'),
             StandardScaler(name='scaler2')],
            name='p2')
        input = HyperInput(name='input1')
        p3 = Pipeline([p1, p2], name='p3')(input)
        space.set_inputs(input)
    return space
Exemplo n.º 8
0
def get_space_num_cat_pipeline_complex(dataframe_mapper_default=False,
                                       lightgbm_fit_kwargs={},
                                       xgb_fit_kwargs={},
                                       catboost_fit_kwargs={}):
    space = HyperSpace()
    with space.as_default():
        input = HyperInput(name='input1')
        p1 = numeric_pipeline_complex()(input)
        p2 = categorical_pipeline_complex()(input)
        # p2 = categorical_pipeline_simple()(input)
        p3 = DataFrameMapper(default=dataframe_mapper_default,
                             input_df=True,
                             df_out=True,
                             df_out_dtype_transforms=[(column_object, 'int')
                                                      ])([p1, p2])

        lightgbm_init_kwargs = {
            'boosting_type': Choice(['gbdt', 'dart', 'goss']),
            'num_leaves': Choice([11, 31, 101, 301, 501]),
            'learning_rate': Real(0.001, 0.1, step=0.005),
            'n_estimators': 100,
            'max_depth': -1,
            'tree_learner': 'data'  # add for dask
            # subsample_for_bin = 200000, objective = None, class_weight = None,
            #  min_split_gain = 0., min_child_weight = 1e-3, min_child_samples = 20,
        }
        lightgbm_est = LightGBMDaskEstimator(task='binary',
                                             fit_kwargs=lightgbm_fit_kwargs,
                                             **lightgbm_init_kwargs)

        xgb_init_kwargs = {
            'tree_method': 'approx'  # add for dask
        }
        xgb_est = XGBoostDaskEstimator(task='binary',
                                       fit_kwargs=xgb_fit_kwargs,
                                       **xgb_init_kwargs)

        # catboost_init_kwargs = {
        #     'silent': True
        # }
        # catboost_est = CatBoostEstimator(task='binary', fit_kwargs=catboost_fit_kwargs, **catboost_init_kwargs)
        # or_est = ModuleChoice([lightgbm_est, xgb_est, catboost_est], name='estimator_options')(p3)

        or_est = ModuleChoice([lightgbm_est, xgb_est],
                              name='estimator_options')(p3)

        space.set_inputs(input)
    return space
Exemplo n.º 9
0
def get_space_column_transformer():
    space = HyperSpace()
    with space.as_default():
        input = HyperInput(name='input1')
        p1 = Pipeline(
            [SimpleImputer(name='imputer1'),
             StandardScaler(name='scaler1')],
            columns=['a', 'b', 'c'],
            name='p1')(input)
        p2 = Pipeline(
            [SimpleImputer(name='imputer2'),
             StandardScaler(name='scaler2')],
            columns=['c', 'd'],
            name='p2')(input)
        p3 = ColumnTransformer()([p1, p2])
        space.set_inputs(input)
    return space
Exemplo n.º 10
0
def get_space_num_cat_pipeline_multi_complex(dataframe_mapper_default=False,
                                             lightgbm_fit_kwargs={},
                                             xgb_fit_kwargs={}):
    space = HyperSpace()
    with space.as_default():
        input = HyperInput(name='input1')
        p1 = numeric_pipeline_complex()(input)
        p2 = categorical_pipeline_complex()(input)
        p3 = DataFrameMapper(default=dataframe_mapper_default,
                             input_df=True,
                             df_out=True,
                             df_out_dtype_transforms=[(column_object,
                                                       'category')])([p1, p2])

        p4 = numeric_pipeline_complex(seq_no=1)(p3)
        p5 = categorical_pipeline_complex(seq_no=1)(p3)
        p6 = DataFrameMapper(default=dataframe_mapper_default,
                             input_df=True,
                             df_out=True,
                             df_out_dtype_transforms=[(column_object,
                                                       'category')])([p4, p5])

        lightgbm_init_kwargs = {
            'boosting_type': Choice(['gbdt', 'dart', 'goss']),
            'num_leaves': Choice([11, 31, 101, 301, 501]),
            'learning_rate': Real(0.001, 0.1, step=0.005),
            'n_estimators': 100,
            'max_depth': -1,
            # subsample_for_bin = 200000, objective = None, class_weight = None,
            #  min_split_gain = 0., min_child_weight = 1e-3, min_child_samples = 20,
        }

        lightgbm_est = LightGBMEstimator(task='binary',
                                         fit_kwargs=lightgbm_fit_kwargs,
                                         **lightgbm_init_kwargs)

        xgb_init_kwargs = {}
        xgb_est = XGBoostEstimator(task='binary',
                                   fit_kwargs=xgb_fit_kwargs,
                                   **xgb_init_kwargs)

        or_est = ModuleChoice([lightgbm_est, xgb_est])(p6)
        space.set_inputs(input)
    return space
Exemplo n.º 11
0
    def __call__(self, *args, **kwargs):
        space = HyperSpace()

        with space.as_default():
            hyper_input = HyperInput(name='input1')

            estimators = []
            if self.enable_dt:
                estimators.append(self.dt)
            if self.enable_dtr:
                estimators.append(self.dtr)
            if self.enable_lr:
                estimators.append(self.lr)
            if self.enable_nn:
                estimators.append(self.nn)

            modules = [ModuleSpace(name=f'{e["cls"].__name__}', **e) for e in estimators]
            outputs = ModuleChoice(modules)(hyper_input)
            space.set_inputs(hyper_input)

        return space