-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
184 lines (147 loc) · 5.69 KB
/
Copy pathmodel.py
File metadata and controls
184 lines (147 loc) · 5.69 KB
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
from torch import argmax
import torch.nn as nn
from gurobi_modules import NamedLinear, NamedConv2d, MILPNet
from torchvision.models import resnet50
class CIFAR10Model(nn.Module):
def __init__(self, internal_dim=2000):
nn.Module.__init__(self)
self.conv = NamedConv2d(3, 6, 4)
self.dense = nn.Linear(5046, internal_dim)
self.milp_model = MILPNet(nn.Sequential(NamedLinear(internal_dim, 10)), w_range=0.1)
def forward(self, x):
h = self.forward_till_dense(x)
y = self.milp_model(h)
out = y
return out
def forward_till_dense(self, x):
x = self.conv(x)
r = nn.functional.relu(x)
h = r.view(x.shape[0], -1)
o = self.dense(h)
o = nn.functional.relu(o)
return o
def predict(self, x):
logits = self.forward(x)
predictions = argmax(logits, dim=1)
return predictions
class CIFAR10ModelDeep(nn.Module):
def __init__(self, internal_dim=500):
nn.Module.__init__(self)
self.conv = NamedConv2d(3, 16, 1, padding=1)
self.conv1 = NamedConv2d(16, 32, 3, 1, padding=1)
self.conv2 = NamedConv2d(32, 64, 3, 1, padding=1)
self.dense = nn.Linear(4*4*64, internal_dim)
self.milp_model = MILPNet(nn.Sequential(NamedLinear(internal_dim, 10)), w_range=0.1)
def forward(self, x):
h = self.forward_till_dense(x)
out = self.milp_model(h)
return out
def forward_till_dense(self, x):
x = self.conv(x)
x = nn.functional.relu(x)
x = nn.functional.max_pool2d(x, 2, 2)
x = self.conv1(x)
x = nn.functional.relu(x)
x = nn.functional.max_pool2d(x, 2, 2)
x = self.conv2(x)
x = nn.functional.relu(x)
x = nn.functional.max_pool2d(x, 2, 2)
h = x.view(x.shape[0], -1)
h = self.dense(h)
o = nn.functional.relu(h)
return o
def predict(self, x):
logits = self.forward(x)
predictions = argmax(logits, dim=1)
return predictions
class MNISTModel(nn.Module):
def __init__(self):
nn.Module.__init__(self)
self.conv = NamedConv2d(1, 3, 4)
self.milp_model = MILPNet(nn.Sequential(NamedLinear(1875, 10)), w_range=0.1)
def forward(self, x):
h = self.forward_till_dense(x)
y = self.milp_model(h)
out = y
return out
def forward_till_dense(self, x):
x = self.conv(x)
r = nn.functional.relu(x)
h = r.view(x.shape[0], -1)
return h
def predict(self, x):
logits = self.forward(x)
predictions = argmax(logits, dim=1)
return predictions
class PreTrainedMNISTModel(nn.Module):
def __init__(self):
nn.Module__init__(self)
# Load a pretrained resnet model from torchvision.models in Pytorch
self.model = resnet50(pretrained=True)
# Change the input layer to take Grayscale image, instead of RGB images.
# Hence in_channels is set as 1 or 3 respectively
# original definition of the first layer on the ResNet class
# self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
self.model.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
self.model = nn.Sequential(*list(self.model.children()[:-1]))
# Change the output layer to output 10 classes instead of 1000 classes
num_ftrs = self.model.fc.in_features
self.milp_model = MILPNet(nn.Sequential(NamedLinear(num_ftrs, 10)), w_range=0.01)
def forward(self, x):
return self.milp_model(self.model(x))
def forward_till_dense(self, x):
return self.model(x)
def predict(self, x):
logits = self.forward(x)
predictions = argmax(logits, dim=1)
return predictions
class SimpleRegression(nn.Module):
def __init__(self, input_dim, output_dim, w_range=0.1):
nn.Module.__init__(self)
assert output_dim < input_dim
self.layer_1 = nn.Linear(input_dim, (input_dim + output_dim)//2)
self.milp_model = MILPNet(nn.Sequential(NamedLinear((input_dim + output_dim)//2, output_dim) ),
classification=False, w_range=w_range)
def forward(self, x):
h = self.forward_till_dense(x)
y = self.milp_model(h)
out = y
return out
def forward_till_dense(self, x):
x = self.layer_1(x)
r = nn.functional.relu(x)
return r
def predict(self, x):
return self.forward(x)
class SingleLayerRegression(nn.Module):
def __init__(self, input_dim, output_dim, w_range=0.1):
nn.Module.__init__(self)
self.milp_model = MILPNet(nn.Sequential(NamedLinear(input_dim, output_dim)) ,
classification=False, w_range=w_range)
def forward(self, x):
y = self.milp_model(x)
out = y
return out
def forward_till_dense(self, x):
return x
def predict(self, x):
return self.forward(x)
class SimpleClassification(nn.Module):
def __init__(self, input_dim, output_dim, w_range=0.1):
nn.Module.__init__(self)
intermediate = (input_dim + output_dim)//2
self.layer_1 = nn.Linear(input_dim, intermediate)
self.milp_model = MILPNet(nn.Sequential(NamedLinear(intermediate, output_dim)),
classification=True, w_range=w_range)
def forward(self, x):
h = self.forward_till_dense(x)
y = self.milp_model(h)
out = y
return out
def forward_till_dense(self, x):
x = self.layer_1(x)
r = nn.functional.relu(x)
return r
def predict(self, x):
logits = self.forward(x)
return argmax(logits, dim=1)