forked from omarfoq/knn-per
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
125 lines (94 loc) · 3.97 KB
/
models.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
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
class LinearLayer(nn.Module):
def __init__(self, input_dimension, num_classes, bias=True):
super(LinearLayer, self).__init__()
self.input_dimension = input_dimension
self.num_classes = num_classes
self.fc = nn.Linear(input_dimension, num_classes, bias=bias)
def forward(self, x):
return self.fc(x)
class TwoLinearLayers(nn.Module):
def __init__(self, input_dimension, hidden_dimension, output_dimension, bias=False):
super(TwoLinearLayers, self).__init__()
self.input_dimension = input_dimension
self.hidden_dimension = hidden_dimension
self.num_classes = output_dimension
self.fc1 = nn.Linear(input_dimension, hidden_dimension, bias=bias)
self.fc2 = nn.Linear(hidden_dimension, output_dimension, bias=bias)
def forward(self, x):
return self.fc2(self.fc1(x))
class FemnistCNN(nn.Module):
"""
Implements a model with two convolutional layers followed by pooling, and a final dense layer with 2048 units.
Same architecture used for FEMNIST in "LEAF: A Benchmark for Federated Settings"__
We use `zero`-padding instead of `same`-padding used in
https://github.com/TalwalkarLab/leaf/blob/master/models/femnist/cnn.py.
"""
def __init__(self, num_classes):
super(FemnistCNN, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(32, 64, 5)
self.fc1 = nn.Linear(64 * 4 * 4, 2048)
self.output = nn.Linear(2048, num_classes)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 64 * 4 * 4)
x = F.relu(self.fc1(x))
x = self.output(x)
return x
class CIFAR10CNN(nn.Module):
def __init__(self, num_classes):
super(CIFAR10CNN, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(32, 64, 5)
self.fc1 = nn.Linear(64 * 5 * 5, 2048)
self.output = nn.Linear(2048, num_classes)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 64 * 5 * 5)
x = F.relu(self.fc1(x))
x = self.output(x)
return x
class NextCharacterLSTM(nn.Module):
def __init__(self, input_size, embed_size, hidden_size, output_size, n_layers):
super(NextCharacterLSTM, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.embed_size = embed_size
self.output_size = output_size
self.n_layers = n_layers
self.encoder = nn.Embedding(input_size, embed_size)
self.rnn =\
nn.LSTM(
input_size=embed_size,
hidden_size=hidden_size,
num_layers=n_layers,
batch_first=True
)
self.decoder = nn.Linear(hidden_size, output_size)
def forward(self, input_):
self.rnn.flatten_parameters()
encoded = self.encoder(input_)
output, (hidden, cell) = self.rnn(encoded)
output = self.decoder(output)
output = output.permute(0, 2, 1) # change dimension to (B, C, T)
hidden = hidden.permute(1, 0, 2) # change to (B, N_LAYERS, T)
cell = cell.permute(1, 0, 2) # change to (B, N_LAYERS, T)
return output, (hidden, cell)
def get_mobilenet(n_classes, pretrained=True):
"""
creates MobileNet model with `n_classes` outputs
:param n_classes:
:param pretrained: (bool)
:return:
model (nn.Module)
"""
model = models.mobilenet_v2(pretrained=pretrained)
model.classifier[1] = nn.Linear(model.classifier[1].in_features, n_classes)
return model