-
Notifications
You must be signed in to change notification settings - Fork 1
/
fed_synthetic.py
395 lines (348 loc) · 12.5 KB
/
fed_synthetic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"""
"""
from typing import Any, Dict, List, Optional, Tuple
import numpy as np
import torch
import torch.utils.data as torchdata
from ..models import nn as mnn
from ..models.utils import top_n_accuracy
from ..utils.misc import set_seed
from ._generate_synthetic import generate_synthetic
from ._register import register_fed_dataset
from .fed_dataset import FedDataset
__all__ = [
"FedSynthetic",
]
@register_fed_dataset()
class FedSynthetic(FedDataset):
"""Federated synthetic dataset.
This dataset is proposed in the FedProx paper [1]_ [2]_.
Parameters
----------
alpha : float
Parameters for generating synthetic data
using normal distributions.
beta : float
Parameters for generating synthetic data
using normal distributions.
iid : bool
Whether to generate iid data.
num_clients : int
The number of clients.
num_classes : int, default 10
The number of classes.
dimension : int, default 60
The dimension of data (feature).
seed : int, default 0
The random seed.
**extra_config : dict, optional
Extra configurations.
References
----------
.. [1] https://arxiv.org/abs/1812.06127
.. [2] https://github.com/litian96/FedProx/tree/master/data
"""
__name__ = "FedSynthetic"
def __init__(
self,
alpha: float,
beta: float,
iid: bool,
num_clients: int,
num_classes: int = 10,
dimension: int = 60,
seed: int = 0,
**extra_config: Any,
) -> None:
super().__init__()
self.alpha = alpha
self.beta = beta
self.iid = iid
self.num_clients = num_clients
self.num_classes = num_classes
self.dimension = dimension
self._preload(seed=seed)
def _preload(self, seed: int = 0) -> None:
"""Preload the dataset.
Parameters
----------
seed : int, default 0
The random seed for data generation.
Returns
-------
None
"""
self.seed = seed
set_seed(self.seed)
self.criterion = torch.nn.CrossEntropyLoss()
self._data = generate_synthetic(
alpha=self.alpha,
beta=self.beta,
iid=self.iid,
num_clients=self.num_clients,
num_classes=self.num_classes,
dimension=self.dimension,
seed=seed,
)
self.DEFAULT_BATCH_SIZE = 8
self.DEFAULT_TRAIN_CLIENTS_NUM = self.num_clients
self.DEFAULT_TEST_CLIENTS_NUM = self.num_clients
def reset_seed(self, seed: int) -> None:
"""Reset the random seed and re-generate the dataset.
Parameters
----------
seed : int
The random seed.
Returns
-------
None
"""
self._preload(seed)
def get_dataloader(
self,
train_bs: Optional[int] = None,
test_bs: Optional[int] = None,
client_idx: Optional[int] = None,
) -> Tuple[torchdata.DataLoader, torchdata.DataLoader]:
"""Get local dataloader at client `client_idx` or get the global dataloader.
Parameters
----------
train_bs : int, optional
Batch size for training dataloader.
If ``None``, use default batch size.
test_bs : int, optional
Batch size for testing dataloader.
If ``None``, use default batch size.
client_idx : int, optional
Index of the client to get dataloader.
If ``None``, get the dataloader containing all data.
Usually used for centralized training.
Returns
-------
train_dl : :class:`torch.utils.data.DataLoader`
Training dataloader.
test_dl : :class:`torch.utils.data.DataLoader`
Testing dataloader.
"""
assert client_idx is None or 0 <= client_idx < self.num_clients
if client_idx is None:
train_X = np.concatenate([d["train_X"] for d in self._data], axis=0)
train_y = np.concatenate([d["train_y"] for d in self._data], axis=0)
test_X = np.concatenate([d["test_X"] for d in self._data], axis=0)
test_y = np.concatenate([d["test_y"] for d in self._data], axis=0)
else:
train_X = self._data[client_idx]["train_X"]
train_y = self._data[client_idx]["train_y"]
test_X = self._data[client_idx]["test_X"]
test_y = self._data[client_idx]["test_y"]
train_bs = train_bs or self.DEFAULT_BATCH_SIZE
if train_bs == -1:
train_bs = len(train_X)
train_dl = torchdata.DataLoader(
dataset=torchdata.TensorDataset(torch.from_numpy(train_X), torch.from_numpy(train_y)),
batch_size=train_bs,
shuffle=True,
drop_last=False,
)
test_bs = test_bs or self.DEFAULT_BATCH_SIZE
if test_bs == -1:
test_bs = len(test_X)
test_dl = torchdata.DataLoader(
dataset=torchdata.TensorDataset(torch.from_numpy(test_X), torch.from_numpy(test_y)),
batch_size=test_bs,
shuffle=True,
drop_last=False,
)
return train_dl, test_dl
def load_partition_data_distributed(self, process_id: int, batch_size: Optional[int] = None) -> tuple:
"""Get local dataloader at client `process_id` or get global dataloader.
Parameters
----------
process_id : int
Index of the client to get dataloader.
If ``None``, get the dataloader containing all data,
usually used for centralized training.
batch_size : int, optional
Batch size for dataloader.
If ``None``, use default batch size.
Returns
-------
tuple
- train_clients_num: :obj:`int`
Number of training clients.
- train_data_num: :obj:`int`
Number of training data.
- train_data_global: :class:`torch.utils.data.DataLoader` or None
Global training dataloader.
- test_data_global: :class:`torch.utils.data.DataLoader` or None
Global testing dataloader.
- local_data_num: :obj:`int`
Number of local training data.
- train_data_local: :class:`torch.utils.data.DataLoader` or None
Local training dataloader.
- test_data_local: :class:`torch.utils.data.DataLoader` or None
Local testing dataloader.
- n_class: :obj:`int`
Number of classes.
"""
_batch_size = batch_size or self.DEFAULT_BATCH_SIZE
if process_id == 0:
# get global dataset
train_data_global, test_data_global = self.get_dataloader(_batch_size, _batch_size)
train_data_num = len(train_data_global.dataset)
test_data_num = len(test_data_global.dataset)
train_data_local = None
test_data_local = None
local_data_num = 0
else:
# get local dataset
train_data_local, test_data_local = self.get_dataloader(_batch_size, _batch_size, process_id - 1)
train_data_num = local_data_num = len(train_data_local.dataset)
train_data_global = None
test_data_global = None
retval = (
self.num_clients,
train_data_num,
train_data_global,
test_data_global,
local_data_num,
train_data_local,
test_data_local,
self.num_classes,
)
return retval
def load_partition_data(self, batch_size: Optional[int] = None) -> tuple:
"""Partition data into all local clients.
Parameters
----------
batch_size : int, optional
Batch size for dataloader.
If ``None``, use default batch size.
Returns
-------
tuple
- train_clients_num: :obj:`int`
Number of training clients.
- train_data_num: :obj:`int`
Number of training data.
- test_data_num: :obj:`int`
Number of testing data.
- train_data_global: :class:`torch.utils.data.DataLoader`
Global training dataloader.
- test_data_global: :class:`torch.utils.data.DataLoader`
Global testing dataloader.
- data_local_num_dict: :obj:`dict`
Number of local training data for each client.
- train_data_local_dict: :obj:`dict`
Local training dataloader for each client.
- test_data_local_dict: :obj:`dict`
Local testing dataloader for each client.
- n_class: :obj:`int`
Number of classes.
"""
_batch_size = batch_size or self.DEFAULT_BATCH_SIZE
# get local dataset
data_local_num_dict = dict()
train_data_local_dict = dict()
test_data_local_dict = dict()
for client_idx in range(self.num_clients):
train_data_local, test_data_local = self.get_dataloader(_batch_size, _batch_size, client_idx)
local_data_num = len(train_data_local.dataset)
data_local_num_dict[client_idx] = local_data_num
train_data_local_dict[client_idx] = train_data_local
test_data_local_dict[client_idx] = test_data_local
# global dataset
train_data_global = torchdata.DataLoader(
torchdata.ConcatDataset(list(dl.dataset for dl in list(train_data_local_dict.values()))),
batch_size=_batch_size,
shuffle=True,
)
train_data_num = len(train_data_global.dataset)
test_data_global = torchdata.DataLoader(
torchdata.ConcatDataset(list(dl.dataset for dl in list(test_data_local_dict.values()) if dl is not None)),
batch_size=_batch_size,
shuffle=True,
)
test_data_num = len(test_data_global.dataset)
retval = (
self.num_clients,
train_data_num,
test_data_num,
train_data_global,
test_data_global,
data_local_num_dict,
train_data_local_dict,
test_data_local_dict,
self.num_classes,
)
return retval
def extra_repr_keys(self) -> List[str]:
return super().extra_repr_keys() + [
"alpha",
"beta",
"iid",
"num_clients",
"num_classes",
"dimension",
]
def evaluate(self, probs: torch.Tensor, truths: torch.Tensor) -> Dict[str, float]:
"""Evaluation using predictions and ground truth.
Parameters
----------
probs : torch.Tensor
Predicted probabilities.
truths : torch.Tensor
Ground truth labels.
Returns
-------
Dict[str, float]
Evaluation results.
"""
return {
"acc": top_n_accuracy(probs, truths, 1),
"top3_acc": top_n_accuracy(probs, truths, 3),
"top5_acc": top_n_accuracy(probs, truths, 5),
"loss": self.criterion(probs, truths).item(),
"num_samples": probs.shape[0],
}
@property
def url(self) -> str:
"""URL for downloading the dataset. Empty for synthetic dataset."""
return ""
@property
def candidate_models(self) -> Dict[str, torch.nn.Module]:
"""A set of candidate models."""
return {
"mlp_d1": mnn.MLP(self.dimension, self.num_classes, ndim=0),
"mlp_d2": mnn.MLP(
self.dimension,
self.num_classes,
[
2 * self.dimension,
],
ndim=0,
),
"mlp_d3": mnn.MLP(
self.dimension,
self.num_classes,
[
int(1.5 * self.dimension),
2 * self.dimension,
],
ndim=0,
),
"mlp_d4": mnn.MLP(
self.dimension,
self.num_classes,
[
int(1.5 * self.dimension),
2 * self.dimension,
int(1.5 * self.dimension),
],
ndim=0,
),
}
@property
def doi(self) -> List[str]:
"""DOI(s) related to the dataset."""
return ["10.48550/ARXIV.1812.06127"]