(self, weights=None, suffix=None, cipher=None)
Use Taylor series expand log loss:
          Loss = - y * log(h(x)) - (1-y) * log(1 - h(x)) where h(x) = 1/(1+exp(-wx))
          Then loss' = - (1/N)*∑(log(1/2) - 1/2*wx + ywx - 1/8(wx)^2)
Use Taylor series expand log loss:
          Loss = - y * log(h(x)) - (1-y) * log(1 - h(x)) where h(x) = 1/(1+exp(-wx))
          Then loss' = - (1/N)*∑(log(1/2) - 1/2*wx + ywx - 1/8(wx)^2)
Use Taylor series expand log loss : Loss = - y * log ( h ( x )) - ( 1 - y ) * log ( 1 - h ( x )) where h ( x ) = 1 / ( 1 + exp ( - wx )) Then loss = - ( 1 / N ) * ∑ ( log ( 1 / 2 ) - 1 / 2 * wx + ywx - 1 / 8 ( wx ) ^2 )
def compute_loss(self, weights=None, suffix=None, cipher=None):
        """
          Use Taylor series expand log loss:
          Loss = - y * log(h(x)) - (1-y) * log(1 - h(x)) where h(x) = 1/(1+exp(-wx))
          Then loss' = - (1/N)*∑(log(1/2) - 1/2*wx + ywx - 1/8(wx)^2)
        """
        LOGGER.info(f"[compute_loss]: Calculate loss ...")
        wx_self_square = (self.wx_self * self.wx_self).reduce(operator.add)

        self.secure_matrix_obj.share_encrypted_matrix(suffix=suffix,
                                                      is_remote=True,
                                                      cipher=cipher,
                                                      wx_self_square=wx_self_square)

        tensor_name = ".".join(("shared_loss",) + suffix)
        share_loss = SecureMatrix.from_source(tensor_name=tensor_name,
                                              source=self.other_party,
                                              cipher=cipher,
                                              q_field=self.fixedpoint_encoder.n,
                                              encoder=self.fixedpoint_encoder,
                                              is_fixedpoint_table=False)

        if self.reveal_every_iter:
            loss_norm = self.optimizer.loss_norm(weights)
            if loss_norm:
                share_loss += loss_norm
            LOGGER.debug(f"share_loss+loss_norm: {share_loss}")
            tensor_name = ".".join(("loss",) + suffix)
            share_loss.broadcast_reconstruct_share(tensor_name=tensor_name)
        else:
            tensor_name = ".".join(("loss",) + suffix)
            share_loss.broadcast_reconstruct_share(tensor_name=tensor_name)
            if self.optimizer.penalty == consts.L2_PENALTY:
                w_self, w_remote = weights

                w_encode = np.hstack((w_self.value, w_remote.value))

                w_encode = np.array([w_encode])

                w_tensor_name = ".".join(("loss_norm_w",) + suffix)
                w_tensor = fixedpoint_numpy.FixedPointTensor(value=w_encode,
                                                             q_field=self.fixedpoint_encoder.n,
                                                             endec=self.fixedpoint_encoder,
                                                             tensor_name=w_tensor_name)

                w_tensor_transpose_name = ".".join(("loss_norm_w_transpose",) + suffix)
                w_tensor_transpose = fixedpoint_numpy.FixedPointTensor(value=w_encode.T,
                                                                       q_field=self.fixedpoint_encoder.n,
                                                                       endec=self.fixedpoint_encoder,
                                                                       tensor_name=w_tensor_transpose_name)

                loss_norm_tensor_name = ".".join(("loss_norm",) + suffix)

                loss_norm = w_tensor.dot(w_tensor_transpose, target_name=loss_norm_tensor_name)
                loss_norm.broadcast_reconstruct_share()
def compute_loss ( self , weights = None , suffix = None , cipher = None ) : LOGGER . info ( f"[compute_loss]: Calculate loss ..." ) wx_self_square = ( self . wx_self * self . wx_self ) . reduce ( operator . add ) self . secure_matrix_obj . share_encrypted_matrix ( suffix = suffix , is_remote = True , cipher = cipher , wx_self_square = wx_self_square ) tensor_name = "." . join ( ( "shared_loss" , ) + suffix ) share_loss = SecureMatrix . from_source ( tensor_name = tensor_name , source = self . other_party , cipher = cipher , q_field = self . fixedpoint_encoder . n , encoder = self . fixedpoint_encoder , is_fixedpoint_table = False ) if self . reveal_every_iter : loss_norm = self . optimizer . loss_norm ( weights ) if loss_norm : share_loss += loss_norm LOGGER . debug ( f"share_loss+loss_norm: {share_loss}" ) tensor_name = "." . join ( ( "loss" , ) + suffix ) share_loss . broadcast_reconstruct_share ( tensor_name = tensor_name ) else : tensor_name = "." . join ( ( "loss" , ) + suffix ) share_loss . broadcast_reconstruct_share ( tensor_name = tensor_name ) if self . optimizer . penalty == consts . L2_PENALTY : w_self , w_remote = weights w_encode = np . hstack ( ( w_self . value , w_remote . value ) ) w_encode = np . array ( [ w_encode ] ) w_tensor_name = "." . join ( ( "loss_norm_w" , ) + suffix ) w_tensor = fixedpoint_numpy . FixedPointTensor ( value = w_encode , q_field = self . fixedpoint_encoder . n , endec = self . fixedpoint_encoder , tensor_name = w_tensor_name ) w_tensor_transpose_name = "." . join ( ( "loss_norm_w_transpose" , ) + suffix ) w_tensor_transpose = fixedpoint_numpy . FixedPointTensor ( value = w_encode . T , q_field = self . fixedpoint_encoder . n , endec = self . fixedpoint_encoder , tensor_name = w_tensor_transpose_name ) loss_norm_tensor_name = "." . join ( ( "loss_norm" , ) + suffix ) loss_norm = w_tensor . dot ( w_tensor_transpose , target_name = loss_norm_tensor_name ) loss_norm . broadcast_reconstruct_share ( )