Beispiel #1
0
    def partial_fit_base(self, X, y):
        check_is_fitted(self, "base_model_")

        batch_indices = generate_batch(
            X, self.autograd_config.get("batch_size", 32))

        esp = 1e-11  # where should this live?
        step_size = self.autograd_config.get("step_size", 0.05)
        callback = (None if self.autograd_config.get("verbose", False) else
                    simple_callback)
        num_iters = self.autograd_config.get("num_iters", 1000)

        nclass = self.n_classes_
        model_dump = self.base_model_.booster_.dump_model()
        trees_ = [m["tree_structure"] for m in model_dump["tree_info"]]

        trees_params = multi_tree_to_param(X, y, trees_)
        model_ = gbm_gen(trees_params[0], X, trees_params[2], trees_params[1],
                         False, 2)

        def training_loss(weights, idx=0):
            # Training loss is the negative log-likelihood of the training labels.
            t_idx_ = batch_indices(idx)
            preds = sigmoid(model_(weights, X[t_idx_, :]))
            label_probabilities = preds * y[t_idx_] + (1 - preds) * (1 -
                                                                     y[t_idx_])
            # print(label_probabilities)
            loglik = -np.sum(np.log(label_probabilities))

            num_unpack = 3
            reg = 0
            # reg_l1 = np.sum(np.abs(flattened)) * 1.
            for idx_ in range(0, len(weights), num_unpack):
                param_temp_ = weights[idx_:idx_ + num_unpack]
                flattened, _ = weights_flatten(param_temp_[:2])
                reg_l1 = np.sum(np.abs(flattened)) * 1.0
                reg += reg_l1
            return loglik + reg

        training_gradient_fun = grad(training_loss)
        param_ = adam(
            training_gradient_fun,
            trees_params[0],
            callback=callback,
            step_size=step_size,
            num_iters=num_iters,
        )

        self.base_param_ = copy.deepcopy(trees_params)
        self.partial_param_ = param_
        self.is_partial = True
        return self
Beispiel #2
0
    def partial_fit_param(self, X, y):
        check_is_fitted(self, "base_model_")
        check_is_fitted(self, "base_param_")
        check_is_fitted(self, "partial_param_")

        batch_indices = generate_batch(
            X, self.autograd_config.get("batch_size", 32))

        esp = 1e-11  # where should this live?
        step_size = self.autograd_config.get("step_size", 0.05)
        callback = (None if self.autograd_config.get("verbose", False) else
                    simple_callback)
        num_iters = self.autograd_config.get("num_iters", 1000)
        nclass = self.n_classes_

        if nclass == 2:
            y_ohe = y
        else:
            y_ohe = LabelBinarizer().fit_transform(y)

        if nclass > 2:
            model_ = gbm_gen(
                self.base_param_[0],
                X,
                self.base_param_[2],
                self.base_param_[1],
                True,
                nclass,
            )

            def training_loss(weights, idx=0):
                # Training loss is the negative log-likelihood of the training labels.
                t_idx_ = batch_indices(idx)
                preds = model_(weights, X[t_idx_, :])
                loglik = -np.sum(np.log(preds + esp) * y_ohe[t_idx_, :])

                num_unpack = 3
                reg = 0
                # reg_l1 = np.sum(np.abs(flattened)) * 1.
                for idx_ in range(0, len(weights), num_unpack):
                    param_temp_ = weights[idx_:idx_ + num_unpack]
                    flattened, _ = weights_flatten(param_temp_[:2])
                    reg_l1 = np.sum(np.abs(flattened)) * 1.0
                    reg += reg_l1
                return loglik + reg

        else:
            model_ = gbm_gen(
                self.base_param_[0],
                X,
                self.base_param_[2],
                self.base_param_[1],
                False,
                2,
            )

            def training_loss(weights, idx=0):
                # Training loss is the negative log-likelihood of the training labels.
                t_idx_ = batch_indices(idx)
                preds = sigmoid(model_(weights, X[t_idx_, :]))
                label_probabilities = preds * y[t_idx_] + (1 - preds) * (
                    1 - y[t_idx_])
                # print(label_probabilities)
                loglik = -np.sum(np.log(label_probabilities))

                num_unpack = 3
                reg = 0
                # reg_l1 = np.sum(np.abs(flattened)) * 1.
                for idx_ in range(0, len(weights), num_unpack):
                    param_temp_ = weights[idx_:idx_ + num_unpack]
                    flattened, _ = weights_flatten(param_temp_[:2])
                    reg_l1 = np.sum(np.abs(flattened)) * 1.0
                    reg += reg_l1
                return loglik + reg

        training_gradient_fun = grad(training_loss)
        param_ = adam(
            training_gradient_fun,
            self.partial_param_,
            callback=callback,
            step_size=step_size,
            num_iters=num_iters,
        )

        self.partial_param_ = param_
        self.is_partial = True
        return self