From dd74860db1e1e2761d65d8d9831e3c4c45159031 Mon Sep 17 00:00:00 2001 From: Yu Wu Date: Thu, 21 Dec 2023 18:01:50 +0800 Subject: [PATCH 1/2] edit sshe lr & linr (#5227) edit examples Signed-off-by: Yu Wu --- examples/pipeline/sshe_linr/test_linr_cv.py | 4 ++-- examples/pipeline/sshe_linr/test_linr_warm_start.py | 6 +++--- examples/pipeline/sshe_lr/test_lr_cv.py | 2 +- examples/pipeline/sshe_lr/test_lr_multi_class.py | 2 +- examples/pipeline/sshe_lr/test_lr_warm_start.py | 6 +++--- python/fate/ml/glm/hetero/sshe/sshe_linr.py | 8 ++++++-- python/fate/ml/glm/hetero/sshe/sshe_lr.py | 8 ++++++-- 7 files changed, 22 insertions(+), 14 deletions(-) diff --git a/examples/pipeline/sshe_linr/test_linr_cv.py b/examples/pipeline/sshe_linr/test_linr_cv.py index b0ef69afb5..0b8e662bd1 100644 --- a/examples/pipeline/sshe_linr/test_linr_cv.py +++ b/examples/pipeline/sshe_linr/test_linr_cv.py @@ -37,8 +37,8 @@ def main(config="../config.yaml", namespace=""): reader_0.hosts[0].task_parameters(namespace=f"experiment{namespace}", name="motor_hetero_host") psi_0 = PSI("psi_0", input_data=reader_0.outputs["output_data"]) linr_0 = SSHELinR("linr_0", - epochs=10, - batch_size=None, + epochs=3, + batch_size=100, learning_rate=0.05, init_param={"fit_intercept": True}, cv_data=psi_0.outputs["output_data"], diff --git a/examples/pipeline/sshe_linr/test_linr_warm_start.py b/examples/pipeline/sshe_linr/test_linr_warm_start.py index e43936779b..eb57e3581a 100644 --- a/examples/pipeline/sshe_linr/test_linr_warm_start.py +++ b/examples/pipeline/sshe_linr/test_linr_warm_start.py @@ -39,7 +39,7 @@ def main(config="../config.yaml", namespace=""): psi_0 = PSI("psi_0", input_data=reader_0.outputs["output_data"]) linr_0 = SSHELinR("linr_0", epochs=4, - batch_size=None, + batch_size=100, init_param={"fit_intercept": True, "method": "zeros"}, train_data=psi_0.outputs["output_data"], learning_rate=0.05, @@ -50,7 +50,7 @@ def main(config="../config.yaml", namespace=""): linr_1 = SSHELinR("linr_1", train_data=psi_0.outputs["output_data"], warm_start_model=linr_0.outputs["output_model"], epochs=2, - batch_size=None, + batch_size=100, learning_rate=0.05, reveal_every_epoch=True, early_stop="diff", @@ -58,7 +58,7 @@ def main(config="../config.yaml", namespace=""): ) linr_2 = SSHELinR("linr_2", epochs=6, - batch_size=None, + batch_size=100, init_param={"fit_intercept": True, "method": "zeros"}, train_data=psi_0.outputs["output_data"], learning_rate=0.05, diff --git a/examples/pipeline/sshe_lr/test_lr_cv.py b/examples/pipeline/sshe_lr/test_lr_cv.py index f10e6c870a..f081aaafd5 100644 --- a/examples/pipeline/sshe_lr/test_lr_cv.py +++ b/examples/pipeline/sshe_lr/test_lr_cv.py @@ -45,7 +45,7 @@ def main(config="../config.yaml", namespace=""): lr_0 = SSHELR("lr_0", learning_rate=0.15, epochs=2, - batch_size=None, + batch_size=300, init_param={"fit_intercept": True}, cv_data=psi_0.outputs["output_data"], cv_param={"n_splits": 3}, diff --git a/examples/pipeline/sshe_lr/test_lr_multi_class.py b/examples/pipeline/sshe_lr/test_lr_multi_class.py index 607220ab77..3d237b3305 100644 --- a/examples/pipeline/sshe_lr/test_lr_multi_class.py +++ b/examples/pipeline/sshe_lr/test_lr_multi_class.py @@ -47,7 +47,7 @@ def main(config="../config.yaml", namespace=""): lr_0 = SSHELR("lr_0", learning_rate=0.15, epochs=10, - batch_size=None, + batch_size=300, reveal_every_epoch=False, early_stop="diff", reveal_loss_freq=1, diff --git a/examples/pipeline/sshe_lr/test_lr_warm_start.py b/examples/pipeline/sshe_lr/test_lr_warm_start.py index 4bf3420605..4b6bf9eec7 100644 --- a/examples/pipeline/sshe_lr/test_lr_warm_start.py +++ b/examples/pipeline/sshe_lr/test_lr_warm_start.py @@ -45,7 +45,7 @@ def main(config="../config.yaml", namespace=""): psi_0 = PSI("psi_0", input_data=reader_0.outputs["output_data"]) lr_0 = SSHELR("lr_0", epochs=4, - batch_size=None, + batch_size=300, learning_rate=0.05, init_param={"fit_intercept": True, "method": "zeros"}, train_data=psi_0.outputs["output_data"], @@ -56,7 +56,7 @@ def main(config="../config.yaml", namespace=""): lr_1 = SSHELR("lr_1", train_data=psi_0.outputs["output_data"], warm_start_model=lr_0.outputs["output_model"], epochs=2, - batch_size=None, + batch_size=300, learning_rate=0.05, reveal_every_epoch=False, early_stop="diff", @@ -64,7 +64,7 @@ def main(config="../config.yaml", namespace=""): ) lr_2 = SSHELR("lr_2", epochs=6, - batch_size=None, + batch_size=300, learning_rate=0.05, init_param={"fit_intercept": True, "method": "zeros"}, train_data=psi_0.outputs["output_data"], diff --git a/python/fate/ml/glm/hetero/sshe/sshe_linr.py b/python/fate/ml/glm/hetero/sshe/sshe_linr.py index d8cdf8753c..f204381a31 100644 --- a/python/fate/ml/glm/hetero/sshe/sshe_linr.py +++ b/python/fate/ml/glm/hetero/sshe/sshe_linr.py @@ -211,9 +211,13 @@ def fit_single_model(self, ctx: Context, train_data: DataFrame, valid_data: Data loss = loss_fn(z, y) if i % self.reveal_loss_freq == 0: if epoch_loss is None: - epoch_loss = loss.get(dst=rank_b) * h.shape[0] + epoch_loss = loss.get(dst=rank_b) + if ctx.is_on_guest: + epoch_loss = epoch_loss * h.shape[0] else: - epoch_loss += loss.get(dst=rank_b) * h.shape[0] + batch_loss = loss.get(dst=rank_b) + if ctx.is_on_guest: + epoch_loss += batch_loss * h.shape[0] loss.backward() optimizer.step() if epoch_loss is not None and ctx.is_on_guest: diff --git a/python/fate/ml/glm/hetero/sshe/sshe_lr.py b/python/fate/ml/glm/hetero/sshe/sshe_lr.py index bd362e8838..bde42e3d42 100644 --- a/python/fate/ml/glm/hetero/sshe/sshe_lr.py +++ b/python/fate/ml/glm/hetero/sshe/sshe_lr.py @@ -315,9 +315,13 @@ def fit_single_model(self, ctx: Context, train_data: DataFrame, valid_data: Data loss = loss_fn(z, y) if i % self.reveal_loss_freq == 0: if epoch_loss is None: - epoch_loss = loss.get(dst=rank_b) * h.shape[0] + epoch_loss = loss.get(dst=rank_b) + if epoch_loss: + epoch_loss = epoch_loss * h.shape[0] else: - epoch_loss += loss.get(dst=rank_b) * h.shape[0] + batch_loss = loss.get(dst=rank_b) + if batch_loss: + epoch_loss += batch_loss * h.shape[0] loss.backward() optimizer.step() if epoch_loss is not None and ctx.is_on_guest: From a25bb8dbf14452fad7c94e23bf1c7dc95af94f12 Mon Sep 17 00:00:00 2001 From: Yu Wu Date: Thu, 21 Dec 2023 18:08:12 +0800 Subject: [PATCH 2/2] edit sshe lr & linr (#5227) edit examples Signed-off-by: Yu Wu --- examples/pipeline/sshe_linr/test_linr_cv.py | 2 +- examples/pipeline/sshe_linr/test_linr_validate.py | 2 +- examples/pipeline/sshe_linr/test_linr_warm_start.py | 6 +++--- examples/pipeline/sshe_lr/test_lr_warm_start.py | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/pipeline/sshe_linr/test_linr_cv.py b/examples/pipeline/sshe_linr/test_linr_cv.py index 0b8e662bd1..6de03fd905 100644 --- a/examples/pipeline/sshe_linr/test_linr_cv.py +++ b/examples/pipeline/sshe_linr/test_linr_cv.py @@ -39,7 +39,7 @@ def main(config="../config.yaml", namespace=""): linr_0 = SSHELinR("linr_0", epochs=3, batch_size=100, - learning_rate=0.05, + learning_rate=0.15, init_param={"fit_intercept": True}, cv_data=psi_0.outputs["output_data"], cv_param={"n_splits": 3}, diff --git a/examples/pipeline/sshe_linr/test_linr_validate.py b/examples/pipeline/sshe_linr/test_linr_validate.py index 2af85bb579..34ff25cac3 100644 --- a/examples/pipeline/sshe_linr/test_linr_validate.py +++ b/examples/pipeline/sshe_linr/test_linr_validate.py @@ -49,7 +49,7 @@ def main(config="../config.yaml", namespace=""): reveal_every_epoch=False, early_stop="diff", reveal_loss_freq=1, - learning_rate=0.1) + learning_rate=0.15) evaluation_0 = Evaluation("evaluation_0", runtime_parties=dict(guest=guest), default_eval_setting="regression", diff --git a/examples/pipeline/sshe_linr/test_linr_warm_start.py b/examples/pipeline/sshe_linr/test_linr_warm_start.py index eb57e3581a..4ea6703620 100644 --- a/examples/pipeline/sshe_linr/test_linr_warm_start.py +++ b/examples/pipeline/sshe_linr/test_linr_warm_start.py @@ -42,7 +42,7 @@ def main(config="../config.yaml", namespace=""): batch_size=100, init_param={"fit_intercept": True, "method": "zeros"}, train_data=psi_0.outputs["output_data"], - learning_rate=0.05, + learning_rate=0.15, reveal_every_epoch=False, early_stop="diff", reveal_loss_freq=1, @@ -51,7 +51,7 @@ def main(config="../config.yaml", namespace=""): warm_start_model=linr_0.outputs["output_model"], epochs=2, batch_size=100, - learning_rate=0.05, + learning_rate=0.15, reveal_every_epoch=True, early_stop="diff", reveal_loss_freq=1, @@ -61,7 +61,7 @@ def main(config="../config.yaml", namespace=""): batch_size=100, init_param={"fit_intercept": True, "method": "zeros"}, train_data=psi_0.outputs["output_data"], - learning_rate=0.05, + learning_rate=0.15, reveal_every_epoch=False, early_stop="diff", reveal_loss_freq=1, diff --git a/examples/pipeline/sshe_lr/test_lr_warm_start.py b/examples/pipeline/sshe_lr/test_lr_warm_start.py index 4b6bf9eec7..748a338fd4 100644 --- a/examples/pipeline/sshe_lr/test_lr_warm_start.py +++ b/examples/pipeline/sshe_lr/test_lr_warm_start.py @@ -46,7 +46,7 @@ def main(config="../config.yaml", namespace=""): lr_0 = SSHELR("lr_0", epochs=4, batch_size=300, - learning_rate=0.05, + learning_rate=0.15, init_param={"fit_intercept": True, "method": "zeros"}, train_data=psi_0.outputs["output_data"], reveal_every_epoch=False, @@ -57,7 +57,7 @@ def main(config="../config.yaml", namespace=""): warm_start_model=lr_0.outputs["output_model"], epochs=2, batch_size=300, - learning_rate=0.05, + learning_rate=0.15, reveal_every_epoch=False, early_stop="diff", reveal_loss_freq=1, @@ -65,7 +65,7 @@ def main(config="../config.yaml", namespace=""): lr_2 = SSHELR("lr_2", epochs=6, batch_size=300, - learning_rate=0.05, + learning_rate=0.15, init_param={"fit_intercept": True, "method": "zeros"}, train_data=psi_0.outputs["output_data"], reveal_every_epoch=False,