-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
194 lines (164 loc) · 5.45 KB
/
model.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from collections import OrderedDict
from typing import List, Tuple
import numpy as np
import torch
from torch import nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
import settings
class FLNet(nn.Module):
"""
Default implementation of a Neural Network that is supported by the Flwr framework.
"""
def set_parameters(self, parameters: List[np.ndarray]) -> None:
"""
Sets the parameters of the model according to the input given.
:param parameters: List of np arrays containing weights.
:return: None
"""
params_dict = zip(self.state_dict().keys(), parameters)
state_dict = OrderedDict({k: torch.Tensor(v) for k, v in params_dict})
self.load_state_dict(state_dict, strict=True)
def get_parameters(self) -> List[np.ndarray]:
"""
Returns the parameters of the Neural Network.
:return: List of np arrays containing the parameters
"""
return [val.cpu().numpy() for _, val in self.state_dict().items()]
def __str__(self):
return self.__class__.__name__
class Net(FLNet):
def __init__(self) -> None:
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, settings.NUM_CLASSES)
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, List[torch.Tensor]]:
outputs = []
x = self.conv1(x)
outputs.append(x)
x = F.relu(x, inplace=False)
x = self.pool(x)
x = self.conv2(x)
outputs.append(x)
x = F.relu(x, inplace=False)
x = self.pool(x)
x = x.view(-1, 16 * 5 * 5)
x = self.fc1(x)
outputs.append(x)
x = F.relu(x, inplace=False)
x = self.fc2(x)
outputs.append(x)
x = F.relu(x, inplace=False)
x = self.fc3(x)
outputs.append(x)
x = F.relu(x, inplace=False)
return x, outputs
class Texas100Net(FLNet):
def __init__(self):
super(Texas100Net, self).__init__()
self.fc1 = nn.Linear(6169, 1024)
self.fc2 = nn.Linear(1024, 512)
self.fc3 = nn.Linear(512, 256)
self.fc4 = nn.Linear(256, 128)
self.fc5 = nn.Linear(128, 100)
def forward(self, x):
outputs = []
x = torch.relu(self.fc1(x))
outputs.append(x)
x = torch.relu(self.fc2(x))
outputs.append(x)
x = torch.relu(self.fc3(x))
outputs.append(x)
x = torch.relu(self.fc4(x))
outputs.append(x)
x = torch.relu(self.fc5(x))
outputs.append(x)
return x, outputs
class Purchase100Net(FLNet):
def __init__(self):
super(Purchase100Net, self).__init__()
self.fc1 = nn.Linear(600, 1024)
self.fc2 = nn.Linear(1024, 512)
self.fc3 = nn.Linear(512, 256)
self.fc4 = nn.Linear(256, 128)
self.fc5 = nn.Linear(128, 100)
def forward(self, x):
outputs = []
x = torch.relu(self.fc1(x))
outputs.append(x)
x = torch.relu(self.fc2(x))
outputs.append(x)
x = torch.relu(self.fc3(x))
outputs.append(x)
x = torch.relu(self.fc4(x))
outputs.append(x)
x = torch.relu(self.fc5(x))
outputs.append(x)
return x, outputs
class HeartNet(FLNet):
def __init__(self):
super(HeartNet, self).__init__()
self.fc1 = nn.Linear(settings.HEARTNET_FEATURES, 1024)
self.fc2 = nn.Linear(1024, 512)
self.fc3 = nn.Linear(512, 256)
self.fc4 = nn.Linear(256, 128)
self.fc5 = nn.Linear(128, 2)
def forward(self, x):
outputs = []
x = torch.relu(self.fc1(x))
outputs.append(x)
x = torch.relu(self.fc2(x))
outputs.append(x)
x = torch.relu(self.fc3(x))
outputs.append(x)
x = torch.relu(self.fc4(x))
outputs.append(x)
x = torch.relu(self.fc5(x))
outputs.append(x)
return x, outputs
class GeneratedDatasetNetwork(FLNet):
def __init__(self):
super(GeneratedDatasetNetwork, self).__init__()
self.fc1 = nn.Linear(settings.NUM_FEATURES, 1024)
self.fc2 = nn.Linear(1024, 512)
self.fc3 = nn.Linear(512, 256)
self.fc4 = nn.Linear(256, 128)
self.fc5 = nn.Linear(128, settings.NUM_CLASSES)
def forward(self, x):
outputs = []
x = torch.relu(self.fc1(x))
outputs.append(x)
x = torch.relu(self.fc2(x))
outputs.append(x)
x = torch.relu(self.fc3(x))
outputs.append(x)
x = torch.relu(self.fc4(x))
outputs.append(x)
x = torch.relu(self.fc5(x))
outputs.append(x)
return x, outputs
class StudentNet(FLNet):
def __init__(self):
super(StudentNet, self).__init__()
self.fc1 = nn.Linear(50, 1024)
self.fc2 = nn.Linear(1024, 512)
self.fc3 = nn.Linear(512, 256)
self.fc4 = nn.Linear(256, 128)
self.fc5 = nn.Linear(128, 2)
def forward(self, x):
outputs = []
x = torch.relu(self.fc1(x))
outputs.append(x)
x = torch.relu(self.fc2(x))
outputs.append(x)
x = torch.relu(self.fc3(x))
outputs.append(x)
x = torch.relu(self.fc4(x))
outputs.append(x)
x = torch.relu(self.fc5(x))
outputs.append(x)
return x, outputs