-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathclient.py
78 lines (58 loc) · 2.44 KB
/
client.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
import torch
import torch.nn as nn
from model import LeNet5, MLP, CNN1, CNN2
from opacus import PrivacyEngine
def init_model(model_type, in_channel, n_class):
if model_type == 'LeNet5':
model = LeNet5(in_channel, n_class)
elif model_type == 'MLP':
model = MLP(n_class)
elif model_type == 'CNN1':
model = CNN1(in_channel, n_class)
elif model_type == 'CNN2':
model = CNN2(in_channel, n_class)
else:
raise ValueError(f"Unknown model type {model_type}")
return model
def init_optimizer(model, args):
if args.optimizer == 'sgd':
optimizer = torch.optim.SGD(model.parameters(),
lr=args.lr,
momentum=args.momentum,
weight_decay=5e-4)
elif args.optimizer == 'adam':
optimizer = torch.optim.Adam(model.parameters(),
lr=args.lr,
weight_decay=1e-4)
else:
raise ValueError("Unknown optimizer")
return optimizer
def init_dp_optimizer(model, data_size, args):
opt = init_optimizer(model, args)
orders = [1 + x / 10. for x in range(1, 100)] + list(range(12, 64))
privacy_engine = PrivacyEngine(
model,
sample_rate=args.batch_size / data_size,
alphas=orders,
noise_multiplier=args.noise_multiplier,
max_grad_norm=args.l2_norm_clip,
)
# print(f"Using DP-SGD with sigma={args.noise_multiplier} and clipping norm max={args.l2_norm_clip}")
privacy_engine.attach(opt)
return opt
class Client(nn.Module):
def __init__(self, data, args):
super(Client, self).__init__()
self.private_data = data
self.private_model = init_model(args.private_model_type, args.in_channel, args.n_class).to(args.device)
self.proxy_model = init_model(args.proxy_model_type, args.in_channel, args.n_class).to(args.device)
if args.use_private_SGD:
# Using DP proxies, train private model without DP
self.proxy_opt = init_dp_optimizer(self.proxy_model, self.private_data[0].shape[0], args)
else:
self.proxy_opt = init_optimizer(self.proxy_model, args)
# Private model training always not DP
self.private_opt = init_optimizer(self.private_model, args)
self.device = args.device
self.tot_epochs = 0
self.privacy_budget = 0.