-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
202 lines (173 loc) · 6.71 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
195
196
197
198
199
200
201
from __future__ import print_function
import math
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
import torch.optim as optim
class MLPNet(nn.Module):
def __init__(self):
super(MLPNet, self).__init__()
self.fc1 = nn.Linear(28*28, 256)
self.fc2 = nn.Linear(256, 10)
def forward(self, x):
x = x.view(-1, 28*28)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv1 = nn.Conv2d(1,6,5,stride=1,padding=2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(400, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
out = F.relu(self.conv1(x))
out = F.max_pool2d(out, 2)
out = F.relu(self.conv2(out))
out = F.max_pool2d(out, 2)
out = out.view(out.size(0), -1)
out = F.relu(self.fc1(out))
out = F.relu(self.fc2(out))
out = self.fc3(out)
return out
class LeNet_bayes(nn.Module):
def __init__(self):
super(LeNet_bayes, self).__init__()
self.conv1 = nn.Conv2d(1,6,5,stride=1,padding=2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(400, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 100)
def forward(self, x):
out = F.relu(self.conv1(x))
out = F.max_pool2d(out, 2)
out = F.relu(self.conv2(out))
out = F.max_pool2d(out, 2)
out = out.view(out.size(0), -1)
out = F.relu(self.fc1(out))
out = F.relu(self.fc2(out))
out = self.fc3(out)
return out
class CNN_small(nn.Module):
def __init__(self, num_classes=10):
super(CNN_small, self).__init__()
self.conv1 = nn.Conv2d(1, 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, 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, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def call_bn(bn, x):
return bn(x)
class CNN(nn.Module):
def __init__(self, input_channel=3, n_outputs=10, dropout_rate=0.25, top_bn=False):
self.dropout_rate = dropout_rate
self.top_bn = top_bn
super(CNN, self).__init__()
self.c1=nn.Conv2d(input_channel,128,kernel_size=3,stride=1, padding=1)
self.c2=nn.Conv2d(128,128,kernel_size=3,stride=1, padding=1)
self.c3=nn.Conv2d(128,128,kernel_size=3,stride=1, padding=1)
self.c4=nn.Conv2d(128,256,kernel_size=3,stride=1, padding=1)
self.c5=nn.Conv2d(256,256,kernel_size=3,stride=1, padding=1)
self.c6=nn.Conv2d(256,256,kernel_size=3,stride=1, padding=1)
self.c7=nn.Conv2d(256,512,kernel_size=3,stride=1, padding=0)
self.c8=nn.Conv2d(512,256,kernel_size=3,stride=1, padding=0)
self.c9=nn.Conv2d(256,128,kernel_size=3,stride=1, padding=0)
self.l_c1=nn.Linear(128,n_outputs)
self.bn1=nn.BatchNorm2d(128)
self.bn2=nn.BatchNorm2d(128)
self.bn3=nn.BatchNorm2d(128)
self.bn4=nn.BatchNorm2d(256)
self.bn5=nn.BatchNorm2d(256)
self.bn6=nn.BatchNorm2d(256)
self.bn7=nn.BatchNorm2d(512)
self.bn8=nn.BatchNorm2d(256)
self.bn9=nn.BatchNorm2d(128)
def forward(self, x,):
h=x
h=self.c1(h)
h=F.leaky_relu(call_bn(self.bn1, h), negative_slope=0.01)
h=self.c2(h)
h=F.leaky_relu(call_bn(self.bn2, h), negative_slope=0.01)
h=self.c3(h)
h=F.leaky_relu(call_bn(self.bn3, h), negative_slope=0.01)
h=F.max_pool2d(h, kernel_size=2, stride=2)
h=F.dropout2d(h, p=self.dropout_rate)
h=self.c4(h)
h=F.leaky_relu(call_bn(self.bn4, h), negative_slope=0.01)
h=self.c5(h)
h=F.leaky_relu(call_bn(self.bn5, h), negative_slope=0.01)
h=self.c6(h)
h=F.leaky_relu(call_bn(self.bn6, h), negative_slope=0.01)
h=F.max_pool2d(h, kernel_size=2, stride=2)
h=F.dropout2d(h, p=self.dropout_rate)
h=self.c7(h)
h=F.leaky_relu(call_bn(self.bn7, h), negative_slope=0.01)
h=self.c8(h)
h=F.leaky_relu(call_bn(self.bn8, h), negative_slope=0.01)
h=self.c9(h)
h=F.leaky_relu(call_bn(self.bn9, h), negative_slope=0.01)
h=F.avg_pool2d(h, kernel_size=h.data.shape[2])
h = h.view(h.size(0), h.size(1))
logit=self.l_c1(h)
if self.top_bn:
logit=call_bn(self.bn_c1, logit)
return logit
class NewsNet(nn.Module):
def __init__(self, weights_matrix, context_size=1000, hidden_size=300, num_classes=7):
super(NewsNet, self).__init__()
n_embed, d_embed = weights_matrix.shape
self.embedding = nn.Embedding(n_embed, d_embed)
self.embedding.weight.data.copy_(torch.Tensor(weights_matrix))
self.avgpool=nn.AdaptiveAvgPool1d(16*hidden_size)
self.fc1 = nn.Linear(16*hidden_size, 4*hidden_size)
self.bn1=nn.BatchNorm1d(4*hidden_size)
self.ac = nn.Softsign()
self.fc2 = nn.Linear(4*hidden_size, hidden_size)
self.bn2=nn.BatchNorm1d(hidden_size)
self.fc3 = nn.Linear(hidden_size, num_classes)
def forward(self, x):
embed = self.embedding(x) # input (128, 1000)
embed = embed.detach() # embed (128, 1000, 300)
out = embed.view((1, embed.size()[0], -1)) # (1, 128, 300 000)
out = self.avgpool(out)
out = out.squeeze(0)
out = self.fc1(out)
out = self.bn1(out)
out = self.ac(out)
out = self.fc2(out)
out = self.bn2(out)
out = self.ac(out)
out = self.fc3(out)
return out
class LeNet_5_Caffe(nn.Module):
"""
This is based on Caffe's implementation of Lenet-5 and is slightly different
from the vanilla LeNet-5. Note that the first layer does NOT have padding
and therefore intermediate shapes do not match the official LeNet-5.
"""
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 20, 5, padding=0)
self.conv2 = nn.Conv2d(20, 50, 5)
self.fc3 = nn.Linear(50 * 4 * 4, 500)
self.fc4 = nn.Linear(500, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.fc3(x.view(-1, 50 * 4 * 4)))
x = self.fc4(x)
return x