-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
243 lines (212 loc) · 8.46 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
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
from re import M
import torch
from torch import nn, relu
from torch.distributions.kl import kl_divergence
from torch.distributions.multivariate_normal import MultivariateNormal
class LinearVariationalEncoder(nn.Module):
def __init__(self, input_dim, latent_dim):
super(LinearVariationalEncoder, self).__init__()
self.input_dim = input_dim
self.latent_dim = latent_dim
self.nn_mean = nn.Linear(input_dim, latent_dim, bias=False)
self.nn_logvar = nn.Linear(input_dim, latent_dim, bias=True)
def forward(self, x):
batch_size = x.size(0)
eps = torch.randn(batch_size, self.latent_dim)
mu = self.nn_mean(x)
logvar = self.nn_logvar(x)
sigma = logvar.div(2).exp()
return {'z': mu + sigma * eps,
'mu': mu,
'sigma': sigma}
class LinearVariationalDecoder(nn.Module):
def __init__(self, latent_dim, target_dim):
super(LinearVariationalDecoder, self).__init__()
self.latent_dim = latent_dim
self.target_dim = target_dim
self.l = nn.Linear(latent_dim, target_dim, bias=False)
def forward(self, z):
y = self.l(z)
return y
class LinearBetaVAE(nn.Module):
def __init__(self, input_dim, latent_dim, target_dim, eta_dec_sq, eta_prior_sq, beta, **kwargs):
super(LinearBetaVAE, self).__init__()
self.input_dim = input_dim
self.latent_dim = latent_dim
self.target_dim = target_dim
self.eta_dec_sq = eta_dec_sq
self.eta_prior_sq = eta_prior_sq
self.beta = beta
self.encoder = LinearVariationalEncoder(input_dim, latent_dim)
self.decoder = LinearVariationalDecoder(latent_dim, target_dim)
def forward(self, x, y):
encoded = self.encoder(x)
y_pred = self.decoder(encoded['z'])
rec_loss = torch.square(y - y_pred).sum(-1).mean(0) / self.eta_dec_sq / 2
kl_loss = .5 * (
- torch.log(encoded['sigma']**2 / self.eta_prior_sq).sum(-1)
- self.latent_dim
+ torch.norm(encoded['mu'], p=2, dim=-1) ** 2 / self.eta_prior_sq
+ (encoded['sigma'] ** 2 / self.eta_prior_sq).sum(-1)
).mean(0)
loss = rec_loss + self.beta * kl_loss
forward_dict = {
'z': encoded['z'],
'mu': encoded['mu'],
'sigma': encoded['sigma'],
'y_pred': y_pred,
'loss': loss,
'rec_loss': rec_loss,
'kl_loss': kl_loss * self.beta,
'enc_norm': self.encoder.nn_mean.weight.norm(),
'dec_norm': self.decoder.l.weight.norm()
}
return forward_dict
class ReLUVariationalEncoder(nn.Module):
def __init__(self, input_dim, latent_dim, hidden_dim):
super(ReLUVariationalEncoder, self).__init__()
self.input_dim = input_dim
self.latent_dim = latent_dim
self.hidden_dim = hidden_dim
self.nn_mean = nn.Sequential(
nn.Linear(input_dim, hidden_dim, bias=False),
nn.ReLU(),
nn.Linear(hidden_dim, latent_dim)
)
self.nn_logvar = nn.Sequential(
nn.Linear(input_dim, hidden_dim, bias=True),
nn.ReLU(),
nn.Linear(hidden_dim, latent_dim)
)
def forward(self, x):
batch_size = x.size(0)
eps = torch.randn(batch_size, self.latent_dim)
mu = self.nn_mean(x)
logvar = self.nn_logvar(x)
sigma = logvar.div(2).exp()
return {'z': mu + sigma * eps,
'mu': mu,
'sigma': sigma}
class ReLUVariationalDecoder(nn.Module):
def __init__(self, latent_dim, target_dim, hidden_dim):
super(ReLUVariationalDecoder, self).__init__()
self.latent_dim = latent_dim
self.target_dim = target_dim
self.hidden_dim = hidden_dim
self.l = nn.Sequential(
nn.Linear(latent_dim, hidden_dim, bias=False),
nn.ReLU(),
nn.Linear(hidden_dim, target_dim)
)
def forward(self, z):
y = self.l(z)
return y
class ReLUBetaVAE(nn.Module):
def __init__(self, input_dim, latent_dim, target_dim, hidden_dim, eta_dec_sq, eta_prior_sq, beta, **kwargs):
super(ReLUBetaVAE, self).__init__()
self.input_dim = input_dim
self.latent_dim = latent_dim
self.target_dim = target_dim
self.hidden_dim = hidden_dim
self.eta_dec_sq = eta_dec_sq
self.eta_prior_sq = eta_prior_sq
self.beta = beta
self.encoder = ReLUVariationalEncoder(input_dim, latent_dim, hidden_dim)
self.decoder = ReLUVariationalDecoder(latent_dim, target_dim, hidden_dim)
def forward(self, x, y):
encoded = self.encoder(x)
y_pred = self.decoder(encoded['z'])
rec_loss = torch.square(y - y_pred).sum(-1).mean(0) / self.eta_dec_sq / 2
kl_loss = .5 * (
- torch.log(encoded['sigma']**2 / self.eta_prior_sq).sum(-1)
- self.latent_dim
+ torch.norm(encoded['mu'], p=2, dim=-1) ** 2 / self.eta_prior_sq
+ (encoded['sigma'] ** 2 / self.eta_prior_sq).sum(-1)
).mean(0)
loss = rec_loss + self.beta * kl_loss
forward_dict = {
'z': encoded['z'],
'mu': encoded['mu'],
'sigma': encoded['sigma'],
'y_pred': y_pred,
'loss': loss,
'rec_loss': rec_loss,
'kl_loss': kl_loss * self.beta,
# 'enc_norm': self.encoder.nn_mean.weight.norm(),
# 'dec_norm': self.decoder.l.weight.norm()
}
return forward_dict
class TanhVariationalEncoder(nn.Module):
def __init__(self, input_dim, latent_dim, hidden_dim):
super(TanhVariationalEncoder, self).__init__()
self.input_dim = input_dim
self.latent_dim = latent_dim
self.hidden_dim = hidden_dim
self.nn_mean = nn.Sequential(
nn.Linear(input_dim, hidden_dim, bias=False),
nn.ReLU(),
nn.Linear(hidden_dim, latent_dim)
)
self.nn_logvar = nn.Sequential(
nn.Linear(input_dim, hidden_dim, bias=True),
nn.ReLU(),
nn.Linear(hidden_dim, latent_dim)
)
def forward(self, x):
batch_size = x.size(0)
eps = torch.randn(batch_size, self.latent_dim)
mu = self.nn_mean(x)
logvar = self.nn_logvar(x)
sigma = logvar.div(2).exp()
return {'z': mu + sigma * eps,
'mu': mu,
'sigma': sigma}
class TanhVariationalDecoder(nn.Module):
def __init__(self, latent_dim, target_dim, hidden_dim):
super(TanhVariationalDecoder, self).__init__()
self.latent_dim = latent_dim
self.target_dim = target_dim
self.hidden_dim = hidden_dim
self.l = nn.Sequential(
nn.Linear(latent_dim, hidden_dim, bias=False),
nn.ReLU(),
nn.Linear(hidden_dim, target_dim)
)
def forward(self, z):
y = self.l(z)
return y
class TanhBetaVAE(nn.Module):
def __init__(self, input_dim, latent_dim, target_dim, hidden_dim, eta_dec_sq, eta_prior_sq, beta, **kwargs):
super(TanhBetaVAE, self).__init__()
self.input_dim = input_dim
self.latent_dim = latent_dim
self.target_dim = target_dim
self.hidden_dim = hidden_dim
self.eta_dec_sq = eta_dec_sq
self.eta_prior_sq = eta_prior_sq
self.beta = beta
self.encoder = TanhVariationalEncoder(input_dim, latent_dim, hidden_dim)
self.decoder = TanhVariationalDecoder(latent_dim, target_dim, hidden_dim)
def forward(self, x, y):
encoded = self.encoder(x)
y_pred = self.decoder(encoded['z'])
rec_loss = torch.square(y - y_pred).sum(-1).mean(0) / self.eta_dec_sq / 2
kl_loss = .5 * (
- torch.log(encoded['sigma']**2 / self.eta_prior_sq).sum(-1)
- self.latent_dim
+ torch.norm(encoded['mu'], p=2, dim=-1) ** 2 / self.eta_prior_sq
+ (encoded['sigma'] ** 2 / self.eta_prior_sq).sum(-1)
).mean(0)
loss = rec_loss + self.beta * kl_loss
forward_dict = {
'z': encoded['z'],
'mu': encoded['mu'],
'sigma': encoded['sigma'],
'y_pred': y_pred,
'loss': loss,
'rec_loss': rec_loss,
'kl_loss': kl_loss * self.beta,
# 'enc_norm': self.encoder.nn_mean.weight.norm(),
# 'dec_norm': self.decoder.l.weight.norm()
}
return forward_dict