Example #1
0
    def predict(self, data_instances):

        self._abnormal_detection(data_instances)
        self.init_schema(data_instances)

        data_instances = self.align_data_header(data_instances, self.header)

        LOGGER.info("Start predict is a one_vs_rest task: {}".format(
            self.need_one_vs_rest))
        if self.need_one_vs_rest:
            predict_result = self.one_vs_rest_obj.predict(data_instances)
            return predict_result

        # predict_wx = self.compute_wx(data_instances, self.model_weights.coef_, self.model_weights.intercept_)
        pred_prob = data_instances.mapValues(lambda v: activation.sigmoid(
            vec_dot(v.features, self.model_weights.coef_) + self.model_weights.
            intercept_))

        predict_result = self.predict_score_to_output(
            data_instances,
            pred_prob,
            classes=[0, 1],
            threshold=self.model_param.predict_param.threshold)

        return predict_result
Example #2
0
    def predict(self, data=None, predict_param=None):
        # synchronize encryption information
        if not self.has_sychronized_encryption:
            self.__synchronize_encryption()
            self.__send_host_mode()

        for idx, use_encrypt in enumerate(self.host_use_encryption):
            if use_encrypt:
                encrypter = self.host_encrypter[idx]
                predict_wx_id = self.transfer_variable.generate_transferid(
                    self.transfer_variable.predict_wx)
                predict_wx = federation.get(
                    name=self.transfer_variable.predict_wx.name,
                    tag=predict_wx_id,
                    idx=idx)
                decrypted_wx = encrypter.distribute_decrypt(predict_wx)
                pred_prob = decrypted_wx.mapValues(
                    lambda x: activation.sigmoid(x))
                pred_label = self.classified(pred_prob,
                                             predict_param.threshold)
                predict_result_id = self.transfer_variable.generate_transferid(
                    self.transfer_variable.predict_result)
                federation.remote(
                    pred_label,
                    name=self.transfer_variable.predict_result.name,
                    tag=predict_result_id,
                    role=consts.HOST,
                    idx=idx)
        LOGGER.info("Finish predicting, result has been sent back")
        return
Example #3
0
    def predict(self, data_instances, predict_param):
        LOGGER.info("Start predict ...")
        prob_guest = self.compute_wx(data_instances, self.coef_,
                                     self.intercept_)
        prob_host = federation.get(
            name=self.transfer_variable.host_prob.name,
            tag=self.transfer_variable.generate_transferid(
                self.transfer_variable.host_prob),
            idx=0)
        LOGGER.info("Get probability from Host")

        # guest probability
        pred_prob = prob_guest.join(prob_host,
                                    lambda g, h: activation.sigmoid(g + h))
        pred_label = self.classified(pred_prob, predict_param.threshold)
        if predict_param.with_proba:
            labels = data_instances.mapValues(lambda v: v.label)
            predict_result = labels.join(pred_prob, lambda label, prob:
                                         (label, prob))
        else:
            predict_result = data_instances.mapValues(lambda v:
                                                      (v.label, None))

        predict_result = predict_result.join(pred_label, lambda r, p:
                                             (r[0], r[1], p))
        return predict_result
Example #4
0
    def predict(self, data_instances, predict_param):
        if not self.has_sychronized_encryption:
            self.__synchronize_encryption()
            self.__load_arbiter_model()
        else:
            LOGGER.info("in predict, has synchronize encryption information")

        wx = self.compute_wx(data_instances, self.coef_, self.intercept_)

        if self.use_encrypt:
            encrypted_wx_id = self.transfer_variable.generate_transferid(self.transfer_variable.predict_wx)
            # LOGGER.debug("predict_wd_id: {}".format(encrypted_wx_id))
            federation.remote(wx,
                              name=self.transfer_variable.predict_wx.name,
                              tag=encrypted_wx_id,
                              role=consts.ARBITER,
                              idx=0)
            predict_result_id = self.transfer_variable.generate_transferid(self.transfer_variable.predict_result)
            # LOGGER.debug("predict_result_id: {}".format(predict_result_id))
            predict_result = federation.get(name=self.transfer_variable.predict_result.name,
                                            tag=predict_result_id,
                                            idx=0)
            # local_predict_table = predict_result.collect()
            predict_result_table = predict_result.join(data_instances, lambda p, d: (d.label, None, p))
        else:
            pred_prob = wx.mapValues(lambda x: activation.sigmoid(x))
            pred_label = self.classified(pred_prob, predict_param.threshold)
            if predict_param.with_proba:
                predict_result = data_instances.mapValues(lambda x: x.label)
                predict_result = predict_result.join(pred_prob, lambda x, y: (x, y))
            else:
                predict_result = data_instances.mapValues(lambda x: (x.label, None))
            predict_result_table = predict_result.join(pred_label, lambda x, y: (x[0], x[1], y))
        return predict_result_table
Example #5
0
    def predict(self, data_instances, predict_param):
        """
        Prediction of lr
        Parameters
        ----------
        data_instance:DTable of Instance, input data
        predict_param: PredictParam, the setting of prediction.

        Returns
        ----------
        DTable
            include input data label, predict probably, label
        """
        LOGGER.info("Start predict ...")

        data_features = self.transform(data_instances)

        prob_guest = self.compute_wx(data_features, self.coef_, self.intercept_)
        prob_host = federation.get(name=self.transfer_variable.host_prob.name,
                                   tag=self.transfer_variable.generate_transferid(
                                       self.transfer_variable.host_prob),
                                   idx=0)
        LOGGER.info("Get probability from Host")

        # guest probability
        pred_prob = prob_guest.join(prob_host, lambda g, h: activation.sigmoid(g + h))
        pred_label = self.classified(pred_prob, predict_param.threshold)
        if predict_param.with_proba:
            labels = data_instances.mapValues(lambda v: v.label)
            predict_result = labels.join(pred_prob, lambda label, prob: (label, prob))
        else:
            predict_result = data_instances.mapValues(lambda v: (v.label, None))

        predict_result = predict_result.join(pred_label, lambda r, p: (r[0], r[1], p))
        return predict_result
Example #6
0
    def predict(self, data_instantces=None):
        LOGGER.info(f'Start predict task')
        current_suffix = ('predict',)
        host_ciphers = self.cipher.paillier_keygen(key_length=self.model_param.encrypt_param.key_length,
                                                   suffix=current_suffix)

        # LOGGER.debug("Loaded arbiter model: {}".format(self.model_weights.unboxed))
        for idx, cipher in host_ciphers.items():
            if cipher is None:
                continue
            encrypted_model_weights = self.model_weights.encrypted(cipher, inplace=False)
            self.transfer_variable.aggregated_model.remote(obj=encrypted_model_weights.for_remote(),
                                                           role=consts.HOST,
                                                           idx=idx,
                                                           suffix=current_suffix)

        # Receive wx results

        for idx, cipher in host_ciphers.items():
            if cipher is None:
                continue
            encrypted_predict_wx = self.transfer_variable.predict_wx.get(idx=idx, suffix=current_suffix)
            predict_wx = cipher.distribute_decrypt(encrypted_predict_wx)

            prob_table = predict_wx.mapValues(lambda x: activation.sigmoid(x))
            predict_table = prob_table.mapValues(lambda x: 1 if x > self.model_param.predict_param.threshold else 0)

            self.transfer_variable.predict_result.remote(predict_table,
                                                         role=consts.HOST,
                                                         idx=idx,
                                                         suffix=current_suffix)
            self.host_predict_results.append((prob_table, predict_table))
Example #7
0
    def test_numeric_stability(self):
        x_list = np.linspace(-709, 709, 10000)

        # Original function
        # a = 1. / (1. + np.exp(-x))
        for x in x_list:
            a1 = 1. / (1. + np.exp(-x))
            a2 = activation.sigmoid(x)
            self.assertTrue(np.abs(a1 - a2) < 1e-5)
Example #8
0
    def predict(self, data_instances):

        self._abnormal_detection(data_instances)
        self.init_schema(data_instances)

        data_instances = self.align_data_header(data_instances, self.header)
        # predict_wx = self.compute_wx(data_instances, self.model_weights.coef_, self.model_weights.intercept_)
        pred_prob = data_instances.mapValues(lambda v: activation.sigmoid(vec_dot(v.features, self.model_weights.coef_)
                                                                          + self.model_weights.intercept_))

        predict_result = self.predict_score_to_output(data_instances, pred_prob, classes=[0, 1],
                                                      threshold=self.model_param.predict_param.threshold)

        return predict_result
Example #9
0
    def predict(self, data_instances):

        LOGGER.info(f'Start predict task')
        self._abnormal_detection(data_instances)
        self.init_schema(data_instances)
        data_instances = self.align_data_header(data_instances, self.header)
        suffix = ('predict', )
        if self.component_properties.has_arbiter:
            pubkey = self.cipher.gen_paillier_pubkey(enable=self.use_encrypt,
                                                     suffix=suffix)
        else:
            if self.use_encrypt:
                raise ValueError(f"In use_encrypt case, arbiter should be set")
            pubkey = None
        if self.use_encrypt:
            self.cipher_operator.set_public_key(pubkey)

            final_model = self.transfer_variable.aggregated_model.get(
                idx=0, suffix=suffix)
            model_weights = LogisticRegressionWeights(final_model.unboxed,
                                                      self.fit_intercept)
            wx = self.compute_wx(data_instances, model_weights.coef_,
                                 model_weights.intercept_)
            self.transfer_variable.predict_wx.remote(wx,
                                                     consts.ARBITER,
                                                     0,
                                                     suffix=suffix)
            predict_result = self.transfer_variable.predict_result.get(
                idx=0, suffix=suffix)
            # predict_result = predict_result.join(data_instances, lambda p, d: [d.label, p, None,
            #                                                                    {"0": None, "1": None}])
            predict_result = predict_result.join(
                data_instances, lambda p, d: Instance(
                    features=[d.label, p, None, {
                        "0": None,
                        "1": None
                    }],
                    inst_id=d.inst_id))
        else:
            pred_prob = data_instances.mapValues(lambda v: activation.sigmoid(
                vec_dot(v.features, self.model_weights.coef_) + self.
                model_weights.intercept_))
            predict_result = self.predict_score_to_output(
                data_instances,
                pred_prob,
                classes=[0, 1],
                threshold=self.model_param.predict_param.threshold)

        return predict_result
Example #10
0
    def predict(self, data_instances, predict_param):
        wx = self.compute_wx(data_instances, self.coef_, self.intercept_)
        pred_prob = wx.mapValues(lambda x: activation.sigmoid(x))
        pred_label = self.classified(pred_prob, predict_param.threshold)

        if predict_param.with_proba:
            predict_result = data_instances.mapValues(lambda x: x.label)
            predict_result = predict_result.join(pred_prob, lambda x, y:
                                                 (x, y))
        else:
            predict_result = data_instances.mapValues(lambda x:
                                                      (x.label, None))

        predict_result = predict_result.join(pred_label, lambda x, y:
                                             (x[0], x[1], y))
        return predict_result
Example #11
0
    def predict(value):
        """
        Predict method for using sigmoid cross entropy
            Formula : probability = 1.0 / (1.0 + exp(-value))

        Parameters
        ----------
        value : float, The input value of sigmoid function

        Returns
        -------
        probability : float, the output of sigmoid function

        """

        return activation.sigmoid(value)
Example #12
0
    def predict(self, data_instances):
        """
        Prediction of lr
        Parameters
        ----------
        data_instances: Table of Instance, input data

        Returns
        ----------
        Table
            include input data label, predict probably, label
        """
        LOGGER.info("Start predict ...")
        self._abnormal_detection(data_instances)
        data_instances = self.align_data_header(data_instances, self.header)
        if self.need_one_vs_rest:
            predict_result = self.one_vs_rest_obj.predict(data_instances)
            return predict_result
        LOGGER.debug(
            f"Before_predict_reveal_strategy: {self.model_param.reveal_strategy}, {self.is_respectively_reveal}"
        )

        def _vec_dot(v, coef, intercept):
            return fate_operator.vec_dot(v.features, coef) + intercept

        f = functools.partial(_vec_dot,
                              coef=self.model_weights.coef_,
                              intercept=self.model_weights.intercept_)

        pred_prob = data_instances.mapValues(f)
        host_probs = self.transfer_variable.host_prob.get(idx=-1)

        LOGGER.info("Get probability from Host")

        # guest probability
        for host_prob in host_probs:
            if not self.is_respectively_reveal:
                host_prob = self.cipher.distribute_decrypt(host_prob)
            pred_prob = pred_prob.join(host_prob, lambda g, h: g + h)
        pred_prob = pred_prob.mapValues(lambda p: activation.sigmoid(p))
        threshold = self.model_param.predict_param.threshold
        predict_result = self.predict_score_to_output(data_instances,
                                                      pred_prob,
                                                      classes=[0, 1],
                                                      threshold=threshold)

        return predict_result
Example #13
0
    def predict(self, data_instances):
        """
        Prediction of lr
        Parameters
        ----------
        data_instances: DTable of Instance, input data

        Returns
        ----------
        DTable
            include input data label, predict probably, label
        """
        LOGGER.info("Start predict is a one_vs_rest task: {}".format(
            self.need_one_vs_rest))
        self._abnormal_detection(data_instances)
        data_instances = self.align_data_header(data_instances, self.header)
        if self.need_one_vs_rest:
            predict_result = self.one_vs_rest_obj.predict(data_instances)
            return predict_result

        # data_features = self.transform(data_instances)
        pred_prob = self.compute_wx(data_instances, self.model_weights.coef_,
                                    self.model_weights.intercept_)
        host_probs = self.transfer_variable.host_prob.get(idx=-1)

        LOGGER.info("Get probability from Host")

        # guest probability
        for host_prob in host_probs:
            pred_prob = pred_prob.join(host_prob, lambda g, h: g + h)
        pred_prob = pred_prob.mapValues(lambda p: activation.sigmoid(p))
        threshold = self.model_param.predict_param.threshold

        # pred_label = pred_prob.mapValues(lambda x: 1 if x > threshold else 0)

        # predict_result = data_instances.mapValues(lambda x: x.label)
        # predict_result = predict_result.join(pred_prob, lambda x, y: (x, y))
        # predict_result = predict_result.join(pred_label, lambda x, y: [x[0], y, x[1],
        #                                                               {"0": (1 - x[1]), "1": x[1]}])
        predict_result = self.predict_score_to_output(data_instances,
                                                      pred_prob,
                                                      classes=[0, 1],
                                                      threshold=threshold)

        return predict_result
Example #14
0
    def predict(self, data_instances):
        """
        Prediction of lr
        Parameters
        ----------
        data_instances:DTable of Instance, input data
        predict_param: PredictParam, the setting of prediction.

        Returns
        ----------
        DTable
            include input data label, predict probably, label
        """
        LOGGER.info("Start predict is a one_vs_rest task: {}".format(
            self.need_one_vs_rest))
        if self.need_one_vs_rest:
            predict_result = self.one_vs_rest_obj.predict(data_instances)
            return predict_result

        data_features = self.transform(data_instances)
        pred_prob = self.compute_wx(data_features, self.model_weights.coef_,
                                    self.model_weights.intercept_)
        host_probs = self.transfer_variable.host_prob.get(idx=-1)

        LOGGER.info("Get probability from Host")

        # guest probability
        for host_prob in host_probs:
            pred_prob = pred_prob.join(host_prob, lambda g, h: g + h)
        pred_prob = pred_prob.mapValues(lambda p: activation.sigmoid(p))
        pred_label = pred_prob.mapValues(
            lambda x: 1 if x > self.model_param.predict_param.threshold else 0)

        predict_result = data_instances.mapValues(lambda x: x.label)
        predict_result = predict_result.join(pred_prob, lambda x, y: (x, y))
        predict_result = predict_result.join(
            pred_label,
            lambda x, y: [x[0], y, x[1], {
                "0": (1 - x[1]),
                "1": x[1]
            }])

        return predict_result
Example #15
0
    def predict(self, data=None):
        LOGGER.debug("In arbiter's predict, need run: {}".format(
            self.need_run))
        if not self.need_run:
            return data

        # synchronize encryption information
        if not self.has_sychronized_encryption:
            self.__synchronize_encryption(mode='predict')
            self.__send_host_mode()

        for idx, use_encrypt in enumerate(self.host_use_encryption):
            if use_encrypt:
                encrypter = self.host_encrypter[idx]
                predict_wx_id = self.transfer_variable.generate_transferid(
                    self.transfer_variable.predict_wx)
                LOGGER.debug(
                    "Arbiter encrypted wx id: {}".format(predict_wx_id))

                predict_wx = federation.get(
                    name=self.transfer_variable.predict_wx.name,
                    tag=predict_wx_id,
                    idx=idx)
                decrypted_wx = encrypter.distribute_decrypt(predict_wx)
                pred_prob = decrypted_wx.mapValues(
                    lambda x: activation.sigmoid(x))
                pred_label = self.classified(pred_prob,
                                             self.predict_param.threshold)
                predict_result_id = self.transfer_variable.generate_transferid(
                    self.transfer_variable.predict_result)
                LOGGER.debug("predict_result_id: {}".format(predict_result_id))

                LOGGER.debug(
                    "Start to remote pred_label: {}, transfer_id: {}".format(
                        pred_label, predict_result_id))
                federation.remote(
                    pred_label,
                    name=self.transfer_variable.predict_result.name,
                    tag=predict_result_id,
                    role=consts.HOST,
                    idx=idx)
        LOGGER.info("Finish predicting, result has been sent back")
        return
Example #16
0
    def predict(self, data_instances):

        if not self.need_run:
            return data_instances
        LOGGER.debug(
            "homo_lr guest need run predict, coef: {}, instercept: {}".format(
                len(self.coef_), self.intercept_))
        wx = self.compute_wx(data_instances, self.coef_, self.intercept_)
        pred_prob = wx.mapValues(lambda x: activation.sigmoid(x))
        pred_label = self.classified(pred_prob, self.predict_param.threshold)

        predict_result = data_instances.mapValues(lambda x: x.label)
        predict_result = predict_result.join(pred_prob, lambda x, y: (x, y))
        predict_result = predict_result.join(
            pred_label,
            lambda x, y: [x[0], y, x[1], {
                "1": x[1],
                "0": (1 - x[1])
            }])
        return predict_result
Example #17
0
    def predict(self, data_instances):
        """
        Prediction of lr
        Parameters
        ----------
        data_instances:DTable of Instance, input data
        predict_param: PredictParam, the setting of prediction.

        Returns
        ----------
        DTable
            include input data label, predict probably, label
        """
        LOGGER.info("Start predict ...")

        data_features = self.transform(data_instances)
        prob_guest = self.compute_wx(data_features, self.coef_,
                                     self.intercept_)
        prob_host = federation.get(
            name=self.transfer_variable.host_prob.name,
            tag=self.transfer_variable.generate_transferid(
                self.transfer_variable.host_prob),
            idx=0)
        LOGGER.info("Get probability from Host")

        # guest probability
        pred_prob = prob_guest.join(prob_host,
                                    lambda g, h: activation.sigmoid(g + h))
        pred_label = self.classified(pred_prob, self.predict_param.threshold)

        predict_result = data_instances.mapValues(lambda x: x.label)
        predict_result = predict_result.join(pred_prob, lambda x, y: (x, y))
        predict_result = predict_result.join(
            pred_label,
            lambda x, y: [x[0], y, x[1], {
                "0": (1 - x[1]),
                "1": x[1]
            }])

        return predict_result
Example #18
0
    def predict(self, data_instances):
        """
        Prediction of fm
        Parameters
        ----------
        data_instances:DTable of Instance, input data

        Returns
        ----------
        DTable
            include input data label, predict probably, label
        """
        LOGGER.info("Start predict is a one_vs_rest task: {}".format(self.need_one_vs_rest))
        if self.need_one_vs_rest:
            predict_result = self.one_vs_rest_obj.predict(data_instances)
            return predict_result

        prob_guest = self.compute_fm(data_instances, self.model_weights)
        vx_guest = self.compute_vx(data_instances, self.model_weights.embed_)
        prob_guest = prob_guest.join(vx_guest, lambda a, b: (a, b))

        host_probs = self.transfer_variable.host_prob.get(idx=-1)

        LOGGER.info("Get probability from Host")

        # guest probability
        assert(len(host_probs)==1, "Currently Hetero FM only support single host.")
        host_prob = host_probs[0]

        pred_prob = prob_guest.join(host_prob, lambda g, h: g[0] + h[0] + np.dot(h[1], g[1]))
        pred_prob = pred_prob.mapValues(lambda p: activation.sigmoid(p))
        pred_label = pred_prob.mapValues(lambda x: 1 if x > self.model_param.predict_param.threshold else 0)

        predict_result = data_instances.mapValues(lambda x: x.label)
        predict_result = predict_result.join(pred_prob, lambda x, y: (x, y))
        predict_result = predict_result.join(pred_label, lambda x, y: [x[0], y, x[1], {"0": (1 - x[1]), "1": x[1]}])

        return predict_result
Example #19
0
 def predict(x):
     prob = activation.sigmoid(x)
     pred_label = 1 if prob > threshold else 0
     return prob, pred_label
Example #20
0
 def predict(self, U_B):
     if self.y_A_u_A is None:
         self.U_A = self.localModel.transform(self.X)
         self.y_A_u_A = self.__compute_yA_uA(self.U_A, self.y)
     return sigmoid(np.matmul(U_B, self.y_A_u_A.transpose()))
Example #21
0
 def predict(value):
     return activation.sigmoid(value)
Example #22
0
    def predict(self, data_instances):
        if not self.need_run:
            return data_instances

        if not self.has_sychronized_encryption:
            self.__synchronize_encryption(mode='predict')
            self.__load_arbiter_model()
        else:
            LOGGER.info("in predict, has synchronize encryption information")

        feature_shape = get_features_shape(data_instances)
        LOGGER.debug("Shape of coef_ : {}, feature shape: {}".format(
            len(self.coef_), feature_shape))
        local_data = data_instances.first()
        LOGGER.debug("One data, features: {}".format(local_data[1].features))
        wx = self.compute_wx(data_instances, self.coef_, self.intercept_)

        if self.use_encrypt:
            encrypted_wx_id = self.transfer_variable.generate_transferid(
                self.transfer_variable.predict_wx)
            LOGGER.debug("Host encrypted wx id: {}".format(encrypted_wx_id))
            LOGGER.debug("Start to remote wx: {}, transfer_id: {}".format(
                wx, encrypted_wx_id))
            federation.remote(wx,
                              name=self.transfer_variable.predict_wx.name,
                              tag=encrypted_wx_id,
                              role=consts.ARBITER,
                              idx=0)
            predict_result_id = self.transfer_variable.generate_transferid(
                self.transfer_variable.predict_result)
            LOGGER.debug("predict_result_id: {}".format(predict_result_id))

            predict_result = federation.get(
                name=self.transfer_variable.predict_result.name,
                tag=predict_result_id,
                idx=0)
            # local_predict_table = predict_result.collect()
            LOGGER.debug(
                "predict_result count: {}, data_instances count: {}".format(
                    predict_result.count(), data_instances.count()))

            predict_result_table = predict_result.join(
                data_instances,
                lambda p, d: [d.label, None, p, {
                    "0": None,
                    "1": None
                }])

        else:
            pred_prob = wx.mapValues(lambda x: activation.sigmoid(x))
            pred_label = self.classified(pred_prob,
                                         self.predict_param.threshold)
            if self.predict_param.with_proba:
                predict_result = data_instances.mapValues(lambda x: x.label)
                predict_result = predict_result.join(pred_prob, lambda x, y:
                                                     (x, y))
            else:
                predict_result = data_instances.mapValues(lambda x:
                                                          (x.label, None))
            predict_result_table = predict_result.join(
                pred_label,
                lambda x, y: [x[0], y, x[1], {
                    "0": None,
                    "1": None
                }])

        LOGGER.debug("Finish predict")

        LOGGER.debug("In host predict, predict_result_table is : {}".format(
            predict_result_table.first()))
        return predict_result_table
Example #23
0
 def predict(self, uB):
     if self.phi is None:
         self.uA = self.localModel.transform(self.X)
         self.phi = self.__compute_phi(self.uA, self.y)
     return sigmoid(np.matmul(uB, self.phi.transpose()))