active_learning.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:Steal-ML 作者: ftramer 项目源码 文件源码
def do(self):
        # get some initial points
        self.ex.collect_up_to_budget(self.budget_per_round * 2)
        x, y = self.ex.pts_near_b, self.ex.pts_near_b_labels

        if len(np.unique(y)) < 2:
            return 1, 1

        # gamma_range = np.logspace(-5, 1, 10, base=10)
        # param_grid = dict(gamma=gamma_range)

        try:
            # cv = StratifiedShuffleSplit(y, n_iter=5, test_size=.2)
            # grid = GridSearchCV(svm.SVC(C=1e5), param_grid=param_grid, cv=cv, n_jobs=-1)
            # grid.fit(x, y)
            # h_best = grid.best_estimator_
            raise ValueError
        except ValueError:
            h_best = svm.SVC(C=1e5)
            h_best.fit(x, y)

        for i in range(1, self.n_rounds - 1):
            online_ = OnlineBase('', +1, self.NEG, h_best.predict, self.n_features, 'uniform', error=.1)
            x_, _ = online_.collect_pts(self.budget_per_round, 50000)  # budget doesn't matter

            xx_ = None
            if x_ is None or len(x_) < self.budget_per_round:
                print('Run out of budget when getting x_')
                xx_ = np.random.uniform(-1, 1, (self.budget_per_round - len(x_), self.n_features))

            if x_ is not None and len(x_) > 0:
                x.extend(x_)
                y.extend(self.oracle(x_))

            if xx_ is not None:
                x.extend(xx_)
                y.extend(self.oracle(xx_))

            try:
                # cv = StratifiedShuffleSplit(y, n_iter=5, test_size=.2)
                # grid = GridSearchCV(svm.SVC(C=1e5), param_grid=param_grid, cv=cv, n_jobs=-1)
                # grid.fit(x, y)
                # h_best = grid.best_estimator_
                raise ValueError
            except ValueError:
                h_best = svm.SVC(C=1e5)
                h_best.fit(x, y)

            # h_best.fit(x, y)

        self.set_clf2(h_best)
        return self.benchmark() # (ex.batch_predict, h_.predict, test_x, n_features)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号