def predict(self, X): check_is_fitted(self, 'base_model_') if not self.is_partial: return self.base_model_.predict(X) else: multi_class = self.n_classes_ > 2 model_ = gbm_gen(self.partial_param_, X, self.base_param_[2], self.base_param_[1], multi_class, self.n_classes_) preds = model_(self.partial_param_, X) if self.n_classes_ > 2: return np.argmax(preds, axis=1) else: return np.round(sigmoid(preds))
def predict_proba(self, X): check_is_fitted(self, 'base_model_') if not self.is_partial: return self.base_model_.predict_proba(X) else: multi_class = self.n_classes_ > 2 model_ = gbm_gen(self.partial_param_, X, self.base_param_[2], self.base_param_[1], multi_class, self.n_classes_) preds = model_(self.partial_param_, X) if self.n_classes_ > 2: return preds else: pred_positive = sigmoid(preds) return np.stack([1 - pred_positive, pred_positive], axis=-1)
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