(self, weights, suffix, cipher=None)
return loss
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, suffix, 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 = (-0.5 * self.encrypted_wx).reduce(operator.add)
        ywx = (self.encrypted_wx * self.labels).reduce(operator.add)

        wx_square = (2 * self.wx_remote * self.wx_self).reduce(operator.add) + \
                    (self.wx_self * self.wx_self).reduce(operator.add)

        wx_remote_square = self.secure_matrix_obj.share_encrypted_matrix(suffix=suffix,
                                                                         is_remote=False,
                                                                         cipher=None,
                                                                         wx_self_square=None)[0]

        wx_square = (wx_remote_square + wx_square) * -0.125

        batch_num = self.batch_num[int(suffix[2])]
        loss = (wx + ywx + wx_square) * (-1 / batch_num) - np.log(0.5)

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

        tensor_name = ".".join(("loss",) + suffix)
        loss = share_loss.get(tensor_name=tensor_name,
                              broadcast=False)[0]

        if self.reveal_every_iter:
            loss_norm = self.optimizer.loss_norm(weights)
            if loss_norm:
                loss += loss_norm
        else:
            if self.optimizer.penalty == consts.L2_PENALTY:
                w_self, w_remote = weights

                w_encode = np.hstack((w_remote.value, w_self.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).get(broadcast=False)
                loss_norm = 0.5 * self.optimizer.alpha * loss_norm[0][0]
                loss = loss + loss_norm

        LOGGER.info(f"[compute_loss]: loss={loss}, reveal_every_iter={self.reveal_every_iter}")

        return loss
def compute_loss ( self , weights , suffix , cipher = None ) : LOGGER . info ( f"[compute_loss]: Calculate loss ..." ) wx = ( - 0.5 * self . encrypted_wx ) . reduce ( operator . add ) ywx = ( self . encrypted_wx * self . labels ) . reduce ( operator . add ) wx_square = ( 2 * self . wx_remote * self . wx_self ) . reduce ( operator . add ) + ( self . wx_self * self . wx_self ) . reduce ( operator . add ) wx_remote_square = self . secure_matrix_obj . share_encrypted_matrix ( suffix = suffix , is_remote = False , cipher = None , wx_self_square = None ) [ 0 ] wx_square = ( wx_remote_square + wx_square ) * - 0.125 batch_num = self . batch_num [ int ( suffix [ 2 ] ) ] loss = ( wx + ywx + wx_square ) * ( - 1 / batch_num ) - np . log ( 0.5 ) tensor_name = "." . join ( ( "shared_loss" , ) + suffix ) share_loss = SecureMatrix . from_source ( tensor_name = tensor_name , source = loss , cipher = None , q_field = self . fixedpoint_encoder . n , encoder = self . fixedpoint_encoder ) tensor_name = "." . join ( ( "loss" , ) + suffix ) loss = share_loss . get ( tensor_name = tensor_name , broadcast = False ) [ 0 ] if self . reveal_every_iter : loss_norm = self . optimizer . loss_norm ( weights ) if loss_norm : loss += loss_norm else : if self . optimizer . penalty == consts . L2_PENALTY : w_self , w_remote = weights w_encode = np . hstack ( ( w_remote . value , w_self . 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 ) . get ( broadcast = False ) loss_norm = 0.5 * self . optimizer . alpha * loss_norm [ 0 ] [ 0 ] loss = loss + loss_norm LOGGER . info ( f"[compute_loss]: loss={loss}, reveal_every_iter={self.reveal_every_iter}" ) return loss