-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmodules.py
433 lines (335 loc) · 14.1 KB
/
modules.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from utils import concatenate_dict
def mixture_of_bivariate_normal_nll(
data, log_pi, mu, log_sigma, rho, eps=1e-6
):
x, y = data.unsqueeze(-2).unbind(-1)
mu_1, mu_2 = mu.unbind(-1)
log_sigma_1, log_sigma_2 = log_sigma.unbind(-1)
sigma_1 = log_sigma_1.exp() + eps
sigma_2 = log_sigma_2.exp() + eps
# Compute log prob of bivariate normal distribution
Z = torch.pow((x - mu_1) / sigma_1, 2) + torch.pow((y - mu_2) / sigma_2, 2)
Z -= 2 * rho * ((x - mu_1) * (y - mu_2)) / (sigma_1 * sigma_2)
log_N = -Z / (2 * (1 - rho ** 2) + eps)
log_N -= np.log(2 * np.pi) + log_sigma_1 + log_sigma_2
log_N -= .5 * torch.log(1 - rho ** 2 + eps)
# Use log_sum_exp to accurately compute log prob of mixture distribution
nll = -torch.logsumexp(log_pi + log_N, dim=-1)
return nll
def mixture_of_bivariate_normal_sample(
log_pi, mu, log_sigma, rho, eps=1e-6, bias=0.
):
batch_size = log_pi.shape[0]
ndims = log_pi.dim()
if ndims > 2:
# Collapse batch and seq_len dimensions
log_pi, mu, log_sigma, rho = [
x.reshape(-1, *x.shape[2:])
for x in [log_pi, mu, log_sigma, rho]
]
# Sample mixture index using mixture probabilities pi
pi = log_pi.exp() * (1 + bias)
mixture_idx = pi.multinomial(1).squeeze(1)
# Index the correct mixture for mu, log_sigma and rho
mu, log_sigma, rho = [
x[torch.arange(mixture_idx.shape[0]), mixture_idx]
for x in [mu, log_sigma, rho]
]
# Calculate biased variances
sigma = (log_sigma - bias).exp()
# Sample from the bivariate normal distribution
mu_1, mu_2 = mu.unbind(-1)
sigma_1, sigma_2 = sigma.unbind(-1)
z_1 = torch.randn_like(mu_1)
z_2 = torch.randn_like(mu_2)
x = mu_1 + sigma_1 * z_1
y = mu_2 + sigma_2 * (z_2 * ((1 - rho ** 2) ** .5) + z_1 * rho)
# Uncollapse the matrix to a tensor (if necessary)
sample = torch.stack([x, y], 1)
if ndims > 2:
sample = sample.view(batch_size, -1, 2)
return sample
class OneHotEncoder(nn.Module):
def __init__(self, vocab_size):
super().__init__()
self.vocab_size = vocab_size
def forward(self, arr, mask):
shp = arr.size() + (self.vocab_size,)
one_hot_arr = torch.zeros(shp).float().cuda()
one_hot_arr.scatter_(-1, arr.unsqueeze(-1), 1)
return one_hot_arr * mask.unsqueeze(-1)
class GaussianAttention(nn.Module):
def __init__(self, hidden_size, n_mixtures, attention_multiplier=.05):
super().__init__()
self.linear = nn.Linear(hidden_size, 3 * n_mixtures)
self.n_mixtures = n_mixtures
self.attention_multiplier = attention_multiplier
def forward(self, h_t, k_tm1, ctx, ctx_mask):
B, T, _ = ctx.shape
device = ctx.device
alpha, beta, kappa = torch.exp(self.linear(h_t))[:, None].chunk(3, dim=-1) # (B, 1, K) each
kappa = kappa * self.attention_multiplier + k_tm1.unsqueeze(1)
u = torch.arange(T, dtype=torch.float32).to(device)
u = u[None, :, None].repeat(B, 1, 1) # (B, T, 1)
phi = alpha * torch.exp(-beta * torch.pow(kappa - u, 2)) # (B, T, K)
phi = phi.sum(-1) * ctx_mask
w = (phi.unsqueeze(-1) * ctx).sum(1)
attention_vars = {
'alpha': alpha.squeeze(1),
'beta': beta.squeeze(1),
'kappa': kappa.squeeze(1),
'phi': phi,
}
return w, attention_vars
class HandwritingSynthesisNetwork(nn.Module):
def __init__(
self, vocab_size, hidden_size, n_layers,
n_mixtures_attention, n_mixtures_output
):
super().__init__()
self.encoder = OneHotEncoder(vocab_size)
self.lstm_0 = nn.LSTMCell(3 + vocab_size, hidden_size)
self.lstm_1 = nn.LSTM(3 + vocab_size + hidden_size, hidden_size, batch_first=True)
self.lstm_2 = nn.LSTM(3 + vocab_size + hidden_size, hidden_size, batch_first=True)
self.attention = GaussianAttention(hidden_size, n_mixtures_attention)
self.fc = nn.Linear(
hidden_size * 3, n_mixtures_output * 6 + 1
)
self.hidden_size = hidden_size
self.vocab_size = vocab_size
self.n_mixtures_output = n_mixtures_output
def __init__hidden(self, bsz):
hid_0 = torch.zeros(bsz, self.hidden_size * 2).float().cuda()
hid_0 = hid_0.chunk(2, dim=-1)
hid_1, hid_2 = None, None
w_0 = torch.zeros(bsz, self.vocab_size).float().cuda()
k_0 = torch.zeros(bsz, 1).float().cuda()
return hid_0, hid_1, hid_2, w_0, k_0
def __parse_outputs(self, out):
K = self.n_mixtures_output
mu, log_sigma, pi, rho, eos = out.split([2 * K, 2 * K, K, K, 1], -1)
# Apply activations to constrain values in the correct range
rho = torch.tanh(rho)
log_pi = F.log_softmax(pi, dim=-1)
eos = torch.sigmoid(-eos)
mu = mu.view(mu.shape[:-1] + (K, 2))
log_sigma = log_sigma.view(log_sigma.shape[:-1] + (K, 2))
return log_pi, mu, log_sigma, rho, eos
def forward(self, chars, chars_mask, strokes, strokes_mask, prev_states=None):
# Encode the characters
chars = self.encoder(chars, chars_mask)
if prev_states is None:
hid_0, hid_1, hid_2, w_t, k_t = self.__init__hidden(chars.size(0))
else:
hid_0, hid_1, hid_2, w_t, k_t = prev_states
lstm_0_out = []
attention_out = []
monitor_vars = {'phi': [], 'alpha': [], 'beta': [], 'kappa': []}
for x_t in strokes.unbind(1):
hid_0 = self.lstm_0(
torch.cat([x_t, w_t], -1),
hid_0
)
w_t, vars_t = self.attention(hid_0[0], k_t, chars, chars_mask)
k_t = vars_t['kappa']
concatenate_dict(monitor_vars, vars_t)
lstm_0_out.append(hid_0[0])
attention_out.append(w_t)
lstm_0_out = torch.stack(lstm_0_out, 1)
attention_out = torch.stack(attention_out, 1)
lstm_1_out, hid_1 = self.lstm_1(
torch.cat([strokes, attention_out, lstm_0_out], -1),
hid_1
)
lstm_2_out, hid_2 = self.lstm_2(
torch.cat([strokes, attention_out, lstm_1_out], -1),
hid_2
)
last_out = self.fc(
torch.cat([lstm_0_out, lstm_1_out, lstm_2_out], -1)
)
output_params = self.__parse_outputs(last_out)
monitor_vars = {x: torch.stack(y, 1) for x, y in monitor_vars.items()}
return output_params, monitor_vars, (hid_0, hid_1, hid_2, w_t, k_t)
def sample(self, chars, chars_mask, maxlen=1000):
chars = self.encoder(chars, chars_mask)
last_idx = (chars_mask.sum(-1) - 2).long()
hid_0, hid_1, hid_2, w_t, k_t = self.__init__hidden(chars.size(0))
x_t = torch.zeros(chars.size(0), 3).float().cuda()
strokes = []
monitor_vars = {'phi': [], 'kappa': [], 'alpha': [], 'beta': []}
for i in range(maxlen):
hid_0 = self.lstm_0(
torch.cat([x_t, w_t], -1),
hid_0
)
w_t, vars_t = self.attention(hid_0[0], k_t, chars, chars_mask)
k_t = vars_t['kappa']
concatenate_dict(monitor_vars, vars_t)
_, hid_1 = self.lstm_1(
torch.cat([x_t, w_t, hid_0[0]], 1).unsqueeze(1),
hid_1
) # hid_1 - tuple of (1, batch_size, hidden_size)
_, hid_2 = self.lstm_2(
torch.cat([x_t, w_t, hid_1[0].squeeze(0)], 1).unsqueeze(1),
hid_2
) # hid_2 - tuple of (1, batch_size, hidden_size)
last_out = self.fc(
torch.cat([hid_0[0], hid_1[0].squeeze(0), hid_2[0].squeeze(0)], 1)
)
output_params = self.__parse_outputs(last_out)
x_t = torch.cat([
output_params[-1].bernoulli(),
mixture_of_bivariate_normal_sample(*output_params[:-1], bias=3.)
], dim=1)
################################################
# Exit Condition #
################################################
phi_t = vars_t['kappa']
check_1 = ~torch.gt(phi_t.max(1)[1], last_idx)
check_2 = torch.sign(phi_t.sum(1)).byte()
is_incomplete = check_1 | check_2
if is_incomplete.sum().item() == 0:
break
x_t = x_t * is_incomplete.float().unsqueeze(-1)
strokes.append(x_t)
monitor_vars = {x: torch.stack(y, 1) for x, y in monitor_vars.items()}
return torch.stack(strokes, 1), monitor_vars
def compute_loss(self, chars, chars_mask, strokes, strokes_mask, prev_states=None):
input_strokes = strokes[:, :-1]
input_strokes_mask = strokes_mask[:, :-1]
output_strokes = strokes[:, 1:]
output_params, monitor_vars, prev_states = self.forward(
chars, chars_mask, input_strokes, input_strokes_mask,
prev_states
)
stroke_loss = mixture_of_bivariate_normal_nll(
output_strokes[:, :, 1:],
*output_params[:-1] # passing everything except eos param
)
stroke_loss = (stroke_loss * input_strokes_mask).sum(-1).mean()
eos_loss = F.binary_cross_entropy(
output_params[-1].squeeze(-1),
output_strokes[:, :, 0],
reduction='none'
)
eos_loss = (eos_loss * input_strokes_mask).sum(-1).mean()
teacher_forced_sample = torch.cat([
output_params[-1].bernoulli(),
mixture_of_bivariate_normal_sample(*output_params[:-1], bias=3.)
], dim=-1)
return stroke_loss, eos_loss, monitor_vars, prev_states, teacher_forced_sample
class HandwritingPredictionNetwork(nn.Module):
def __init__(
self, hidden_size, n_layers, n_mixtures_output
):
super().__init__()
self.lstm_0 = nn.LSTM(3, hidden_size, batch_first=True)
self.lstm_1 = nn.LSTM(3 + hidden_size, hidden_size, batch_first=True)
self.lstm_2 = nn.LSTM(3 + hidden_size, hidden_size, batch_first=True)
self.fc = nn.Linear(
hidden_size * 3, n_mixtures_output * 6 + 1
)
self.hidden_size = hidden_size
self.n_mixtures_output = n_mixtures_output
def __parse_outputs(self, out):
K = self.n_mixtures_output
mu, log_sigma, pi, rho, eos = out.split([2 * K, 2 * K, K, K, 1], -1)
# Apply activations to constrain values in the correct range
rho = torch.tanh(rho)
log_pi = F.log_softmax(pi, dim=-1)
eos = torch.sigmoid(-eos)
mu = mu.view(mu.shape[:-1] + (K, 2))
log_sigma = log_sigma.view(log_sigma.shape[:-1] + (K, 2))
return log_pi, mu, log_sigma, rho, eos
def forward(self, strokes, strokes_mask, prev_states=None):
if prev_states is None:
hid_0, hid_1, hid_2 = None, None, None
else:
hid_0, hid_1, hid_2 = prev_states
lstm_0_out, hid_0 = self.lstm_0(
strokes, hid_0
)
lstm_1_out, hid_1 = self.lstm_1(
torch.cat([strokes, lstm_0_out], -1),
hid_1
)
lstm_2_out, hid_2 = self.lstm_2(
torch.cat([strokes, lstm_1_out], -1),
hid_2
)
last_out = self.fc(
torch.cat([lstm_0_out, lstm_1_out, lstm_2_out], -1)
)
output_params = self.__parse_outputs(last_out)
return output_params, (hid_0, hid_1, hid_2)
def sample(self, batch_size=1, maxlen=1000):
hid_0, hid_1, hid_2 = None, None, None
x_t = torch.zeros(batch_size, 1, 3).float().cuda()
strokes = []
for i in range(maxlen):
_, hid_0 = self.lstm_0(x_t, hid_0)
_, hid_1 = self.lstm_1(
torch.cat([x_t, hid_0[0]], -1),
hid_1
) # hid_1 - tuple of (1, batch_size, hidden_size)
_, hid_2 = self.lstm_2(
torch.cat([x_t, hid_1[0]], -1),
hid_2
) # hid_2 - tuple of (1, batch_size, hidden_size)
last_out = self.fc(
torch.cat([hid_0[0], hid_1[0], hid_2[0]], -1)
).squeeze(1)
output_params = self.__parse_outputs(last_out)
x_t = torch.cat([
output_params[-1].bernoulli(),
mixture_of_bivariate_normal_sample(*output_params[:-1], bias=3.)
], dim=1).unsqueeze(1)
strokes.append(x_t)
return torch.cat(strokes, 1)
def compute_loss(self, strokes, strokes_mask, prev_states=None):
input_strokes = strokes[:, :-1]
input_strokes_mask = strokes_mask[:, :-1]
output_strokes = strokes[:, 1:]
output_params, prev_states = self.forward(
input_strokes, input_strokes_mask,
prev_states
)
stroke_loss = mixture_of_bivariate_normal_nll(
output_strokes[:, :, 1:],
*output_params[:-1] # passing everything except eos param
)
stroke_loss = (stroke_loss * input_strokes_mask).sum(-1).mean()
eos_loss = F.binary_cross_entropy(
output_params[-1].squeeze(-1),
output_strokes[:, :, 0],
reduction='none'
)
eos_loss = (eos_loss * input_strokes_mask).sum(-1).mean()
teacher_forced_sample = torch.cat([
output_params[-1].bernoulli(),
mixture_of_bivariate_normal_sample(*output_params[:-1], bias=3.)
], dim=-1)
return stroke_loss, eos_loss, prev_states, teacher_forced_sample
if __name__ == '__main__':
vocab_size = 60
hidden_size = 400
n_layers = 3
K_att = 6
K_out = 20
model = HandwritingSynthesisNetwork(
vocab_size, hidden_size, n_layers,
K_att, K_out
).cuda()
chars = torch.randint(0, vocab_size, (4, 50)).cuda()
chars_mask = torch.ones_like(chars).float()
strokes = torch.randn(4, 300, 3).cuda()
strokes_mask = torch.ones(4, 300).cuda()
loss = model.compute_loss(chars, chars_mask, strokes, strokes_mask)
print(loss)
out = model.sample(chars, chars_mask)
print(out[0].shape)