Ejemplo n.º 1
0
    def check_jthunder(self, clf: thundersvm.SVC):
        self.assertTrue(
            np.allclose(
                clf.decision_function(self.ds_test.xs).flatten(),
                -jthunder.decision_function(clf, self.jtest_xs),
                rtol=1e-4,
                atol=1e-4,
            ))

        self.assertTrue(
            all(
                clf.predict(self.ds_test.xs).astype(np.int) ==
                jthunder.predict(clf, self.jtest_xs)))

        self.assertTrue(
            np.allclose(jthunder.norm2(clf), jthunder.norm2_naive(clf)))
Ejemplo n.º 2
0
    def run(self):

        model = self.exp_data['model']
        base_workspace = self.exp_data['base_workspace']

        if model == 'ridge':
            space = [Real(1e-3, 1e+3, prior='log-uniform')]
            optimize_types = ['alpha']
            minimizer = MagnitudeAxisMinimizer(base_workspace, optimize_types,
                                               gp_minimize)
            minimizer.model = Ridge
            x0 = [1.0]
        elif model == 'kernel_ridge':
            space = [
                Categorical(['poly', 'rbf', 'sigmoid']),
                Real(1e-3, 1e+3, prior='log-uniform'),
                Integer(1, 8),
                Real(1e-6, 1e+1, prior='log-uniform'),
                Real(-10, 10)
            ]
            optimize_types = ['kernel', 'alpha', 'degree', 'gamma', 'coef0']
            minimizer = MagnitudeAxisMinimizer(base_workspace, optimize_types,
                                               gp_minimize)
            minimizer.model = KernelRidge
            x0 = ['poly', 1.0, 3, 1 / 300, 0]
        elif model == 'kernel_ridge_separation':
            space = [Real(1e-3, 1e+3, prior='log-uniform')]
            optimize_types = ['alpha']
            minimizer = MagnitudeAxisMinimizer(base_workspace, optimize_types,
                                               gp_minimize)
            minimizer.model_fixed_params = {
                'kernel': 'poly',
                'degree': 3,
                'gamma': 1 / 300,
                'coef0': 0
            }
            minimizer.model = KernelRidge
            x0 = [1.0]
        elif model == 'kernel_ridge_random':
            space = [Real(1e-3, 1e+3, prior='log-uniform')]
            optimize_types = ['alpha']
            minimizer = MagnitudeAxisMinimizer(base_workspace, optimize_types,
                                               gp_minimize)
            kernel = random.choice(
                ['poly', 'rbf', 'laplacian', 'sigmoid', 'cosine'])
            degree = random.randint(1, 10)
            coef = random.uniform(-5, 5)
            minimizer.model_fixed_params = {
                'kernel': kernel,
                'degree': degree,
                'gamma': None,
                'coef0': coef
            }
            minimizer.model = KernelRidge
            x0 = [1.0]
        elif model == 'nn':
            space = [
                Integer(16, 256),
                Real(1e-5, 1, prior='log-uniform'),
            ]
            optimize_types = ['n_hidden_units', 'lr']
            minimizer = MagnitudeAxisMinimizer(base_workspace, optimize_types,
                                               gp_minimize)
            minimizer.model_fixed_params = {
                'build_fn': self.build_nn,
                'epochs': 20,
                'batch_size': 256
            }
            minimizer.model = KerasRegressor
            x0 = [64, 0.001]
        elif model == 'kernel_svm':
            space = [
                Categorical(['poly', 'rbf', 'sigmoid']),
                Real(1e-3, 1e+3, prior='log-uniform'),
                Integer(1, 8),
                Real(1e-6, 1e+1, prior='log-uniform'),
                Real(-10, 10)
            ]
            optimize_types = ['kernel', 'C', 'degree', 'gamma', 'coef0']
            minimizer = MagnitudeAxisMinimizer(base_workspace, optimize_types,
                                               gp_minimize)
            minimizer.model_fixed_params = {
                'cache_size': 8000,
                'max_iter': 10000
            }
            minimizer.model = SVR
            x0 = ['poly', 1.0, 3, 1 / 300, 0]
        elif model == 'pca':
            if not hasattr(self, 'pca_component'):
                pca = PCA(n_components=1)
                _, X_nums, *_ = prepare_separation_data(
                    self.name.split('_')[0] + '.txt')
                pca.fit(X_nums)
                # pca.fit(np.concatenate([base_workspace['X'],base_workspace['X_test']]))
                self.pca_component = pca.components_  # 1xd
            error = self.evaluate_w(base_workspace['X_test'],
                                    self.pca_component,
                                    base_workspace['y_test'])
            return error
        elif model == 'proj_pca':
            if not hasattr(self, 'proj_pca_component'):
                X, X_nums, y_label, _ = prepare_separation_data(
                    self.name.split('_')[0] + '.txt')
                svc = SVC(kernel='linear',
                          degree=3,
                          gamma=1 / 300,
                          coef0=0,
                          C=1,
                          cache_size=4000,
                          class_weight='balanced',
                          verbose=True)

                svc.fit(X, y_label)
                beta = svc.coef_  # 1xd
                # X_pred = svc.decision_function(X) nx1
                # X_nums = np.concatenate([base_workspace['X'],base_workspace['X_test']])
                X_proj = X_nums - (
                    (svc.decision_function(X_nums) / beta @ beta.T) @ beta
                )  # nxd

                pca = PCA(n_components=1)
                pca.fit(X_proj)
                self.proj_pca_component = pca.components_  # 1xd
            error = self.evaluate_w(base_workspace['X_test'],
                                    self.proj_pca_component,
                                    base_workspace['y_test'])
            return error
        elif model == 'kernel_proj_pca':
            pass
        else:
            assert False

        res = minimizer.minimize(space, n_calls=40, verbose=True, x0=x0)
        if self.save_results:
            skopt.dump(res, self.name + '.pkl', store_objective=False)

        params = {type: v for type, v in zip(minimizer.optimize_types, res.x)}
        if hasattr(minimizer, 'model_fixed_params'):
            params = {**params, **minimizer.model_fixed_params}
        error = self.fit_test_best_model(minimizer.model, base_workspace['X'],
                                         base_workspace['y'],
                                         base_workspace['X_test'],
                                         base_workspace['y_test'], **params)
        return error