-
Notifications
You must be signed in to change notification settings - Fork 17
/
torch_model.py
331 lines (278 loc) · 8.34 KB
/
torch_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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import pdb
class PermEqui1_max(nn.Module):
def __init__(self, in_dim, out_dim):
super(PermEqui1_max, self).__init__()
self.Gamma = nn.Linear(in_dim, out_dim)
def forward(self, x):
xm, _ = x.max(1, keepdim=True)
x = self.Gamma(x-xm)
return x
class PermEqui2_max(nn.Module):
def __init__(self, in_dim, out_dim):
super(PermEqui2_max, self).__init__()
self.Gamma = nn.Linear(in_dim, out_dim)
self.Lambda = nn.Linear(in_dim, out_dim, bias=False)
def forward(self, x):
xm, _ = x.max(1, keepdim=True)
xm = self.Lambda(xm)
x = self.Gamma(x)
x = x - xm
return x
class PermEqui2_mean(nn.Module):
def __init__(self, in_dim, out_dim):
super(PermEqui2_mean, self).__init__()
self.Gamma = nn.Linear(in_dim, out_dim)
self.Lambda = nn.Linear(in_dim, out_dim, bias=False)
def forward(self, x):
xm = x.mean(1, keepdim=True)
xm = self.Lambda(xm)
x = self.Gamma(x)
x = x - xm
return x
class G_inv_Tanh(nn.Module):
def __init__(self, x_dim, d_dim, z1_dim, pool = 'mean'):
super(G_inv_Tanh, self).__init__()
self.d_dim = d_dim
self.x_dim = x_dim
self.z1_dim = z1_dim
self.pool = pool
if pool == 'max':
self.phi = nn.Sequential(
PermEqui2_max(self.x_dim, self.d_dim),
nn.Tanh(),
PermEqui2_max(self.d_dim, self.d_dim),
nn.Tanh(),
PermEqui2_max(self.d_dim, self.d_dim),
nn.Tanh(),
)
elif pool == 'max1':
self.phi = nn.Sequential(
PermEqui1_max(self.x_dim, self.d_dim),
nn.Tanh(),
PermEqui1_max(self.d_dim, self.d_dim),
nn.Tanh(),
PermEqui1_max(self.d_dim, self.d_dim),
nn.Tanh(),
)
elif pool == 'mean':
self.phi = nn.Sequential(
PermEqui2_mean(self.x_dim, self.d_dim),
nn.Tanh(),
PermEqui2_mean(self.d_dim, self.d_dim),
nn.Tanh(),
PermEqui2_mean(self.d_dim, self.d_dim),
nn.Tanh(),
)
self.ro = nn.Sequential(
nn.Linear(self.d_dim, self.d_dim),
nn.Tanh(),
nn.Linear(self.d_dim, self.z1_dim),
)
print(self)
self.faster_parameters = [p for p in self.parameters()]
def forward(self, x):
phi_output = self.phi(x)
sum_output, _ = phi_output.max(1)
ro_output = self.ro(sum_output)
return ro_output
class G_inv(nn.Module):
def __init__(self, x_dim, d_dim, z1_dim, pool = 'mean'):
super(G_inv, self).__init__()
self.d_dim = d_dim
self.x_dim = x_dim
self.z1_dim = z1_dim
self.pool = pool
if pool == 'max':
self.phi = nn.Sequential(
PermEqui2_max(self.x_dim, self.d_dim),
nn.Softplus(),
PermEqui2_max(self.d_dim, self.d_dim),
nn.Softplus(),
PermEqui2_max(self.d_dim, self.d_dim),
nn.Softplus(),
)
elif pool == 'max1':
self.phi = nn.Sequential(
PermEqui1_max(self.x_dim, self.d_dim),
nn.Softplus(),
PermEqui1_max(self.d_dim, self.d_dim),
nn.Softplus(),
PermEqui1_max(self.d_dim, self.d_dim),
nn.Softplus(),
)
elif pool == 'mean':
self.phi = nn.Sequential(
PermEqui2_mean(self.x_dim, self.d_dim),
nn.Softplus(),
PermEqui2_mean(self.d_dim, self.d_dim),
nn.Softplus(),
PermEqui2_mean(self.d_dim, self.d_dim),
nn.Softplus(),
)
self.ro = nn.Sequential(
nn.Linear(self.d_dim, self.d_dim),
nn.Softplus(),
nn.Linear(self.d_dim, self.z1_dim),
)
print(self)
self.faster_parameters = [p for p in self.parameters()]
def forward(self, x):
phi_output = self.phi(x)
sum_output, _ = phi_output.max(1)
ro_output = self.ro(sum_output)
return ro_output
class D(nn.Module):
def __init__(self, x_dim, z1_dim, d_dim, o_dim=1):
super(D, self).__init__()
self.d_dim = d_dim
self.x_dim = x_dim
self.z1_dim = z1_dim
self.fc = nn.Linear(self.z1_dim, self.d_dim)
self.fu = nn.Linear(self.x_dim, self.d_dim, bias=False)
self.f = nn.Sequential(
nn.Softplus(),
nn.Linear(self.d_dim, self.d_dim),
nn.Softplus(),
nn.Linear(self.d_dim, self.d_dim),
nn.Softplus(),
nn.Linear(self.d_dim, self.d_dim),
nn.Softplus(),
nn.Linear(self.d_dim, o_dim),
)
print(self)
self.faster_parameters = [p for p in self.parameters()]
def forward(self, x, z1):
y = self.fc(z1) + self.fu(x)
return self.f(y)
class skipD(nn.Module):
def __init__(self, x_dim, z1_dim, d_dim, o_dim=1):
super(skipD, self).__init__()
self.d_dim = d_dim
self.x_dim = x_dim
self.z1_dim = z1_dim
#hid_d = 5*(z1_dim+z2_dim)
hid_d = max(1024, 2*z1_dim)
self.fc = nn.Linear(self.z1_dim, hid_d)
self.fu = nn.Linear(self.x_dim, hid_d, bias=False)
self.part1 = nn.Sequential(
nn.Softplus(),
nn.Linear(hid_d, hid_d),
nn.Softplus(),
nn.Linear(hid_d, hid_d - self.z1_dim),
)
self.sc = nn.Linear(self.z1_dim, hid_d)
self.su = nn.Linear(hid_d - self.z1_dim, hid_d, bias=False)
self.part2 = nn.Sequential(
nn.Softplus(),
nn.Linear(hid_d, hid_d),
nn.Softplus(),
nn.Linear(hid_d, hid_d - self.z1_dim)
)
self.tc = nn.Linear(self.z1_dim, hid_d)
self.tu = nn.Linear(hid_d - self.z1_dim, hid_d, bias=False)
self.part3 = nn.Sequential(
nn.Softplus(),
nn.Linear(hid_d, hid_d),
nn.Softplus(),
nn.Linear(hid_d, o_dim)
)
print(self)
self.faster_parameters = [p for p in self.parameters()]
def forward(self, x, z1):
y = self.fc(z1) + self.fu(x)
output = self.part1(y)
y2 = self.sc(z1) + self.su(output)
output1 = self.part2( y2 )
y3 = self.tc(z1) + self.tu(output1)
output2 = self.part3( y3)
return output2
class G(nn.Module):
def __init__(self, x_dim, z1_dim, z2_dim):
super(G, self).__init__()
self.z1_dim = z1_dim
self.z2_dim = z2_dim
self.x_dim = x_dim
hid_d = max(250, 2*z1_dim)
#hid_d = z1_dim+z2_dim
self.fc = nn.Linear(self.z1_dim, hid_d)
self.fu = nn.Linear(self.z2_dim, hid_d, bias=False)
self.main = nn.Sequential(
nn.Softplus(),
nn.Linear(hid_d, hid_d),
nn.Softplus(),
nn.Linear(hid_d, hid_d),
nn.Softplus(),
nn.Linear(hid_d, hid_d),
nn.Softplus(),
nn.Linear(hid_d, hid_d),
nn.Softplus(),
nn.Linear(hid_d, self.x_dim),
)
print(self)
self.faster_parameters = [p for p in self.parameters()]
def forward(self, z1, z2):
x = self.fc(z1) + self.fu(z2)
output = self.main(x)
return output
class skipG(nn.Module):
def __init__(self, x_dim, z1_dim, z2_dim):
super(skipG, self).__init__()
self.z1_dim = z1_dim
self.z2_dim = z2_dim
self.x_dim = x_dim
#hid_d = 5*(z1_dim+z2_dim)
hid_d = 250
self.fc = nn.Linear(self.z1_dim, hid_d)
self.fu = nn.Linear(self.z2_dim, hid_d, bias=False)
self.part1 = nn.Sequential(
nn.Softplus(),
nn.Linear(hid_d, hid_d),
nn.Softplus(),
nn.Linear(hid_d, z2_dim),
)
self.sc = nn.Linear(self.z1_dim, hid_d)
self.su = nn.Linear(self.z2_dim, hid_d, bias=False)
self.part2 = nn.Sequential(
nn.Softplus(),
nn.Linear(hid_d, hid_d),
nn.Softplus(),
nn.Linear(hid_d, x_dim)
)
print(self)
self.faster_parameters = [p for p in self.parameters()]
def forward(self, z1, z2):
x = self.fc(z1) + self.fu(z2)
output = self.part1(x)
x1 = self.sc(z1) + self.su(output)
output1 = self.part2(x1)
return output1
class ALPHA(nn.Module):
def __init__(self, z1_dim):
super(ALPHA, self).__init__()
self.z1_dim = z1_dim
hid_d = min(z1_dim + 50, 100)
#hid_d = z1_dim+z2_dim
self.main = nn.Sequential(
nn.Linear(self.z1_dim, hid_d),
nn.Softplus(),
nn.Linear(hid_d, hid_d),
nn.Softplus(),
nn.Linear(hid_d, hid_d),
nn.Softplus(),
nn.Linear(hid_d, hid_d),
nn.Softplus(),
nn.Linear(hid_d, 1),
)
print(self)
self.faster_parameters = [p for p in self.parameters()]
def forward(self, x):
output = self.main(x)
return output
def zero_weights_init(m):
if isinstance(m, nn.Linear):
m.weight.data.uniform_(-5e-3, 5e-3)
m.bias.data.fill_(0)