82 lines
6 KiB
Markdown
82 lines
6 KiB
Markdown
# Is federated learning private?
|
|
|
|
The instructions and seven questions are on the Canvas page. This README explains the given model, the files and the commands. No machine-learning background is expected. The records are made-up wellness data, and the model is already provided.
|
|
|
|
## Files and commands
|
|
|
|
| File | Who writes it | What it is |
|
|
| :--- | :--- | :--- |
|
|
| `data.py` | given | Ten phones of synthetic records and a test set |
|
|
| `model.py` | given | The model and its training functions |
|
|
| `fedavg.py` | you, Part A | Federated averaging |
|
|
| `leak.py` | you, Part B | What a server can reconstruct from an update |
|
|
| `defend.py` | you, Part C | Clipping, noise and secure aggregation |
|
|
| `selfcheck.py` | given | Finite checks for the functions |
|
|
| `experiments.py` | given | Tables and `results.json` |
|
|
|
|
Run these commands from this folder with plain Python 3. No packages need to be installed.
|
|
|
|
python3 selfcheck.py
|
|
python3 experiments.py
|
|
python3 experiments.py A
|
|
python3 experiments.py B
|
|
python3 experiments.py C
|
|
|
|
`selfcheck.py` reports PASS for a checked case, FAIL with an expected and observed result, or SKIP when a function is not written yet. PASS covers its finite cases, not every possible input. Each `experiments.py` run replaces `results.json` with the parts completed in that run. A letter runs just that part; no letter tries A, B and C. Check the final file for all three keys before submitting the output named on Canvas. Keep actual failures or skipped parts rather than using numbers from an example.
|
|
|
|
The instructor runs clean copies of the given `data.py`, `model.py`, `selfcheck.py` and `experiments.py` against your code. The Python function signatures and `YOUR TASK` markers in the three student files are the coding reference.
|
|
|
|
## The given model
|
|
|
|
Each record is one person-day: hours slept, thousands of steps, resting heart rate and hours of screen time. Each measurement is scaled to roughly 0 to 1, so the record is four numbers, `x = [x0, x1, x2, x3]`. The label `y` is 1 if the app flagged that day as a burnout risk and 0 if not. `data.describe(x)` prints a record in hours and steps.
|
|
|
|
The model has four weights `w` and one bias `b`. It multiplies each measurement by its weight, adds the bias and converts the result to a number between 0 and 1:
|
|
|
|
p = sigmoid(w0*x0 + w1*x1 + w2*x2 + w3*x3 + b)
|
|
|
|
If `p` is at least 0.5, the model predicts "risk". That is a model prediction, not a diagnosis of a real person.
|
|
|
|
Training nudges `w` and `b` so the predictions better match the given labels. For one record, the error is `p - y`: positive if the model guessed too high, negative if too low. The nudge, called the gradient, is:
|
|
|
|
dw[j] = (p - y) * x[j] for each feature j
|
|
db = (p - y)
|
|
|
|
A training step moves the model against that gradient: `w[j] = w[j] - lr * dw[j]`. For a batch of records, the gradient is the average of the per-record gradients. `model.local_train` repeats this over a phone's records a few times; each pass is an epoch. The model and the training routine are given, so you do not need to implement these formulas.
|
|
|
|
To see the units behind the scaled numbers, the given functions can print one synthetic record:
|
|
|
|
python3 -c "import data, model; c = data.make_clients(); print(data.describe(c[0][0][0]), c[0][0][1])"
|
|
|
|
## A worked round of federated averaging
|
|
|
|
To keep the arithmetic small, imagine a model with a single dial, `w`, instead of four weights and a bias. One round has four steps.
|
|
|
|
1. **The server sends its model.** It starts with `w = 0` and sends that same value to every phone.
|
|
2. **Each phone trains on its own records.** Phone 1 has 10 records and phone 2 has 30. Each one nudges its copy of `w` toward what its own records show, using `model.local_train`. Their records differ, so they end in different places: phone 1 finishes at `w = 2` and phone 2 at `w = 6`.
|
|
3. **Each phone sends back only its number,** never its records: phone 1 sends 2, phone 2 sends 6, along with how many records it trained on.
|
|
4. **The server averages the two numbers, weighting each by its phone's record count.**
|
|
|
|
```
|
|
new w = (10 * 2 + 30 * 6) / (10 + 30)
|
|
= (20 + 180) / 40
|
|
= 5
|
|
```
|
|
|
|
Why weight by records? Phone 2's 6 is backed by three times as much evidence as phone 1's 2. A plain average, `(2 + 6) / 2 = 4`, would let 10 records count as much as 30. Weighting gives the answer you would get if each of the 40 records had a vote: 10 votes for 2 and 30 votes for 6. So the result, 5, sits three quarters of the way from 2 to 6, the same three-to-one split as the records.
|
|
|
|
The real model does exactly this for each of its five numbers separately: `w[0]`, `w[1]`, `w[2]`, `w[3]` and `b`. That is `weighted_average` in Part A. Then the next round starts from the new model.
|
|
|
|
The records stayed on the phones; only the model's numbers moved. Work through this round by hand before you write any code.
|
|
|
|
## Updates and defenses
|
|
|
|
A one-step update can contain a gradient. With one record, the server sees `dw` and `db` from the formulas above; with several records, it sees their average. `leak.py` uses the update to test what can be reconstructed. The Canvas question asks you to derive the one-record case before coding it.
|
|
|
|
Clipping limits an update's length, and added noise changes its values. These can change both reconstruction and accuracy; the experiment measures what happens for the supplied data and settings. In secure aggregation, paired masks cancel when updates are added. The server can still learn the sum, so a masked update should not be described as an absolute privacy guarantee.
|
|
|
|
## References
|
|
|
|
- McMahan et al., "Communication-Efficient Learning of Deep Networks from Decentralized Data" (2017), federated averaging: <https://arxiv.org/abs/1602.05629>
|
|
- Zhu, Liu and Han, "Deep Leakage from Gradients" (2019), reconstruction on neural networks: <https://arxiv.org/abs/1906.08935>
|
|
- Bonawitz et al., "Practical Secure Aggregation for Privacy-Preserving Machine Learning" (2017): <https://eprint.iacr.org/2017/281>
|
|
- Python `random` module: <https://docs.python.org/3/library/random.html>
|