-
Notifications
You must be signed in to change notification settings - Fork 251
Dev caesar vfl #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
qbc2016
wants to merge
17
commits into
alibaba:master
Choose a base branch
from
qbc2016:dev_caesar_vfl
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Dev caesar vfl #346
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b62c19f
dev for caesar vfl
qbc2016 3a3d464
second
qbc2016 a7db665
readme modified only
qbc2016 6696f66
Merge branch 'master' of https://github.com/alibaba/FederatedScope in…
qbc2016 e109608
first merge version (add a temp ss scheme which should be fixed later)
qbc2016 9ed2bcd
merged version with a simple ss scheme
qbc2016 7d99dc9
remove redundant
qbc2016 8d2f8ca
delete ready step
qbc2016 c987069
cleaner version by merging the clients' actions
qbc2016 964a259
temp version
qbc2016 c91cb91
add test version
qbc2016 4f5fdfe
Merge branch 'master' of https://github.com/alibaba/FederatedScope in…
qbc2016 d52aa81
refined test_caesar
qbc2016 142f449
right ss but wrong result
qbc2016 7f8752f
right version with many prints
qbc2016 4a56999
truly right version with many prints
qbc2016 5acead8
delete redunant prints
qbc2016 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # You can refer to pyphe for the detail implementation. ( | ||
| # https://github.com/data61/python-paillier/blob/master/phe/paillier.py) | ||
| # Or implement an effective version of Paillier (<Public-key cryptosystems | ||
| # based on composite degree residuosity classes>) | ||
|
|
||
| DEFAULT_KEYSIZE = 3072 | ||
|
|
||
|
|
||
| def generate_paillier_keypair(n_length=DEFAULT_KEYSIZE): | ||
| """Generate public key and private key used Paillier`. | ||
|
|
||
| Args: | ||
| n_length: key size in bits. | ||
|
|
||
| Returns: | ||
| tuple: The generated :class:`PaillierPublicKey` and | ||
| :class:`PaillierPrivateKey` | ||
| """ | ||
| n = p = q = None | ||
| public_key = PaillierPublicKey(n) | ||
| private_key = PaillierPrivateKey(public_key, p, q) | ||
|
|
||
| return public_key, private_key | ||
|
|
||
|
|
||
| class PaillierPublicKey(object): | ||
| """Contains a public key and associated encryption methods. | ||
| """ | ||
| def __init__(self, n): | ||
| pass | ||
|
|
||
| def encrypt(self, value): | ||
| # We only provide an abstract implementation here | ||
|
|
||
| return value | ||
|
|
||
|
|
||
| class PaillierPrivateKey(object): | ||
| """Contains a private key and associated decryption method. | ||
| """ | ||
| def __init__(self, public_key, p, q): | ||
| pass | ||
|
|
||
| def decrypt(self, encrypted_number): | ||
| # We only provide an abstract implementation here | ||
|
|
||
| return encrypted_number | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| ### Caesar Vertical Federated Learning | ||
|
|
||
| We provide an example for seCure lArge-scalE SlArse logistic Regression (caesar) vertical federated learning, you can run with: | ||
|
||
| ```bash | ||
| python3 ../main.py --cfg caesar_v_fl.yaml | ||
| ``` | ||
|
|
||
| You can specify customized configurations in `caesar_v_fl.yaml`, such as `data.type` and `federate.total_round_num`. | ||
| More details of the provided example can be found in [Tutorial](https://federatedscope.io/docs/cross-silo/). | ||
|
|
||
| Note that FederatedScope only provide an `abstract_paillier`, user can refer to [pyphe](https://github.com/data61/python-paillier/blob/master/phe/paillier.py) for the detail implementation, or adopt other homomorphic encryption algorithms. | ||
|
|
||
| More support for vertical federated learning is coming soon! We really appreciate any contributions to FederatedScope ! | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from federatedscope.caesar_v_fl.Paillier.abstract_paillier import * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| use_gpu: False | ||
| federate: | ||
| mode: standalone | ||
| client_num: 2 | ||
| total_round_num: 30 | ||
| model: | ||
| type: lr | ||
| use_bias: False | ||
| train: | ||
| optimizer: | ||
| lr: 0.05 | ||
| data: | ||
| type: caesar_v_fl_data | ||
| batch_size: 50 | ||
| caesar_vertical: | ||
| use: True | ||
| key_size: 256 | ||
| trainer: | ||
| type: none | ||
| eval: | ||
| freq: 5 | ||
| best_res_update_round_wise_key: test_loss |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from federatedscope.caesar_v_fl.dataloader.dataloader \ | ||
| import load_caesar_v_fl_data | ||
|
|
||
| __all__ = ['load_caesar_v_fl_data'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import numpy as np | ||
|
|
||
|
|
||
| def load_caesar_v_fl_data(config=None, generate=False): | ||
| """ | ||
| To generate the synthetic data for vertical FL | ||
|
|
||
| Arguments: | ||
| config: configuration | ||
| generate (bool): whether to generate the synthetic data | ||
| :returns: The synthetic data, the modified config | ||
| :rtype: dict | ||
| """ | ||
|
|
||
| if generate: | ||
| # generate toy data for running a vertical FL example | ||
| INSTANCE_NUM = 1000 | ||
| TRAIN_SPLIT = 0.9 | ||
|
|
||
| total_dims = np.sum(config.caesar_vertical.dims) | ||
| theta = np.random.uniform(low=-1.0, high=1.0, size=(total_dims, 1)) | ||
| x = np.random.choice([-1.0, 1.0, -2.0, 2.0, -3.0, 3.0], | ||
| size=(INSTANCE_NUM, total_dims)) | ||
| y = np.asarray([ | ||
| 1.0 if x >= 0 else -1.0 | ||
| for x in np.reshape(np.matmul(x, theta), -1) | ||
| ]) | ||
|
|
||
| def standardize(X): | ||
| m, n = X.shape | ||
| for j in range(n): | ||
| features = X[:, j] | ||
| meanVal = features.mean(axis=0) | ||
| std = features.std(axis=0) | ||
| if std != 0: | ||
| X[:, j] = (features - meanVal) / std | ||
| else: | ||
| X[:, j] = 0 | ||
| return X | ||
|
|
||
| def normalize(X): | ||
| m, n = X.shape | ||
| for j in range(n): | ||
| features = X[:, j] | ||
| minVal = features.min(axis=0) | ||
| maxVal = features.max(axis=0) | ||
| diff = maxVal - minVal | ||
| if diff != 0: | ||
| X[:, j] = (features - minVal) / diff | ||
| else: | ||
| X[:, j] = 0 | ||
| return X | ||
|
|
||
| x = standardize(x) | ||
| # x = normalize(x) | ||
|
|
||
| train_num = int(TRAIN_SPLIT * INSTANCE_NUM) | ||
| test_data = {'theta': theta, 'x': x[train_num:], 'y': y[train_num:]} | ||
| data = dict() | ||
|
|
||
| # For Server | ||
| data[0] = dict() | ||
| data[0]['train'] = None | ||
| data[0]['val'] = None | ||
| data[0]['test'] = test_data | ||
|
|
||
| # For Client #1 | ||
| data[1] = dict() | ||
| data[1]['train'] = { | ||
| 'x': x[:train_num, :config.caesar_vertical.dims[0]] | ||
| } | ||
| data[1]['val'] = None | ||
| data[1]['test'] = test_data | ||
|
|
||
| # For Client #2 | ||
| data[2] = dict() | ||
| data[2]['train'] = { | ||
| 'x': x[:train_num, config.caesar_vertical.dims[0]:], | ||
| 'y': y[:train_num] | ||
| } | ||
| data[2]['val'] = None | ||
| data[2]['test'] = test_data | ||
|
|
||
| return data, config | ||
| else: | ||
| raise ValueError('You must provide the data file') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import numpy as np | ||
| import math | ||
|
|
||
|
|
||
| def batch_iter(data, batch_size, shuffled=True): | ||
| """ | ||
| A batch iteration | ||
|
|
||
| Arguments: | ||
| data(dict): data | ||
| batch_size (int): the batch size | ||
| shuffled (bool): whether to shuffle the data at the start of each epoch | ||
| :returns: sample index, batch of x, batch_of y | ||
| :rtype: int, ndarray, ndarray | ||
| """ | ||
|
|
||
| assert 'x' in data and 'y' in data | ||
| data_x = data['x'] | ||
| data_y = data['y'] | ||
| data_size = len(data_y) | ||
| num_batches_per_epoch = math.ceil(data_size / batch_size) | ||
|
|
||
| while True: | ||
| shuffled_index = np.random.permutation( | ||
| np.arange(data_size)) if shuffled else np.arange(data_size) | ||
| for batch in range(num_batches_per_epoch): | ||
| start_index = batch * batch_size | ||
| end_index = min(data_size, (batch + 1) * batch_size) | ||
| sample_index = shuffled_index[start_index:end_index] | ||
| yield sample_index, data_x[sample_index], data_y[sample_index] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from federatedscope.caesar_v_fl.worker.vertical_client import vFLClient | ||
| from federatedscope.caesar_v_fl.worker.vertical_server import vFLServer | ||
|
|
||
| __all__ = ['vFLServer', 'vFLClient'] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is the same as
federatedscope/vertical_fl/Paillier/abstract_paillier.py, maybe you can reuse it.