-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
110 lines (96 loc) · 3.66 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
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
import torch
import torch.optim as optim
from copy import deepcopy
import numpy as np
class Client:
def __init__(self, data, targets, device, noise_level=0):
self.data = data.to(device)
self.targets = targets.to(device)
self.device = device
self.length = len(self.data)
if noise_level is None:
noise_level = 0
self.noise_level = noise_level
def train(self, serverModel, criterion, E, B, learning_rate, momentum):
"""
serverModel - server model
criterion - loss function (model, data, targets)
E - number of epochs
B - number of batches
returns clientModel.state_dict() after training
"""
clientModel = deepcopy(serverModel)
clientModel = clientModel.to(self.device)
clientModel.load_state_dict(serverModel.state_dict())
clientOptimiser = optim.SGD(
clientModel.parameters(), lr=learning_rate, momentum=momentum
)
for epoch in range(E):
batch_indices = self.split_indices(B)
for batch in range(B):
data_batch, targets_batch = self.get_subset(batch_indices[batch])
clientOptimiser.zero_grad()
loss = criterion(clientModel, data_batch, targets_batch)
loss.backward()
clientOptimiser.step()
self.model = deepcopy(clientModel)
if self.noise_level > 0:
clientModel = self.add_noise(clientModel, self.noise_level)
return clientModel.state_dict()
def add_noise(self, model, noise_level):
"""
add noise to all model parameters with given noise_level
"""
model_state = model.state_dict()
for key in model_state.keys():
model_state[key] += torch.normal(
mean=torch.zeros_like(model_state[key]), std=noise_level
)
model.load_state_dict(model_state)
return model
def loss(self, model, criterion):
"""
criterion - loss function (model, data, targets)
"""
model.eval()
with torch.no_grad():
loss = criterion(model, self.data, self.targets)
model.train()
return float(loss.cpu())
def accuracy(self):
return self.accuracy_(self.model)
def accuracy_(self, model):
"""
evaluate model accuracy on client's training data
"""
model.eval()
with torch.no_grad():
scores = model(self.data)
_, predictions = scores.max(1)
num_correct = torch.sum(predictions == self.targets)
total = self.length
accuracy = num_correct / total
model.train()
return float(accuracy.cpu())
def get_subset(self, indices):
"""
return a subset of client data and targets with the given indices
"""
data_raw = [self.data[j] for j in indices]
targets_raw = [int(self.targets[j]) for j in indices]
# prepare data and targets for training
data = torch.stack(data_raw, 0).to(device=self.device).to(torch.float32)
targets = torch.tensor(targets_raw).to(device=self.device)
return data, targets
def split_indices(self, B):
"""
return a list of indices for B batches
"""
length = self.length
indices = list(range(length))
np.random.shuffle(indices)
k = int(np.floor(length / B))
# drops the last few datapoints, if needed, to keep batch size fixed
if k == 0:
return [indices for _ in range(B)] # use all datapoints in each batch
return [indices[i : i + k] for i in range(0, len(indices), k)]