forked from caogang/wgan-gp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gan_language.py
300 lines (242 loc) · 9.64 KB
/
gan_language.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
import os, sys
sys.path.append(os.getcwd())
import time
import numpy as np
import torch
import torch.autograd as autograd
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import language_helpers
import tflib as lib
import tflib.plot
from sklearn.preprocessing import OneHotEncoder
torch.manual_seed(1)
use_cuda = torch.cuda.is_available()
if use_cuda:
gpu = 0
# Download Google Billion Word at http://www.statmt.org/lm-benchmark/ and
# fill in the path to the extracted files here!
DATA_DIR = './data_language'
if len(DATA_DIR) == 0:
raise Exception('Please specify path to data directory in gan_language.py!')
BATCH_SIZE = 64 # Batch size
ITERS = 200000 # How many iterations to train for
SEQ_LEN = 32 # Sequence length in characters
DIM = 512 # Model dimensionality. This is fairly slow and overfits, even on
# Billion Word. Consider decreasing for smaller datasets.
CRITIC_ITERS = 10 # How many critic iterations per generator iteration. We
# use 10 for the results in the paper, but 5 should work fine
# as well.
LAMBDA = 10 # Gradient penalty lambda hyperparameter.
MAX_N_EXAMPLES = 10000000#10000000 # Max number of data examples to load. If data loading
# is too slow or takes too much RAM, you can decrease
# this (at the expense of having less training data).
lib.print_model_settings(locals().copy())
lines, charmap, inv_charmap = language_helpers.load_dataset(
max_length=SEQ_LEN,
max_n_examples=MAX_N_EXAMPLES,
data_dir=DATA_DIR
)
table = np.arange(len(charmap)).reshape(-1, 1)
one_hot = OneHotEncoder()
one_hot.fit(table)
# ==================Definition Start======================
def make_noise(shape, volatile=False):
tensor = torch.randn(shape).cuda(gpu) if use_cuda else torch.randn(shape)
return autograd.Variable(tensor, volatile)
class ResBlock(nn.Module):
def __init__(self):
super(ResBlock, self).__init__()
self.res_block = nn.Sequential(
nn.ReLU(True),
nn.Conv1d(DIM, DIM, 5, padding=2),#nn.Linear(DIM, DIM),
nn.ReLU(True),
nn.Conv1d(DIM, DIM, 5, padding=2),#nn.Linear(DIM, DIM),
)
def forward(self, input):
output = self.res_block(input)
return input + (0.3*output)
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.fc1 = nn.Linear(128, DIM*SEQ_LEN)
self.block = nn.Sequential(
ResBlock(),
ResBlock(),
ResBlock(),
ResBlock(),
ResBlock(),
)
self.conv1 = nn.Conv1d(DIM, len(charmap), 1)
self.softmax = nn.Softmax()
def forward(self, noise):
output = self.fc1(noise)
output = output.view(-1, DIM, SEQ_LEN) # (BATCH_SIZE, DIM, SEQ_LEN)
output = self.block(output)
output = self.conv1(output)
output = output.transpose(1, 2)
shape = output.size()
output = output.contiguous()
output = output.view(BATCH_SIZE*SEQ_LEN, -1)
output = self.softmax(output)
return output.view(shape) # (BATCH_SIZE, SEQ_LEN, len(charmap))
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.block = nn.Sequential(
ResBlock(),
ResBlock(),
ResBlock(),
ResBlock(),
ResBlock(),
)
self.conv1d = nn.Conv1d(len(charmap), DIM, 1)
self.linear = nn.Linear(SEQ_LEN*DIM, 1)
def forward(self, input):
output = input.transpose(1, 2) # (BATCH_SIZE, len(charmap), SEQ_LEN)
output = self.conv1d(output)
output = self.block(output)
output = output.view(-1, SEQ_LEN*DIM)
output = self.linear(output)
return output
# Dataset iterator
def inf_train_gen():
while True:
np.random.shuffle(lines)
for i in xrange(0, len(lines)-BATCH_SIZE+1, BATCH_SIZE):
yield np.array(
[[charmap[c] for c in l] for l in lines[i:i+BATCH_SIZE]],
dtype='int32'
)
def calc_gradient_penalty(netD, real_data, fake_data):
alpha = torch.rand(BATCH_SIZE, 1, 1)
alpha = alpha.expand(real_data.size())
alpha = alpha.cuda(gpu) if use_cuda else alpha
interpolates = alpha * real_data + ((1 - alpha) * fake_data)
if use_cuda:
interpolates = interpolates.cuda(gpu)
interpolates = autograd.Variable(interpolates, requires_grad=True)
disc_interpolates = netD(interpolates)
# TODO: Make ConvBackward diffentiable
gradients = autograd.grad(outputs=disc_interpolates, inputs=interpolates,
grad_outputs=torch.ones(disc_interpolates.size()).cuda(gpu) if use_cuda else torch.ones(
disc_interpolates.size()),
create_graph=True, retain_graph=True, only_inputs=True)[0]
gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean() * LAMBDA
return gradient_penalty
def generate_samples(netG):
noise = torch.randn(BATCH_SIZE, 128)
if use_cuda:
noise = noise.cuda(gpu)
noisev = autograd.Variable(noise, volatile=True)
samples = netG(noisev)
samples = samples.view(-1, SEQ_LEN, len(charmap))
# print samples.size()
samples = samples.cpu().data.numpy()
samples = np.argmax(samples, axis=2)
decoded_samples = []
for i in xrange(len(samples)):
decoded = []
for j in xrange(len(samples[i])):
decoded.append(inv_charmap[samples[i][j]])
decoded_samples.append(tuple(decoded))
return decoded_samples
# ==================Definition End======================
netG = Generator()
netD = Discriminator()
print netG
print netD
if use_cuda:
netD = netD.cuda(gpu)
netG = netG.cuda(gpu)
optimizerD = optim.Adam(netD.parameters(), lr=1e-4, betas=(0.5, 0.9))
optimizerG = optim.Adam(netG.parameters(), lr=1e-4, betas=(0.5, 0.9))
one = torch.FloatTensor([1])
mone = one * -1
if use_cuda:
one = one.cuda(gpu)
mone = mone.cuda(gpu)
data = inf_train_gen()
# During training we monitor JS divergence between the true & generated ngram
# distributions for n=1,2,3,4. To get an idea of the optimal values, we
# evaluate these statistics on a held-out set first.
true_char_ngram_lms = [language_helpers.NgramLanguageModel(i+1, lines[10*BATCH_SIZE:], tokenize=False) for i in xrange(4)]
validation_char_ngram_lms = [language_helpers.NgramLanguageModel(i+1, lines[:10*BATCH_SIZE], tokenize=False) for i in xrange(4)]
for i in xrange(4):
print "validation set JSD for n={}: {}".format(i+1, true_char_ngram_lms[i].js_with(validation_char_ngram_lms[i]))
true_char_ngram_lms = [language_helpers.NgramLanguageModel(i+1, lines, tokenize=False) for i in xrange(4)]
for iteration in xrange(ITERS):
start_time = time.time()
############################
# (1) Update D network
###########################
for p in netD.parameters(): # reset requires_grad
p.requires_grad = True # they are set to False below in netG update
for iter_d in xrange(CRITIC_ITERS):
_data = data.next()
data_one_hot = one_hot.transform(_data.reshape(-1, 1)).toarray().reshape(BATCH_SIZE, -1, len(charmap))
#print data_one_hot.shape
real_data = torch.Tensor(data_one_hot)
if use_cuda:
real_data = real_data.cuda(gpu)
real_data_v = autograd.Variable(real_data)
netD.zero_grad()
# train with real
D_real = netD(real_data_v)
D_real = D_real.mean()
# print D_real
# TODO: Waiting for the bug fix from pytorch
D_real.backward(mone)
# train with fake
noise = torch.randn(BATCH_SIZE, 128)
if use_cuda:
noise = noise.cuda(gpu)
noisev = autograd.Variable(noise, volatile=True) # totally freeze netG
fake = autograd.Variable(netG(noisev).data)
inputv = fake
D_fake = netD(inputv)
D_fake = D_fake.mean()
# TODO: Waiting for the bug fix from pytorch
D_fake.backward(one)
# train with gradient penalty
gradient_penalty = calc_gradient_penalty(netD, real_data_v.data, fake.data)
gradient_penalty.backward()
D_cost = D_fake - D_real + gradient_penalty
Wasserstein_D = D_real - D_fake
optimizerD.step()
############################
# (2) Update G network
###########################
for p in netD.parameters():
p.requires_grad = False # to avoid computation
netG.zero_grad()
noise = torch.randn(BATCH_SIZE, 128)
if use_cuda:
noise = noise.cuda(gpu)
noisev = autograd.Variable(noise)
fake = netG(noisev)
G = netD(fake)
G = G.mean()
G.backward(mone)
G_cost = -G
optimizerG.step()
# Write logs and save samples
lib.plot.plot('tmp/lang/time', time.time() - start_time)
lib.plot.plot('tmp/lang/train disc cost', D_cost.cpu().data.numpy())
lib.plot.plot('tmp/lang/train gen cost', G_cost.cpu().data.numpy())
lib.plot.plot('tmp/lang/wasserstein distance', Wasserstein_D.cpu().data.numpy())
if iteration % 100 == 99:
samples = []
for i in xrange(10):
samples.extend(generate_samples(netG))
for i in xrange(4):
lm = language_helpers.NgramLanguageModel(i+1, samples, tokenize=False)
lib.plot.plot('tmp/lang/js{}'.format(i+1), lm.js_with(true_char_ngram_lms[i]))
with open('tmp/lang/samples_{}.txt'.format(iteration), 'w') as f:
for s in samples:
s = "".join(s)
f.write(s + "\n")
if iteration % 100 == 99:
lib.plot.flush()
lib.plot.tick()