-
Notifications
You must be signed in to change notification settings - Fork 1
/
inferer.py
202 lines (138 loc) · 7.63 KB
/
inferer.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class Prior(nn.Module):
def __init__(self, hidden_size, latent_size, dpt=0.3):
super(Prior, self).__init__()
self.latent_size = latent_size
self.hidden_size = hidden_size
self.linear = nn.Linear(2*hidden_size, latent_size)
self.linear_mu = nn.Linear(latent_size, latent_size)
self.linear_var = nn.Linear(latent_size, latent_size)
self.dropout = nn.Dropout(p=dpt)
def forward(self, src, encoded_src):
encoded_src = encoded_src.transpose(0,1).transpose(1,2)
h_src = F.avg_pool1d(encoded_src, encoded_src.size(2)).view(encoded_src.size(0), -1)
h_src = self.dropout(h_src)
h_z = F.tanh(self.linear(h_src))
h_z = self.dropout(h_z)
mu = self.linear_mu(h_z)
log_var = self.linear_var(h_z)
return mu, log_var
class SelfAttentionPrior(nn.Module):
def __init__(self, hidden_size, latent_size, dpt=0.3):
super(SelfAttentionPrior, self).__init__()
self.latent_size = latent_size
self.hidden_size = hidden_size
self.linear = nn.Linear(4*hidden_size, latent_size)
self.linear_mu = nn.Linear(latent_size, latent_size)
self.linear_var = nn.Linear(latent_size, latent_size)
self.dropout = nn.Dropout(p=dpt)
def forward(self, src, encoded_src):
h_src = encoded_src.transpose(0,1)
attn_src = torch.bmm(h_src, h_src.transpose(1, 2)) # b x t_o x t_i
attn_src = F.softmax(attn_src, dim=2)
c_src = torch.bmm(attn_src, h_src) # b x t_o x h
c_src = self.dropout(c_src)
h = torch.cat((c_src.mean(dim=1), h_src.mean(dim=1)), dim=1)
h_z = F.tanh(self.linear(h))
h_z = self.dropout(h_z)
mu = self.linear_mu(h_z)
log_var = self.linear_var(h_z)
return mu, log_var
class ApproximatePosterior(nn.Module):
def __init__(self, hidden_size, latent_size, dpt=0.3):
super(ApproximatePosterior, self).__init__()
self.latent_size = latent_size
self.hidden_size = hidden_size
self.linear = nn.Linear(4*hidden_size, latent_size)
self.linear_mu = nn.Linear(latent_size, latent_size)
self.linear_var = nn.Linear(latent_size, latent_size)
self.dropout = nn.Dropout(p=dpt)
def forward(self, src, encoded_src, trg, encoded_trg):
encoded_src = encoded_src.transpose(0,1).transpose(1,2)
encoded_trg = encoded_trg.transpose(0,1).transpose(1,2)
h_src = F.avg_pool1d(encoded_src, encoded_src.size(2)).view(encoded_src.size(0), -1)
h_trg = F.avg_pool1d(encoded_trg, encoded_trg.size(2)).view(encoded_trg.size(0), -1)
h_src = self.dropout(h_src)
h_trg = self.dropout(h_trg)
h_z = F.tanh(self.linear(torch.cat((h_src, h_trg), dim=1)))
h_z = self.dropout(h_z)
mu = self.linear_mu(h_z)
log_var = self.linear_var(h_z)
return mu, log_var
class AttentionApproximatePosterior(nn.Module):
def __init__(self, src_vocab_size, trg_vocab_size, embed_size, hidden_size, latent_size, dpt=0.3, src_embedding=None, trg_embedding=None):
super(AttentionApproximatePosterior, self).__init__()
self.latent_size = latent_size
self.hidden_size = hidden_size
if src_embedding is not None:
self.src_embedding = src_embedding
else:
self.src_embedding = nn.Embedding(src_vocab_size, embed_size)
self.src_embedding.weight.data.copy_((torch.rand(src_vocab_size, embed_size) - 0.5) * 2)
if trg_embedding is not None:
self.trg_embedding = trg_embedding
else:
self.trg_embedding = nn.Embedding(trg_vocab_size, embed_size)
self.trg_embedding.weight.data.copy_((torch.rand(trg_vocab_size, embed_size) - 0.5) * 2)
self.linear_src = nn.Linear(2*embed_size, hidden_size)
self.linear_trg = nn.Linear(2*embed_size, hidden_size)
self.linear = nn.Linear(2*hidden_size, latent_size)
self.linear_mu = nn.Linear(latent_size, latent_size)
self.linear_var = nn.Linear(latent_size, latent_size)
self.dropout = nn.Dropout(p=dpt)
def forward(self, src, encoded_src, trg, encoded_trg):
x_src = self.src_embedding(src).transpose(0, 1) # b x t_i x e
x_trg = self.trg_embedding(trg).transpose(0, 1) # b x t_o x e
# Currently just basic dot attention on embeddings. May want to change later
attn_src = torch.bmm(x_trg, x_src.transpose(1, 2)) # b x t_o x t_i
attn_src = F.softmax(attn_src, dim=2)
c_src = torch.bmm(attn_src, x_src) # b x t_o x e
attn_trg = torch.bmm(x_src, x_trg.transpose(1, 2)) # b x t_i x t_o
attn_trg = F.softmax(attn_trg, dim=2)
c_trg = torch.bmm(attn_trg, x_trg) # b x t_i x e
c_src = self.dropout(c_src)
c_trg = self.dropout(c_trg)
v_src = F.tanh(self.linear_src(torch.cat((c_trg, x_src), dim=2)).sum(dim=1)) # b x h
v_trg = F.tanh(self.linear_trg(torch.cat((c_src, x_trg), dim=2)).sum(dim=1)) # b x h
h_z = F.tanh(self.linear(torch.cat((v_src, v_trg), dim=1)))
h_z = self.dropout(h_z)
mu = self.linear_mu(h_z)
log_var = self.linear_var(h_z)
return mu, log_var
class LSTMAttentionApproximatePosterior(nn.Module):
def __init__(self, hidden_size, latent_size, dpt=0.3):
super(LSTMAttentionApproximatePosterior, self).__init__()
self.latent_size = latent_size
self.hidden_size = hidden_size
#self.proj_src = nn.Sequential(nn.Linear(2*hidden_size, 2*hidden_size), nn.Tanh(), nn.Linear(2*hidden_size, 2*hidden_size))
#self.proj_trg = nn.Sequential(nn.Linear(2*hidden_size, 2*hidden_size), nn.Tanh(), nn.Linear(2*hidden_size, 2*hidden_size))
#self.linear_src = nn.Linear(4*hidden_size, hidden_size)
#self.linear_trg = nn.Linear(4*hidden_size, hidden_size)
self.linear = nn.Linear(8*hidden_size, latent_size)
self.linear_mu = nn.Linear(latent_size, latent_size)
self.linear_var = nn.Linear(latent_size, latent_size)
self.dropout = nn.Dropout(p=dpt)
def forward(self, src, encoded_src, trg, encoded_trg):
#h_src = self.proj_src(encoded_src.transpose(0, 1)) # b x t_i x h
#h_trg = self.proj_trg(encoded_trg.transpose(0, 1)) # b x t_o x h
h_src = encoded_src.transpose(0, 1)
h_trg = encoded_trg.transpose(0, 1)
attn_src = torch.bmm(h_trg, h_src.transpose(1, 2)) # b x t_o x t_i
attn_src = F.softmax(attn_src, dim=2)
c_src = torch.bmm(attn_src, h_src) # b x t_o x h
attn_trg = torch.bmm(h_src, h_trg.transpose(1, 2)) # b x t_i x t_o
attn_trg = F.softmax(attn_trg, dim=2)
c_trg = torch.bmm(attn_trg, h_trg) # b x t_i x h
c_src = self.dropout(c_src)
c_trg = self.dropout(c_trg)
#v_src = F.tanh(self.linear_src(torch.cat((c_trg, h_src), dim=2)).sum(dim=1)) # b x h
#v_trg = F.tanh(self.linear_trg(torch.cat((c_src, h_trg), dim=2)).sum(dim=1)) # b x h
#h_z = F.tanh(self.linear(torch.cat((v_src, v_trg), dim=1)))
h = torch.cat((c_src.mean(dim=1), c_trg.mean(dim=1), h_src.mean(dim=1), h_trg.mean(dim=1)), dim=1)
h_z = F.tanh(self.linear(h))
h_z = self.dropout(h_z)
mu = self.linear_mu(h_z)
log_var = self.linear_var(h_z)
return mu, log_var