def create_guest(ftl_model_param): transfer_variable = HeteroFTLTransferVariable() Wh = np.ones((4, 2)) bh = np.zeros(2) ftl_local_model = MockAutoencoder("02") ftl_local_model.build(2, Wh, bh) return GuestFactory.create(ftl_model_param, transfer_variable, ftl_local_model)
def __init__(self, guest, model_param, transfer_variable): super(TestHeteroFTLGuest, self).__init__(guest, model_param, transfer_variable) U_B = np.array([[4, 2, 3, 1, 2], [6, 5, 1, 4, 5], [7, 4, 1, 9, 10], [6, 5, 1, 4, 5]]) overlap_indexes = [1, 2] Wh = np.ones((5, U_B.shape[1])) bh = np.zeros(U_B.shape[1]) autoencoderB = MockAutoencoder(1) autoencoderB.build(U_B.shape[1], Wh, bh) self.host = PlainFTLHostModel(autoencoderB, self.model_param) self.host.set_batch(U_B, overlap_indexes)
def test_hetero_plain_guest_prepare_table(self): U_A = np.array([[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11], [4, 5, 6, 7, 8]]) y = np.array([[1], [-1], [1], [-1]]) Wh = np.ones((5, U_A.shape[1])) bh = np.zeros(U_A.shape[1]) model_param = FTLModelParam(alpha=1, max_iteration=1) autoencoderA = MockAutoencoder(0) autoencoderA.build(U_A.shape[1], Wh, bh) guest = PlainFTLGuestModel(autoencoderA, model_param) converge_func = MockDiffConverge(None) ftl_guest = TestHeteroFTLGuest(guest, model_param, HeteroFTLTransferVariable()) ftl_guest.set_converge_function(converge_func) guest_sample_indexes = np.array([0, 1, 2, 3]) guest_x_dict = {} guest_label_dict = {} instance_dict = {} instance_list = [] np.random.seed(100) for i, feature, label, in zip(guest_sample_indexes, U_A, y): instance = Instance(inst_id=i, features=feature, label=label[0]) guest_x_dict[i] = feature guest_label_dict[i] = label[0] instance_dict[i] = instance instance_list.append(instance) guest_x = create_table(instance_list, indexes=guest_sample_indexes) guest_x, overlap_indexes, non_overlap_indexes, guest_y = ftl_guest.prepare_data( guest_x) print("guest_x", guest_x) print("overlap_indexes", overlap_indexes) print("non_overlap_indexes", non_overlap_indexes) print("guest_y", guest_y)
def test_party_a_gradient_checking_test(self): U_A = np.array([[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11], [4, 5, 6, 7, 8]]) U_B = np.array([[4, 2, 3, 1, 2], [6, 5, 1, 4, 5], [7, 4, 1, 9, 10], [6, 5, 1, 4, 5]]) y = np.array([[1], [-1], [1], [-1]]) overlap_indexes = [1, 2] non_overlap_indexes = [0, 3] Wh = np.ones((4, U_A.shape[1])) bh = np.zeros(U_A.shape[1]) autoencoderA = MockAutoencoder(0) autoencoderA.build(U_A.shape[1], Wh, bh) autoencoderB = MockAutoencoder(1) autoencoderB.build(U_B.shape[1], Wh, bh) partyA, _ = run_one_party_msg_exchange(autoencoderA, autoencoderB, U_A, U_B, y, overlap_indexes, non_overlap_indexes, self.public_key, self.private_key) loss_grads_A_1 = partyA.get_loss_grads() loss1 = partyA.send_loss() U_A_prime = np.array([[1, 2, 3, 4, 5], [4, 5.001, 6, 7, 8], [7, 8, 9, 10, 11], [4, 5, 6, 7, 8]]) partyA, _ = run_one_party_msg_exchange( autoencoderA, autoencoderB, U_A_prime, U_B, y, overlap_indexes, non_overlap_indexes, self.public_key, self.private_key) loss_grads_A_2 = partyA.get_loss_grads() loss2 = partyA.send_loss() loss_grads_A_1 = np.array( encryption.decrypt_matrix(self.private_key, loss_grads_A_1)) loss_grads_A_2 = np.array( encryption.decrypt_matrix(self.private_key, loss_grads_A_2)) loss1 = encryption.decrypt(self.private_key, loss1) loss2 = encryption.decrypt(self.private_key, loss2) grad_approx = (loss2 - loss1) / 0.001 grad_real = loss_grads_A_1[1, 1] grad_diff = np.abs(grad_approx - grad_real) assert grad_diff < 0.001
def test_party_b_gradient_checking_test(self): U_A = np.array([[1, 2, 3, 4, 5], [4, 5, 6, 7, 8], [7, 8, 9, 10, 11], [4, 5, 6, 7, 8]]) U_B = np.array([[4, 2, 3, 1, 2], [6, 5, 1, 4, 5], [7, 4, 1, 9, 10], [6, 5, 1, 4, 5]]) y = np.array([[1], [-1], [1], [-1]]) overlap_indexes = [1, 2] non_overlap_indexes = [0, 3] Wh = np.ones((5, U_A.shape[1])) bh = np.zeros(U_A.shape[1]) autoencoderA = MockAutoencoder(0) autoencoderA.build(U_A.shape[1], Wh, bh) autoencoderB = MockAutoencoder(1) autoencoderB.build(U_B.shape[1], Wh, bh) partyA, partyB = run_one_party_msg_exchange(autoencoderA, autoencoderB, U_A, U_B, y, overlap_indexes, non_overlap_indexes) loss_grads_B_1 = partyB.get_loss_grads() loss1 = partyA.send_loss() U_B_prime = np.array([[4, 2, 3, 1, 2], [6, 5, 1.001, 4, 5], [7, 4, 1, 9, 10], [6, 5, 1, 4, 5]]) partyA, partyB = run_one_party_msg_exchange(autoencoderA, autoencoderB, U_A, U_B_prime, y, overlap_indexes, non_overlap_indexes) loss_grads_B_2 = partyB.get_loss_grads() loss2 = partyA.send_loss() grad_approx = (loss2 - loss1) / 0.001 grad_real = loss_grads_B_1[0, 2] grad_diff = np.abs(grad_approx - grad_real) assert grad_diff < 0.001