Skip to content

Commit

Permalink
final version
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasakesson committed Sep 10, 2024
1 parent 2fe040a commit 7394ba1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
8 changes: 7 additions & 1 deletion examples/mnist-pytorch-DPSGD/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Prerequisites
- `Python >=3.8, <=3.12 <https://www.python.org/downloads>`__
- `A project in FEDn Studio <https://fedn.scaleoutsystems.com/signup>`__

Edit Differential Privacy budget
--------------------------
- The **Differential Privacy budget** (`FINAL_EPSILON`, `DELTA`) is configured in the `compute` package at `client/train.py` (lines 35 and 39).
- If `HARDLIMIT` (line 40) is set to `True`, the `FINAL_EPSILON` will not exceed its specified limit.
- If `HARDLIMIT` is set to `False`, the expected `FINAL_EPSILON` will be around its specified value given the server runs `ROUNDS` variable (line 36).

Creating the compute package and seed model
-------------------------------------------

Expand All @@ -26,7 +32,7 @@ Clone this repository, then locate into this directory:
.. code-block::
git clone https://github.com/scaleoutsystems/fedn.git
cd fedn/examples/mnist-pytorch
cd fedn/examples/mnist-pytorch-DPSGD
Create the compute package:

Expand Down
33 changes: 19 additions & 14 deletions examples/mnist-pytorch-DPSGD/client/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __getitem__(self, idx):
EPOCHS = 5
EPSILON = FINAL_EPSILON/ROUNDS
DELTA = 1e-5
HARDLIMIT = False

MAX_PHYSICAL_BATCH_SIZE = 32

Expand Down Expand Up @@ -111,24 +112,28 @@ def train(in_model_path, out_model_path, data_path=None, batch_size=32, epochs=1

d_epsilon = privacy_engine.get_epsilon(DELTA)
print("epsilon spent: ", d_epsilon)
tot_epsilon += d_epsilon
tot_epsilon = np.sqrt(tot_epsilon**2 d_epsilon**2)
print("saving tot_epsilon: ", tot_epsilon)
np.save('epsilon.npy', tot_epsilon)

# Metadata needed for aggregation server side
metadata = {
# num_examples are mandatory
"num_examples": len(x_train),
"batch_size": batch_size,
"epochs": epochs,
"lr": lr,
}
if HARDLIMIT and tot_epsilon >= FINAL_EPSILON:
print("DP Budget Exceeded: The differential privacy budget has been exhausted, no model updates will be applied to preserve privacy guarantees.")

# Save JSON metadata file (mandatory)
save_metadata(metadata, out_model_path)

# Save model update (mandatory)
save_parameters(model, out_model_path)
else:
# Metadata needed for aggregation server side
metadata = {
# num_examples are mandatory
"num_examples": len(x_train),
"batch_size": batch_size,
"epochs": epochs,
"lr": lr,
}

# Save JSON metadata file (mandatory)
save_metadata(metadata, out_model_path)

# Save model update (mandatory)
save_parameters(model, out_model_path)

def accuracy(preds, labels):
return (preds == labels).mean()
Expand Down

0 comments on commit 7394ba1

Please sign in to comment.