-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmlp.py
159 lines (131 loc) · 5.08 KB
/
mlp.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
import torch
import torch.nn as nn
class Swish(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x):
return x * torch.sigmoid(x)
def mlp_ebm(nin, nint=256, nout=1):
return nn.Sequential(
nn.Linear(nin, nint),
Swish(),
nn.Linear(nint, nint),
Swish(),
nn.Linear(nint, nint),
Swish(),
nn.Linear(nint, nout),
)
class MLPEBM_cat(nn.Module):
def __init__(self, nin, n_proj, n_cat=256, nint=256, nout=1):
super().__init__()
self.proj = nn.Linear(n_cat, n_proj)
self.n_proj = n_proj
self.net = mlp_ebm(nin * n_proj, nint, nout=nout)
def forward(self, x):
xr = x.view(x.size(0) * x.size(1), x.size(2))
xr_p = self.proj(xr)
x_p = xr_p.view(x.size(0), x.size(1), self.n_proj)
x_p = x_p.view(x.size(0), x.size(1) * self.n_proj)
return self.net(x_p)
def conv_transpose_3x3(in_planes, out_planes, stride=1):
return nn.ConvTranspose2d(in_planes, out_planes,
kernel_size=3, stride=stride, padding=1, output_padding=1, bias=True)
def conv3x3(in_planes, out_planes, stride=1):
if stride < 0:
return conv_transpose_3x3(in_planes, out_planes, stride=-stride)
else:
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=True)
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_planes, planes, stride=1, out_nonlin=True):
super(BasicBlock, self).__init__()
self.nonlin1 = Swish()
self.nonlin2 = Swish()
self.conv1 = conv3x3(in_planes, planes, stride)
self.conv2 = conv3x3(planes, planes)
self.out_nonlin = out_nonlin
self.shortcut_conv = None
if stride != 1 or in_planes != self.expansion * planes:
if stride < 0:
self.shortcut_conv = nn.ConvTranspose2d(in_planes, self.expansion*planes,
kernel_size=1, stride=-stride,
output_padding=1, bias=True)
else:
self.shortcut_conv = nn.Conv2d(in_planes, self.expansion*planes,
kernel_size=1, stride=stride, bias=True)
def forward(self, x):
out = self.nonlin1(self.conv1(x))
out = self.conv2(out)
if self.shortcut_conv is not None:
out_sc = self.shortcut_conv(x)
out += out_sc
else:
out += x
if self.out_nonlin:
out = self.nonlin2(out)
return out
class ResNetEBM(nn.Module):
def __init__(self, n_channels=64):
super().__init__()
self.proj = nn.Conv2d(1, n_channels, 3, 1, 1)
downsample = [
BasicBlock(n_channels, n_channels, 2),
BasicBlock(n_channels, n_channels, 2)
]
main = [BasicBlock(n_channels, n_channels, 1) for _ in range(6)]
all = downsample + main
self.net = nn.Sequential(*all)
self.energy_linear = nn.Linear(n_channels, 1)
def forward(self, input):
input = input.view(input.size(0), 1, 28, 28)
input = self.proj(input)
out = self.net(input)
out = out.view(out.size(0), out.size(1), -1).mean(-1)
return self.energy_linear(out).squeeze()
class MNISTConvNet(nn.Module):
def __init__(self, nc=16):
super().__init__()
self.net = nn.Sequential(
nn.Conv2d(1, nc, 3, 1, 1),
Swish(),
nn.Conv2d(nc, nc * 2, 4, 2, 1),
Swish(),
nn.Conv2d(nc * 2, nc * 2, 3, 1, 1),
Swish(),
nn.Conv2d(nc * 2, nc * 4, 4, 2, 1),
Swish(),
nn.Conv2d(nc * 4, nc * 4, 3, 1, 1),
Swish(),
nn.Conv2d(nc * 4, nc * 8, 4, 2, 1),
Swish(),
nn.Conv2d(nc * 8, nc * 8, 3, 1, 0),
Swish(),
)
self.out = nn.Linear(nc * 8, 1)
def forward(self, input):
input = input.view(input.size(0), 1, 28, 28)
out = self.net(input)
out = out.squeeze()
return self.out(out).squeeze()
class ResNetEBM_cat(nn.Module):
def __init__(self, shape, n_proj, n_cat=256, n_channels=64):
super().__init__()
self.shape = shape
self.n_cat = n_cat
self.proj = nn.Conv2d(n_cat, n_proj, 1, 1, 0)
self.proj2 = nn.Conv2d(n_proj, n_channels, 3, 1, 1)
downsample = [
BasicBlock(n_channels, n_channels, 2),
BasicBlock(n_channels, n_channels, 2)
]
main = [BasicBlock(n_channels, n_channels, 1) for _ in range(6)]
all = downsample + main
self.net = nn.Sequential(*all)
self.energy_linear = nn.Linear(n_channels, 1)
def forward(self, input):
input = input.view(input.size(0), self.shape[1], self.shape[2], self.n_cat).permute(0, 3, 1, 2)
input = self.proj(input)
input = self.proj2(input)
out = self.net(input)
out = out.view(out.size(0), out.size(1), -1).mean(-1)
return self.energy_linear(out).squeeze()