-
Notifications
You must be signed in to change notification settings - Fork 6
/
models.py
182 lines (161 loc) · 5.15 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
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
# import PyTorch
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net_MNIST(nn.Module):
def __init__(self):
super(Net_MNIST, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 5, 1, 2)
self.conv2 = nn.Conv2d(32, 64, 5, 1, 2)
self.fc1 = nn.Linear(7*7*64, 512)
self.fc2 = nn.Linear(512, 16)
self.head = None
self.cur_model = None
def forward(self, x):
assert self.head is not None
assert self.cur_model is not None
out = self.cur_model(x)
out = self.head(out)
return out
'''
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2, 2)
x = x.view(-1, 7*7*64)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return F.log_softmax(x, dim=1)
'''
def gen_submodel(self):
model = SubModel(self.cur_model, self.head)
return model
def set_submodel(self, ind):
if ind == -1:
self.head = self.fc2
self.cur_model = nn.Sequential(self.conv1,
nn.ReLU(),
nn.MaxPool2d((2, 2)),
self.conv2,
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Flatten(),
self.fc1,
nn.ReLU())
'''
class Net_CIFAR10(nn.Module):
def __init__(self):
super(Net_CIFAR10, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 3)
self.conv2_1 = nn.Conv2d(32, 64, 3)
self.conv2_2 = nn.Conv2d(64, 64, 3)
self.fc1 = nn.Linear(4*4*64, 64)
self.fc2 = nn.Linear(64, 16)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.conv2_1(x))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.conv2_2(x))
x = x.view(-1, 4*4*64)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
'''
class SubModel(nn.Module):
def __init__(self, cur_model, head):
super(SubModel, self).__init__()
self.cur_model = cur_model
self.head = head
def forward(self, x):
x = self.cur_model(x)
x = self.head(x)
return x
def print_weight(self):
for n, p in self.cur_model.named_parameters():
print(n, p)
def return_num_parameters(self):
total = 0
for p in self.cur_model.parameters():
total += torch.numel(p)
for p in self.head.parameters():
total += torch.numel(p)
return total
class Net_CIFAR10(nn.Module):
def __init__(self):
super(Net_CIFAR10, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 3)
self.conv2_1 = nn.Conv2d(32, 64, 3)
self.conv2_2 = nn.Conv2d(64, 64, 3)
self.fc1 = nn.Linear(4*4*64, 64)
self.fc2 = nn.Linear(64, 16)
self.head = None
self.cur_model = None
def forward(self, x):
assert self.head is not None
assert self.cur_model is not None
out = self.cur_model(x)
out = self.head(out)
return out
'''
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.conv2_1(x))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.conv2_2(x))
x = x.view(-1, 4*4*64)
x = F.relu(self.fc1(x))
x = self.fc2(x)
'''
def gen_submodel(self):
model = SubModel(self.cur_model, self.head)
return model
def set_submodel(self, ind):
if ind == -1:
self.head = self.fc2
self.cur_model = nn.Sequential(self.conv1,
nn.ReLU(),
nn.MaxPool2d((2, 2)),
self.conv2_1,
nn.ReLU(),
nn.MaxPool2d(2, 2),
self.conv2_2,
nn.ReLU(),
nn.Flatten(),
self.fc1,
nn.ReLU())
elif ind == 0:
self.head = nn.Sequential(nn.AdaptiveAvgPool2d(1),
nn.Flatten(),
nn.Linear(32, 10))
self.cur_model = nn.Sequential(self.conv1,
nn.ReLU(),
nn.MaxPool2d((2, 2)))
elif ind == 1:
self.head = nn.Sequential(nn.AdaptiveAvgPool2d(1),
nn.Flatten(),
nn.Linear(64, 10))
self.cur_model = nn.Sequential(*self.cur_model,
self.conv2_1,
nn.ReLU(),
nn.MaxPool2d(2, 2))
elif ind == 2:
self.head = nn.Sequential(nn.AdaptiveAvgPool2d(1),
nn.Flatten(),
nn.Linear(64, 10))
self.cur_model = nn.Sequential(*self.cur_model,
self.conv2_2,
nn.ReLU())
elif ind == 3:
self.head = self.fc2
self.cur_model = nn.Sequential(*self.cur_model,
nn.Flatten(),
self.fc1,
nn.ReLU())
def return_num_parameters(self):
total = 0
for p in self.cur_model.parameters():
total += torch.numel(p)
for p in self.head.parameters():
total += torch.numel(p)
return total