示例#1
0
    def test_hyper_dt(self):
        rs = RandomSearcher(mini_dt_space, optimize_direction=OptimizeDirection.Maximize, )
        hdt = HyperDT(rs,
                      callbacks=[SummaryCallback()],
                      reward_metric='accuracy',
                      dnn_params={
                          'hidden_units': ((256, 0, False), (256, 0, False)),
                          'dnn_activation': 'relu',
                      },
                      cache_preprocessed_data=True,
                      cache_home=homedir + '/cache'
                      )
        x1 = np.random.randint(0, 10, size=(100), dtype='int')
        x2 = np.random.randint(0, 2, size=(100)).astype('str')
        x3 = np.random.randint(0, 2, size=(100)).astype('str')
        x4 = np.random.normal(0.0, 1.0, size=(100))

        y = np.random.randint(0, 2, size=(100), dtype='int')
        df = pd.DataFrame({'x1': x1, 'x2': x2, 'x3': x3, 'x4': x4})
        hdt.search(df, y, df, y, max_trials=3, epochs=1)
        best_trial = hdt.get_best_trial()
        model = hdt.load_estimator(best_trial.model_file)
        assert model
        score = model.predict(df)
        result = model.evaluate(df, y)
        assert len(score) == 100
        assert result
        assert isinstance(model, DeepTable)

        estimator = hdt.final_train(best_trial.space_sample, df, y, epochs=1)
        score = estimator.predict(df)
        result = estimator.evaluate(df, y)
        assert len(score) == 100
        assert result
        assert isinstance(estimator.model, DeepTable)
示例#2
0
    def train(self, X, y, X_test):
        searcher = EvolutionSearcher(
            mini_dt_space,
            optimize_direction=OptimizeDirection.Maximize,
            population_size=30,
            sample_size=10,
            regularized=True,
            candidates_size=10)
        es = EarlyStoppingCallback(self.earlystop_rounds,
                                   'max',
                                   time_limit=self.time_limit,
                                   expected_reward=self.expected_reward)

        hdt = HyperDT(
            searcher,
            callbacks=[es],
            reward_metric=self.reward_metric,
            cache_preprocessed_data=True,
        )
        stratify = y
        if self.task == 'regression':
            stratify = None
        X_train, X_eval, y_train, y_eval = train_test_split(X,
                                                            y,
                                                            test_size=0.3,
                                                            random_state=9527,
                                                            stratify=stratify)

        hdt.search(X_train,
                   y_train,
                   X_eval,
                   y_eval,
                   max_trials=self.max_trials,
                   epochs=self.epochs)
        best_trial = hdt.get_best_trial()
        self.estimator = hdt.load_estimator(best_trial.model_file)