def build_SharedNN_LabelClient(arg_dict: dict): client = SharedNN_LabelClient(arg_dict["channel"], arg_dict["logger"], arg_dict["mpc_paras"], CSVDataLoader(arg_dict["data_path"] + "train.csv"), CSVDataLoader(arg_dict["data_path"] + "test.csv"), get_loss(arg_dict["loss"]), get_metric(arg_dict["metric"]), arg_dict["task_path"]) return ClientHandle(client, {"record": lambda: client.test_record}, client.start_train)
def build_SecureXGBoost_LabelClient(arg_dict: dict): client = SecureXGBoost_LabelClient( arg_dict["channel"], arg_dict["logger"], arg_dict["mpc_paras"], CSVDataLoader(arg_dict["data_path"] + "train.csv"), CSVDataLoader(arg_dict["data_path"] + "test.csv"), ) return ClientHandle(client, {"record": lambda: client.test_record}, client.start_train)
def test_credit_logistic(dims): train_data = CSVDataLoader("Test/TestDataset/credit_default.csv", list(range(40000)), dims) train_data.set_random_seed({"seed": 8964}) test_data = CSVDataLoader("Test/TestDataset/credit_default.csv", list(range(40000, 50000)), dims) logistic_model = k.Sequential(k.layers.Dense(1, k.activations.sigmoid)) logistic_model.compile(k.optimizers.SGD(0.1), k.losses.mean_squared_error) metrics = [] start_time = time.time() for i in range(100000): if i % 1000 == 0: xys = test_data.get_batch(None) pred_ys = logistic_model.predict_on_batch(xys[:, :-1]) metric = AUC_KS(xys[:, -1:], pred_ys) print("Train round {}, metric {}".format(i, metric)) metrics.append([i, time.time() - start_time] + metric) else: xys = train_data.get_batch(32) loss = logistic_model.train_on_batch(xys[:, :-1], xys[:, -1:]) return metrics
def test_credit_data_2pc(): # Disable GPU since server do not have any computation other than sigmoid print("\n======== Test mpc SecureXGBoost protocol with Credit Default TestDataset ============\n") ip_dict = { 0: "127.0.0.1:19001", 1: "127.0.0.1:19002", 2: "127.0.0.1:19003", 3: "127.0.0.1:19004", 4: "127.0.0.1:19005" } channel0 = Peer(0, "[::]:19001", 10, ip_dict, 13, logger=Logger(prefix="Channel0:")) channel1 = Peer(1, "[::]:19002", 10, ip_dict, 13, logger=Logger(prefix="Channel1:", level=1)) channel2 = Peer(2, "[::]:19003", 10, ip_dict, 13, logger=Logger(prefix="Channel2:")) channel3 = Peer(3, "[::]:19004", 10, ip_dict, 13, logger=Logger(prefix="Channel3:")) channel4 = Peer(4, "[::]:19005", 10, ip_dict, 13, logger=Logger(prefix="Channel4:")) mpc_paras = MPCClientParas([2, 3], 4, 0, 1) config = { "learning_rate": 0.1, "sync_info": { "seed": 8964 }, "max_iteration": 5, "max_depth": 2, "reg_lambda" : 1, "gamma" : 0., "col_sample_ratio" : 0.8, "row_sample_ratio" : 1., "batch_size" : None, "test_batch_size" : None } main_client = MainClient(channel0, Logger(prefix="Main client:"), mpc_paras, AUC_KS, config) # triplets_provider = TripletProducer(channel1, Logger(prefix="Triplet provider:"), mpc_paras, [2, 3]) data_client0 = FeatureClient(channel2, Logger(prefix="Data client 0:"), mpc_paras, CSVDataLoader("Test/TestDataset/Data/credit_default.csv", list(range(400)), list(range(30))), CSVDataLoader("Test/TestDataset/Data/credit_default.csv", list(range(400, 500)), list(range(30)))) data_client1 = FeatureClient(channel3, Logger(prefix="Data client 1:"), mpc_paras, CSVDataLoader("Test/TestDataset/Data/credit_default.csv", list(range(400)), list(range(30, 72))), CSVDataLoader("Test/TestDataset/Data/credit_default.csv", list(range(400, 500)), list(range(30, 72)))) label_client = LabelClient(channel4, Logger(prefix="Lable client:"), mpc_paras, CSVDataLoader("Test/TestDataset/Data/credit_default.csv", list(range(400)), list(range(72, 73))), CSVDataLoader("Test/TestDataset/Data/credit_default.csv", list(range(400, 500)), list(range(72, 73))), AUC_KS, "") main_client_start_th = threading.Thread( target=main_client.start_train, ) data_client0_th = threading.Thread(target=data_client0.start_train) data_client1_th = threading.Thread(target=data_client1.start_train) label_client_th = threading.Thread(target=label_client.start_train) # triplets_provider_th = threading.Thread(target=triplets_provider.start_listening) # triplets_provider_th.start() data_client0_th.start() data_client1_th.start() label_client_th.start() time.sleep(1) main_client_start_th.start() print("====== Stop the triplet provider, the training should be auto exited =========") main_client_start_th.join() data_client0_th.join() data_client1_th.join() label_client_th.join() print("====== MPC SharedNN Test finished =============")
from Client.MPCClient import MPCClientParas, ClientMode from Client.SharedNN.DataProviders import LabelClient from Utils.Log import Logger from Client.Data.DataLoader import CSVDataLoader ip_dict = { 0: "127.0.0.1:19001", 1: "127.0.0.1:19002", 2: "127.0.0.1:19003", 3: "127.0.0.1:19004", 4: "127.0.0.1:19005" } mpc_paras = MPCClientParas([2, 3], 4, 0, 1) channel = Peer(4, "[::]:19005", 3, ip_dict, 13, logger=Logger(prefix="Channel4:")) label_client = LabelClient( channel, Logger(prefix="Lable client:"), mpc_paras, ClientMode.Train, CSVDataLoader("Test/TestDataset/Data/credit_default.csv", list(range(40000)), list(range(72, 73))), CSVDataLoader("Test/TestDataset/Data/credit_default.csv", list(range(40000, 50000)), list(range(72, 73))), MSELoss(), AUC_KS) label_client.start_train()
from Communication.RPCComm import Peer from Client.MPCClient import MPCClientParas, ClientMode from Client.SharedNN.DataProviders import FeatureClient from Utils.Log import Logger from Client.Data.DataLoader import CSVDataLoader ip_dict = { 0: "127.0.0.1:19001", 1: "127.0.0.1:19002", 2: "127.0.0.1:19003", 3: "127.0.0.1:19004", 4: "127.0.0.1:19005" } mpc_paras = MPCClientParas([2, 3], 4, 0, 1) channel = Peer(3, "[::]:19004", 3, ip_dict, 13, logger=Logger(prefix="Channel3:")) feature_client2 = FeatureClient( channel, Logger(prefix="Data client 1:"), mpc_paras, ClientMode.Train, CSVDataLoader("Test/TestDataset/Data/credit_default.csv", list(range(40000)), list(range(30, 72))), CSVDataLoader("Test/TestDataset/Data/credit_default.csv", list(range(40000, 50000)), list(range(30, 72)))) feature_client2.start_train()
def build_SharedNN_FeatureClient(arg_dict: dict): client = SharedNN_FeatureClient(arg_dict["channel"], arg_dict["logger"], arg_dict["mpc_paras"], CSVDataLoader(arg_dict["data_path"] + "train.csv"), CSVDataLoader(arg_dict["data_path"] + "test.csv")) return ClientHandle(client, dict(), client.start_train)
def test_credit_data_2pc(): # Disable GPU since server do not have any computation other than sigmoid print( "\n======== Test mpc SharedNN protocol with Credit Default TestDataset ============\n" ) ip_dict = { 0: "127.0.0.1:19001", 1: "127.0.0.1:19002", 2: "127.0.0.1:19003", 3: "127.0.0.1:19004", 4: "127.0.0.1:19005" } channel0 = Peer(0, "[::]:19001", 10, ip_dict, 13, logger=Logger(prefix="Channel0:")) channel1 = Peer(1, "[::]:19002", 10, ip_dict, 13, logger=Logger(prefix="Channel1:", level=1)) channel2 = Peer(2, "[::]:19003", 10, ip_dict, 13, logger=Logger(prefix="Channel2:")) channel3 = Peer(3, "[::]:19004", 10, ip_dict, 13, logger=Logger(prefix="Channel3:")) channel4 = Peer(4, "[::]:19005", 10, ip_dict, 13, logger=Logger(prefix="Channel4:")) mpc_paras = MPCClientParas([2, 3], 4, 0, 1) main_client = MainClient(channel0, Logger(prefix="Main client:"), mpc_paras, in_dim=64, out_dim=1, layers=[1], batch_size=64, test_batch_size=10000, test_per_batches=11, learning_rate=0.1, max_iter=33) triplets_provider = TripletProducer(channel1, Logger(prefix="Triplet provider:"), mpc_paras, [2, 3]) data_client0 = FeatureClient( channel2, Logger(prefix="Data client 0:"), mpc_paras, CSVDataLoader("Test/TestDataset/Data/credit_default.csv", list(range(40000)), list(range(30))), CSVDataLoader("Test/TestDataset/Data/credit_default.csv", list(range(40000, 50000)), list(range(30)))) data_client1 = FeatureClient( channel3, Logger(prefix="Data client 1:"), mpc_paras, CSVDataLoader("Test/TestDataset/Data/credit_default.csv", list(range(40000)), list(range(30, 72))), CSVDataLoader("Test/TestDataset/Data/credit_default.csv", list(range(40000, 50000)), list(range(30, 72)))) label_client = LabelClient( channel4, Logger(prefix="Lable client:"), mpc_paras, CSVDataLoader("Test/TestDataset/Data/credit_default.csv", list(range(40000)), list(range(72, 73))), CSVDataLoader("Test/TestDataset/Data/credit_default.csv", list(range(40000, 50000)), list(range(72, 73))), MSELoss(), AUC_KS, "") main_client_start_th = threading.Thread(target=main_client.start_train, ) data_client0_th = threading.Thread(target=data_client0.start_train) data_client1_th = threading.Thread(target=data_client1.start_train) label_client_th = threading.Thread(target=label_client.start_train) triplets_provider_th = threading.Thread( target=triplets_provider.start_listening) triplets_provider_th.start() data_client0_th.start() data_client1_th.start() label_client_th.start() time.sleep(1) main_client_start_th.start() print( "====== Stop the triplet provider, the training should be auto exited =========" ) main_client_start_th.join() data_client0_th.join() data_client1_th.join() label_client_th.join() print("====== MPC SharedNN Test finished =============") np.savetxt("mpc_record.csv", np.array(label_client.test_record), delimiter=",") triplets_provider.stop_listening()