-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
52 lines (45 loc) · 1.91 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
import flwr as fl
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
class Client(fl.client.Client):
def __init__(self, model, train_loader):
self.model = model
self.train_loader = train_loader
def get_parameters(self):
return [param.data.numpy() for param in self.model.parameters()]
def set_parameters(self, parameters):
for param, new_param in zip(self.model.parameters(), parameters):
param.data = torch.tensor(new_param)
def fit(self, parameters, config):
self.set_parameters(parameters)
self.model.train()
optimizer = optim.SGD(self.model.parameters(), lr=0.01)
for epoch in range(config['epochs']):
for data, target in self.train_loader:
optimizer.zero_grad()
output = self.model(data)
loss = nn.CrossEntropyLoss()(output, target)
loss.backward()
optimizer.step()
return self.get_parameters(), len(self.train_loader.dataset), {}
def evaluate(self, parameters, config):
self.set_parameters(parameters)
self.model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in self.train_loader:
output = self.model(data)
test_loss += nn.CrossEntropyLoss()(output, target).item()
pred = output.argmax(dim=1, keepdim=True)
correct += pred.eq(target.view_as(pred)).sum().item()
return float(test_loss), len(self.train_loader.dataset), {"accuracy": correct / len(self.train_loader.dataset)}
def main():
model = ...
train_loader = ...
client = Client(model, train_loader)
fl.client.start_numpy_client(server_address="localhost:8080", client=client)
if __name__ == "__main__":
main()