-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,43 @@ | ||
""" | ||
Implementation of several useful tool | ||
Please copy this script to target path | ||
""" | ||
import numpy as np | ||
import random | ||
import torch | ||
|
||
|
||
def mnist_noniid(dataset, num_users): | ||
""" | ||
Sample non-I.I.D client data from MNIST dataset | ||
:param dataset: | ||
:param num_users: | ||
:return: | ||
""" | ||
# num_shards, num_imgs = 30, 2000 | ||
num_shards = int(num_users*3) | ||
num_imgs = int(60000 / num_shards) | ||
idx_shard = [i for i in range(num_shards)] | ||
dict_users = {i: np.array([], dtype='int64') for i in range(num_users)} | ||
idxs = np.arange(num_shards*num_imgs) | ||
labels = dataset.targets.numpy() | ||
|
||
# sort labels | ||
idxs_labels = np.vstack((idxs, labels)) | ||
idxs_labels = idxs_labels[:,idxs_labels[1,:].argsort()] | ||
idxs = idxs_labels[0,:] | ||
|
||
# divide and assign | ||
for i in range(num_users): | ||
rand_set = set(np.random.choice(idx_shard, 3, replace=False)) | ||
idx_shard = list(set(idx_shard) - rand_set) | ||
for rand in rand_set: | ||
dict_users[i] = np.concatenate((dict_users[i], idxs[rand*num_imgs:(rand+1)*num_imgs]), axis=0) | ||
return dict_users | ||
|
||
|
||
def gaussian_noise(data_shape, s, sigma, device=None): | ||
""" | ||
Gaussian noise for CDP-FedAVG-LS Algorithm | ||
""" | ||
return torch.normal(0, sigma * s, data_shape).to(device) |