(self, host_components, epoch_idx, overlap_num)
compute training loss
compute training loss
compute training loss
def compute_loss(self, host_components, epoch_idx, overlap_num):

        """
        compute training loss
        """

        overlap_ub, overlap_ub_2, mapping_comp_b = host_components[0], host_components[1], host_components[2]

        if self.mode == 'plain':

            loss_overlap = np.sum((-self.overlap_ua * self.constant_k) * overlap_ub)

            ub_phi = np.matmul(overlap_ub, self.phi.transpose())
            part1 = -0.5*np.sum(self.overlap_y*ub_phi)
            part2 = 1.0/8*np.sum(ub_phi * ub_phi)
            part3 = len(self.overlap_y)*np.log(2)
            loss_y = part1 + part2 + part3
            return self.alpha * (loss_y/overlap_num) + loss_overlap/overlap_num

        elif self.mode == 'encrypted':

            loss_overlap = overlap_ub.element_wise_product((-self.overlap_ua*self.constant_k))
            sum = np.sum(loss_overlap.reduce_sum())
            ub_phi = overlap_ub.T.fast_matmul_2d(self.phi.transpose())

            part1 = -0.5 * np.sum((self.overlap_y * ub_phi))
            ub_2 = overlap_ub_2.reduce_sum()
            enc_phi_uB_2_phi = np.matmul(np.matmul(self.phi, ub_2), self.phi.transpose())
            part2 = 1/8 * np.sum(enc_phi_uB_2_phi)
            part3 = len(self.overlap_y)*np.log(2)

            loss_y = part1 + part2 + part3
            en_loss = (self.alpha/self.overlap_num) * loss_y + sum / overlap_num

            loss_val = self.decrypt_loss_val(en_loss, epoch_idx)

            return loss_val
def compute_loss ( self , host_components , epoch_idx , overlap_num ) : overlap_ub , overlap_ub_2 , mapping_comp_b = host_components [ 0 ] , host_components [ 1 ] , host_components [ 2 ] if self . mode == 'plain' : loss_overlap = np . sum ( ( - self . overlap_ua * self . constant_k ) * overlap_ub ) ub_phi = np . matmul ( overlap_ub , self . phi . transpose ( ) ) part1 = - 0.5 * np . sum ( self . overlap_y * ub_phi ) part2 = 1.0 / 8 * np . sum ( ub_phi * ub_phi ) part3 = len ( self . overlap_y ) * np . log ( 2 ) loss_y = part1 + part2 + part3 return self . alpha * ( loss_y / overlap_num ) + loss_overlap / overlap_num elif self . mode == 'encrypted' : loss_overlap = overlap_ub . element_wise_product ( ( - self . overlap_ua * self . constant_k ) ) sum = np . sum ( loss_overlap . reduce_sum ( ) ) ub_phi = overlap_ub . T . fast_matmul_2d ( self . phi . transpose ( ) ) part1 = - 0.5 * np . sum ( ( self . overlap_y * ub_phi ) ) ub_2 = overlap_ub_2 . reduce_sum ( ) enc_phi_uB_2_phi = np . matmul ( np . matmul ( self . phi , ub_2 ) , self . phi . transpose ( ) ) part2 = 1 / 8 * np . sum ( enc_phi_uB_2_phi ) part3 = len ( self . overlap_y ) * np . log ( 2 ) loss_y = part1 + part2 + part3 en_loss = ( self . alpha / self . overlap_num ) * loss_y + sum / overlap_num loss_val = self . decrypt_loss_val ( en_loss , epoch_idx ) return loss_val