-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathct_module.py
232 lines (196 loc) · 7.16 KB
/
ct_module.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class LearnedSoftPlus(torch.nn.Module):
def __init__(self, init_beta=1.0, threshold=20):
super().__init__()
# keep beta > 0
self.log_beta = torch.nn.Parameter(torch.tensor(float(init_beta)).log())
self.threshold = 20
def forward(self, x):
beta = self.log_beta.exp()
beta_x = beta * x
return torch.where(beta_x < 20, torch.log1p(beta_x.exp()) / beta, x)
sigma = LearnedSoftPlus()
class DGMCell(nn.Module):
def __init__(self, d, M, growing, weight_norm):
super().__init__()
wn = WN if weight_norm else lambda x: x
self.Uz = wn(nn.Linear(d, M, bias=False))
self.Ug = wn(nn.Linear(d, M, bias=False))
self.Ur = wn(nn.Linear(d, M, bias=False))
self.Uh = wn(nn.Linear(d, M, bias=False))
self.Wz = wn(nn.Linear(M, M))
self.Wg = wn(nn.Linear(M, M))
self.Wr = wn(nn.Linear(M, M))
self.Wh = wn(nn.Linear(M, M))
self.A = (lambda x: x) if growing else sigma
def forward(self, SX):
S, X = SX
Z = sigma(self.Uz(X) + self.Wz(S))
G = sigma(self.Ug(X) + self.Wg(S))
R = sigma(self.Ur(X) + self.Wr(S))
H = self.A(self.Uh(X) + self.Wh(S*R))
S = (1-G)*H + Z*S
return S, X
def _set_convert(flag):
if flag: return lambda X: X[0]
return lambda X: torch.stack(X, -1)
class RNNLikeDGM(DGMCell):
"""
Args:
-----
d_in and d_out- input and ouput dimensions of the problem
M - layers' width
L - recurrency depth
"""
def __init__(
self, d_in, d_out, M=50, L=3,
growing=False, as_array=True, weight_norm=False):
super().__init__(d_in, M, growing, weight_norm)
self.L = L
wn = WN if weight_norm else lambda x: x
self.W0 = wn(nn.Linear(d_in, M))
self.W1 = wn(nn.Linear(M, d_out))
self._convert = _set_convert(as_array)
print('hello')
def forward(self, *X):
X = self._convert(X)
S = sigma(self.W0(X))
for l in range(self.L):
Z = sigma(self.Uz(X) + self.Wz(S))
G = sigma(self.Ug(X) + self.Wg(S))
R = sigma(self.Ur(X) + self.Wr(S))
H = self.A(self.Uh(X) + self.Wh(S*R))
S = (1-G)*H + Z*S
return self.W1(S)
class RNNEncoderLikeDGM(DGMCell):
"""
Args:
-----
d_in and d_out- input and ouput dimensions of the problem
M - layers' width
L - recurrency depth
"""
def __init__(
self, d_in, M=50, L=3,
growing=False, as_array=True, weight_norm=False):
super().__init__(d_in, M, growing, weight_norm)
self.L = L
wn = WN if weight_norm else lambda x: x
self.W0 = wn(nn.Linear(d_in, M))
self.W1 = wn(nn.Linear(M, M))
self._convert = _set_convert(as_array)
def forward(self, *X):
X = self._convert(X)
S = sigma(self.W0(X))
for l in range(self.L):
Z = sigma(self.Uz(X) + self.Wz(S))
G = sigma(self.Ug(X) + self.Wg(S))
R = sigma(self.Ur(X) + self.Wr(S))
H = self.A(self.Uh(X) + self.Wh(S*R))
S = (1-G)*H + Z*S
return self.W1(S)
def get_last_shared_layer(self):
return self.W1
class Decoder(nn.Module):
def __init__(self, M, d_out, weight_norm=False):
super().__init__()
wn = WN if weight_norm else lambda x: x
self.linear = wn(nn.Linear(M, d_out))
def forward(self, feature):
return self.linear(feature).squeeze_(-1)
class FFN(nn.Module):
def __init__(self):
super(FFN, self).__init__()
self.f = nn.Sequential(nn.Linear(2, 50),
nn.Tanh(),
nn.Linear(50, 50),
nn.Tanh(),
nn.Linear(50, 50),
nn.Tanh(),
nn.Linear(50, 50),
nn.Tanh())
# x represents our data
def forward(self, feature):
return self.f(feature)
class HDL(nn.Module):
def __init__(self):
super(HDL, self).__init__()
self.fc = nn.Linear(50, 1)
def forward(self, feature):
return self.fc(feature).squeeze_(-1)
class FFN_mtl(nn.Module):
def __init__(self):
super(FFN_mtl, self).__init__()
self.f = nn.Sequential(nn.Linear(2, 50),
nn.Tanh(),
nn.Linear(50, 50),
nn.Tanh(),
nn.Linear(50, 50),
nn.Tanh())
self.W1 = nn.Sequential(nn.Linear(50, 50), nn.Tanh())
self.fc1 = nn.Linear(50, 1)
self.fc2 = nn.Linear(50, 1)
# x represents our data
def forward(self, feature):
enc = self.f(feature)
enc = self.W1(enc)
return self.fc1(enc).squeeze_(-1), self.fc2(enc).squeeze_(-1)
# class RNNLikeDGM(DGMCell):
# """
# Args:
# -----
# d_in and d_out- input and ouput dimensions of the problem
# M - layers' width
# L - recurrency depth
# """
# def __init__(
# self, d_in, d_out, M=50, L=3,
# growing=False, as_array=True, weight_norm=False):
# super().__init__(d_in, M, growing, weight_norm)
# self.L = L
# wn = WN if weight_norm else lambda x: x
# self.W0 = wn(nn.Linear(d_in, M))
# self.W1 = wn(nn.Linear(M, M))
# self.out1 = wn(nn.Linear(M, d_out))
# self.out2 = wn(nn.Linear(M, d_out))
# self._convert = _set_convert(as_array)
# def forward(self, *X):
# X = self._convert(X)
# S = sigma(self.W0(X))
# for l in range(self.L):
# Z = sigma(self.Uz(X) + self.Wz(S))
# G = sigma(self.Ug(X) + self.Wg(S))
# R = sigma(self.Ur(X) + self.Wr(S))
# H = self.A(self.Uh(X) + self.Wh(S*R))
# S = (1-G)*H + Z*S
# S = self.W1(S)
# return self.out1(S).squeeze_(-1), self.out2(S).squeeze_(-1)
def get_last_shared_layer(self):
return self.W1
def get_model(d_in, d_out, M):
model = {}
model['rep'] = RNNEncoderLikeDGM(d_in, M=M)
model['f'] = Decoder(M, d_out)
model['s'] = Decoder(M, d_out)
return model
def get_model_FFN(d_in, d_out, M):
model = {}
model['rep'] = FFN()
model['f'] = HDL()
model['s'] = HDL()
return model
class CrossStich(nn.Module):
def __init__(self,):
super(CrossStich, self).__init__()
self.transform = nn.Parameter(data=torch.eye(2), requires_grad=True)
def forward(self, input_1, input_2):
return self.transform[0][0]*input_1 + self.transform[0][1]*input_2, self.transform[1][0]*input_1 + self.transform[1][1]*input_2
class LargeCrossStich(nn.Module):
def __init__(self, size):
super(LargeCrossStich, self).__init__()
self.transform = nn.Parameter(data=torch.eye(2*size), requires_grad=True)
def forward(self, input_1, input_2):
out = torch.mm(torch.cat([input_1, input_2], 1), self.transform)
return out[:, 0:input_1.shape[1]], out[:, input_1.shape[1]:]