-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
376 lines (299 loc) · 12.6 KB
/
Copy pathmodels.py
File metadata and controls
376 lines (299 loc) · 12.6 KB
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
from utils import to_gpu
import json
class MLP_D(nn.Module):
def __init__(self, ninput, noutput, layers,
activation=nn.LeakyReLU(0.2), gpu=False):
super(MLP_D, self).__init__()
self.ninput = ninput
self.noutput = noutput
layer_sizes = [ninput] + [int(x) for x in layers.split('-')]
self.layers = []
for i in range(len(layer_sizes) - 1):
layer = nn.Linear(layer_sizes[i], layer_sizes[i + 1])
self.layers.append(layer)
self.add_module("layer" + str(i + 1), layer)
# No batch normalization after first layer
if i != 0:
bn = nn.BatchNorm1d(
layer_sizes[i + 1], eps=1e-05, momentum=0.1)
self.layers.append(bn)
self.add_module("bn" + str(i + 1), bn)
self.layers.append(activation)
self.add_module("activation" + str(i + 1), activation)
layer = nn.Linear(layer_sizes[-1], noutput)
self.layers.append(layer)
self.add_module("layer" + str(len(self.layers)), layer)
self.init_weights()
def forward(self, source, target):
x = torch.cat([source, target], dim=1)
for i, layer in enumerate(self.layers):
x = layer(x)
x = torch.mean(x)
return x
def init_weights(self):
init_std = 0.02
for layer in self.layers:
try:
layer.weight.data.normal_(0, init_std)
layer.bias.data.fill_(0)
except:
pass
class MLP_G(nn.Module):
def __init__(self, ninput, noutput, layers,
activation=nn.ReLU(), gpu=False):
super(MLP_G, self).__init__()
self.ninput = ninput
self.noutput = noutput
layer_sizes = [ninput] + [int(x) for x in layers.split('-')]
self.layers = []
for i in range(len(layer_sizes) - 1):
layer = nn.Linear(layer_sizes[i], layer_sizes[i + 1])
self.layers.append(layer)
self.add_module("layer" + str(i + 1), layer)
bn = nn.BatchNorm1d(layer_sizes[i + 1], eps=1e-05, momentum=0.1)
self.layers.append(bn)
self.add_module("bn" + str(i + 1), bn)
self.layers.append(activation)
self.add_module("activation" + str(i + 1), activation)
layer = nn.Linear(layer_sizes[-1], noutput)
self.layers.append(layer)
self.add_module("layer" + str(len(self.layers)), layer)
self.init_weights()
def forward(self, x):
for i, layer in enumerate(self.layers):
x = layer(x)
return x
def init_weights(self):
init_std = 0.02
for layer in self.layers:
try:
layer.weight.data.normal_(0, init_std)
layer.bias.data.fill_(0)
except:
pass
class Seq2Seq(nn.Module):
def __init__(self, emsize, nhidden, ntokens, nlayers, max_len=20, noise_radius=0.2,
hidden_init=False, dropout=0, gpu=False):
super(Seq2Seq, self).__init__()
self.nhidden = nhidden
self.emsize = emsize
self.ntokens = ntokens
self.nlayers = nlayers
self.noise_radius = noise_radius
self.hidden_init = hidden_init
self.dropout = dropout
self.gpu = gpu
self.start_symbols = to_gpu(gpu, Variable(torch.ones(10, 1).long()))
# Vocabulary embedding
self.embedding = nn.Embedding(ntokens, emsize)
self.embedding_decoder = nn.Embedding(ntokens, emsize)
# one more <sos> or <eos>
max_len += 1
emsize_len = int(np.ceil(np.log(max_len) / np.log(2)))
self.embedding_length = nn.Embedding(max_len, emsize_len)
# RNN Encoder and Decoder
self.encoder = nn.LSTM(input_size=emsize,
hidden_size=nhidden,
num_layers=nlayers,
dropout=dropout,
batch_first=True)
decoder_input_size = emsize + nhidden + emsize_len
self.decoder = nn.LSTM(input_size=decoder_input_size,
hidden_size=nhidden,
num_layers=1,
dropout=dropout,
batch_first=True)
# Initialize Linear Transformation
self.linear = nn.Linear(nhidden, ntokens)
self.init_weights()
def init_weights(self):
initrange = 0.1
# Initialize Vocabulary Matrix Weight
self.embedding.weight.data.uniform_(-initrange, initrange)
self.embedding_decoder.weight.data.uniform_(-initrange, initrange)
self.embedding_length.weight.data.uniform_(-initrange, initrange)
# Initialize Encoder and Decoder Weights
for p in self.encoder.parameters():
p.data.uniform_(-initrange, initrange)
for p in self.decoder.parameters():
p.data.uniform_(-initrange, initrange)
# Initialize Linear Weight
self.linear.weight.data.uniform_(-initrange, initrange)
self.linear.bias.data.fill_(0)
def init_hidden(self, bsz):
zeros1 = Variable(torch.zeros(self.nlayers, bsz, self.nhidden))
zeros2 = Variable(torch.zeros(self.nlayers, bsz, self.nhidden))
return (to_gpu(self.gpu, zeros1), to_gpu(self.gpu, zeros2))
def init_state(self, bsz):
zeros = Variable(torch.zeros(self.nlayers, bsz, self.nhidden))
return to_gpu(self.gpu, zeros)
def forward(self, indices, length, noise):
batch_size, maxlen = indices.size()
hidden = self.encode(indices, noise)
embed_len = self.embedding_length(length.unsqueeze(1))
hidden = torch.cat([hidden,
embed_len.squeeze(1)],
dim=1)
decoded = self.decode(hidden, batch_size, maxlen,
indices=indices)
return decoded
def encode(self, indices, noise=False):
embeddings = self.embedding(indices)
# Encode
packed_output, state = self.encoder(embeddings)
hidden, cell = state
# batch_size x nhidden
hidden = hidden[-1] # get hidden state of last layer of encoder
# normalize to unit ball (l2 norm of 1) - p=2, dim=1
norms = torch.norm(hidden, 2, 1)
hidden = torch.div(hidden, norms.expand_as(hidden))
if noise and self.noise_radius > 0:
gauss_noise = torch.normal(means=torch.zeros(hidden.size()),
std=self.noise_radius)
hidden = hidden + to_gpu(self.gpu, Variable(gauss_noise))
return hidden
def decode(self, hidden, batch_size, maxlen, indices=None):
# batch x hidden
all_hidden = hidden.unsqueeze(1).repeat(1, maxlen, 1)
if self.hidden_init:
# initialize decoder hidden state to encoder output
state = (hidden.unsqueeze(0), self.init_state(batch_size))
else:
state = self.init_hidden(batch_size)
embeddings = self.embedding_decoder(indices)
augmented_embeddings = torch.cat([embeddings, all_hidden], 2)
output, state = self.decoder(augmented_embeddings, state)
# reshape to batch_size*maxlen x nhidden before linear over vocab
decoded = self.linear(output.contiguous().view(-1, self.nhidden))
decoded = decoded.view(batch_size, maxlen, self.ntokens)
return decoded
def generate(self, hidden, length, sample=False, temp=1.0):
"""Generate through decoder; no backprop"""
embed_len = self.embedding_length(length.unsqueeze(1))
hidden = torch.cat([hidden,
embed_len.squeeze(1)],
dim=1)
batch_size = hidden.size(0)
if self.hidden_init:
# initialize decoder hidden state to encoder output
state = (hidden.unsqueeze(0), self.init_state(batch_size))
else:
state = self.init_hidden(batch_size)
# <sos>
self.start_symbols.data.resize_(batch_size, 1)
self.start_symbols.data.fill_(1)
embedding = self.embedding_decoder(self.start_symbols)
inputs = torch.cat([embedding, hidden.unsqueeze(1)], 2)
# unroll
all_indices = []
for i in range(torch.max(length.data)):
output, state = self.decoder(inputs, state)
overvocab = self.linear(output.squeeze(1))
if not sample:
vals, indices = torch.max(overvocab, 1)
else:
# sampling
probs = F.softmax(overvocab / temp)
indices = torch.multinomial(probs, 1)
all_indices.append(indices)
embedding = self.embedding_decoder(indices)
inputs = torch.cat([embedding, hidden.unsqueeze(1)], 2)
max_indices = torch.cat(all_indices, 1)
return max_indices
def load_ae(ae_args_file, ae_model, vocab_file):
ae_args = json.load(open(ae_args_file, "r"))
word2idx = json.load(open(vocab_file, "r"))
idx2word = {v: k for k, v in word2idx.items()}
autoencoder = Seq2Seq(emsize=ae_args['emsize'],
nhidden=ae_args['nhidden'],
ntokens=ae_args['ntokens'],
nlayers=ae_args['nlayers'],
hidden_init=ae_args['hidden_init'])
autoencoder.load_state_dict(torch.load(ae_model))
return ae_args, autoencoder, idx2word
def load_models(ae_args_file, gan_args_file, vocab_file, ae_model, g_model, d_model):
ae_args = json.load(open(ae_args_file, "r"))
gan_args = json.load(open(gan_args_file, 'r'))
word2idx = json.load(open(vocab_file, "r"))
idx2word = {v: k for k, v in word2idx.items()}
autoencoder = Seq2Seq(emsize=ae_args['emsize'],
nhidden=ae_args['nhidden'],
ntokens=ae_args['ntokens'],
nlayers=ae_args['nlayers'],
hidden_init=ae_args['hidden_init'],
gpu=True)
gan_gen = MLP_G(ninput=gan_args['nhidden'],
noutput=gan_args['nhidden'],
layers=gan_args['arch_g'])
gan_disc = MLP_D(ninput=2 * gan_args['nhidden'],
noutput=1,
layers=gan_args['arch_d'])
autoencoder.load_state_dict(torch.load(ae_model))
gan_gen.load_state_dict(torch.load(g_model))
gan_disc.load_state_dict(torch.load(d_model))
return ae_args, gan_args, idx2word, autoencoder, gan_gen, gan_disc
def decode_idx(vocab, idx):
# generated sentence
words = [vocab[x] for x in idx]
# truncate sentences to first occurrence of <eos>
truncated_sent = []
for w in words:
if w == '<sos>' or w == '<pad>':
continue
if w != '<eos>':
truncated_sent.append(w)
else:
break
sent = "".join(truncated_sent)
return sent
def generate_from_hidden(autoencoder, hidden, vocab, sample, maxlen):
# autoencoder.eval()
max_indices = autoencoder.generate(hidden=hidden,
maxlen=maxlen,
sample=sample)
max_indices = max_indices.data.cpu().numpy()
sentences = []
for idx in max_indices:
# generated sentence
words = [vocab[x] for x in idx]
# truncate sentences to first occurrence of <eos>
truncated_sent = []
for w in words:
if w != '<eos>':
truncated_sent.append(w)
else:
break
sent = "".join(truncated_sent)
sentences.append(sent)
return sentences
def generate(autoencoder, gan_gen, inp, vocab, sample, maxlen):
"""
Assume inp is batch_size x max_sen_len
"""
gan_gen.eval()
autoencoder.eval()
# generate from the previous line
fake_hidden = gan_gen(autoencoder.encode(inp))
max_indices = autoencoder.generate(hidden=fake_hidden,
maxlen=maxlen,
sample=sample)
max_indices = max_indices.data.cpu().numpy()
sentences = []
for idx in max_indices:
# generated sentence
words = [vocab[x] for x in idx]
# truncate sentences to first occurrence of <eos>
truncated_sent = []
for w in words:
if w != '<eos>':
truncated_sent.append(w)
else:
break
sent = "".join(truncated_sent)
sentences.append(sent)
return sentences