-
Notifications
You must be signed in to change notification settings - Fork 0
/
Noise_add.py
58 lines (52 loc) · 1.77 KB
/
Noise_add.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
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 6 10:59:16 2019
@author: WEIKANG
"""
import numpy as np
import copy
import torch
import random
#from Calculate import get_1_norm, get_2_norm, inner_product, avg_grads
def noise_add(args, noise_scale, w):
w_noise = copy.deepcopy(w)
if isinstance(w[0],np.ndarray) == True:
noise = np.random.normal(0,noise_scale,w.size())
w_noise = w_noise + noise
else:
for k in range(len(w)):
for i in w[k].keys():
noise = np.random.normal(0,noise_scale,w[k][i].size())
if args.gpu != -1:
noise = torch.from_numpy(noise).float().cuda()
else:
noise = torch.from_numpy(noise).float()
w_noise[k][i] = w_noise[k][i] + noise
return w_noise
def noise_add1(args, noise_scale, w):
w_noise = copy.deepcopy(w)
for i in w.keys():
noise = np.random.normal(0,noise_scale,w[i].size())
#w_noise[i] = w_noise[i].cuda()
if args.gpu != -1:
noise = torch.from_numpy(noise).float().cuda()
else:
noise = torch.from_numpy(noise).float()
w_noise[i] = w_noise[i] + noise
return w_noise
def users_sampling(args, w, chosenUsers):
if args.num_chosenUsers < args.num_users:
w_locals = []
for i in range(len(chosenUsers)):
w_locals.append(w[chosenUsers[i]])
else:
w_locals = copy.deepcopy(w)
return w_locals
# def clipping(args, w):
# if get_1_norm(w) > args.clipthr:
# w_local = copy.deepcopy(w)
# for i in w.keys():
# w_local[i]=copy.deepcopy(w[i]*args.clipthr/get_1_norm(w))
# else:
# w_local = copy.deepcopy(w)
# return w_local